diff --git a/bin/dns.py b/bin/dns.py index 03935ae..8010c89 100644 --- a/bin/dns.py +++ b/bin/dns.py @@ -4,7 +4,9 @@ import threading import json import base64 +import logging +logging.basicConfig(level=logging.INFO) dict_data = {} dict_config = {} Remote_dns_server='114.114.114.114' @@ -63,7 +65,7 @@ def SendDnsData(data,s,addr): try: rspdata = sock.recv(4096) except Exception as e: - print "sock.recv info:",e + logging.warn("Recv:\t%s"%e) break s.sendto(rspdata ,addr) break @@ -74,7 +76,7 @@ def main(s): data, addr = s.recvfrom(2048) Tthreading(data,s,addr) except Exception as e: - print "Unknow error :\t",e + logging.warn("Unknow error:\t%s"%e) if __name__ == '__main__': @@ -92,6 +94,7 @@ def main(s): except Exception as e: print "\nBinding failed! Please run as administrator,\n\nAnd check the local IP address and port is correct?\n" print "==========Error message==========" - raise e + logging.critical(e) + sys.exit(-1) print "Bind successfully! Running ..." main(s) diff --git a/bin/dnslib/dnsfucation.py b/bin/dnslib/dnsfucation.py index 2cf4206..b3df0e2 100644 --- a/bin/dnslib/dnsfucation.py +++ b/bin/dnslib/dnsfucation.py @@ -1,5 +1,6 @@ # -*- coding:utf-8 -*- - +import logging +logging.basicConfig(level=logging.INFO) def Search_key_ip(string,dict_data): string = string[:-1] if string in dict_data.keys(): @@ -19,13 +20,13 @@ def Hex2Ascii(string): Int = int(string,16) return chr(Int) except Exception as e: - print "Hex2Ascii info:\t",e + logging.warn("Hex2Ascii info:\t%s"%e) def Hex2Str_dec(string): try: return str(int(string,16)) except Exception as e: - print "Hex2Str_dec info:\t",e + logging.warn("Hex2Str_dec info:\t%s"%e) def HexIP2DecIP(string) : string = string.replace('\t','').replace('\n','').replace(' ','') @@ -36,7 +37,7 @@ def HexIP2DecIP(string) : else: return "?.?.?.?" except Exception as e: - print "HexIP2DecIP info:\t",e + logging("HexIP2DecIP info:\t%s"%e) def DnshextoDomain(string,start=24,end=26): '''原始数据:域名指针默认在24-26个字节位置, @@ -57,7 +58,7 @@ def DnshextoDomain(string,start=24,end=26): end +=2 return ''.join(Domain),end except Exception as e: - print "DnshextoDomain info:\t",e + logging.warn("DnshextoDomain info:\t%s"%e) def GetDnsDomainIP(data): '''提取域名和IP @@ -92,7 +93,7 @@ def GetDnsDomainIP(data): return iptext except Exception as e: - print "GetDnsDomainIP info:\t",e + logging("GetDnsDomainIP info:\t%s"%e) return [] def analysis(data,dict_data): @@ -112,6 +113,16 @@ def analysis(data,dict_data): data = data.encode('hex').replace(iplist[i],dnsip).decode('hex') return data +def IP2HEX(ip): + zone = ip.split(".") + HEX = '' + for i in zone: + i = hex(int(i)).replace("0x",'') + if len(i) < 2: + i = '0' + i + HEX += i + return HEX + def analysis2(data,dict_data): '''构造DNS报文''' data = data.encode('hex') @@ -119,7 +130,7 @@ def analysis2(data,dict_data): ip = None if len(domain) >0: - print "Query:\t",domain + logging.info("Query:\t%s"%domain) ip = Search_key_ip(domain,dict_data) if ip : if data[end+2:end+4] == '1c': @@ -129,8 +140,9 @@ def analysis2(data,dict_data): data = data[0:4] + '81800001000100000000'+data[24:end]+'00010001c00c000100010000003f0004' #十进制表示的IP变为十六进制表示的IP - dnsip = '{:02X}{:02X}{:02X}{:02X}'.format(*map(int, ip.split('.'))).lower() - print "Revise:\t",domain + #dnsip = '{:02X}{:02X}{:02X}{:02X}'.format(*map(int, ip.split('.'))).lower() + dnsip = IP2HEX(ip) + logging.info("Revise:\t%s"%domain) data = data + dnsip return 1,data.decode('hex') return 0,data.decode('hex') diff --git a/conf/data.json b/conf/data.json index 56837fb..164ffd9 100644 --- a/conf/data.json +++ b/conf/data.json @@ -1,7 +1,8 @@ { "hosts":{ "racaljk":"https://coding.net/u/scaffrey/p/hosts/git/raw/master/hosts", - "yhosts":"https://raw.githubusercontent.com/vokins/yhosts/master/hosts" + "yhosts":"https://raw.githubusercontent.com/vokins/yhosts/master/hosts", + "adaway":"https://raw.githubusercontent.com/aoccin/adaway/master/hosts" }, "DNSMasq":"I'm too lazy, you know", "jsonb64":"https://raw.githubusercontent.com/dowsnature/dowsDNS/master/data/wrcd.base64"