Commit f5b9360d authored by nanahira's avatar nanahira

check proxy

parent 31890961
Pipeline #210 passed with stages
in 1 minute and 23 seconds
import axios, { AxiosProxyConfig, AxiosRequestConfig } from "axios"; import axios, { AxiosProxyConfig, AxiosRequestConfig } from "axios";
const proxySourceList = [
"http://www.89ip.cn/tqdl.html?api=1&num=9999", "http://www.66ip.cn/mo.php?tqsl=9999"
]
for (let i = 1; i <= 2000; ++i) {
proxySourceList.push(`http://www.xiladaili.com/http/${i}/`);
}
export class ProxyFetcher { export class ProxyFetcher {
proxies: AxiosProxyConfig[]; proxies: AxiosProxyConfig[];
counter: number; counter: number;
...@@ -11,7 +19,7 @@ export class ProxyFetcher { ...@@ -11,7 +19,7 @@ export class ProxyFetcher {
if (process.env.NO_PROXY) { if (process.env.NO_PROXY) {
return; return;
} }
while (true) { //while (true) {
try { try {
const proxyPage: string = (await axios.get(url, { const proxyPage: string = (await axios.get(url, {
responseType: "document" responseType: "document"
...@@ -20,17 +28,27 @@ export class ProxyFetcher { ...@@ -20,17 +28,27 @@ export class ProxyFetcher {
for (let proxyString of proxyMatches) { for (let proxyString of proxyMatches) {
const [host, _port] = proxyString.split(":"); const [host, _port] = proxyString.split(":");
const port = parseInt(_port); const port = parseInt(_port);
this.proxies.push({ host, port }); const proxy = { host, port };
let isProxyUsable = false;
try {
await axios.get("http://mirrors.aliyun.com/debian/pool", { proxy });
isProxyUsable = true;
} catch (e) {
console.error(`Proxy ${proxyString} is broken: ${e.toString()}`);
}
if (isProxyUsable) {
this.proxies.push(proxy);
}
} }
console.error(`Got ${proxyMatches.length} proxies from ${url}.`); console.error(`Got ${proxyMatches.length} proxies from ${url}.`);
return; return;
} catch (e) { } catch (e) {
console.error(`Failed fetching proxy list from ${url}: ${e.toString()}`) console.error(`Failed fetching proxy list from ${url}: ${e.toString()}`)
} }
} //}
} }
async initProxies() { async initProxies() {
await Promise.all(["http://www.89ip.cn/tqdl.html?api=1&num=9999", "http://www.66ip.cn/mo.php?tqsl=9999"].map((m) => { await Promise.race(["http://www.89ip.cn/tqdl.html?api=1&num=9999", "http://www.66ip.cn/mo.php?tqsl=9999"].map((m) => {
return this.initProxiesFrom(m); return this.initProxiesFrom(m);
})); }));
} }
...@@ -52,10 +70,10 @@ export class ProxyFetcher { ...@@ -52,10 +70,10 @@ export class ProxyFetcher {
})).data; })).data;
return data; return data;
} catch (e) { } catch (e) {
/*if (!process.env.NO_PROXY) { if (!process.env.NO_PROXY) {
this.proxies.splice(proxyIndex, 1); this.proxies.splice(proxyIndex, 1);
}*/ }
console.error(`Failed fetching data from ${url}: ${e.toString()}`) console.error(`Failed fetching data from ${url}: ${e.toString()} ${this.proxies.length} proxies left.`)
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment