From 37c4ed6ff6870edcc74bf27404247932800deb9e Mon Sep 17 00:00:00 2001 From: qubicllj Date: Fri, 26 Sep 2014 18:57:29 +0800 Subject: [PATCH 1/3] add iproute2 support --- chnroutes.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/chnroutes.py b/chnroutes.py index 99401a6..1221761 100755 --- a/chnroutes.py +++ b/chnroutes.py @@ -18,6 +18,44 @@ def generate_ovpn(metric): print "Usage: Append the content of the newly created routes.txt to your openvpn config file," \ " and also add 'max-routes %d', which takes a line, to the head of the file." % (len(results)+20) +def generate_linux_iproute2(metric): + results = fetch_ip_data() + upscript_header=textwrap.dedent("""\ + #!/bin/bash + export PATH="/bin:/sbin:/usr/sbin:/usr/bin" + CHNROUTE_PATH="/usr/local/sbin" + OLDGW=`ip route show | grep '^default' | sed -e 's/default via \\([^ ]*\\).*/\\1/'` + + sed -i "s/via \([0-9]\+\.\)\{3\}[0-9]\+/via $OLDGW/g" $CHNROUTE_PATH/iproute2-ip-up.txt + + ip -batch $CHNROUTE_PATH/iproute2-ip-up.txt + + """) + + downscript_header=textwrap.dedent("""\ + #!/bin/bash + export PATH="/bin:/sbin:/usr/sbin:/usr/bin" + CHNROUTE_PATH="/usr/local/sbin" + + ip -batch $CHNROUTE_PATH/iproute2-ip-down.txt + + """) + + upfile=open('iproute2-ip-up.sh','w') + upipfile=open('iproute2-ip-up.txt','w') + downfile=open('iproute2-ip-down.sh','w') + downipfile=open('iproute2-ip-down.txt','w') + + upfile.write(upscript_header) + upfile.write('\n') + downfile.write(downscript_header) + downfile.write('\n') + + for ip,mask,_ in results: + upipfile.write('route add %s/%s via 192.168.1.1\n'%(ip,mask)) + downipfile.write('route del %s/%s \n'%(ip,mask)) + + print "Copy the files whose prefix match iproute2-* to the folder /usr/local/sbin/ " def generate_linux(metric): results = fetch_ip_data() @@ -247,6 +285,8 @@ def fetch_ip_data(): if args.platform.lower() == 'openvpn': generate_ovpn(args.metric) + elif args.platform.lower() == 'linux-iproute2': + generate_linux_iproute2(args.metric) elif args.platform.lower() == 'linux': generate_linux(args.metric) elif args.platform.lower() == 'mac': From fb1e0a6adaefbfbef6592e8786c88212d796fd69 Mon Sep 17 00:00:00 2001 From: qubic Date: Wed, 5 Sep 2018 19:16:46 +0800 Subject: [PATCH 2/3] Simplify iproute2 support --- chnroutes.py | 152 +++++++++++++++++++++++++-------------------------- 1 file changed, 76 insertions(+), 76 deletions(-) diff --git a/chnroutes.py b/chnroutes.py index 236f1c6..e316ec6 100755 --- a/chnroutes.py +++ b/chnroutes.py @@ -7,9 +7,8 @@ import math import textwrap - def generate_ovpn(metric): - results = fetch_ip_data() + results = fetch_ip_data() rfile=open('routes.txt','w') for ip,mask,_ in results: route_item="route %s %s net_gateway %d\n"%(ip,mask,metric) @@ -20,40 +19,41 @@ def generate_ovpn(metric): def generate_linux_iproute2(metric): results = fetch_ip_data() - upscript_header=textwrap.dedent("""\ + up_template = textwrap.dedent("""\ #!/bin/bash export PATH="/bin:/sbin:/usr/sbin:/usr/bin" - CHNROUTE_PATH="/usr/local/sbin" OLDGW=`ip route show | grep '^default' | sed -e 's/default via \\([^ ]*\\).*/\\1/'` - - sed -i "s/via \([0-9]\+\.\)\{3\}[0-9]\+/via $OLDGW/g" $CHNROUTE_PATH/iproute2-ip-up.txt - - ip -batch $CHNROUTE_PATH/iproute2-ip-up.txt - + + ip -batch - < /tmp/vpn_oldgw fi - + """) - + downscript_header=textwrap.dedent("""\ #!/bin/bash export PATH="/bin:/sbin:/usr/sbin:/usr/bin" - + OLDGW=`cat /tmp/vpn_oldgw` - + """) - + upfile=open('ip-pre-up','w') downfile=open('ip-down','w') - + upfile.write(upscript_header) upfile.write('\n') downfile.write(downscript_header) downfile.write('\n') - + for ip,mask,_ in results: upfile.write('route add -net %s netmask %s gw $OLDGW\n'%(ip,mask)) downfile.write('route del -net %s netmask %s\n'%(ip,mask)) @@ -103,128 +103,128 @@ def generate_linux(metric): def generate_mac(metric): results=fetch_ip_data() - + upscript_header=textwrap.dedent("""\ #!/bin/sh export PATH="/bin:/sbin:/usr/sbin:/usr/bin" - + OLDGW=`netstat -nr | grep '^default' | grep -v 'ppp' | sed 's/default *\\([0-9\.]*\\) .*/\\1/' | awk '{if($1){print $1}}'` if [ ! -e /tmp/pptp_oldgw ]; then echo "${OLDGW}" > /tmp/pptp_oldgw fi - + dscacheutil -flushcache route add 10.0.0.0/8 "${OLDGW}" route add 172.16.0.0/12 "${OLDGW}" route add 192.168.0.0/16 "${OLDGW}" """) - + downscript_header=textwrap.dedent("""\ #!/bin/sh export PATH="/bin:/sbin:/usr/sbin:/usr/bin" - + if [ ! -e /tmp/pptp_oldgw ]; then exit 0 fi - + OLDGW=`cat /tmp/pptp_oldgw` route delete 10.0.0.0/8 "${OLDGW}" route delete 172.16.0.0/12 "${OLDGW}" route delete 192.168.0.0/16 "${OLDGW}" """) - + upfile=open('ip-up','w') downfile=open('ip-down','w') - + upfile.write(upscript_header) upfile.write('\n') downfile.write(downscript_header) downfile.write('\n') - + for ip,_,mask in results: upfile.write('route add %s/%s "${OLDGW}"\n'%(ip,mask)) downfile.write('route delete %s/%s ${OLDGW}\n'%(ip,mask)) - + downfile.write('\n\nrm /tmp/pptp_oldgw\n') upfile.close() downfile.close() - + print "For pptp on mac only, please copy ip-up and ip-down to the /etc/ppp folder," \ "don't forget to make them executable with the chmod command." def generate_win(metric): - results = fetch_ip_data() + results = fetch_ip_data() upscript_header=textwrap.dedent("""@echo off for /F "tokens=3" %%* in ('route print ^| findstr "\\<0.0.0.0\\>"') do set "gw=%%*" - + """) - + upfile=open('vpnup.bat','w') downfile=open('vpndown.bat','w') - + upfile.write(upscript_header) upfile.write('\n') upfile.write('ipconfig /flushdns\n\n') - + downfile.write("@echo off") downfile.write('\n') - + for ip,mask,_ in results: upfile.write('route add %s mask %s %s metric %d\n'%(ip,mask,"%gw%",metric)) downfile.write('route delete %s\n'%(ip)) - + upfile.close() downfile.close() - + # up_vbs_wrapper=open('vpnup.vbs','w') # up_vbs_wrapper.write('Set objShell = CreateObject("Wscript.shell")\ncall objShell.Run("vpnup.bat",0,FALSE)') # up_vbs_wrapper.close() # down_vbs_wrapper=open('vpndown.vbs','w') # down_vbs_wrapper.write('Set objShell = CreateObject("Wscript.shell")\ncall objShell.Run("vpndown.bat",0,FALSE)') # down_vbs_wrapper.close() - + print "For pptp on windows only, run vpnup.bat before dialing to vpn," \ "and run vpndown.bat after disconnected from the vpn." def generate_android(metric): results = fetch_ip_data() - + upscript_header=textwrap.dedent("""\ #!/bin/sh alias nestat='/system/xbin/busybox netstat' alias grep='/system/xbin/busybox grep' alias awk='/system/xbin/busybox awk' alias route='/system/xbin/busybox route' - + OLDGW=`netstat -rn | grep ^0\.0\.0\.0 | awk '{print $2}'` - + """) - + downscript_header=textwrap.dedent("""\ #!/bin/sh alias route='/system/xbin/busybox route' - + """) - + upfile=open('vpnup.sh','w') downfile=open('vpndown.sh','w') - + upfile.write(upscript_header) upfile.write('\n') downfile.write(downscript_header) downfile.write('\n') - + for ip,mask,_ in results: upfile.write('route add -net %s netmask %s gw $OLDGW\n'%(ip,mask)) downfile.write('route del -net %s netmask %s\n'%(ip,mask)) - + upfile.close() downfile.close() - + print "Old school way to call up/down script from openvpn client. " \ "use the regular openvpn 2.1 method to add routes if it's possible" @@ -234,17 +234,17 @@ def fetch_ip_data(): print "Fetching data from apnic.net, it might take a few minutes, please wait..." url=r'https://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' data=urllib2.urlopen(url).read() - + cnregex=re.compile(r'apnic\|cn\|ipv4\|[0-9\.]+\|[0-9]+\|[0-9]+\|a.*',re.IGNORECASE) cndata=cnregex.findall(data) - + results=[] for item in cndata: unit_items=item.split('|') starting_ip=unit_items[3] num_ip=int(unit_items[4]) - + imask=0xffffffff^(num_ip-1) #convert to string imask=hex(imask)[2:] @@ -253,16 +253,16 @@ def fetch_ip_data(): mask[1]=imask[2:4] mask[2]=imask[4:6] mask[3]=imask[6:8] - + #convert str to int mask=[ int(i,16 ) for i in mask] mask="%d.%d.%d.%d"%tuple(mask) - + #mask in *nix format mask2=32-int(math.log(num_ip,2)) - + results.append((starting_ip,mask,mask2)) - + return results @@ -272,7 +272,7 @@ def fetch_ip_data(): dest='platform', default='openvpn', nargs='?', - help="Target platforms, it can be openvpn, mac, linux," + help="Target platforms, it can be openvpn, mac, linux," "win, android. openvpn by default.") parser.add_argument('-m','--metric', dest='metric', @@ -280,9 +280,9 @@ def fetch_ip_data(): nargs='?', type=int, help="Metric setting for the route rules") - + args = parser.parse_args() - + if args.platform.lower() == 'openvpn': generate_ovpn(args.metric) elif args.platform.lower() == 'linux-iproute2': From 79dee3351561c17ae9d26f3e1e35c1658bab6b1e Mon Sep 17 00:00:00 2001 From: qubic Date: Wed, 5 Sep 2018 19:23:36 +0800 Subject: [PATCH 3/3] Fix the wrong variable reference --- chnroutes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chnroutes.py b/chnroutes.py index e316ec6..143961a 100755 --- a/chnroutes.py +++ b/chnroutes.py @@ -43,7 +43,7 @@ def generate_linux_iproute2(metric): down_rules = "" for ip,mask,mask2 in results: - up_rules += 'route add %s/%s via ${OLDGW}\n' % (ip,mask2) + up_rules += 'route add %s/%s via $OLDGW\n' % (ip,mask2) down_rules += 'route del %s/%s \n' % (ip,mask2) up_content = up_template.format(rules = up_rules)