внешняя конфигурация

This commit is contained in:
Alexey Kasyanchuk 2017-08-24 08:53:39 +03:00
parent 2342035af7
commit 69a2ad1c54
4 changed files with 55 additions and 12 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
node_modules/* node_modules/*
build/ build/
config.json

View File

@ -43,6 +43,7 @@ class Spider extends Emiter {
this.token = new Token() this.token = new Token()
this.client = client this.client = client
this.ignore = false; // ignore all requests this.ignore = false; // ignore all requests
this.initialized = false;
this.walkInterval = config.spider.walkInterval; this.walkInterval = config.spider.walkInterval;
this.cpuLimit = config.spider.cpuLimit; this.cpuLimit = config.spider.cpuLimit;
@ -194,6 +195,10 @@ class Spider extends Emiter {
} }
listen(port) { listen(port) {
if(this.initialized)
return
this.initialized = true
this.udp.bind(port) this.udp.bind(port)
this.udp.on('listening', () => { this.udp.on('listening', () => {
console.log(`Listen DHT protocol on ${this.udp.address().address}:${this.udp.address().port}`) console.log(`Listen DHT protocol on ${this.udp.address().address}:${this.udp.address().port}`)

View File

@ -1,5 +1,5 @@
module.exports = { let config = {
indexer: true, indexer: false,
domain: 'ratsontheboat.org', domain: 'ratsontheboat.org',
httpPort: 8095, httpPort: 8095,
@ -34,3 +34,40 @@ module.exports = {
spaceQuota: false, spaceQuota: false,
spaceDiskLimit: 7 * 1024 * 1024 * 1024, 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()

View File

@ -1,7 +1,4 @@
const config = require('./config'); const config = require('./config');
let settings = {
dhtDisabled: false
}
const client = new (require('./bt/client')) const client = new (require('./bt/client'))
const spider = new (require('./bt/spider'))(client) const spider = new (require('./bt/spider'))(client)
const mysql = require('mysql'); const mysql = require('mysql');
@ -401,7 +398,9 @@ io.on('connection', function(socket)
if(typeof callback != 'function') if(typeof callback != 'function')
return; return;
callback(settings) callback({
dhtDisabled: !config.indexer
})
}); });
socket.on('setAdmin', function(options, callback) socket.on('setAdmin', function(options, callback)
@ -409,13 +408,15 @@ io.on('connection', function(socket)
if(typeof options !== 'object') if(typeof options !== 'object')
return; return;
settings.dhtDisabled = !!options.dhtDisabled; config.indexer = !options.dhtDisabled;
spider.ignore = settings.dhtDisabled; spider.ignore = !config.indexer;
if(settings.dhtDisabled) if(!config.indexer)
showFakeTorrents() showFakeTorrents()
else else {
hideFakeTorrents() hideFakeTorrents()
spider.listen(config.spiderPort)
}
if(typeof callback === 'function') if(typeof callback === 'function')
callback(true) callback(true)
@ -498,7 +499,7 @@ let popDatabaseBalance = () => {
if(undoneQueries == 0) if(undoneQueries == 0)
{ {
balanceDebug('balance done, queries:', undoneQueries); balanceDebug('balance done, queries:', undoneQueries);
spider.ignore = settings.dhtDisabled; spider.ignore = !config.indexer;
} }
}; };
@ -768,7 +769,6 @@ function hideFakeTorrents()
fakeTorrentsDebug('hidding fake torrents'); fakeTorrentsDebug('hidding fake torrents');
} }
if(config.indexer) { if(config.indexer) {
spider.listen(config.spiderPort) spider.listen(config.spiderPort)
} else { } else {