-
Notifications
You must be signed in to change notification settings - Fork 1
/
electron-test.ts
197 lines (172 loc) · 6.56 KB
/
electron-test.ts
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
import axios from "axios";
import Bluebird from "bluebird";
import { app, BrowserWindow } from "electron";
import fs from "fs";
import path from "path";
import { md5 } from "sbg-utility";
// set timezone
process.env.TZ = "America/Los_Angeles";
app.whenReady().then(async () => {
const proxies = readFile(path.join(__dirname, "proxies.txt"))
.map((s) => s.trim())
.filter((s) => s.length > 7);
const workingProxies = [] as string[];
for (let i = 0; i < proxies.length; i++) {
const proxy = proxies[i];
const check = await checkProxy("http://" + proxy);
if (check) workingProxies.push(proxy);
}
console.log("total working proxies", workingProxies.length);
// if (check) {
// const geo = await getLocation(proxy).catch((_) => null);
// if (geo && geo.length > 0) {
// console.log(geo);
// }
// }
});
function readFile(filePath: string): string[] {
// Read the file synchronously
const data: string = fs.readFileSync(filePath, "utf-8");
// Split the content by lines
const lines: string[] = data.split(/\r?\n/);
return lines;
}
/**
* check proxy using electron
* @param proxy http://ip:port socks://ip:port
*/
function checkProxy(proxy: string): Bluebird<boolean> {
return new Bluebird((resolve) => {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
// Set your Electron web preferences here
sandbox: false, // Example: This corresponds to --no-sandbox
enableBlinkFeatures: "IdleDetection" // Example: This corresponds to --enable-blink-features=IdleDetection
}
});
if (mainWindow.minimizable) mainWindow.minimize();
// 213.131.230.22:3128
mainWindow.webContents.session.setUserAgent(
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9"
);
// set window proxy
mainWindow.webContents.session.setProxy({ proxyRules: proxy });
// Set user data directory
app.setPath("userData", path.join(process.cwd(), "tmp/profiles/default"));
// Set proxy server
app.commandLine.appendSwitch("proxy-server", proxy);
// Set other command line switches
app.commandLine.appendSwitch("force-color-profile", "srgb");
app.commandLine.appendSwitch("metrics-recording-only");
app.commandLine.appendSwitch("no-first-run");
// Add other command line switches as needed
// Load your app's HTML file
mainWindow.loadURL("https://proxy6.net/en/privacy");
// mainWindow.loadURL('https://bing.com');
mainWindow.webContents.on("did-fail-load", (_event, errorCode, errorDescription, validatedURL) => {
if (["ERR_PROXY_CONNECTION_FAILED"].includes(errorDescription)) {
console.error(proxy, "not working, proxy failed");
moveString(path.join(__dirname, "proxies.txt"), path.join(__dirname, "dead.txt"), proxy.replace("http://", ""));
} else if (errorDescription == "ERR_CERT_AUTHORITY_INVALID") {
console.error(proxy, "not working, Non-SSL");
moveString(path.join(__dirname, "proxies.txt"), path.join(__dirname, "dead.txt"), proxy.replace("http://", ""));
} else {
console.error(`Failed to load URL: ${validatedURL}`);
console.error(`Error code: ${errorCode}, Description: ${errorDescription}`);
console.error(proxy, "not working, fail load page");
}
if (mainWindow.closable) mainWindow.close();
resolve(false);
});
mainWindow.webContents.on("did-finish-load", () => {
let result = false;
const title = mainWindow.getTitle();
if (title.includes("Anonymity check")) {
console.log(proxy, "working");
result = true;
} else {
console.error(proxy, "not working");
moveString(path.join(__dirname, "proxies.txt"), path.join(__dirname, "dead.txt"), proxy.replace("http://", ""));
}
if (mainWindow.closable) mainWindow.close();
resolve(result);
});
});
}
async function getLocation(proxy: string) {
const [ip, port] = proxy.split(":");
console.log(`check geo ${proxy} -> ${ip}:${port}`);
try {
const url = `http://ip-get-geolocation.com/api/json/${ip}`;
let response: Record<string, any> = {};
const cacheFile = path.join(__dirname, ".cache", md5(url));
const isCached = fs.existsSync(cacheFile);
const proxyConfig = {
host: ip,
port: parseInt(port)
};
if (isCached) {
response = { data: JSON.parse(fs.readFileSync(cacheFile, "utf-8")) };
} else {
response = await axios
.get(url, {
proxy: proxyConfig
})
.catch(() => {
return { data: null };
});
}
const myIP = await axios
.get("https://api.ipify.org?format=json", {
proxy: proxyConfig
})
.catch(() => {
return { data: { ip: null } };
});
const proxyIpAddress = myIP.data.ip;
console.log(proxyIpAddress, proxy);
if (response.data) {
const locationArray = response.data;
if (typeof locationArray == "object" && locationArray) {
if (locationArray.status && locationArray.status == "fail") {
// max reached
return "";
} else if (locationArray.country) {
const item = `${locationArray.region}|${locationArray.city}|${locationArray.country}|${locationArray.timezone}`;
// write cache
if (!isCached) fs.writeFileSync(cacheFile, JSON.stringify(locationArray));
return item;
} else {
console.log(locationArray);
}
}
} else {
// throw new Error('Empty response');
return "";
}
} catch (error: any) {
console.error(error.message);
return ""; // or handle error accordingly
}
return "";
}
function moveString(originalFilePath: string, newFilePath: string, searchString: string): void {
// Read the original file
let originalContent: string = fs.readFileSync(originalFilePath, "utf8");
// Find the index of the search string
const index: number = originalContent.indexOf(searchString);
if (index === -1) {
console.log("String not found in original file.");
return;
}
// Extract the string to move
const movedString: string = originalContent.substring(index, index + searchString.length);
// Append the string to the new file
fs.appendFileSync(newFilePath, movedString + "\n"); // Append to a new line
// Remove the string from the original content
originalContent = originalContent.substring(0, index) + originalContent.substring(index + searchString.length);
// Write back to the original file
fs.writeFileSync(originalFilePath, originalContent);
}