This repository has been archived by the owner on Dec 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
137 lines (120 loc) · 3.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require('dotenv').config();
const debug = require('debug');
const lDebug = debug('dTorrent:app:debug');
const lError = debug('dTorrent:app:error');
const client = require('./src/clients/client');
const serverWorker = require('./src/servers');
const manager = require('./src/manager');
const availableClients = ['rtorrent'];
let staticPID = 1;
const servers = [];
/**
* @return {Promise.<exports>}
*/
module.exports.manager = (serverName) => {
const server = servers.filter((srv) => srv.server_name === serverName)[0];
if(!server) {
// throw new Error(`This server does not exists - ${serverName}`);
return {
addListener: manager.addListener,
removeListener: manager.removeListener,
extractTorrentFile: manager.extractTorrentFile,
};
}
return {
...manager,
resume: (hash) => manager.resume(server.pid, hash),
pause: (hash) => manager.pause(server.pid, hash),
remove: (hash) => manager.remove(server.pid, hash),
};
};
/**
* Start server dTorrent
* @param config
* @return {Promise.<void>}
*/
module.exports.start = async(config) => {
try {
config = addConfig(config);
const serverPID = staticPID++;
const server = {
pid: serverPID,
server_name: config.name || `Server ${serverPID}`,
client: config.clientScript,
config,
};
servers.push(server);
client.create(server);
await serverWorker.start(server);
return serverPID;
} catch(e) {
lError(`dTorrentException : ${e}`);
}
};
/**
* @param pid
* @returns {*}
*/
module.exports.getServer = (pid) => {
for(let i=0; i<servers.length; i++) {
if(servers[i].pid === pid) {
return servers[i];
}
}
lError('getServer : This PID does not exists');
throw new Error('This PID does not exists');
};
/**
* Add config
* @param config
*/
function addConfig(config) {
lDebug('Check configuration');
if(!config) {
config = {};
}
const defaultConfig = [
{name: 'interval_check', default: 1500},
{name: 'client', default: 'rtorrent'}
];
for(let i=0; i<defaultConfig.length; i++) {
if(config[defaultConfig[i].name]) {
if(typeof config[defaultConfig[i].name] === 'string') {
config[defaultConfig[i].name] = config[defaultConfig[i].name].toLowerCase();
} else {
config[defaultConfig[i].name] = config[defaultConfig[i].name];
}
} else {
config[defaultConfig[i].name] = defaultConfig[i].default;
}
}
if(!config.client) {
throw new Error('[ConfigError] You should add client');
}
if(!config.host) {
throw new Error('[ConfigError] You should add host');
}
if(!config.port) {
throw new Error('[ConfigError] You should add port');
}
if(!config.endpoint) {
throw new Error('[ConfigError] You should add endpoint');
}
if(availableClients.indexOf(config.client) !== -1) {
config.clientScript = require(`./src/clients/${config.client}`);
} else {
throw new Error(`This client is not supported : ${config.client}`);
}
return config;
}
/**
* Launch fake torrent listener
* @param config
* @return {Promise.<void>}
*/
module.exports.fake = async(config) => {
for(const i in config) {
config[i].client = require('./tests/client/fake');
}
await module.exports.start(config);
};