Skip to content

Instantly share code, notes, and snippets.

@binux
Created January 16, 2014 14:48
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binux/8456129 to your computer and use it in GitHub Desktop.
Save binux/8456129 to your computer and use it in GitHub Desktop.
自动添加gfwlist域名的对应ip到路由表,实现动态自动翻墙
#!/bin/sh
while :
do
dev=$(ip link | grep state | awk '{ sub(":", "", $2); print $2 }' | grep vpn)
if [ $? -ne 0 ]; then
sleep 5
continue
fi
break
done
ip rule | grep "lookup vpn" || ip rule add table vpn
ip route add 8.8.8.8 dev pptp-vpn table vpn
ip route add 8.8.4.4 dev pptp-vpn table vpn
logread -f \
| awk 'BEGIN {
i=0;
while (getline < "/root/gfwlist") {
m = match($1, "^(\\|\\|)?[a-zA-Z0-9.]+$")
if (m == 1) {
if (match($1, "^\\|\\|") == 1) $1 = substr($1, 3);
gfwlist[i++] = $1;
}
}
}
function gfw_match(domain) {
for (i in gfwlist) {
if (index(domain, gfwlist[i]) > 0)
return 1;
}
return 0;
}
match($0, "dnsmasq\\[[0-9]+\\]: reply") {
#Jan 15 15:13:01 OpenWrt daemon.info dnsmasq[20413]: reply js.users.51.la is 222.187.221.28
domain_pos = match($0, ": reply [^ ]+ is");
if (domain_pos != 0) {
domain = substr($0, RSTART+8, RLENGTH-11);
if (!gfw_match(domain)) {
return;
}
}
ip_pos = match($NF, "([0-9]+\.?){4}");
if (ip_pos != 0) {
ip = $NF;
if (!(ip in added)) {
added[ip] = 1;
print "ip route add "ip" dev pptp-vpn table vpn"
print domain, ip >> "/tmp/auto_vpn.log"
}
}
}' | sh -x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment