Skip to content

Instantly share code, notes, and snippets.

@yulanggong
Last active February 28, 2019 08:53
Show Gist options
  • Save yulanggong/0b1cd913536869a462bf0ce5e529b126 to your computer and use it in GitHub Desktop.
Save yulanggong/0b1cd913536869a462bf0ce5e529b126 to your computer and use it in GitHub Desktop.
Auto change proxy for work and shadowsocks on MacOS
#! /bin/bash
pushd ~/.config;
curl -s -o proxyss.pac http://127.0.0.1:1089/proxy.pac
if [ "$?" = "0" ]; then
cat proxyss.pac proxyw2.js > proxy.pac
cat proxyw2_pre.js proxyw2.js > proxyw2.pac
# find active network service and set autoproxy url
services=$(networksetup -listnetworkserviceorder | grep 'Hardware Port')
while read line; do
sname=$(echo $line | awk -F "(, )|(: )|[)]" '{print $2}')
sdev=$(echo $line | awk -F "(, )|(: )|[)]" '{print $4}')
#echo "Current service: $sname, $sdev, $currentservice"
if [ -n "$sdev" ]; then
ifout="$(ifconfig $sdev 2>/dev/null)"
echo "$ifout" | grep 'status: active' > /dev/null 2>&1
rc="$?"
if [ "$rc" -eq 0 ]; then
currentservice="$sname"
currentdevice="$sdev"
currentmac=$(echo "$ifout" | awk '/ether/{print $2}')
networksetup -setautoproxyurl "$currentservice" "http://127.0.0.1:1989/proxy${1}.pac"
echo "Set autoproxy url for $currentservice, $currentdevice, $currentmac"
fi
fi
done <<< "$(echo "$services")"
if [ -z "$currentservice" ]; then
>&2 echo "Could not find active network service"
exit 1
else
nohup python -m SimpleHTTPServer 1989 > /dev/null 2>&1 &
echo "PAC file serving at http://127.0.0.1:1989/proxy${1}.pac"
fi
else
echo "Set Shadowsocks to PAC mod at first."
fi
popd
;var w2Proxy = "PROXY 127.0.0.1:8899; DIRECT;";
var w2Rules = [
".baidu.cn",
".qq.com",
".taobao.com"
]
var w2Matcher = new CombinedMatcher();
for (var i = 0; i < w2Rules.length; i++) {
w2Matcher.add(Filter.fromText(w2Rules[i]));
}
function FindProxyForURL(url, host) {
// return w2Proxy;
if (
shExpMatch(host, '*.direct.com')
|| shExpMatch(host, 'must.direct.com')
) {
return direct;
}
if (w2Matcher.matchesAny(url, host) instanceof BlockingFilter) {
return w2Proxy;
}
if (defaultMatcher.matchesAny(url, host) instanceof BlockingFilter) {
return proxy;
}
return w2Proxy;
//return direct;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment