/
adminAccount.js
374 lines (361 loc) · 13.1 KB
/
adminAccount.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
const macAccount = appRequire('plugins/macAccount/index');
const account = appRequire('plugins/account/index');
const dns = require('dns');
const net = require('net');
const knex = appRequire('init/knex').knex;
const flowPlugin = appRequire('plugins/flowSaver/flow');
const moment = require('moment');
const formatMacAddress = mac => mac.replace(/-/g, '').replace(/:/g, '').toLowerCase();
exports.getMacAccount = (req, res) => {
const userId = +req.query.userId;
macAccount.getAccount(userId, -1).then(success => {
res.send(success);
}).catch(err => {
console.log(err);
res.status(403).end();
});
};
exports.addMacAccount = (req, res) => {
const mac = formatMacAddress(req.params.macAddress);
const userId = req.body.userId;
const accountId = req.body.accountId;
const serverId = req.body.serverId;
macAccount.newAccount(mac, userId, serverId, accountId).then(success => {
res.send('success');
}).catch(err => {
console.log(err);
res.status(403).end();
});
};
exports.editMacAccount = (req, res) => {
const id = req.body.id;
const mac = formatMacAddress(req.body.macAddress);
const userId = req.body.userId;
const accountId = req.body.accountId;
const serverId = req.body.serverId;
macAccount.editAccount(id, mac, serverId, accountId).then(success => {
res.send('success');
}).catch(err => {
console.log(err);
res.status(403).end();
});
};
exports.deleteMacAccount = (req, res) => {
const accountId = +req.query.id;
macAccount.deleteAccount(accountId).then(success => {
res.send('success');
}).catch(err => {
console.log(err);
res.status(403).end();
});
};
exports.getMacAccountForUser = (req, res) => {
const mac = req.params.macAddress;
const ip = req.headers['x-real-ip'] || req.connection.remoteAddress;
const noPassword = !!(+req.query.noPassword);
const noFlow = !!(+req.query.noFlow);
const type = req.query.type || 'Shadowsocks';
macAccount.getAccountForUser(mac.toLowerCase(), ip, {
type,
noPassword,
noFlow,
}).then(success => {
res.send(success);
}).catch(err => {
console.log(err);
res.status(403).end();
});
};
exports.getNoticeForUser = (req, res) => {
const mac = req.params.macAddress;
const ip = req.headers['x-real-ip'] || req.connection.remoteAddress;
macAccount
.getNoticeForUser(mac.toLowerCase(), ip)
.then(success => {
res.send(success);
}).catch(err => {
console.log(err);
res.status(403).end();
});
};
exports.banAccount = (req, res) => {
const serverId = +req.params.serverId;
const accountId = +req.params.accountId;
const time = +req.body.time;
account.banAccount({
serverId,
accountId,
time,
}).then(success => {
res.send('success');
}).catch(err => {
console.log(err);
res.status(403).end();
});
};
exports.getBanAccount = (req, res) => {
const serverId = +req.params.serverId;
const accountId = +req.params.accountId;
account.getBanAccount({
serverId,
accountId,
}).then(success => {
res.send(success);
}).catch(err => {
console.log(err);
res.status(403).end();
});
};
const isMacAddress = str => {
return str.match(/^([A-Fa-f0-9]{2}[:-]?){5}[A-Fa-f0-9]{2}$/);
};
const getAddress = (address, ip) => {
let myAddress = address;
if(address.indexOf(':') >= 0) {
const hosts = address.split(':');
const number = Math.ceil(Math.random() * (hosts.length - 1));
myAddress = hosts[number];
}
if(!ip) {
return Promise.resolve(myAddress);
}
if(net.isIP(myAddress)) {
return Promise.resolve(myAddress);
}
return new Promise((resolve, reject) => {
dns.lookup(myAddress, (err, ip, family) => {
if(err) {
return resolve(myAddress);
}
return resolve(ip);
});
});
};
const urlsafeBase64 = str => {
return Buffer.from(str).toString('base64').replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
};
exports.getSubscribeAccountForUser = async (req, res) => {
try {
const ssr = req.query.ssr;
let type = req.query.type || 'shadowrocket';
if(ssr === '1') { type = 'ssr'; }
const resolveIp = req.query.ip;
const showFlow = req.query.flow || 0;
const token = req.params.token;
const ip = req.headers['x-real-ip'] || req.connection.remoteAddress;
let subscribeAccount = {};
let trojanServers = [];
if(isMacAddress(token)) {
subscribeAccount = await macAccount.getMacAccountForSubscribe(token, ip);
} else {
const isSubscribeOn = await knex('webguiSetting').where({
key: 'account'
}).then(s => s[0]).then(s => JSON.parse(s.value).subscribe);
if(!isSubscribeOn) { return res.status(404).end(); }
const accounts = await account.getAccountForSubscribe(token, ip);
subscribeAccount = {
...accounts,
server: accounts.server.filter(server => server.type === 'Shadowsocks'),
};
trojanServers = accounts.server.filter(server => server.type === 'Trojan');
}
for(const s of subscribeAccount.server) {
s.host = await getAddress(s.host, +resolveIp);
}
for(const s of trojanServers) {
if (s.host.split(':').length !== 1) {
const hosts = s.host.split(':');
const number = Math.ceil(Math.random() * (hosts.length - 1));
s.host = hosts[number];
}
}
const baseSetting = await knex('webguiSetting').where({
key: 'base'
}).then(s => s[0]).then(s => JSON.parse(s.value));
const ssdInfo = {
airport: 'ssmgr',
port: subscribeAccount.account.port,
encryption: 'aes-256-gcm',
password: subscribeAccount.account.password,
servers: subscribeAccount.server.filter(s => !s.subscribeName).map(s => {
return {
id: s.id,
server: s.host,
port: subscribeAccount.account.port + s.shift,
encryption: s.method,
remarks: s.name,
};
}),
};
if(subscribeAccount.account.type !== 1 && +showFlow) {
const random = Math.floor(Math.random() * 9999) % (subscribeAccount.server.length - 1);
const insert = JSON.parse(JSON.stringify(subscribeAccount.server[random]));
const time = {
'2': 7 * 24 * 3600000,
'3': 30 * 24 * 3600000,
'4': 24 * 3600000,
'5': 3600000,
};
const expire = subscribeAccount.account.data.create + subscribeAccount.account.data.limit * time[subscribeAccount.account.type];
ssdInfo.expiry = moment(expire).format('YYYY-MM-DD HH:mm:ss');
if(Date.now() >= expire) {
insert.subscribeName = '已过期';
} else if((expire - Date.now()) >= 48 * 3600 * 1000) {
insert.subscribeName = moment(expire).format('YYYY-MM-DD过期');
} else if((expire - Date.now()) >= 3600 * 1000) {
insert.subscribeName = (Math.floor((expire - Date.now())/(3600 * 1000))) + '小时后过期';
} else if((expire - Date.now()) > 0) {
insert.subscribeName = (Math.floor((expire - Date.now())/(60 * 1000))) + '分钟后过期';
}
let insertFlow;
if(subscribeAccount.account.multiServerFlow) {
insertFlow = JSON.parse(JSON.stringify(subscribeAccount.server[random]));
const flow = subscribeAccount.account.data.flow;
const time = {
'2': 7 * 24 * 3600000,
'3': 30 * 24 * 3600000,
'4': 24 * 3600000,
'5': 3600000,
};
let from = subscribeAccount.account.data.create;
let to = subscribeAccount.account.data.create + time[subscribeAccount.account.type];
while(to <= Date.now()) {
from = to;
to = from + time[subscribeAccount.account.type];
}
const [ currentFlow ] = await flowPlugin.getServerPortFlowWithScale(insertFlow.id, subscribeAccount.account.id, [from, to], true);
ssdInfo.traffic_used = currentFlow / (1000 * 1000 * 1000);
ssdInfo.traffic_total = flow / (1000 * 1000 * 1000);
const toFlowString = input => {
const K = 1000;
const M = 1000 * 1000;
const G = 1000 * 1000 * 1000;
const T = 1000 * 1000 * 1000 * 1000;
const P = 1000 * 1000 * 1000 * 1000 * 1000;
if (input < K) {
return input + 'B';
} else if (input < M) {
return (input / K).toFixed(1) + 'KB';
} else if (input < G) {
return (input / M).toFixed(1) + 'MB';
} else if (input < T) {
return (input / G).toFixed(2) + 'GB';
} else if (input < P) {
return (input / T).toFixed(3) + 'TB';
} else {
return input;
}
};
insertFlow.subscribeName = toFlowString(currentFlow) + '/' + toFlowString(flow);
}
subscribeAccount.server.unshift(insert);
if(insertFlow) { subscribeAccount.server.unshift(insertFlow); }
}
if(type === 'ssd') {
return res.send('ssd://' + Buffer.from(JSON.stringify(ssdInfo)).toString('base64'));
}
if(type === 'clash') {
const yaml = require('js-yaml');
const clashConfig = appRequire('plugins/webgui/server/clash');
const proxyNames = [
...subscribeAccount.server.map(server => server.subscribeName || server.name),
...trojanServers.map(server => server.subscribeName || server.name),
];
clashConfig.Proxy = [
...subscribeAccount.server.map(server => ({
cipher: server.method,
name: server.subscribeName || server.name,
password: String(subscribeAccount.account.password),
port: subscribeAccount.account.port + server.shift,
server: server.host,
type: 'ss'
})),
...trojanServers.map(server => ({
name: server.subscribeName || server.name,
type: 'trojan',
server: server.host,
port: server.tjPort,
password: `${subscribeAccount.account.port}:${subscribeAccount.account.password}`,
}))
];
clashConfig['Proxy Group'] = clashConfig['Proxy Group'].map(group => {
if (!group.proxies.includes('placeholder')) {
return group;
} else {
let proxies = group.proxies.slice();
proxies.splice(group.proxies.indexOf('placeholder'), 1, ...proxyNames);
return {
...group,
proxies
};
}
});
return res.send(yaml.safeDump(clashConfig));
}
if(type === 'mellow') {
const template = [
'[Endpoint]',
'@{SERVERS}',
'Direct, builtin, freedom, domainStrategy=UseIP',
'Dns-Out, builtin, dns',
'[EndpointGroup]',
'@{GROUP}',
'[RoutingRule]',
'DOMAIN-KEYWORD, geosite:cn, Direct',
'GEOIP, cn, Direct',
'GEOIP, private, Direct',
'FINAL, allServers',
'[Dns]',
'hijack = Dns-Out',
'[DnsServer]',
'localhost',
'8.8.8.8',
].join('\n')
.replace('@{SERVERS}', subscribeAccount.server.map(s => {
return `${s.subscribeName || s.name}, ss, ss://${s.method}:${subscribeAccount.account.password}@${s.host}:${(subscribeAccount.account.port + + s.shift)}\n`;
}).join(''))
.replace('@{GROUP}', 'allServers, ' + subscribeAccount.server.map(s => {
return `${s.subscribeName || s.name}`;
}).join(':') + ', latency, interval=300, timeout=6');
return res.send(template);
}
if(type === 'android') {
const servers = subscribeAccount.server.map(s => {
return {
id: s.id,
remarks: s.name,
server: s.host,
server_port: subscribeAccount.account.port + s.shift,
password: subscribeAccount.account.password,
method: s.method,
};
});
return res.json({
version: 1,
remarks: 'ssmgr',
servers,
});
}
if(type === 'shadowrocket') {
const ssServers = subscribeAccount.server.map(s => {
const base64string = `${s.method}:${subscribeAccount.account.password}@${s.host}:${subscribeAccount.account.port + s.shift}`;
return `ss://${Buffer.from(base64string).toString('base64')}#${encodeURIComponent(s.subscribeName || s.name)}`;
});
const tjServers = trojanServers.map(s =>
`trojan://${encodeURIComponent(`${subscribeAccount.account.port}:${subscribeAccount.account.password}`)}@${s.host}:${s.tjPort}#${encodeURIComponent(s.subscribeName || s.name)}`
);
return res.send(Buffer.from([...ssServers, ...tjServers].join('\r\n')).toString('base64'));
}
const result = subscribeAccount.server.map(s => {
if(type === 'potatso') {
return 'ss://' + Buffer.from(s.method + ':' + subscribeAccount.account.password + '@' + s.host + ':' + (subscribeAccount.account.port + s.shift)).toString('base64') + '#' + (s.subscribeName || s.name);
} else if(type === 'ssr') {
return 'ssr://' + urlsafeBase64(s.host + ':' + (subscribeAccount.account.port + s.shift) + ':origin:' + s.method + ':plain:' + urlsafeBase64(subscribeAccount.account.password) + '/?obfsparam=&remarks=' + urlsafeBase64(s.subscribeName || s.name) + '&group=' + urlsafeBase64(baseSetting.title));
}
}).join('\r\n');
return res.send(Buffer.from(result).toString('base64'));
} catch (err) {
console.log(err);
res.status(403).end();
}
};