Skip to content
This repository has been archived by the owner on Jun 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' into multiple-postcards-same-address-#168
Browse files Browse the repository at this point in the history
  • Loading branch information
fvictorio committed Jun 7, 2018
2 parents 7ea398e + cbf1542 commit 23190cf
Show file tree
Hide file tree
Showing 9 changed files with 420 additions and 8 deletions.
7 changes: 4 additions & 3 deletions web-dapp/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const config = require('./server-config');

const app = express();

Expand All @@ -17,11 +18,11 @@ app.use('/help', express.static(path.join(__dirname, 'build')));
app.use('/my-addresses', express.static(path.join(__dirname, 'build')));

// api
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true, limit: config.bodySizeLimit }));
app.use(bodyParser.json({ limit: config.bodySizeLimit }));

const routes = require('./routes')({});
app.use('/api', routes);
app.use('/confirm/api', routes);

module.exports = app;
module.exports = app;
6 changes: 6 additions & 0 deletions web-dapp/controllers/notifyRegTx.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ const validateTx = (txId, sha3cc) => {
});
};

const unlockSession = (sessionKey, prelog) => {
logger.log(`${prelog} unlocking session: ${sessionKey}`);
return db.unlock(sessionKey);
};

module.exports = {
validateData,
normalizeData,
Expand All @@ -170,4 +175,5 @@ module.exports = {
createPostCard,
removeUsedSessionKey,
validateTx,
unlockSession,
};
10 changes: 9 additions & 1 deletion web-dapp/routes/notify_reg_tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,15 @@ module.exports = () => {
})
.catch(error => {
logger.error(`${prelog} ${error.msg}`);
return sendResponse(res, { ok: false, err: error.msg });
return notifyRegTxController
.unlockSession(sessionKey, prelog)
.then(
() => {},
() => logger.error(`${prelog} Could not unlock key ${sessionKey}`)
)
.then(() => {
return sendResponse(res, { ok: false, err: error.msg });
});
});
});

Expand Down
1 change: 1 addition & 0 deletions web-dapp/server-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var cfg = {
type: 'memory',
params: {},
},
bodySizeLimit: '3kb',

rpc: 'http://localhost:8545',

Expand Down
6 changes: 3 additions & 3 deletions web-dapp/server-lib/req_id.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ function req_id(req, res, next) {
req.x_id = rnd_alphanum(config.reqIdLength);
var ip = req.headers && req.headers['x-forwarded-for'];
if (ip) {
if ( ip && (ip.indexOf(':') >= 0) && (ip.split(':').length - 1 === 1) ) {
ip = (ip.split(':'))[0];
}
if ( ip && (ip.indexOf(',') >= 0) ) {
var IPa = ip.split(',');
if ( IPa[0].startsWith('192.168.') || IPa[0].startsWith('10.') || IPa[0].startsWith('192.0.0.') || IPa[0] === 'unknown' ) {
Expand All @@ -28,6 +25,9 @@ function req_id(req, res, next) {
ip = IPa[0];
}
}
if ( ip && (ip.indexOf(':') >= 0) && (ip.split(':').length - 1 === 1) ) {
ip = (ip.split(':'))[0];
}
if (ip) {
req.x_ip = ip;
}
Expand Down
5 changes: 5 additions & 0 deletions web-dapp/server-lib/session-stores/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ module.exports = function () {
return resolve(db[k1(k)]);
});
},
unlock: (k) => {
db[k] = db[k1(k)];
delete db[k1(k)];
return Promise.resolve();
},
unset: (k) => {
delete db[k1(k)];
return new Promise((resolve) => {
Expand Down
30 changes: 29 additions & 1 deletion web-dapp/server-lib/session-stores/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@ const logger = require('../logger');
const redis = require('redis'); // https://github.com/NodeRedis/node_redis

const prelog = '[redis]';
const lockedPrefix = 'locked';

function k1(k) {
return `locked:${k}`;
return `${lockedPrefix}:${k}`;
}

function unlock(client, k) {
return new Promise((resolve, reject) => {
client.rename(k1(k), k, (err, res) => {
if (err) return reject(err);

resolve(res);
});
});
}

module.exports = function () {
Expand All @@ -30,6 +41,20 @@ module.exports = function () {
logger.error(`${prelog} connection closed`);
});

client.keys(`${lockedPrefix}:*`, (err, keys) => {
if (err) {
logger.error('Could not get keys to unlock');
return;
}

keys.forEach((key) => {
unlock(client, key.replace(`${lockedPrefix}:`, ''))
.catch((e) => {
logger.error(`Could not unlock key ${key}`, e);
});
});
});

return {
set: function (k, v) {
return new Promise((resolve) => {
Expand All @@ -55,6 +80,9 @@ module.exports = function () {
});
});
},
unlock: (k) => {
return unlock(client, k);
},
get: (k) => {
return new Promise((resolve, reject) => {
client.get(k, (err, v) => {
Expand Down
1 change: 1 addition & 0 deletions web-dapp/test/server/notify_reg_tx.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jest.mock('../../controllers/notifyRegTx', () => ({
createPostCard: jest.fn(() => Promise.resolve({ postcard: 'PostCard' })),
removeUsedSessionKey: jest.fn(() => Promise.resolve({ ok: true, result: { postcard: 'PostCard' } })),
validateTx: jest.fn(() => Promise.resolve()),
unlockSession: jest.fn(() => Promise.resolve()),
}));

describe('notify_reg_tx', () => {
Expand Down
Loading

0 comments on commit 23190cf

Please sign in to comment.