-
Notifications
You must be signed in to change notification settings - Fork 19
/
telegram.js
38 lines (35 loc) · 1.17 KB
/
telegram.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
const fs = require("fs");
var iplist = [];
module.exports = {
loadIPList(path) {
"use strict";
if (!fs.existsSync(path)) {
throw new Error("Not found ip range file of telegram.");
}
iplist = [];
var iplistFileContent = fs.readFileSync(path).toString();
for (let ipRange of iplistFileContent.split("\n")) {
ipRange = ipRange.trim();
var ip = ipRange.substring(0, ipRange.indexOf("/"));
var ipArray = new Buffer(ip.split("."));
if (ipArray.length !== 4) continue;
var ipLong = ipArray.readInt32BE(0);
var netmask = parseInt(ipRange.substring(ipRange.indexOf("/") + 1, ipRange.length));
var range = 2 << (32 - netmask - 1);
iplist.push({
start: ipLong,
end: ipLong + range
})
}
},
isTelegramIP(str) {
var ipArray = new Buffer(str.split("."));
var ipLong = ipArray.readInt32BE(0);
for (var range of iplist) {
if (ipLong >= range.start && ipLong <= range.end) {
return true;
}
}
return false;
}
};