Merge branch 'master' into feature/sockets-fix

This commit is contained in:
emad-salah 2021-04-20 10:25:33 +00:00
commit c6f4259a18
7 changed files with 96 additions and 45 deletions

View file

@ -1,10 +1,21 @@
// config/log.js // config/log.js
const winston = require("winston"); const winston = require("winston");
const util = require("util")
require("winston-daily-rotate-file"); require("winston-daily-rotate-file");
const winstonAttached = new Map(); const winstonAttached = new Map();
const transform = (info) => {
const args = info[Symbol.for('splat')];
if (args) {
return {...info, message: util.format(info.message, ...args)};
}
return info;
}
const logFormatter = () => ({ transform })
/** /**
* @param {string} logFileName * @param {string} logFileName
* @param {string} logLevel * @param {string} logLevel
@ -24,15 +35,18 @@ module.exports = (logFileName, logLevel) => {
winston.add(new winston.transports.Console({ winston.add(new winston.transports.Console({
format: winston.format.combine( format: winston.format.combine(
winston.format.colorize(), winston.format.colorize(),
logFormatter(),
winston.format.prettyPrint(),
winston.format.timestamp(), winston.format.timestamp(),
winston.format.simple(),
winston.format.align(), winston.format.align(),
winston.format.printf((info) => { winston.format.printf((info) => {
const { const {
timestamp, level, message, ...args timestamp, level, message
} = info; } = info;
const ts = timestamp.slice(0, 19).replace('T', ' '); const ts = timestamp.slice(0, 19).replace('T', ' ');
return `${ts} [${level}]: ${message} ${Object.keys(args).length ? JSON.stringify(args, null, 2) : ''}`; return `${ts} [${level}]: ${typeof message === "object" ? JSON.stringify(message, null, 2) : message}`;
}), }),
) )
})) }))

View file

@ -1,6 +1,6 @@
{ {
"name": "shockapi", "name": "shockapi",
"version": "2021.04.10", "version": "2021.04.19",
"description": "", "description": "",
"main": "src/server.js", "main": "src/server.js",
"scripts": { "scripts": {
@ -44,7 +44,7 @@
"jsonfile": "^4.0.0", "jsonfile": "^4.0.0",
"jsonwebtoken": "^8.3.0", "jsonwebtoken": "^8.3.0",
"localtunnel": "git://github.com/shocknet/localtunnel#40cc2c2a46b05da2217bf2e20da11a5343a5cce7", "localtunnel": "git://github.com/shocknet/localtunnel#40cc2c2a46b05da2217bf2e20da11a5343a5cce7",
"lodash": "^4.17.20", "lodash": "^4.17.21",
"method-override": "^2.3.7", "method-override": "^2.3.7",
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",
"node-persist": "^3.1.0", "node-persist": "^3.1.0",

View file

@ -3,7 +3,7 @@ const setAccessControlHeaders = (req, res) => {
res.header("Access-Control-Allow-Methods", "OPTIONS,POST,GET,PUT,DELETE") res.header("Access-Control-Allow-Methods", "OPTIONS,POST,GET,PUT,DELETE")
res.header( res.header(
"Access-Control-Allow-Headers", "Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization, public-key-for-decryption, encryption-device-id" "Origin, X-Requested-With, Content-Type, Accept, Authorization, public-key-for-decryption, encryption-device-id, public-key-for-decryption"
); );
}; };

View file

@ -695,8 +695,8 @@ module.exports = async (
let intervalID = null let intervalID = null
intervalID = setInterval(() => { intervalID = setInterval(() => {
if (tries === 3) { if (tries === 7) {
rej(new Error(`Wallet did not warm up in under 3 seconds.`)) rej(new Error(`Wallet did not warm up in under 7 seconds.`))
clearInterval(intervalID) clearInterval(intervalID)
return return

View file

@ -6,7 +6,6 @@
* Module dependencies. * Module dependencies.
*/ */
const server = program => { const server = program => {
const localtunnel = require('localtunnel')
const Http = require('http') const Http = require('http')
const Express = require('express') const Express = require('express')
const Crypto = require('crypto') const Crypto = require('crypto')
@ -15,6 +14,9 @@ const server = program => {
const Path = require('path') const Path = require('path')
const { Logger: CommonLogger } = require('shock-common') const { Logger: CommonLogger } = require('shock-common')
const binaryParser = require('socket.io-msgpack-parser') const binaryParser = require('socket.io-msgpack-parser')
const { fork } = require('child_process')
const EventEmitter = require('events')
const ECC = require('../utils/ECC') const ECC = require('../utils/ECC')
const LightningServices = require('../utils/lightningServices') const LightningServices = require('../utils/lightningServices')
const Encryption = require('../utils/encryptionStore') const Encryption = require('../utils/encryptionStore')
@ -54,6 +56,31 @@ const server = program => {
require('../utils/server-utils')(module) require('../utils/server-utils')(module)
logger.info('Mainnet Mode:', !!program.mainnet) logger.info('Mainnet Mode:', !!program.mainnet)
const tunnelTimeout = 5000
let latestAliveTunnel = 0
let tunnelHealthInterval = null
const tunnelHealthManager = new EventEmitter()
tunnelHealthManager.on('fork', ({ params, cb }) => {
if (latestAliveTunnel !== 0 && latestAliveTunnel < tunnelTimeout) {
return
}
clearInterval(tunnelHealthInterval)
tunnelHealthInterval = setInterval(() => {
if (Date.now() - latestAliveTunnel > tunnelTimeout) {
console.log('oh no! tunnel is dead, will restart it now')
tunnelHealthManager.emit('fork', { params, cb })
}
}, 2000)
const forked = fork('src/tunnel.js')
forked.on('message', msg => {
//console.log('Message from child', msg);
if (msg && msg.type === 'info') {
cb(msg.tunnel)
}
latestAliveTunnel = Date.now()
})
forked.send(params)
})
if (process.env.DISABLE_SHOCK_ENCRYPTION === 'true') { if (process.env.DISABLE_SHOCK_ENCRYPTION === 'true') {
logger.error('Encryption Mode: false') logger.error('Encryption Mode: false')
@ -203,10 +230,6 @@ const server = program => {
// eslint-disable-next-line consistent-return // eslint-disable-next-line consistent-return
const startServer = async () => { const startServer = async () => {
/**
* @type {localtunnel.Tunnel}
*/
let tunnelRef = null
try { try {
LightningServices.setDefaults(program) LightningServices.setDefaults(program)
if (!LightningServices.isInitialized()) { if (!LightningServices.isInitialized()) {
@ -284,8 +307,9 @@ const server = program => {
} else { } else {
logger.info('Creating new tunnel... ') logger.info('Creating new tunnel... ')
} }
const tunnel = await localtunnel(tunnelOpts) tunnelHealthManager.emit('fork', {
tunnelRef = tunnel params: tunnelOpts,
cb: async tunnel => {
logger.info('Tunnel created! connect to: ' + tunnel.url) logger.info('Tunnel created! connect to: ' + tunnel.url)
const dataToQr = JSON.stringify({ const dataToQr = JSON.stringify({
internalIP: tunnel.url, internalIP: tunnel.url,
@ -312,6 +336,8 @@ const server = program => {
]) ])
} }
} }
})
}
const storePersistentRandomField = async ({ fieldName, length = 16 }) => { const storePersistentRandomField = async ({ fieldName, length = 16 }) => {
const randomField = await Storage.getItem(fieldName) const randomField = await Storage.getItem(fieldName)
@ -443,9 +469,6 @@ const server = program => {
} catch (err) { } catch (err) {
logger.error({ exception: err, message: err.message, code: err.code }) logger.error({ exception: err, message: err.message, code: err.code })
logger.info('Restarting server in 30 seconds...') logger.info('Restarting server in 30 seconds...')
if (tunnelRef) {
tunnelRef.close()
}
await wait(30) await wait(30)
startServer() startServer()
return false return false

14
src/tunnel.js Normal file
View file

@ -0,0 +1,14 @@
const localtunnel = require('localtunnel')
process.on('message', async (tunnelOpts) => {
console.log('Message from parent:', tunnelOpts);
const tunnel = await localtunnel(tunnelOpts)
process.send({ type: 'info', tunnel:{
url:tunnel.url,
token:tunnel.token,
clientId:tunnel.clientId,
} });
});
setInterval(() => {
process.send({ type: "ping" });
}, 1000);

View file

@ -4687,10 +4687,10 @@ lodash@=4.17.4:
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
integrity sha1-eCA6TRwyiuHYbcpkYONptX9AVa4= integrity sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=
lodash@^4.17.11, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20, lodash@^4.17.4, lodash@^4.17.5: lodash@^4.17.11, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.21, lodash@^4.17.4, lodash@^4.17.5:
version "4.17.20" version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.20.tgz#b44a9b6297bcb698f1c51a3545a2b3b368d59c52" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA== integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
log-symbols@^3.0.0: log-symbols@^3.0.0:
version "3.0.0" version "3.0.0"