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
|
import socket,asyncore,sys from Crypto.Cipher import ARC4 from Crypto.Hash import SHA DEBUG=False cipher_rc4_obj=None class Transmiter(asyncore.dispatcher): '''数据交换类,两个此类的实例可互换tx和rx,若初始化socket则表示server,否则是client''' def __init__(self,socket_info,transmitee=None): '''参数为socket_info,另一个交换实例'''\ '''(作为client时socket_info为tuple(ip,port),作为server时socket_info为accept得到的socket)'''\ '''例如Transmiter((12.34.56.78,8080),transee)实例化了一个client端'''\ '''Transmiter(socket_acptd,transee)实例化了一个server端''' self.buf_Tx='' self.buf_Rx='' self.transmitee=transmitee if None!=transmitee and None==transmitee.transmitee: transmitee.transmitee=self if isinstance(socket_info,socket.socket): asyncore.dispatcher.__init__(self,socket_info) if DEBUG: print 'Transer server' elif isinstance(socket_info[0],str) and isinstance(socket_info[1],int): asyncore.dispatcher.__init__(self) self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.connect(socket_info) if DEBUG: print 'Transer client' else: raise Exception
def handle_read(self): read = self.recv(4096) if DEBUG: print 'Transer Read\t%d bytes' % (len(read)) if None!=self.transmitee: self.transmitee.buf_Tx += read else: print 'Transer Drop\t%d bytes' % (len(read)) def writable(self): return (len(self.buf_Tx) > 0 ) def handle_write(self): sent = self.send(self.buf_Tx) self.buf_Tx = self.buf_Tx[sent:] if DEBUG: print 'Transer Write\t%d bytes' % (sent) def handle_close(self): self.close() if DEBUG: print 'Transer close' if None!=self.transmitee: self.transmitee.close() if DEBUG: print 'Transer\'s transee close' def handle_error(self): if DEBUG: print 'Transer error occur' self.handle_close() def handle_expt(self): if DEBUG: print 'Transer exception occur' self.handle_close() class Connector(object): def __init__(self,remoteaddr1,remoteaddr2): self.remoteaddr1=remoteaddr1 self.remoteaddr2=remoteaddr2 self.trans1=Transmiter(remoteaddr1) self.trans2=Transmiter(remoteaddr2,self.trans1) class Listener(asyncore.dispatcher): def __init__(self,localaddr,backlog=5): asyncore.dispatcher.__init__(self) self.lasttrans=None self.create_socket(socket.AF_INET,socket.SOCK_STREAM) self.set_reuse_addr() self.bind(localaddr) self.listen(backlog) def handle_accept(self): conn, addr = self.accept() print 'accepted %s' % str(addr) if None==self.lasttrans: print 'wait for another' self.lasttrans=Transmiter(conn) else: print 'paired to last one' Transmiter(conn,self.lasttrans) self.lasttrans=None class Forwarder(asyncore.dispatcher): def __init__(self, localaddr, remoteaddr, backlog=5): asyncore.dispatcher.__init__(self) self.remoteaddr=remoteaddr self.create_socket(socket.AF_INET,socket.SOCK_STREAM) self.set_reuse_addr() self.bind(localaddr) self.listen(backlog) def handle_accept(self): conn, addr = self.accept() print 'Forward from %s to %s' % (str(addr),self.remoteaddr) trans_Acptd=Transmiter(conn) trans_toRemote=Transmiter(self.remoteaddr,trans_Acptd) class TransmiterWithCipher(Transmiter): def __init__(self,socket_info,cipher_obj=None,isEnc=False,transmitee=None): Transmiter.__init__(self,socket_info,transmitee) self.cipher_obj=cipher_obj self.isEnc=isEnc def handle_read(self): read = self.recv(4096) if DEBUG: print 'Transer Read\t%d bytes' % (len(read)) if None!=self.cipher_obj and False!=self.isEnc: read=self.cipher_obj.decrypt(read) if None!=self.transmitee: if None != self.transmitee.cipher_obj and False!=self.transmitee.isEnc: read = self.transmitee.cipher_obj.encrypt(read) self.transmitee.buf_Tx +=read else: print 'Transer Drop\t%d bytes' % (len(read)) class ForwarderWithCipher(Forwarder): def __init__(self, localaddr, remoteaddr, cipher_obj=None,localenc=False,remoteenc=False,backlog=5): self.cipher_obj=cipher_obj self.localenc=localenc self.remoteenc=remoteenc Forwarder.__init__(self, localaddr, remoteaddr, backlog) def handle_accept(self): conn, addr = self.accept() print 'Forward from %s to %s' % (str(addr),self.remoteaddr) trans_Acptd=TransmiterWithCipher(conn,self.cipher_obj,self.localenc) trans_toRemote=TransmiterWithCipher(self.remoteaddr,self.cipher_obj,self.remoteenc,trans_Acptd)
|