From dba71624afe9f3e4e339de580a91cd20f2a9ea70 Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Sun, 12 Aug 2018 02:42:24 +0300 Subject: [PATCH] fix(db): broke remote connection (security purposes) also fix starting on used ports of db --- package-lock.json | 6 +++--- package.json | 1 + src/background/background.js | 4 ++-- src/background/config.js | 8 +++++++- src/background/dbPatcher.js | 4 ++-- src/background/portCheck.js | 11 +++++++++++ src/background/server.js | 25 ++++++++++++++++--------- src/background/sphinx.js | 29 +++++++++++++++++++++-------- tests/init.js | 2 ++ 9 files changed, 65 insertions(+), 25 deletions(-) create mode 100644 src/background/portCheck.js diff --git a/package-lock.json b/package-lock.json index cfbf50b..cc48aa2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11928,9 +11928,9 @@ } }, "mime": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/mime/-/mime-2.2.0.tgz", - "integrity": "sha512-0Qz9uF1ATtl8RKJG4VRfOymh7PyEor6NbrI/61lRfuRe4vx9SNATrvAeTj2EWVRKjEQGskrzWkJBBY5NbaVHIA==" + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/mime/-/mime-2.3.1.tgz", + "integrity": "sha512-OEUllcVoydBHGN1z84yfQDimn58pZNNNXgZlHXSboxMlFvgI6MXSWpWKpFRra7H1HxpVhHTkrghfRW49k6yjeg==" }, "mime-db": { "version": "1.30.0", diff --git a/package.json b/package.json index ae29383..a159c37 100644 --- a/package.json +++ b/package.json @@ -127,6 +127,7 @@ "json-socket": "^0.3.0", "lodash": "^4.17.5", "material-ui": "^0.20.0", + "mime": "^2.3.1", "moment": "^2.20.1", "mysql": "^2.15.0", "nat-upnp": "^1.1.1", diff --git a/src/background/background.js b/src/background/background.js index 874b2a7..2b6c1be 100644 --- a/src/background/background.js +++ b/src/background/background.js @@ -171,8 +171,8 @@ autoUpdater.on('update-downloaded', () => { let tray = undefined -app.on("ready", () => { - sphinx = startSphinx(() => { +app.on("ready", async () => { + sphinx = await startSphinx(() => { mainWindow = createWindow("main", { width: 1000, diff --git a/src/background/config.js b/src/background/config.js index f0094fc..4edf4b8 100644 --- a/src/background/config.js +++ b/src/background/config.js @@ -28,6 +28,7 @@ let config = { sphinx: { host : '127.0.0.1', port : 9306, + interfacePort: 9312, connectionLimit: 10 }, @@ -107,7 +108,7 @@ const configProxy = new Proxy(config, { }) config.load = () => { - debug('loading configuration') + debug('loading configuration', configPath) if(fs.existsSync(configPath)) { debug('finded configuration', configPath) @@ -133,4 +134,9 @@ config.load = () => { return configProxy } +config.reload = (path) => { + configPath = path + '/rats.json' + return config.load() +} + module.exports = configProxy.load() \ No newline at end of file diff --git a/src/background/dbPatcher.js b/src/background/dbPatcher.js index a200c9a..cc90343 100644 --- a/src/background/dbPatcher.js +++ b/src/background/dbPatcher.js @@ -246,9 +246,9 @@ module.exports = async (callback, mainWindow, sphinxApp) => { logT('patcher', 'cleaned torrents db structure, rectreating again') i = 1 - await new Promise((resolve) => { + await new Promise(async (resolve) => { // reopen sphinx - sphinxApp = sphinxApp.start(async () => { + sphinxApp = await sphinxApp.start(async () => { sphinx = await single().waitConnection() resolve() }) // same args diff --git a/src/background/portCheck.js b/src/background/portCheck.js new file mode 100644 index 0000000..11f0a9b --- /dev/null +++ b/src/background/portCheck.js @@ -0,0 +1,11 @@ +const net = require('net') + +module.exports = (port, host = '127.0.0.1') => new Promise((resolve, reject) => { + const tester = net.createServer() + .once('error', err => (err.code === 'EADDRINUSE' ? resolve(false) : reject(err))) + .once('listening', () => tester.once('close', () => resolve(true)).close()) + .listen({ + host, + port + }) +}) diff --git a/src/background/server.js b/src/background/server.js index fd28bdc..191625b 100644 --- a/src/background/server.js +++ b/src/background/server.js @@ -68,14 +68,17 @@ io.on('connection', (socket) => } }) -sphinx = startSphinx(() => { - dbPatcher(() => { - spider = new spiderCall((...data) => io.sockets.emit(...data), (message, callback) => { - socketMessages[message] = callback - }, path.resolve(packageJson.serverDataDirectory), packageJson.version, 'production') - }, null, sphinx) -}, path.resolve(packageJson.serverDataDirectory), () => {}) - +const start = async () => +{ + sphinx = await startSphinx(() => { + dbPatcher(() => { + spider = new spiderCall((...data) => io.sockets.emit(...data), (message, callback) => { + socketMessages[message] = callback + }, path.resolve(packageJson.serverDataDirectory), packageJson.version, 'production') + }, null, sphinx) + }, path.resolve(packageJson.serverDataDirectory), () => {}) +} +start() var rl = require("readline").createInterface({ input: process.stdin, @@ -92,8 +95,12 @@ process.on("SIGINT", () => { { spider.stop(() => sphinx.stop(() => process.exit())) } - else + else if(sphinx) { sphinx.stop(() => process.exit()) } + else + { + process.exit() + } }); \ No newline at end of file diff --git a/src/background/sphinx.js b/src/background/sphinx.js index 80ef98c..eed25b3 100644 --- a/src/background/sphinx.js +++ b/src/background/sphinx.js @@ -11,8 +11,21 @@ const appConfig = require('./config') const findFiles = require('./findFiles') const _ = require('lodash') const isRunning = require('is-running') +const portCheck = require('./portCheck') + +const findGoodPort = async (port, host) => { + while (!(await portCheck(port, host))) { + port++ + logT('sphinx', 'port is busy, listen on', port) + } + return port +} + +const writeSphinxConfig = async (path, dbPath) => { + appConfig.sphinx.port = await findGoodPort(appConfig.sphinx.port) + appConfig.sphinx.interfacePort = await findGoodPort(appConfig.sphinx.interfacePort) + appConfig.sphinx = appConfig.sphinx -const writeSphinxConfig = (path, dbPath) => { let config = ` index torrents { @@ -83,8 +96,8 @@ const writeSphinxConfig = (path, dbPath) => { searchd { - listen = 9312 - listen = 9306:mysql41 + listen = 127.0.0.1:${appConfig.sphinx.interfacePort} + listen = 127.0.0.1:${appConfig.sphinx.port}:mysql41 read_timeout = 5 max_children = 30 seamless_rotate = 1 @@ -143,8 +156,8 @@ const writeSphinxConfig = (path, dbPath) => { return {isInitDb} } -module.exports = (callback, dataDirectory, onClose) => { - const start = (callback) => { +module.exports = async (callback, dataDirectory, onClose) => { + const start = async (callback) => { const sphinxPath = path.resolve(appPath('searchd')) logT('sphinx', 'Sphinx Path:', sphinxPath) @@ -163,7 +176,7 @@ module.exports = (callback, dataDirectory, onClose) => { if(isSphinxExternal) logT('sphinx', `founded running sphinx instance in ${sphinxPid}, using it`) - const { isInitDb } = isSphinxExternal ? {isInitDb: false} : writeSphinxConfig(sphinxConfigDirectory, appConfig.dbPath) + const { isInitDb } = isSphinxExternal ? {isInitDb: false} : await writeSphinxConfig(sphinxConfigDirectory, appConfig.dbPath) const config = `${sphinxConfigDirectory}/sphinx.conf` const options = ['--config', config] @@ -288,7 +301,7 @@ module.exports = (callback, dataDirectory, onClose) => { sphinx.fixing = false - _.merge(sphinx, sphinx.start(callback)); + _.merge(sphinx, await sphinx.start(callback)); } if (isSphinxExternal && callback) setTimeout(()=>{logT('sphinx', 'external sphinx signalled');callback()}, 0); @@ -296,5 +309,5 @@ module.exports = (callback, dataDirectory, onClose) => { return sphinx } - return start(callback) + return await start(callback) } \ No newline at end of file diff --git a/tests/init.js b/tests/init.js index e911421..7da3134 100644 --- a/tests/init.js +++ b/tests/init.js @@ -9,6 +9,8 @@ describe("application", () => { it("check start", async function() { const { app } = this await app.client.waitForExist('#index-window') + // fix realtime config + require('../src/background/config').reload(await app.electron.remote.app.getPath('userData')) }); //TESTS