From 69a2ad1c54fad7e4ab52e0eb049f7fadf1727542 Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Thu, 24 Aug 2017 08:53:39 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B2=D0=BD=D0=B5=D1=88=D0=BD=D1=8F=D1=8F=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=84=D0=B8=D0=B3=D1=83=D1=80=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + bt/spider.js | 5 +++++ config.js | 41 +++++++++++++++++++++++++++++++++++++++-- index.js | 20 ++++++++++---------- 4 files changed, 55 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 5fa293f..2490548 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/* build/ +config.json \ No newline at end of file diff --git a/bt/spider.js b/bt/spider.js index c6e4754..9fbdf37 100644 --- a/bt/spider.js +++ b/bt/spider.js @@ -43,6 +43,7 @@ class Spider extends Emiter { this.token = new Token() this.client = client this.ignore = false; // ignore all requests + this.initialized = false; this.walkInterval = config.spider.walkInterval; this.cpuLimit = config.spider.cpuLimit; @@ -194,6 +195,10 @@ class Spider extends Emiter { } listen(port) { + if(this.initialized) + return + this.initialized = true + this.udp.bind(port) this.udp.on('listening', () => { console.log(`Listen DHT protocol on ${this.udp.address().address}:${this.udp.address().port}`) diff --git a/config.js b/config.js index 7dc6a0b..52fac50 100644 --- a/config.js +++ b/config.js @@ -1,5 +1,5 @@ -module.exports = { - indexer: true, +let config = { + indexer: false, domain: 'ratsontheboat.org', httpPort: 8095, @@ -34,3 +34,40 @@ module.exports = { spaceQuota: false, spaceDiskLimit: 7 * 1024 * 1024 * 1024, } + +const fs = require('fs'); +const debug = require('debug')('config') + +const configProxy = new Proxy(config, { + set: (target, prop, value, receiver) => { + target[prop] = value + + if(!fs.existsSync('config.json')) + fs.writeFileSync('config.json', '{}') + + fs.readFile('config.json', 'utf8', (err, data) => { + let obj = JSON.parse(data) + obj[prop] = value; + fs.writeFileSync('config.json', JSON.stringify(obj, null, 4), 'utf8'); + debug('saving config.json:', prop, '=', value) + }) + } +}) + +config.load = () => { + debug('loading configuration') + if(fs.existsSync('config.json')) + { + debug('finded configuration config.json') + const data = fs.readFileSync('config.json', 'utf8') + const obj = JSON.parse(data); + for(let prop in obj) + { + config[prop] = obj[prop] + debug('config.json:', prop, '=', obj[prop]) + } + } + return configProxy +} + +module.exports = configProxy.load() \ No newline at end of file diff --git a/index.js b/index.js index 4a4ac1c..3c7e0a2 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,4 @@ const config = require('./config'); -let settings = { - dhtDisabled: false -} const client = new (require('./bt/client')) const spider = new (require('./bt/spider'))(client) const mysql = require('mysql'); @@ -401,7 +398,9 @@ io.on('connection', function(socket) if(typeof callback != 'function') return; - callback(settings) + callback({ + dhtDisabled: !config.indexer + }) }); socket.on('setAdmin', function(options, callback) @@ -409,13 +408,15 @@ io.on('connection', function(socket) if(typeof options !== 'object') return; - settings.dhtDisabled = !!options.dhtDisabled; - spider.ignore = settings.dhtDisabled; + config.indexer = !options.dhtDisabled; + spider.ignore = !config.indexer; - if(settings.dhtDisabled) + if(!config.indexer) showFakeTorrents() - else + else { hideFakeTorrents() + spider.listen(config.spiderPort) + } if(typeof callback === 'function') callback(true) @@ -498,7 +499,7 @@ let popDatabaseBalance = () => { if(undoneQueries == 0) { balanceDebug('balance done, queries:', undoneQueries); - spider.ignore = settings.dhtDisabled; + spider.ignore = !config.indexer; } }; @@ -768,7 +769,6 @@ function hideFakeTorrents() fakeTorrentsDebug('hidding fake torrents'); } - if(config.indexer) { spider.listen(config.spiderPort) } else {