fix(db): broke remote connection (security purposes)

also fix starting on used ports of db
This commit is contained in:
Alexey Kasyanchuk
2018-08-12 02:42:24 +03:00
parent 82e77640d6
commit dba71624af
9 changed files with 65 additions and 25 deletions

6
package-lock.json generated
View File

@ -11928,9 +11928,9 @@
} }
}, },
"mime": { "mime": {
"version": "2.2.0", "version": "2.3.1",
"resolved": "https://registry.npmjs.org/mime/-/mime-2.2.0.tgz", "resolved": "https://registry.npmjs.org/mime/-/mime-2.3.1.tgz",
"integrity": "sha512-0Qz9uF1ATtl8RKJG4VRfOymh7PyEor6NbrI/61lRfuRe4vx9SNATrvAeTj2EWVRKjEQGskrzWkJBBY5NbaVHIA==" "integrity": "sha512-OEUllcVoydBHGN1z84yfQDimn58pZNNNXgZlHXSboxMlFvgI6MXSWpWKpFRra7H1HxpVhHTkrghfRW49k6yjeg=="
}, },
"mime-db": { "mime-db": {
"version": "1.30.0", "version": "1.30.0",

View File

@ -127,6 +127,7 @@
"json-socket": "^0.3.0", "json-socket": "^0.3.0",
"lodash": "^4.17.5", "lodash": "^4.17.5",
"material-ui": "^0.20.0", "material-ui": "^0.20.0",
"mime": "^2.3.1",
"moment": "^2.20.1", "moment": "^2.20.1",
"mysql": "^2.15.0", "mysql": "^2.15.0",
"nat-upnp": "^1.1.1", "nat-upnp": "^1.1.1",

View File

@ -171,8 +171,8 @@ autoUpdater.on('update-downloaded', () => {
let tray = undefined let tray = undefined
app.on("ready", () => { app.on("ready", async () => {
sphinx = startSphinx(() => { sphinx = await startSphinx(() => {
mainWindow = createWindow("main", { mainWindow = createWindow("main", {
width: 1000, width: 1000,

View File

@ -28,6 +28,7 @@ let config = {
sphinx: { sphinx: {
host : '127.0.0.1', host : '127.0.0.1',
port : 9306, port : 9306,
interfacePort: 9312,
connectionLimit: 10 connectionLimit: 10
}, },
@ -107,7 +108,7 @@ const configProxy = new Proxy(config, {
}) })
config.load = () => { config.load = () => {
debug('loading configuration') debug('loading configuration', configPath)
if(fs.existsSync(configPath)) if(fs.existsSync(configPath))
{ {
debug('finded configuration', configPath) debug('finded configuration', configPath)
@ -133,4 +134,9 @@ config.load = () => {
return configProxy return configProxy
} }
config.reload = (path) => {
configPath = path + '/rats.json'
return config.load()
}
module.exports = configProxy.load() module.exports = configProxy.load()

View File

@ -246,9 +246,9 @@ module.exports = async (callback, mainWindow, sphinxApp) => {
logT('patcher', 'cleaned torrents db structure, rectreating again') logT('patcher', 'cleaned torrents db structure, rectreating again')
i = 1 i = 1
await new Promise((resolve) => { await new Promise(async (resolve) => {
// reopen sphinx // reopen sphinx
sphinxApp = sphinxApp.start(async () => { sphinxApp = await sphinxApp.start(async () => {
sphinx = await single().waitConnection() sphinx = await single().waitConnection()
resolve() resolve()
}) // same args }) // same args

View File

@ -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
})
})

View File

@ -68,14 +68,17 @@ io.on('connection', (socket) =>
} }
}) })
sphinx = startSphinx(() => { const start = async () =>
dbPatcher(() => { {
spider = new spiderCall((...data) => io.sockets.emit(...data), (message, callback) => { sphinx = await startSphinx(() => {
socketMessages[message] = callback dbPatcher(() => {
}, path.resolve(packageJson.serverDataDirectory), packageJson.version, 'production') spider = new spiderCall((...data) => io.sockets.emit(...data), (message, callback) => {
}, null, sphinx) socketMessages[message] = callback
}, path.resolve(packageJson.serverDataDirectory), () => {}) }, path.resolve(packageJson.serverDataDirectory), packageJson.version, 'production')
}, null, sphinx)
}, path.resolve(packageJson.serverDataDirectory), () => {})
}
start()
var rl = require("readline").createInterface({ var rl = require("readline").createInterface({
input: process.stdin, input: process.stdin,
@ -92,8 +95,12 @@ process.on("SIGINT", () => {
{ {
spider.stop(() => sphinx.stop(() => process.exit())) spider.stop(() => sphinx.stop(() => process.exit()))
} }
else else if(sphinx)
{ {
sphinx.stop(() => process.exit()) sphinx.stop(() => process.exit())
} }
else
{
process.exit()
}
}); });

View File

@ -11,8 +11,21 @@ const appConfig = require('./config')
const findFiles = require('./findFiles') const findFiles = require('./findFiles')
const _ = require('lodash') const _ = require('lodash')
const isRunning = require('is-running') 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 = ` let config = `
index torrents index torrents
{ {
@ -83,8 +96,8 @@ const writeSphinxConfig = (path, dbPath) => {
searchd searchd
{ {
listen = 9312 listen = 127.0.0.1:${appConfig.sphinx.interfacePort}
listen = 9306:mysql41 listen = 127.0.0.1:${appConfig.sphinx.port}:mysql41
read_timeout = 5 read_timeout = 5
max_children = 30 max_children = 30
seamless_rotate = 1 seamless_rotate = 1
@ -143,8 +156,8 @@ const writeSphinxConfig = (path, dbPath) => {
return {isInitDb} return {isInitDb}
} }
module.exports = (callback, dataDirectory, onClose) => { module.exports = async (callback, dataDirectory, onClose) => {
const start = (callback) => { const start = async (callback) => {
const sphinxPath = path.resolve(appPath('searchd')) const sphinxPath = path.resolve(appPath('searchd'))
logT('sphinx', 'Sphinx Path:', sphinxPath) logT('sphinx', 'Sphinx Path:', sphinxPath)
@ -163,7 +176,7 @@ module.exports = (callback, dataDirectory, onClose) => {
if(isSphinxExternal) if(isSphinxExternal)
logT('sphinx', `founded running sphinx instance in ${sphinxPid}, using it`) 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 config = `${sphinxConfigDirectory}/sphinx.conf`
const options = ['--config', config] const options = ['--config', config]
@ -288,7 +301,7 @@ module.exports = (callback, dataDirectory, onClose) => {
sphinx.fixing = false 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); if (isSphinxExternal && callback) setTimeout(()=>{logT('sphinx', 'external sphinx signalled');callback()}, 0);
@ -296,5 +309,5 @@ module.exports = (callback, dataDirectory, onClose) => {
return sphinx return sphinx
} }
return start(callback) return await start(callback)
} }

View File

@ -9,6 +9,8 @@ describe("application", () => {
it("check start", async function() { it("check start", async function() {
const { app } = this const { app } = this
await app.client.waitForExist('#index-window') await app.client.waitForExist('#index-window')
// fix realtime config
require('../src/background/config').reload(await app.electron.remote.app.getPath('userData'))
}); });
//TESTS //TESTS