внешняя конфигурация
This commit is contained in:
parent
2342035af7
commit
69a2ad1c54
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
node_modules/*
|
||||
build/
|
||||
config.json
|
@ -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}`)
|
||||
|
41
config.js
41
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()
|
20
index.js
20
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 {
|
||||
|
Loading…
Reference in New Issue
Block a user