最近 Shadowsock s 不稳定,极大的影响了工作效率。不过 ssh 还能连上自己的机器,所以掏出 ssh -D
满足自己
#!/usr/bin/env bash
disable_proxy()
{
networksetup -setsocksfirewallproxystate Wi-Fi off
networksetup -setsocksfirewallproxystate Ethernet off
echo "SOCKS proxy disabled."
}
trap disable_proxy EXIT # Ctrl+C 的时候执行 disable_proxy
IP=127.0.0.1
PORT=`python -c 'import socket; s=socket.socket(); s.bind(("localhost", 0)); print(s.getsockname()[1]); s.close()'`
networksetup -setsocksfirewallproxy Wi-Fi $IP $PORT
networksetup -setsocksfirewallproxy Ethernet $IP $PORT
networksetup -setsocksfirewallproxystate Wi-Fi on
networksetup -setsocksfirewallproxystate Ethernet on
echo "SOCKS proxy enabled."
echo "Tunneling..."
ssh -ND $PORT root@xxx.xxx.xxx.xxx
上面的已经够用了,但是还不够“智能”,所有网页都通过代理来访问, 国内的也网站也要绕一圈,没能根据一个列表来决定哪些网址需要代理,哪些不需要。
要满足这一点,需要 networksetup -setautoproxyurl networkservice url
networksetup -setautoproxyurl networkservice url
Set proxy auto-config to url for and enable it.
首先需要一个 .pac 文件,这个文件怎么搞到呢,可以从网上下载一个, 也可以自己写。其实这个文件主要是一个函数, 类似这样:
function FindProxyForURL(url, host) {
var proxy = "SOCKS5 127.0.0.1:1280; SOCKS 127.0.0.1:1280; DIRECT;"
var direct = 'DIRECT;';
if (defaultMatcher.matchesAny(url, host) instanceof BlockingFilter) {
return proxy;
}
return 'DIRECT;'
}
我们修改后的 shell 脚本文件长这样:
#!/usr/bin/env bash
# https://www.cnblogs.com/hangj/p/11838259.html
IP=localhost
FILE=proxy.pac
# 找到一个可用的 port
SOCKSPORT=`python -c 'import socket; s=socket.socket(); s.bind(("localhost", 0)); print(s.getsockname()[1]); s.close()'`
HTTPPORT=`python -c 'import socket; s=socket.socket(); s.bind(("localhost", 0)); print(s.getsockname()[1]); s.close()'`
URL=http://$IP:$HTTPPORT/$FILE
# 设置配置中的 port
sed "s/var proxy = .*/var proxy = \"SOCKS5 127.0.0.1:$SOCKSPORT; SOCKS 127.0.0.1:$SOCKSPORT; DIRECT;\";/" $FILE.bak > $FILE
python3 -m http.server $HTTPPORT --bind $IP &
echo "http server started."
disable_proxy()
{
ps -ef | grep "-m http.server $HTTPPORT" | grep -v grep | awk '{print $2}' | xargs kill
lsof -nP -i :$HTTPPORT | grep $HTTPPORT | awk '{print $2}' | xargs kill
networksetup -setautoproxyurl Wi-Fi " "
networksetup -setautoproxyurl Ethernet " "
echo "SOCKS proxy disabled."
}
trap disable_proxy EXIT
networksetup -setautoproxyurl Wi-Fi $URL
networksetup -setautoproxyurl Ethernet $URL
echo "SOCKS proxy enabled."
echo "Tunneling..."
ssh -ND $PORT root@xxx.xxx.xxx.xxx
我的配置文件地址:proxy.pac.bak
PT09IHN0YXJ0L3N0b3Agc2hhZG93c29ja3MgJiYgc2hhZG93c29ja3MgY29uZmlnDQoNCiAgIGhvdyB0byBidWlsZCBzczogaHR0cHM6Ly9naXN0LmdpdGh1Yi5jb20vMzAxMzIxNjAyNy80NjllN2I5ODhlZGRlN2U1ZjgwNmY1NWE5OTNiOWVmYQ0KICAgc3RhcnQgc3M6IHNzc2VydmVyIC1jIC9ldGMvc2hhZG93c29ja3MuanNvbiAtZCBzdGFydA0KICAgc3RvcCBzczogc3NzZXJ2ZXIgLWMgL2V0Yy9zaGFkb3dzb2Nrcy5qc29uIC1kIHN0b3ANCiAgIHNzIGNvbmZpZzogL2V0Yy9zaGFkb3dzb2Nrcy5qc29uDQoNCj09PSBhYm91dCBpcHRhYmxzDQogICBpbnN0YWxsIGlwdGFibGVz
手机扫一扫
移动阅读更方便
你可能感兴趣的文章