-rw-r--r-- | backend/node/src/app.js | 18 | ||||
-rw-r--r-- | backend/node/src/clipperz.js | 2 | ||||
-rw-r--r-- | backend/node/src/package.json | 15 |
3 files changed, 20 insertions, 15 deletions
diff --git a/backend/node/src/app.js b/backend/node/src/app.js index 61c2c72..ca05e6f 100644 --- a/backend/node/src/app.js +++ b/backend/node/src/app.js | |||
@@ -1,51 +1,51 @@ | |||
1 | var BUNYAN = require('bunyan'); | 1 | var BUNYAN = require('bunyan'); |
2 | var LOGGER = BUNYAN.createLogger({ | 2 | var LOGGER = BUNYAN.createLogger({ |
3 | name: 'clipperz', | 3 | name: 'clipperz', |
4 | streams: [ | 4 | streams: [ |
5 | { name: "console", stream:process.stderr,level:'trace'} | 5 | { name: "console", stream:process.stderr,level:'trace'} |
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(EXPRESS.logger('dev')); | 33 | app.use(require('morgan')('dev')); |
34 | app.use(EXPRESS.urlencoded()); | 34 | app.use(require('body-parser').urlencoded({extended:true})); |
35 | app.use(EXPRESS.methodOverride()); | 35 | app.use(require('cookie-parser')('your secret here')); |
36 | app.use(EXPRESS.cookieParser('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 })); |
37 | app.use(EXPRESS.session({secret:'99 little bugs in the code', key:'sid', store: clipperz.session_store() })); | 37 | |
38 | app.use(app.router); | 38 | app.post('/json',clipperz.json); |
39 | app.get('/beta/dump',clipperz.dump); | ||
40 | |||
39 | app.use(EXPRESS.static(PATH.join(__dirname, 'htdocs/'))); | 41 | app.use(EXPRESS.static(PATH.join(__dirname, 'htdocs/'))); |
40 | if ('development' == app.get('env')) { | 42 | if ('development' == app.get('env')) { |
41 | app.use(EXPRESS.errorHandler()); | 43 | app.use(require('express-error-with-sources')()); |
42 | } | 44 | } |
43 | 45 | ||
44 | 46 | ||
45 | app.post('/json',clipperz.json); | ||
46 | app.get('/beta/dump',clipperz.dump); | ||
47 | 47 | ||
48 | 48 | ||
49 | HTTP.createServer(app).listen(app.get('port'), function(){ | 49 | HTTP.createServer(app).listen(app.get('port'), function(){ |
50 | LOGGER.info({port:app.get('port')},"Listener established"); | 50 | LOGGER.info({port:app.get('port')},"Listener established"); |
51 | }); | 51 | }); |
diff --git a/backend/node/src/clipperz.js b/backend/node/src/clipperz.js index 04b054a..c51b0bc 100644 --- a/backend/node/src/clipperz.js +++ b/backend/node/src/clipperz.js | |||
@@ -1,134 +1,134 @@ | |||
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 | 5 | ||
6 | var express_store = require('express').session.Store; | 6 | var express_store = require('express-session').Store; |
7 | 7 | ||
8 | function clipperz_hash(v) { | 8 | function clipperz_hash(v) { |
9 | return CRYPTO.createHash('sha256').update( | 9 | return CRYPTO.createHash('sha256').update( |
10 | CRYPTO.createHash('sha256').update(v).digest('binary') | 10 | CRYPTO.createHash('sha256').update(v).digest('binary') |
11 | ).digest('hex'); | 11 | ).digest('hex'); |
12 | }; | 12 | }; |
13 | function clipperz_random() { | 13 | function clipperz_random() { |
14 | for(var r = '';r.length<64;r+=''+BIGNUM(Math.floor(Math.random()*1e18)).toString(16)); | 14 | for(var r = '';r.length<64;r+=''+BIGNUM(Math.floor(Math.random()*1e18)).toString(16)); |
15 | return r.substr(0,64); | 15 | return r.substr(0,64); |
16 | }; | 16 | }; |
17 | function clipperz_store(PG) { | 17 | function clipperz_store(PG) { |
18 | var rv = function(o) { express_store.call(this,o); } | 18 | var rv = function(o) { express_store.call(this,o); } |
19 | rv.prototype.get = function(sid,cb) { PG.Q( | 19 | rv.prototype.get = function(sid,cb) { PG.Q( |
20 | "SELECT s_data FROM clipperz.thesession WHERE s_id=$1",[sid], | 20 | "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)); } | 21 | function(e,r) { cb(e,(e||!r.rowCount)?null:JSON.parse(r.rows[0].s_data)); } |
22 | ) }; | 22 | ) }; |
23 | rv.prototype.set = function(sid,data,cb) { | 23 | rv.prototype.set = function(sid,data,cb) { |
24 | var d = JSON.stringify(data); | 24 | var d = JSON.stringify(data); |
25 | PG.Q( | 25 | PG.Q( |
26 | "UPDATE clipperz.thesession SET s_data=$1, s_mtime=current_timestamp" | 26 | "UPDATE clipperz.thesession SET s_data=$1, s_mtime=current_timestamp" |
27 | +" WHERE s_id=$2",[d,sid], function(e,r) { | 27 | +" WHERE s_id=$2",[d,sid], function(e,r) { |
28 | if(e) return cb(e); | 28 | if(e) return cb(e); |
29 | if(r.rowCount) return cb(); | 29 | if(r.rowCount) return cb(); |
30 | PG.Q("INSERT INTO clipperz.thesession (s_id,s_data) VALUES ($1,$2)",[sid,d],cb); | 30 | PG.Q("INSERT INTO clipperz.thesession (s_id,s_data) VALUES ($1,$2)",[sid,d],cb); |
31 | }); | 31 | }); |
32 | }; | 32 | }; |
33 | rv.prototype.destroy = function(sid,cb) { PG.Q( | 33 | rv.prototype.destroy = function(sid,cb) { PG.Q( |
34 | "DELETE FROM clipperz.thesession WHERE s_id=$1",[sid],cb | 34 | "DELETE FROM clipperz.thesession WHERE s_id=$1",[sid],cb |
35 | ) }; | 35 | ) }; |
36 | rv.prototype.length = function(cb) { PG.Q( | 36 | rv.prototype.length = function(cb) { PG.Q( |
37 | "SELECT count(*) AS c FROM clipperz.thesession", function(e,r) { | 37 | "SELECT count(*) AS c FROM clipperz.thesession", function(e,r) { |
38 | cb(e,e?null:r.rows[0].c); | 38 | cb(e,e?null:r.rows[0].c); |
39 | } | 39 | } |
40 | ) }; | 40 | ) }; |
41 | rv.prototype.length = function(cb) { PQ.Q( | 41 | rv.prototype.length = function(cb) { PQ.Q( |
42 | "DELETE FROM clipperz.thesession", cb | 42 | "DELETE FROM clipperz.thesession", cb |
43 | ) }; | 43 | ) }; |
44 | rv.prototype.__proto__ = express_store.prototype; | 44 | rv.prototype.__proto__ = express_store.prototype; |
45 | return rv; | 45 | return rv; |
46 | } | 46 | } |
47 | 47 | ||
48 | var srp_g = BIGNUM(2); | 48 | var srp_g = BIGNUM(2); |
49 | var srp_n = BIGNUM("115b8b692e0e045692cf280b436735c77a5a9e8a9e7ed56c965f87db5b2a2ece3",16); | 49 | var srp_n = BIGNUM("115b8b692e0e045692cf280b436735c77a5a9e8a9e7ed56c965f87db5b2a2ece3",16); |
50 | var n123 = '112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00'; | 50 | var n123 = '112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00'; |
51 | 51 | ||
52 | 52 | ||
53 | var CLIPPERZ = module.exports = function(CONFIG) { | 53 | var CLIPPERZ = module.exports = function(CONFIG) { |
54 | 54 | ||
55 | var LOGGER = CONFIG.logger||{trace:function(){}}; | 55 | var LOGGER = CONFIG.logger||{trace:function(){}}; |
56 | 56 | ||
57 | var PG = { | 57 | var PG = { |
58 | url: CONFIG.psql, | 58 | url: CONFIG.psql, |
59 | PG: require('pg').native, | 59 | PG: require('pg').native, |
60 | Q: function(q,a,cb) { | 60 | Q: function(q,a,cb) { |
61 | if('function'===typeof a) cb=a,a=[]; | 61 | if('function'===typeof a) cb=a,a=[]; |
62 | LOGGER.trace({query:q,args:a},'SQL: %s',q); | 62 | LOGGER.trace({query:q,args:a},'SQL: %s',q); |
63 | PG.PG.connect(PG.url,function(e,C,D) { | 63 | PG.PG.connect(PG.url,function(e,C,D) { |
64 | if(e) return cb(e); | 64 | if(e) return cb(e); |
65 | var t0=new Date(); | 65 | var t0=new Date(); |
66 | C.query(q,a,function(e,r) { | 66 | C.query(q,a,function(e,r) { |
67 | var t1=new Date(), dt=t1-t0; | 67 | var t1=new Date(), dt=t1-t0; |
68 | D(); | 68 | D(); |
69 | LOGGER.trace({query:q,args:a,ms:dt,rows:r&&r.rowCount,err:e},"SQL query '%s' took %dms",q,dt); | 69 | LOGGER.trace({query:q,args:a,ms:dt,rows:r&&r.rowCount,err:e},"SQL query '%s' took %dms",q,dt); |
70 | cb(e,r); | 70 | cb(e,r); |
71 | }); | 71 | }); |
72 | }); | 72 | }); |
73 | }, | 73 | }, |
74 | T: function(cb) { | 74 | T: function(cb) { |
75 | PG.PG.connect(PG.url,function(e,C,D) { | 75 | PG.PG.connect(PG.url,function(e,C,D) { |
76 | if(e) return cb(e); | 76 | if(e) return cb(e); |
77 | C.query('BEGIN',function(e){ | 77 | C.query('BEGIN',function(e){ |
78 | if(e) return D(),cb(e); | 78 | if(e) return D(),cb(e); |
79 | LOGGER.trace('SQL: transaction begun'); | 79 | LOGGER.trace('SQL: transaction begun'); |
80 | cb(null,{ | 80 | cb(null,{ |
81 | Q: function(q,a,cb) { | 81 | Q: function(q,a,cb) { |
82 | LOGGER.trace({query:q,args:a},'SQL: %s',q); | 82 | LOGGER.trace({query:q,args:a},'SQL: %s',q); |
83 | if(this.over) return cb(new Error('game over')); | 83 | if(this.over) return cb(new Error('game over')); |
84 | if('function'===typeof a) cb=a,a=[]; | 84 | if('function'===typeof a) cb=a,a=[]; |
85 | var t0=new Date(); | 85 | var t0=new Date(); |
86 | C.query(q,a,function(e,r) { | 86 | C.query(q,a,function(e,r) { |
87 | var t1=new Date(), dt=t1-t0; | 87 | var t1=new Date(), dt=t1-t0; |
88 | LOGGER.trace({query:q,args:a,ms:dt,rows:r&&r.rowCount,err:e},"SQL query '%s' took %dms",q,dt); | 88 | LOGGER.trace({query:q,args:a,ms:dt,rows:r&&r.rowCount,err:e},"SQL query '%s' took %dms",q,dt); |
89 | cb(e,r); | 89 | cb(e,r); |
90 | }); | 90 | }); |
91 | }, | 91 | }, |
92 | commit: function(cb) { | 92 | commit: function(cb) { |
93 | LOGGER.trace('SQL: commit'); | 93 | LOGGER.trace('SQL: commit'); |
94 | if(this.over) return cb(new Error('game over')); | 94 | if(this.over) return cb(new Error('game over')); |
95 | return (this.over=true),C.query('COMMIT',function(e){D();cb&&cb(e)}); | 95 | return (this.over=true),C.query('COMMIT',function(e){D();cb&&cb(e)}); |
96 | }, | 96 | }, |
97 | rollback: function(cb) { | 97 | rollback: function(cb) { |
98 | LOGGER.trace('SQL: rollback'); | 98 | LOGGER.trace('SQL: rollback'); |
99 | if(this.over) return cb(new Error('game over')); | 99 | if(this.over) return cb(new Error('game over')); |
100 | return (this.over=true),C.query('ROLLBACK',function(e){D();cb&&cb(e)}); | 100 | return (this.over=true),C.query('ROLLBACK',function(e){D();cb&&cb(e)}); |
101 | }, | 101 | }, |
102 | end: function(e,cb) { | 102 | end: function(e,cb) { |
103 | if(e) return LOGGER.trace(e,"rolling back transaction due to an error"),this.rollback(cb); | 103 | if(e) return LOGGER.trace(e,"rolling back transaction due to an error"),this.rollback(cb); |
104 | this.commit(cb); | 104 | this.commit(cb); |
105 | } | 105 | } |
106 | }); | 106 | }); |
107 | }); | 107 | }); |
108 | }); | 108 | }); |
109 | } | 109 | } |
110 | }; | 110 | }; |
111 | 111 | ||
112 | 112 | ||
113 | var rv = { | 113 | var rv = { |
114 | 114 | ||
115 | json: function clipperz_json(req,res,cb) { | 115 | json: function clipperz_json(req,res,cb) { |
116 | var method = req.body.method, pp = JSON.parse(req.body.parameters).parameters; | 116 | var method = req.body.method, pp = JSON.parse(req.body.parameters).parameters; |
117 | var message = pp.message; | 117 | var message = pp.message; |
118 | var ppp = pp.parameters; | 118 | var ppp = pp.parameters; |
119 | res.res = function(o) { return res.json({result:o}) }; | 119 | res.res = function(o) { return res.json({result:o}) }; |
120 | LOGGER.trace({method:method,parameters:pp},"JSON request"); | 120 | LOGGER.trace({method:method,parameters:pp},"JSON request"); |
121 | 121 | ||
122 | switch(method) { | 122 | switch(method) { |
123 | case 'registration': | 123 | case 'registration': |
124 | switch(message) { | 124 | switch(message) { |
125 | case 'completeRegistration': return PG.Q( | 125 | case 'completeRegistration': return PG.Q( |
126 | "INSERT INTO clipperz.theuser" | 126 | "INSERT INTO clipperz.theuser" |
127 | +" (u_name, u_srp_s,u_srp_v, u_authversion,u_header,u_statistics,u_version,u_lock)" | 127 | +" (u_name, u_srp_s,u_srp_v, u_authversion,u_header,u_statistics,u_version,u_lock)" |
128 | +" VALUES ($1, $2,$3, $4,$5,$6,$7,$8)", | 128 | +" VALUES ($1, $2,$3, $4,$5,$6,$7,$8)", |
129 | [pp.credentials.C, pp.credentials.s, pp.credentials.v, | 129 | [pp.credentials.C, pp.credentials.s, pp.credentials.v, |
130 | pp.credentials.version,pp.user.header, pp.user.statistics, | 130 | pp.credentials.version,pp.user.header, pp.user.statistics, |
131 | pp.user.version, pp.user.lock], function(e,r) { | 131 | pp.user.version, pp.user.lock], function(e,r) { |
132 | if(e) return cb(e); | 132 | if(e) return cb(e); |
133 | res.res({lock:pp.user.lock,result:'done'}); | 133 | res.res({lock:pp.user.lock,result:'done'}); |
134 | }); | 134 | }); |
diff --git a/backend/node/src/package.json b/backend/node/src/package.json index 825f756..98755bd 100644 --- a/backend/node/src/package.json +++ b/backend/node/src/package.json | |||
@@ -1,15 +1,20 @@ | |||
1 | { | 1 | { |
2 | "name": "clipperz", | 2 | "name": "clipperz", |
3 | "version": "0.0.0", | 3 | "version": "0.0.0", |
4 | "private": true, | 4 | "private": true, |
5 | "scripts": { | 5 | "scripts": { |
6 | "start": "node app 2>&1 | ./node_modules/.bin/bunyan" | 6 | "start": "node app 2>&1 | ./node_modules/.bin/bunyan" |
7 | }, | 7 | }, |
8 | "dependencies": { | 8 | "dependencies": { |
9 | "express": "3.4.4", | 9 | "async": "^0.9.0", |
10 | "async": "~0.2.9", | 10 | "bignum": "^0.8.0", |
11 | "bignum": "~0.6.2", | 11 | "body-parser": "^1.4.3", |
12 | "pg": "~2.8.3", | 12 | "bunyan": "^0.23.1", |
13 | "bunyan": "~0.22.0" | 13 | "cookie-parser": "^1.3.2", |
14 | "express": "^4.4.5", | ||
15 | "express-error-with-sources": "^1.3.1", | ||
16 | "express-session": "^1.6.1", | ||
17 | "morgan": "^1.1.1", | ||
18 | "pg": "^3.3.0" | ||
14 | } | 19 | } |
15 | } | 20 | } |