1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
import SocketServer import struct import socket as socketlib
class SinDNSQuery: def __init__(self, data): i = 1 self.name = '' while True: d = ord(data[i]) if d == 0: break; if d < 32: self.name = self.name + '.' else: self.name = self.name + chr(d) i = i + 1 self.querybytes = data[0:i + 1] (self.type, self.classify) = struct.unpack('>HH', data[i + 1:i + 5]) self.len = i + 5 def getbytes(self): return self.querybytes + struct.pack('>HH', self.type, self.classify)
class SinDNSAnswer: def __init__(self, ip): self.name = 49164 self.type = 1 self.classify = 1 self.timetolive = 190 self.datalength = 4 self.ip = ip
def getbytes(self): res = struct.pack('>HHHLH', self.name, self.type, self.classify, self.timetolive, self.datalength) s = self.ip.split('.') res = res + struct.pack('BBBB', int(s[0]), int(s[1]), int(s[2]), int(s[3])) return res
class SinDNSFrame: def __init__(self, data): (self.id, self.flags, self.quests, self.answers, self.author, self.addition) = struct.unpack('>HHHHHH', data[0:12]) self.query = SinDNSQuery(data[12:])
def getname(self): return self.query.name
def setip(self, ip): self.answer = SinDNSAnswer(ip) self.answers = 1 self.flags = 33152
def getbytes(self):
res = struct.pack('>HHHHHH', self.id, self.flags, self.quests, self.answers, self.author, self.addition) res = res + self.query.getbytes() if self.answers != 0: res = res + self.answer.getbytes() return res
class SinDNSUDPHandler(SocketServer.BaseRequestHandler): def handle(self): data = self.request[0].strip() dns = SinDNSFrame(data) socket = self.request[1] namemap = SinDNSServer.namemap if(dns.query.type==1): name = dns.getname(); toip = None ifrom = "map" if namemap.__contains__(name): toip = namemap[name] elif namemap.__contains__('*'): toip = namemap['*'] else: try: toip = socketlib.getaddrinfo(name,0)[0][4][0] ifrom = "sev" except Exception, e: print 'get ip fail' if toip: dns.setip(toip)
print '%s: %s-->%s (%s)'%(self.client_address[0], name, toip, ifrom) socket.sendto(dns.getbytes(), self.client_address) else: socket.sendto(data, self.client_address)
class SinDNSServer: def __init__(self, port=53): SinDNSServer.namemap = {} self.port = port
def addname(self, name, ip): SinDNSServer.namemap[name] = ip
def start(self): HOST, PORT = "0.0.0.0", self.port server = SocketServer.UDPServer((HOST, PORT), SinDNSUDPHandler) server.serve_forever()
if __name__ == "__main__": sev = SinDNSServer() sev.addname('ns0.nogan.ga','x.x.x.x') sev.addname('ns1.nogan.ga','x.x.x.x') sev.addname('www.nogan.ga','y.y.y.y') sev.addname('*', '127.0.0.1')
sev.start()
|