Created
May 29, 2024 07:49
-
-
Save mikoloism/4c728af20e842569f992b746474f7a61 to your computer and use it in GitHub Desktop.
http-proxy.pac file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function FindProxyForURL(url, host) { | |
var PROXY_LIST_FILE = 'https://raw.githubusercontent.com/TheSpeedX/PROXY-List/master/http.txt'; | |
var TEST_TARGET_URL = 'https://google.com'; | |
var TEST_TIMEOUT = 5000; | |
var proxyList = []; | |
function addProxy(proxy) { | |
if (proxyList.indexOf(proxy) === -1) { | |
proxyList.push(proxy); | |
} | |
} | |
function testProxy(proxy) { | |
function isHostReachable() { | |
try { | |
return shExpMatch(host, proxy); | |
} catch (err) { | |
return false; | |
} | |
} | |
function testHttpProxy(proxy) { | |
try { | |
var proxyUrl = 'http://' + proxy; | |
var proxyConfig = { | |
proxy: { http: proxyUrl }, | |
}; | |
var res = $http.get(TEST_TARGET_URL, { | |
sys: proxyConfig, | |
timeout: TEST_TIMEOUT, | |
}); | |
return res.status === 200; | |
} catch (err) { | |
return false; | |
} | |
} | |
function testSocksProxy(proxy) { | |
try { | |
var proxyUrl = 'socks://' + proxy; | |
var proxyConfig = { | |
proxy: { socks: proxyUrl }, | |
}; | |
var res = $http.get(TEST_TARGET_URL, { | |
sys: proxyConfig, | |
timeout: TEST_TIMEOUT, | |
}); | |
return res.status === 200; | |
} catch (err) { | |
return false; | |
} | |
} | |
if (isHostReachable()) { | |
if (testHttpProxy(proxy)) { | |
addProxy(proxy); | |
return 'PROXY ' + proxy; | |
} | |
if (testSocksProxy(proxy)) { | |
addProxy(proxy); | |
return 'SOCKS ' + proxy; | |
} | |
} | |
return 'DIRECT'; | |
} | |
var proxyContent = $http.get(PROXY_LIST_FILE).body; | |
var proxies = proxyContent.split('\n'); | |
for (var i = 0; i < proxies.length; i++) { | |
var proxy = proxies[i].trim(); | |
if (proxy.length > 0) { | |
testProxy(proxy); | |
} | |
} | |
if (proxyList.length > 0) { | |
return proxyList.join(';') + '; DIRECT'; | |
} | |
return 'DIRECT'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment