Skip to content

Instantly share code, notes, and snippets.

@wadee
Forked from lanceliao/dnsmasq-gfwlist.py
Last active April 25, 2021 07:08
Show Gist options
  • Save wadee/2be067c51e90d962eddf to your computer and use it in GitHub Desktop.
Save wadee/2be067c51e90d962eddf to your computer and use it in GitHub Desktop.
将gfwlist转换成带ipset的dnsmasq规则,适用于OpenWrt智能上网
#!/usr/bin/env python
#coding=utf-8
#
# Generate a list of dnsmasq rules with ipset for gfwlist
#
# Copyright (C) 2014 http://www.shuyz.com
# Ref https://code.google.com/p/autoproxy-gfwlist/wiki/Rules
import urllib2
import re
import os
import datetime
import base64
import shutil
mydnsip = '127.0.0.1'
mydnsport = '1053'
# the url of gfwlist
baseurl = 'https://autoproxy-gfwlist.googlecode.com/svn/trunk/gfwlist.txt'
# match comments/title/whitelist/ip address
comment_pattern = '^\!|\[|^@@|^\d+\.\d+\.\d+\.\d+'
domain_pattern = '([\w\-\_]+\.[\w\.\-\_]+)[\/\*]*'
tmpfile = '/tmp/gfwlisttmp'
# do not write to router internal flash directly
outfile = '/tmp/gfwlist.conf'
rulesfile = '/etc/dnsmasq.d/gfwlist.conf'
fs = file(outfile, 'w')
fs.write('# gfw list ipset rules for dnsmasq\n')
fs.write('# updated on ' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '\n')
fs.write('#\n')
print 'fetching list...'
content = urllib2.urlopen(baseurl, timeout=15).read().decode('base64')
# write the decoded content to file then read line by line
tfs = open(tmpfile, 'w')
tfs.write(content)
tfs.close()
tfs = open(tmpfile, 'r')
print 'page content fetched, analysis...'
# remember all blocked domains, in case of duplicate records
domainlist = []
for line in tfs.readlines():
if re.findall(comment_pattern, line):
print 'this is a comment line: ' + line
#fs.write('#' + line)
else:
domain = re.findall(domain_pattern, line)
if domain:
try:
found = domainlist.index(domain[0])
print domain[0] + ' exists.'
except ValueError:
print 'saving ' + domain[0]
domainlist.append(domain[0])
fs.write('server=/.%s/%s#%s\n'%(domain[0],mydnsip,mydnsport))
fs.write('ipset=/.%s/gfwlist\n'%domain[0])
else:
print 'no valid domain in this line: ' + line
tfs.close()
fs.close();
print 'moving generated file to dnsmasg directory'
shutil.move(outfile, rulesfile)
print 'restart dnsmasq...'
print os.popen('/etc/init.d/dnsmasq restart').read()
print 'done!'
package main
import "fmt"
const (
UINT_MAX = ^uint(0)
INT_MAX = int(UINT_MAX >> 1)
INT_MIN = -INT_MAX - 1
)
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
len1 := len(nums1)
len2 := len(nums2)
if len1 > len2 {
return findMedianSortedArrays(nums2, nums1)
}
k := (len1 + len2 + 1) / 2
isOdd := false
if (len1+len2)%2 == 1 {
isOdd = true
}
l, r := 0, len1
for l < r {
m1 := l + (r-l)/2
m2 := k - m1
if nums2[m2-1] > nums1[m1] {
l = m1 + 1
} else {
r = m1
}
}
m1 := l
m2 := k - l
median := max(getValue(nums1, m1-1), getValue(nums2, m2-1))
if isOdd {
return float64(median)
}
median1 := min(getValue(nums1, m1), getValue(nums2, m2))
return (float64(median1) + float64(median)) / 2
}
func getValue(nums []int, index int) int {
if index < 0 {
return INT_MIN
}
if index == len(nums) {
return INT_MAX
}
return nums[index]
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func main() {
nums1 := []int{1, 3, 4}
nums2 := []int{2}
fmt.Println(findMedianSortedArrays(nums1, nums2))
}
#!/bin/sh /etc/rc.common
# Copyright (C) 2006-2011 OpenWrt.org
# ref http://ipset.netfilter.org/ipset.man.html
START=95
SERVICE_USE_PID=1
SERVICE_WRITE_PID=1
SERVICE_DAEMONIZE=1
start() {
echo starting ss-redir...
service_start /opt/bin/ss-redir -c /etc/shadowsocks.json
echo loading firewall rules...
ipset create gfwlist hash:ip counters timeout 1200
iptables -t nat -A zone_lan_prerouting -p tcp -m set --match-set gfwlist dst -j REDIRECT --to-port 1081
echo done.
}
stop() {
echo stopping ss-redir...
service_stop /opt/bin/ss-redir
echo restarting firewall...
/etc/init.d/firewall restart
echo done.
}
@wadee
Copy link
Author

wadee commented Oct 1, 2015

good jobs!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment