-rw-r--r-- | backend/node/src/app.js | 7 | ||||
-rw-r--r-- | backend/node/src/clipperz.js | 6 |
2 files changed, 10 insertions, 3 deletions
diff --git a/backend/node/src/app.js b/backend/node/src/app.js index bbc62f8..09dcfac 100644 --- a/backend/node/src/app.js +++ b/backend/node/src/app.js | |||
@@ -6,46 +6,47 @@ var LOGGER = BUNYAN.createLogger({ | |||
6 | ], | 6 | ], |
7 | serializers: { | 7 | serializers: { |
8 | req: BUNYAN.stdSerializers.req, | 8 | req: BUNYAN.stdSerializers.req, |
9 | res: BUNYAN.stdSerializers.res, | 9 | res: BUNYAN.stdSerializers.res, |
10 | err: BUNYAN.stdSerializers.err | 10 | err: BUNYAN.stdSerializers.err |
11 | }, | 11 | }, |
12 | src: true | 12 | src: true |
13 | }); | 13 | }); |
14 | 14 | ||
15 | 15 | ||
16 | var EXPRESS = require('express'); | 16 | var EXPRESS = require('express'); |
17 | var HTTP = require('http'); | 17 | var HTTP = require('http'); |
18 | var PATH = require('path'); | 18 | var PATH = require('path'); |
19 | 19 | ||
20 | 20 | ||
21 | var CLIPPERZ = require('./clipperz'); | 21 | var CLIPPERZ = require('./clipperz'); |
22 | var CONF = require('./conf'); | 22 | var CONF = require('./conf'); |
23 | var clipperz = CLIPPERZ({ | 23 | var clipperz = CLIPPERZ({ |
24 | psql: CONF.psql||'postgresql:///clipperz', | 24 | psql: CONF.psql||'postgresql:///clipperz', |
25 | logger: LOGGER, | 25 | logger: LOGGER, |
26 | dump_template: PATH.join(__dirname,'htdocs/beta/index.html') | 26 | dump_template: PATH.join(__dirname,'htdocs/beta/index.html') |
27 | }); | 27 | }); |
28 | 28 | ||
29 | 29 | ||
30 | var app = EXPRESS(); | 30 | var app = EXPRESS(); |
31 | 31 | ||
32 | app.set('port', process.env.PORT || 3000); | 32 | app.set('port', process.env.PORT || 3000); |
33 | app.use(require('morgan')('dev')); | 33 | app.use(require('morgan')('dev')); |
34 | app.use(require('body-parser').urlencoded({extended:true})); | 34 | app.use(require('body-parser').urlencoded({extended:true})); |
35 | app.use(require('cookie-parser')('your secret here')); | 35 | app.use(require('cookie-parser')('your secret here')); |
36 | app.use(require('express-session')({secret:'99 little bugs in the code', key:'sid', store: clipperz.session_store(), resave: false, saveUninitialized: false })); | 36 | app.use(require('express-session')({secret:'99 little bugs in the code', key:'sid', store: clipperz.session_store(), resave: false, saveUninitialized: false })); |
37 | 37 | ||
38 | app.post('/json',clipperz.json); | 38 | /* Like this: */ |
39 | app.get('/dump',clipperz.dump); | 39 | app.use(clipperz.router); |
40 | /* Or this: */ | ||
41 | app.use('/clz/',clipperz.router); | ||
40 | 42 | ||
41 | app.use(EXPRESS.static(PATH.join(__dirname, 'htdocs/'))); | ||
42 | if ('development' == app.get('env')) { | 43 | if ('development' == app.get('env')) { |
43 | app.use(require('express-error-with-sources')()); | 44 | app.use(require('express-error-with-sources')()); |
44 | } | 45 | } |
45 | 46 | ||
46 | 47 | ||
47 | 48 | ||
48 | 49 | ||
49 | HTTP.createServer(app).listen(app.get('port'), function(){ | 50 | HTTP.createServer(app).listen(app.get('port'), function(){ |
50 | LOGGER.info({port:app.get('port')},"Listener established"); | 51 | LOGGER.info({port:app.get('port')},"Listener established"); |
51 | }); | 52 | }); |
diff --git a/backend/node/src/clipperz.js b/backend/node/src/clipperz.js index 842de31..2a3df73 100644 --- a/backend/node/src/clipperz.js +++ b/backend/node/src/clipperz.js | |||
@@ -1,36 +1,38 @@ | |||
1 | var FS = require('fs'); | 1 | var FS = require('fs'); |
2 | var CRYPTO = require('crypto'); | 2 | var CRYPTO = require('crypto'); |
3 | var BIGNUM = require('bignum'); | 3 | var BIGNUM = require('bignum'); |
4 | var ASYNC = require('async'); | 4 | var ASYNC = require('async'); |
5 | var EXPRESS = require('express'); | ||
6 | var PATH = require('path'); | ||
5 | 7 | ||
6 | var express_store = require('express-session').Store; | 8 | var express_store = require('express-session').Store; |
7 | 9 | ||
8 | function clipperz_hash(v) { | 10 | function clipperz_hash(v) { |
9 | return CRYPTO.createHash('sha256').update( | 11 | return CRYPTO.createHash('sha256').update( |
10 | CRYPTO.createHash('sha256').update(v).digest('binary') | 12 | CRYPTO.createHash('sha256').update(v).digest('binary') |
11 | ).digest('hex'); | 13 | ).digest('hex'); |
12 | }; | 14 | }; |
13 | function clipperz_random() { | 15 | function clipperz_random() { |
14 | for(var r = '';r.length<64;r+=''+BIGNUM(Math.floor(Math.random()*1e18)).toString(16)); | 16 | for(var r = '';r.length<64;r+=''+BIGNUM(Math.floor(Math.random()*1e18)).toString(16)); |
15 | return r.substr(0,64); | 17 | return r.substr(0,64); |
16 | }; | 18 | }; |
17 | function clipperz_store(PG) { | 19 | function clipperz_store(PG) { |
18 | var rv = function(o) { express_store.call(this,o); } | 20 | var rv = function(o) { express_store.call(this,o); } |
19 | rv.prototype.get = function(sid,cb) { PG.Q( | 21 | rv.prototype.get = function(sid,cb) { PG.Q( |
20 | "SELECT s_data FROM clipperz.thesession WHERE s_id=$1",[sid], | 22 | "SELECT s_data FROM clipperz.thesession WHERE s_id=$1",[sid], |
21 | function(e,r) { cb(e,(e||!r.rowCount)?null:JSON.parse(r.rows[0].s_data)); } | 23 | function(e,r) { cb(e,(e||!r.rowCount)?null:JSON.parse(r.rows[0].s_data)); } |
22 | ) }; | 24 | ) }; |
23 | rv.prototype.set = function(sid,data,cb) { | 25 | rv.prototype.set = function(sid,data,cb) { |
24 | var d = JSON.stringify(data); | 26 | var d = JSON.stringify(data); |
25 | PG.Q( | 27 | PG.Q( |
26 | "UPDATE clipperz.thesession SET s_data=$1, s_mtime=current_timestamp" | 28 | "UPDATE clipperz.thesession SET s_data=$1, s_mtime=current_timestamp" |
27 | +" WHERE s_id=$2",[d,sid], function(e,r) { | 29 | +" WHERE s_id=$2",[d,sid], function(e,r) { |
28 | if(e) return cb(e); | 30 | if(e) return cb(e); |
29 | if(r.rowCount) return cb(); | 31 | if(r.rowCount) return cb(); |
30 | PG.Q("INSERT INTO clipperz.thesession (s_id,s_data) VALUES ($1,$2)",[sid,d],cb); | 32 | PG.Q("INSERT INTO clipperz.thesession (s_id,s_data) VALUES ($1,$2)",[sid,d],cb); |
31 | }); | 33 | }); |
32 | }; | 34 | }; |
33 | rv.prototype.destroy = function(sid,cb) { PG.Q( | 35 | rv.prototype.destroy = function(sid,cb) { PG.Q( |
34 | "DELETE FROM clipperz.thesession WHERE s_id=$1",[sid],cb | 36 | "DELETE FROM clipperz.thesession WHERE s_id=$1",[sid],cb |
35 | ) }; | 37 | ) }; |
36 | rv.prototype.length = function(cb) { PG.Q( | 38 | rv.prototype.length = function(cb) { PG.Q( |
@@ -550,35 +552,39 @@ var CLIPPERZ = module.exports = function(CONFIG) { | |||
550 | header: r.rv_header, data: r.rv_data, version: r.rv_version, | 552 | header: r.rv_header, data: r.rv_data, version: r.rv_version, |
551 | creationDate: r.rv_ctime.toString(), | 553 | creationDate: r.rv_ctime.toString(), |
552 | updateDate: r.rv_mtime.toString(), | 554 | updateDate: r.rv_mtime.toString(), |
553 | accessDate: r.rv_atime.toString() | 555 | accessDate: r.rv_atime.toString() |
554 | }; | 556 | }; |
555 | }); | 557 | }); |
556 | cb(null,rv); | 558 | cb(null,rv); |
557 | }); | 559 | }); |
558 | }, | 560 | }, |
559 | html: function(cb) { | 561 | html: function(cb) { |
560 | FS.readFile(CONFIG.dump_template,{encoding:'utf-8'},cb); | 562 | FS.readFile(CONFIG.dump_template,{encoding:'utf-8'},cb); |
561 | } | 563 | } |
562 | },function(e,r) { | 564 | },function(e,r) { |
563 | if(e) return cb(e); | 565 | if(e) return cb(e); |
564 | var d = new Date(); | 566 | var d = new Date(); |
565 | res.attachment('Clipperz_'+d.getFullYear()+'_'+(d.getMonth()+1)+'_'+d.getDate()+'.html'); | 567 | res.attachment('Clipperz_'+d.getFullYear()+'_'+(d.getMonth()+1)+'_'+d.getDate()+'.html'); |
566 | var ojs = { users: { | 568 | var ojs = { users: { |
567 | catchAllUser: { __masterkey_test_value__: 'masterkey', s: n123, v: n123 } | 569 | catchAllUser: { __masterkey_test_value__: 'masterkey', s: n123, v: n123 } |
568 | } }; | 570 | } }; |
569 | r.u.d.records = r.records; | 571 | r.u.d.records = r.records; |
570 | ojs.users[r.u.u] = r.u.d; | 572 | ojs.users[r.u.u] = r.u.d; |
571 | res.send(r.html.replace('/*offline_data_placeholder*/', | 573 | res.send(r.html.replace('/*offline_data_placeholder*/', |
572 | "_clipperz_dump_data_="+JSON.stringify(ojs) | 574 | "_clipperz_dump_data_="+JSON.stringify(ojs) |
573 | +";" | 575 | +";" |
574 | +"Clipperz.PM.Proxy.defaultProxy = new Clipperz.PM.Proxy.Offline();" | 576 | +"Clipperz.PM.Proxy.defaultProxy = new Clipperz.PM.Proxy.Offline();" |
575 | +"Clipperz.Crypto.PRNG.defaultRandomGenerator().fastEntropyAccumulationForTestingPurpose();")); | 577 | +"Clipperz.Crypto.PRNG.defaultRandomGenerator().fastEntropyAccumulationForTestingPurpose();")); |
576 | }); | 578 | }); |
577 | } | 579 | } |
578 | 580 | ||
579 | }; | 581 | }; |
580 | rv.__defineGetter__('session_store',function(){ return function(o) { return new (clipperz_store(PG))(o) } }); | 582 | rv.__defineGetter__('session_store',function(){ return function(o) { return new (clipperz_store(PG))(o) } }); |
581 | 583 | ||
584 | (rv.router = require('express').Router()) | ||
585 | .post('/json',rv.json).get('/dump',rv.dump) | ||
586 | .use(EXPRESS.static(PATH.join(__dirname,'htdocs/'))); | ||
587 | |||
582 | return rv; | 588 | return rv; |
583 | 589 | ||
584 | }; | 590 | }; |