Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rule support for ChinaDNS. #79

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ can use it somewhere else, like in other type of connections.
Since Windows doesn't provide hook scripts for PPTP dialing, you have to run
those manually before connecting and after disconnecting.

ChinaDNS Usage
----------

The generated chnroute.txt file in this section is general-purpose for other
OpenWRT application than ChinaDNS (such as [Shadowsocks-libev for OpenWrt]).

### ChinaDNS (including other OpenWRT applications)

* Download `chnroutes.py`.
* `cd` into the download destination and run `python chnroutes.py -p chinadns`.
This generate `chnroute.txt` file.

Using this on a router
----------------------

Expand Down Expand Up @@ -131,4 +143,5 @@ write an issue.
[APNIC Delegated List]:https://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest
[chinaip]:https://github.com/liudongmiao/chinaip
[PR48]:https://github.com/fivesheep/chnroutes/pull/48
[autoddvpn]:https://github.com/lincank/autoddvpn
[autoddvpn]:https://github.com/lincank/autoddvpn
[Shadowsocks-libev for OpenWrt]:https://github.com/shadowsocks/openwrt-shadowsocks
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ PPTP 用法

如果机器上没有安装 Python, 可以直接从下载页面上下载已经预生成的 bat 文件。

ChinaDNS 用法
---------

生成的chnroute.txt是OpenWRT大部分应用通用的(如[Shadowsocks-libev for OpenWrt])。

### ChinaDNS (包括其他 OpenWRT 应用)

* 下载 `chnroutes.py`。
* 从终端进入下载目录, 执行 `python chnroutes.py -p chinadns`, 执行之后会生成 `chnroute.txt`
文件。

在路由器上使用
--------------

Expand Down Expand Up @@ -137,4 +148,5 @@ emule, BT 等 P2P 下载软件(P2P 流量不应穿过 VPN)。
[APNIC Delegated List]:https://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest
[chinaip]:https://github.com/liudongmiao/chinaip
[PR48]:https://github.com/fivesheep/chnroutes/pull/48
[autoddvpn]:https://github.com/lincank/autoddvpn
[autoddvpn]:https://github.com/lincank/autoddvpn
[Shadowsocks-libev for OpenWrt]:https://github.com/shadowsocks/openwrt-shadowsocks
25 changes: 22 additions & 3 deletions chnroutes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import argparse
import math
import textwrap
import ssl

ssl._create_default_https_context = ssl._create_unverified_context

def generate_ovpn(metric):
results = fetch_ip_data()
Expand All @@ -18,6 +20,15 @@ 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_chinadns(metric):
exchange_mask = lambda ipmask: sum(bin(int(i)).count('1') \
for i in ipmask.split('.'))
results = fetch_ip_data()
rfile=open('chnroute.txt','w')
for ip,mask,_ in results:
route_item="%s/%s\r\n"%(ip,exchange_mask(mask))
rfile.write(route_item)
rfile.close()

def generate_linux(metric):
results = fetch_ip_data()
Expand Down Expand Up @@ -195,8 +206,14 @@ def fetch_ip_data():
#fetch data from apnic
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()

# data=urllib2.urlopen(url).read()
data=None
try:
# global data
data=urllib2.urlopen(url, timeout = 3).read()
except urllib2.URLError, e:
raise MyException("There was an error: %r" % e)
exit()
cnregex=re.compile(r'apnic\|cn\|ipv4\|[0-9\.]+\|[0-9]+\|[0-9]+\|a.*',re.IGNORECASE)
cndata=cnregex.findall(data)

Expand Down Expand Up @@ -245,7 +262,9 @@ def fetch_ip_data():

args = parser.parse_args()

if args.platform.lower() == 'openvpn':
if args.platform.lower() == 'chinadns':
generate_chinadns(args.metric)
elif args.platform.lower() == 'openvpn':
generate_ovpn(args.metric)
elif args.platform.lower() == 'linux':
generate_linux(args.metric)
Expand Down