-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxyip.js
More file actions
198 lines (185 loc) · 7.41 KB
/
Copy pathproxyip.js
File metadata and controls
198 lines (185 loc) · 7.41 KB
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
const tls = require("tls");
const cluster = require("cluster");
const os = require("os");
const fs = require("fs");
const color = {
reset: "\x1b[0m",
gray: (text) => `\x1b[90m${text}\x1b[0m`,
blue: (text) => `\x1b[34m${text}\x1b[0m`,
cyan: (text) => `\x1b[36m${text}\x1b[0m`,
green: (text) => `\x1b[32m${text}\x1b[0m`,
red: (text) => `\x1b[31m${text}\x1b[0m`,
yellow: (text) => `\x1b[33m${text}\x1b[0m`,
};
function log(message, type = "info") {
const timestamp = new Date().toISOString().split("T")[1].split(".")[0];
const prefix = {
info: color.blue("[INFO]"),
success: color.green("[SUCCESS]"),
error: color.red("[ERROR]"),
};
console.log(`${color.gray(timestamp)} ${prefix[type] || prefix.info} ${message}`);
}
const formatDuration = (milliseconds) => {
let totalSeconds = Math.floor(milliseconds / 1000);
let hours = Math.floor(totalSeconds / 3600);
let minutes = Math.floor((totalSeconds % 3600) / 60);
let seconds = totalSeconds % 60;
let formatted = [];
if (hours > 0) formatted.push(`${hours} hours`);
if (minutes > 0) formatted.push(`${minutes} minutes`);
if (seconds > 0 || formatted.length === 0) formatted.push(`${seconds} seconds`);
return formatted.join(" ");
};
if (cluster.isPrimary) {
let completedWorkers = 0;
let numCPUs;
const startTime = Date.now();
let totalProxiesFound = 0;
const outputFile = `prxip.txt`;
const jsonOutputFile = `prxip.json`;
const activeProxies = [];
fs.writeFileSync(outputFile, "Proxy,Port,Country,Organization,Latency\n");
(async () => {
try {
log("get proxy list...", "info");
const prx = (await (await fetch("https://raw.githubusercontent.com/AFRcloud/ProxyList/refs/heads/main/ProxyList.txt")).text()).trim().split("\n");
const allIPs = [...new Set(prx)];
numCPUs = Math.min(os.cpus().length, allIPs.length) | 8;
const proxyPerThreads = Math.ceil(allIPs.length / numCPUs);
log(`Loaded ${allIPs.length} proxies`, "success");
log(`Starting checking proxy with ${numCPUs} threads`, "info");
for (let i = 0; i < numCPUs; i++) {
const startIndex = i * proxyPerThreads;
if (startIndex >= allIPs.length) break;
const workerProxy = allIPs.slice(startIndex, startIndex + proxyPerThreads);
const worker = cluster.fork();
worker.send({
proxies: workerProxy,
outputFile,
});
worker.on("message", (msg) => {
if (msg.type === "proxyFound") {
totalProxiesFound++;
activeProxies.push(msg.data);
}
});
}
cluster.on("exit", (worker, code, signal) => {
completedWorkers++;
if (completedWorkers === numCPUs) {
fs.writeFileSync(jsonOutputFile, JSON.stringify(activeProxies, null, 2));
const duration = Date.now() - startTime;
log(`Scan completed in ${formatDuration(duration)}`, "success");
log(`Found ${totalProxiesFound} working proxies`, "success");
log(`Results saved to ${outputFile} and ${jsonOutputFile}`, "success");
process.exit(0);
}
});
} catch (error) {
log(`${error.message}`, "error");
process.exit(1);
}
})();
} else {
let proxies = [];
let outputFile;
process.on("message", (msg) => {
if (msg.proxies) {
proxies = msg.proxies;
outputFile = msg.outputFile;
checkProxies()
.then(() => {
process.exit(0);
})
.catch((error) => {
log(`${error.message}`, "error");
process.exit(1);
});
}
});
async function checkIP(cok) {
const [proxy, port] = cok.split(/[^a-zA-Z0-9.\n]+/);
const sendRequest = (host, path, useProxy = true) => {
return new Promise((resolve, reject) => {
const start = Date.now();
const socket = tls.connect({
host: useProxy ? proxy : host,
port: useProxy ? port : 443,
servername: host,
},() => {
const request = `GET ${path} HTTP/1.1\r\n` +
`Host: ${host}\r\n` +
`User-Agent: Mozilla/5.0\r\n` +
`Connection: close\r\n\r\n`;
socket.write(request);
});
let responseBody = "";
socket.on("data", (data) => {
responseBody += data.toString();
});
socket.on("end", () => {
const body = responseBody.split("\r\n\r\n")[1] || "";
const latency = Date.now() - start;
resolve({ body, latency });
});
socket.on("error", (error) => {
reject(error);
});
socket.setTimeout(5000, () => {
reject(new Error("Request timeout"));
socket.end();
});
});
};
return new Promise(async (resolve, reject) => {
if (!cok) return;
try {
const [ipinfo, myip] = await Promise.all([
sendRequest("myip.bexcode.us.to", "/", true),
sendRequest("myip.bexcode.us.to", "/", false),
]);
const ipingfo = JSON.parse(ipinfo.body);
const srvip = JSON.parse(myip.body);
if (ipingfo.myip && ipingfo.myip !== srvip.myip) {
resolve({
proxy: proxy,
port: port,
proxyip: ipingfo.myip !== srvip.myip,
ip: ipingfo.myip,
latency: ipinfo.latency,
...ipingfo
});
} else {
resolve({ proxyip: false });
}
} catch (error) {
resolve({ proxyip: false });
}
});
}
async function checkProxies() {
const promises = [];
const batchSize = 50;
for (const proxy of proxies) {
promises.push(
checkIP(proxy).then((data) => {
if (data.proxyip) {
delete data.myip
process.send({ type: "proxyFound", data });
const outputLine = `${data.proxy},${data.port},${data.countryCode},${data.org},${data.latency}ms\n`;
fs.appendFileSync(outputFile, outputLine);
log(`Found: ${color.cyan(data.proxy)}:${color.cyan(data.port)} | ${color.yellow(data.countryCode)} | ${color.blue(data.org)} | ${color.cyan(data.latency + "ms")}`,"success");
}
})
);
if (promises.length >= batchSize) {
await Promise.all(promises);
promises.length = 0;
}
}
if (promises.length) {
await Promise.all(promises);
}
}
}