-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
104 lines (86 loc) · 2.42 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use strict';
const http = require('http');
const express = require('express');
const fs = require('fs'); //filesystem
const datastore = require('nedb');
const app = express();
const bodyParser = require('body-parser');
const CryptoJS = require("crypto-js");
const async = require("async");
var p = function(e){
if(e){
console.log(e);
}
return e;
};
const configJson = fs.readFileSync('./config.json');
const config = JSON.parse(configJson);
app.use(express.static(config.remoteServer.folder));
app.use(bodyParser.json());
const index_db = new datastore({
filename : config.serverdb.index
});
index_db.loadDatabase();
var prf = function(passphrase,t){
var hash = CryptoJS.HmacSHA256(t, passphrase);
return hash;
};
var encrypt = function(text,passphrase){
var ciphertext = CryptoJS.AES.encrypt(text, passphrase);
return ciphertext;
};
var decrypt = function(ciphertext,passphrase){
var bytes = CryptoJS.AES.decrypt(ciphertext, passphrase);
var plaintext = bytes.toString(CryptoJS.enc.Utf8);
return plaintext;
};
app.post('/setup', function (req, res) {
var l = req.body.data;
console.log('received edb');
console.log(l);
async.forEach(l, function (obj, callback){
index_db.update({ _id : obj.l }, { $set: { d : obj.d }}, {upsert:true}, function (err, numReplaced) {
});
callback();
}, function(err) {
console.log('Done.')
res.json({status:'success'});
});
});
app.post('/search', function (req, res) {
console.log('Search request recieved.');
var k1 = req.body.k1;
var k2 = req.body.k2;
console.log('k1: '+k1);
console.log('k2: '+k2);
var c = 0;
var l = prf(k1.toString(),c.toString()).toString();
var result = true;
var doc_ids = [];
async.doWhilst(function(callback) {
index_db.findOne({ _id: l }, function (err, doc) {
console.log(doc);
if(doc){
var m = decrypt(doc.d,k2);
doc_ids.push(m);
c++;
l = prf(k1.toString(),c.toString()).toString();
}else{
result = false;
}
callback();
});
}, function(){
return result;
}, function(err) {
res.json(doc_ids);
});
});
const httpServer = http.createServer(app);
httpServer.listen(config.remoteServer.port,function(err){
if(err){
console.log(err.message);
return;
}
console.log('listening on '+config.remoteServer.port);
});