Skip to content

Instantly share code, notes, and snippets.

@jnozsc
Created December 24, 2011 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnozsc/1517152 to your computer and use it in GitHub Desktop.
Save jnozsc/1517152 to your computer and use it in GitHub Desktop.
update the chinese vpn iptable, modify from the autoddvpn svn
#!/usr/bin/env python
import urllib
import string
import math
ipv4url='http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest'
up="http://autoddvpn.googlecode.com/svn/trunk/vpnup.sh"
down="http://autoddvpn.googlecode.com/svn/trunk/vpndown.sh"
upfile=open('vpnup.sh','wa')
downfile=open('vpndown.sh','wa')
print "[INFO] fetching the latest vpnup.sh from SVN over http"
uplines = urllib.urlopen(up).readlines()
print "[INFO] fetching the latest vpndown.sh from SVN over http"
downlines = urllib.urlopen(down).readlines()
# count the current route lines
for l in range(len(uplines)):
if uplines[l].find('begin batch route') != -1:
oldcnt_s = l
if uplines[l].find('end batch route') != -1:
oldcnt_e = l
oldcnt = oldcnt_e - oldcnt_s - 1
print "[INFO] %i routes exists on the SVN" % oldcnt
#
# for the vpnup.sh
#
_anchor=0
for l in uplines:
#if _anchor==0: print l.rstrip()
if _anchor==0: upfile.write(l)
if l.find('begin batch route') != -1:
break
#
# for the vpndown.sh
#
_anchor=0
for l in downlines:
#if _anchor==0: print l.rstrip()
if _anchor==0: downfile.write(l)
if l.find('begin batch route') != -1:
break
print "[INFO] getting the IP list from APNIC, this may take a while."
#lstlines = open('country-ipv4.lst').readlines()
lstlines = urllib.urlopen(ipv4url).readlines()
print "[INFO] generating the routes"
cnt=0
for l in lstlines:
if (l.find('CN') != -1) & (l.find('ipv4')!=-1):
list = string.split(l.rstrip(), "|")
ip, mask = list[3], 32-int(math.log(int(list[4]),2))
#print "route add -net %s netmask %s gw $OLDGW" % (ip, mask)
upfile.write("route add -net %s/%s gw $OLDGW\n" % (ip, mask))
downfile.write("route del -net %s/%s\n" % (ip, mask))
cnt+=1
print "[INFO] total %i routes generated(%i route(s) added)" % (cnt, cnt-oldcnt)
#
# for the vpnup.sh
#
_anchor=0
for l in uplines:
#if _anchor==1: print l.rstrip()
if _anchor==1: upfile.write(l)
if l.find('end batch route') != -1:
#print l.rstrip()
upfile.write(l)
_anchor=1
#
# for the vpndown.sh
#
_anchor=0
for l in downlines:
#if _anchor==1: print l.rstrip()
if _anchor==1: downfile.write(l)
if l.find('end batch route') != -1:
_anchor=1
#print l.rstrip()
downfile.write(l)
upfile.close()
downfile.close()
print "[INFO] ALL DONE"
print "[INFO] remember to chmod +x vpnup.sh vpndown.sh"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment