def data_received(self, data): if self.stage == constants.STAGE_HELLO: ver, nmethods = struct.unpack('!BB', data[0:2]) if ver != 5: raise error.NotRecognizeProtocolException('Unsuport') methods = struct.unpack('!' + 'B' * nmethods, data[2:2 + nmethods]) if constants.METHOD_USER in methods: self.method = constants.METHOD_USER elif constants.METHOD_NOAUTH in methods: self.method = constants.METHOD_NOAUTH else: self.method = constants.METHOD_NOAC resp = b'\x05' + struct.pack('!B', self.method) log.info("HELLO FINISH") self.transport.write(resp) self.transport.write_eof() if self.method == constants.METHOD_NOAC: self.transport.close() log.info("No available auth method !") return if self.method == constants.METHOD_NOAUTH: self.stage = constants.STAGE_INIT else: self.stage = constants.STAGE_AUTH elif self.stage == constants.STAGE_AUTH: log.info("AUTH FINISH") self.auth(data) elif self.stage == constants.STAGE_INIT: ver, cmd, rsv, atyp = struct.unpack('!BBBB', data[0:4]) log.info("INIT FINISH") if cmd == constants.SOCKS_CMD_CONNECT: domain, port = self.parse_connect(atyp, data) self.waiter = asyncio.ensure_future( self.cmd_connect(domain, port)) self.stage = constants.STAGE_WORK log.info("connect to {}, {}".format(domain, port)) elif cmd == constants.SOCKS_CMD_BIND: pass else: raise NotImplementedError("Not implement {} yet!".format(cmd)) elif self.stage == constants.STAGE_WORK: log.debug("send data with length {}".format(len(data))) asyncio.ensure_future(self.send_data(data)) else: raise Exception("Unknown stage")
async def send_data(cls, msg): reader, writer = await asyncio.open_connection(cls.host, cls.port, loop=cls.loop) log.info("Client connection host: {} ,port: {} ".format( cls.host, cls.port)) writer.write(msg) await writer.drain() log.info("Client write mesg :{}".format(str(msg))) data = await reader.read(-1) log.info("Client receive resp {}".format(str(data))) # resp = b'' # while data: # resp = resp + data # data = await self.reader.read(1024) # log.info("Client finish receive resp {}".format(str(resp))) writer.close() return data
def connection_lost(self, exc): log.info('Local server closed the connection') log.info('Stop the event loop')
def data_received(self, data): message = data log.info("Local recive data {} ".format(str(message))) func = functools.partial(send_2_local, transport=self.transport) asyncio.ensure_future(Fly6to4Client.send_data(message), loop=self.loop).add_done_callback(func)
def connection_made(self, transport): self.connections[self] = True self.transport = transport log.info('Connection from {}'.format( transport.get_extra_info('peername')))
import asyncio from server.servers import Fly4to6Server from public.logger import log import uvloop import logging if __name__ == '__main__': loop = uvloop.new_event_loop() asyncio.set_event_loop(loop) loop.set_debug(True) logging.getLogger('').setLevel(logging.DEBUG) local_server = loop.create_server(lambda: Fly4to6Server(loop=loop), host='127.0.0.1', port=8080) server = loop.run_until_complete(local_server) try: log.info("Server start at port : {}".format(8080)) loop.run_forever() except KeyboardInterrupt: pass server.close() loop.run_until_complete(server.wait_closed()) loop.close()
def connection_made(self, transport): log.info('Connection from {}'.format( transport.get_extra_info('peername'))) self.transport = transport self.stage = constants.STAGE_HELLO
import asyncio from client.local import Fly4to6local from public.logger import log import logging if __name__ == '__main__': loop = asyncio.get_event_loop() loop.set_debug(True) logging.getLogger('').setLevel(logging.DEBUG) local_server = loop.create_server( lambda: Fly4to6local(loop=loop, host="127.0.0.1", port=8080), host="127.0.0.1", port=1080) server = loop.run_until_complete(local_server) try: log.info("Local start at port : {}".format(1080)) loop.run_forever() except KeyboardInterrupt: pass server.close() loop.run_until_complete(server.wait_closed()) loop.close()