From 8c6aecfb595ac59e0b5563688bb15cf0e4603049 Mon Sep 17 00:00:00 2001 From: b1eberg0n Date: Wed, 20 Apr 2016 17:32:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=A4=9A=E7=A7=8D=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=96=B9=E5=BC=8F;=E5=8E=BB=E6=8E=89=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + bjdns.conf | 10 ------- bjdns.py | 85 +++++++++++++++++++++++++----------------------------- 3 files changed, 41 insertions(+), 55 deletions(-) delete mode 100755 bjdns.conf diff --git a/.gitignore b/.gitignore index a364fae..a245dd0 100644 --- a/.gitignore +++ b/.gitignore @@ -66,3 +66,4 @@ bjdns_sakura1.py cache.txt bjdns.7z bjdns_openshift.py +bjdns.conf diff --git a/bjdns.conf b/bjdns.conf deleted file mode 100755 index 7191e68..0000000 --- a/bjdns.conf +++ /dev/null @@ -1,10 +0,0 @@ -[listen] -ip = 0.0.0.0 -port = 53 - -[fuckgfw] -google_ip = 64.233.162.83 - -[dns] -server = 119.29.29.29 -port = 53 diff --git a/bjdns.py b/bjdns.py index 5d7cbed..d85f869 100755 --- a/bjdns.py +++ b/bjdns.py @@ -6,23 +6,19 @@ import requests def get_data(data,cdn=0): - s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) - if cdn: - s.sendto(data, (dns['server'],dns['port'])) - else: - s.sendto(data, ('208.67.220.220', 5353)) + s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) + s.sendto(data, ('119.29.29.29', 53)) data = s.recv(512) return data def get_data_by_tcp(data): data = pack('>H', len(data)) + data - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.settimeout(2) s.connect(('160.16.101.80', 5333)) - # s.connect(('210.21.196.6', 53)) - # s.connect(('119.29.29.29', 53)) s.send(data) - res = s.recv(512) + res = s.recv(512) return res[2:] @@ -37,42 +33,42 @@ def make_data(data, ip): #data即请求包 (id, flags, quests, answers, author, addition) = unpack('>HHHHHH', data[0:12]) - flags_new = 33152 + flags_new = 33152 answers_new = 1 - res = pack('>HHHHHH', id, flags_new, quests, - answers_new, author, addition) + res = pack('>HHHHHH', id, flags_new, quests, + answers_new, author, addition) - res += data[12:] + res += data[12:] - dns_answer = { + dns_answer = { 'name':49164, 'type':1, 'classify':1, 'ttl':190, 'datalength':4 } - res += pack('>HHHLH', dns_answer['name'], dns_answer['type'], + res += pack('>HHHLH', dns_answer['name'], dns_answer['type'], dns_answer['classify'], dns_answer['ttl'], dns_answer['datalength']) - ip = ip.split('.') - ip_bytes = pack('BBBB', int(ip[0]), int(ip[1]), + ip = ip.split('.') + ip_bytes = pack('BBBB', int(ip[0]), int(ip[1]), int(ip[2]), int(ip[3])) - res += ip_bytes + res += ip_bytes return res def get_ip(res, data_len): index_0101 = res[data_len:].index(b'\x00\x01\x00\x01') - ip_bytes = unpack('BBBB',res[data_len:][index_0101+10:index_0101+14]) - ip = '.'.join( [ str(i) for i in ip_bytes ] ) + ip_bytes = unpack('BBBB',res[data_len:][index_0101+10:index_0101+14]) + ip = '.'.join( [ str(i) for i in ip_bytes ] ) return ip def eva(data, client, server): list_iter = iter(data[13:]) - name = '' + name = '' for bit in iter(lambda: next(list_iter), 0): name += '.' if bit < 32 else chr(bit) @@ -93,34 +89,32 @@ def eva(data, client, server): elif [ 1 for i in cdn_list if name.endswith(i) or name == i[1:] ]: print('cdn', name) - # server.sendto(get_data(data,cdn=1), client) res = get_data(data,cdn=1) # res = get_data_by_tcp(data) server.sendto(res, client) - # if name == 'rss.bjgong.tk': if 'bjgong.tk' in name: ip = get_ip(res, len(data)) cache[name] = ip else: - # ip = get_ip_by_openshift(name) - # server.sendto(make_data(data,ip), client) - - resp = get_data_by_tcp(data) - server.sendto(resp, client) + try: + resp = get_data_by_tcp(data) + server.sendto(resp, client) + ip = get_ip(resp, len(data)) + except socket.timeout: + ip = get_ip_by_openshift(name) + server.sendto(make_data(data,ip), client) + # ip = unpack('BBBB',data[32+len(name):36+len(name)]) # ip = '.'.join( [ str(i) for i in ip ] ) - ip = get_ip(resp, len(data)) print(name, ip) cache[name] = ip - # with open('cache.txt','a') as f: - # f.write('{} {}\n'.format(name,ip)) - # exit() def adem(): server = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) - server.bind((listen['ip'],listen['port'])) + # server.bind((listen['ip'],listen['port'])) + server.bind(('0.0.0.0', 53)) while 1: try: data, client = server.recvfrom(512,) @@ -132,17 +126,18 @@ def adem(): if __name__ == "__main__": - cf = configparser.ConfigParser() - cf.read('bjdns.conf') - listen = { - 'ip':cf.get('listen','ip'), - 'port':int(cf.get('listen','port')) - } - dns = { - 'server':cf.get('dns','server'), - 'port':int(cf.get('dns','port')) - } - google_ip = cf.get('fuckgfw','google_ip') + # cf = configparser.ConfigParser() + # cf.read('bjdns.conf') + # listen = { + # 'ip':cf.get('listen','ip'), + # 'port':int(cf.get('listen','port')) + # } + # dns = { + # 'server':cf.get('dns','server'), + # 'port':int(cf.get('dns','port')) + # } + # google_ip = cf.get('fuckgfw','google_ip') + google_ip = '64.233.162.83' cdn_list = open('cdnlist.txt','r').read().split('\n') cdn_list.pop() @@ -175,7 +170,7 @@ def quit(): sys.exit() - print(dns) + # print(dns) root = Tk() root.tk.call('package', 'require', 'Winico') icon = root.tk.call('winico', 'createfrom', os.path.join(os.getcwd(), 'py.ico')) # New icon resources