Skip to content

Instantly share code, notes, and snippets.

@FradSer
Created Mar 11, 2016
Embed
What would you like to do?
Surge for Mac 的相关 function,实现快速添加规则,快速配置适合 Surge for Mac 的网络配置,以及还原网络配置。
# Surge for Mac. Portnumber is 6152.
function surge_add() {
if [ "$1" == "--proxy" -o "$1" == "-p" -o "$1" == "--direct" -o "$1" == "-d" ]; then
if [ "$(cat ~/.surge.conf | grep -c $2)" -eq 0 ]; then
if [ "$1" == "--proxy" -o "$1" == "-p" ]; then
sed -i '' -e '539i \
DOMAIN-SUFFIX,'$2',Proxy' ~/.surge.conf;
fi;
if [ "$1" == "--direct" -o "$1" == "-d" ]; then
sed -i '' -e '539i \
DOMAIN-SUFFIX,'$2',DIRECT' ~/.surge.conf;
fi;
osascript -e 'tell application "Surge" to quit';
osascript -e 'run application "/Applications/Surge.app/"';
else
echo '"'"$2"'"'' already exists.';
cat ~/.surge.conf | grep $2;
fi;
else
echo "usage: surge_add [--proxy|-p|--direct|-d] [URL].";
fi
}
function surge_on() {
sudo -v;
DEVICE=$(saveIFS="$IFS"
IFS='
'
services=( $(networksetup -listallnetworkservices | sed '/An asterisk ([*]) denotes that a network service is disabled./d; s/^[*]//') )
IFS="$saveIFS"
for i in "${services[@]}"; do
if [ $(networksetup getinfo $i | grep -c "IP address:\W[1-9]") -eq 1 ]; then
echo $i
fi
done
);
sudo networksetup -setwebproxy ${DEVICE%,*} 127.0.0.1 6152;
sudo networksetup -setsecurewebproxy ${DEVICE%,*} 127.0.0.1 6152;
sudo networksetup -setwebproxystate ${DEVICE%,*} on;
sudo networksetup -setsecurewebproxystate ${DEVICE%,*} on;
sudo networksetup -setproxybypassdomains ${DEVICE%,*} 127.0.0.1 192.168.0.0/16 10.0.0.0/8 172.16.0.0/12 localhost *.local;
osascript -e 'run application "/Applications/Surge.app/"';
}
function surge_off() {
sudo -v;
DEVICE=$(saveIFS="$IFS"
IFS='
'
services=( $(networksetup -listallnetworkservices | sed '/An asterisk ([*]) denotes that a network service is disabled./d; s/^[*]//') )
IFS="$saveIFS"
for i in "${services[@]}"; do
if [ $(networksetup getinfo $i | grep -c "IP address:\W[1-9]") -eq 1 ]; then
echo $i
fi
done
);
sudo networksetup -setwebproxystate ${DEVICE%,*} off;
sudo networksetup -setsecurewebproxystate ${DEVICE%,*} off;
sudo networksetup -setproxybypassdomains ${DEVICE%,*} null;
osascript -e 'tell application "Surge" to quit';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment