import os import sys import asyncio from Controller import Log, Config from Util import SocksProxy #version version = "0.1 Reborn" #main execute if __name__ == "__main__": Log.info("Starting brbypass [" + version + "]...") Log.debug("Config: " + Config.config.host + ":" + str(Config.config.port) + " @ \"" + Config.config.password + "\" % " + Config.config.mode) #mode change if Config.config.mode == "socks": loop = asyncio.get_event_loop() if (loop is None): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) proxyServer = SocksProxy.SocksProxyServer(Config.config.host, Config.config.port) try: proxyServer.start(loop) except KeyboardInterrupt: proxyServer.stop(loop) os._exit(0)
async def handle_client(self, reader, writer): timeout = 15 client_peername = writer.get_extra_info('peername') Log.info("New Connection from: " + str(client_peername)) remote = None loop = asyncio.get_event_loop() while True: try: headBuffer = await asyncio.wait_for(reader.read(2), timeout=timeout) except: break if (headBuffer is None): continue else: if headBuffer == b'\x07\x02': cmd = await asyncio.wait_for(reader.read(1), timeout=timeout) Log.debug('cmd==' + str(cmd)) if (cmd == b'\x01'): if (len(Config.config.password) > 0): _len = await asyncio.wait_for(reader.read(1), timeout=timeout) length = int.from_bytes(_len, byteorder='little') _password = await asyncio.wait_for( reader.read(length), timeout=timeout) password = _password.decode('utf-8') if (password == Config.config.password): # Response Message # 0 1 2 3 # x06 x03 CMD STATUS writer.write(b'\x06\x03\x02\x00') await writer.drain() else: writer.write(b'\x06\x03\x02\x01') await writer.drain() else: writer.write(b'\x06\x03\x02\x00') await writer.drain() Log.debug("Auth resposne has sent to local.") elif (cmd == b'\x02'): address = None port = None b_type = await asyncio.wait_for(reader.read(1), timeout=timeout) if (b_type == b'\x01'): # process header address = "" for i in range(4): address = address + str( int.from_bytes(await asyncio.wait_for( reader.read(1), timeout=timeout), byteorder='little')) if (i != 3): address = address + '.' b_port = await asyncio.wait_for(reader.read(2), timeout=timeout) port = int.from_bytes(b_port, byteorder='little') elif (b_type == b'\x03'): # process header b_dLength = await asyncio.wait_for(reader.read(2), timeout=timeout) domainLength = int.from_bytes(b_dLength, byteorder='little') b_port = await asyncio.wait_for(reader.read(2), timeout=timeout) port = int.from_bytes(b_port, byteorder='little') address = await asyncio.wait_for( reader.read(domainLength), timeout=timeout) elif (b_type == b'\x04'): # process header address = "" for i in range(4): address = address + \ binascii.b2a_hex(await asyncio.wait_for(reader.read(4), timeout=timeout)) if i != 3: address = address + ":" b_port = await asyncio.wait_for(reader.read(2), timeout=timeout) port = int.from_bytes(b_port, byteorder='little') addrs = await loop.getaddrinfo(host=address, port=port) if len(addrs) == 0: Log.error('Get addrinfo failed.') return af, socktype, proto, canonname, sa = addrs[0] remote = socket.socket(af, socktype, proto) remote.setblocking(0) remote.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) try: await loop.sock_connect(remote, sa) except Exception as e: Log.error("Can't connect to remote: " + str(e)) return elif (cmd == b'\x03'): # Get contentLength b_length = await asyncio.wait_for(reader.read(2), timeout=timeout) contentLength = int.from_bytes(b_length, byteorder='little') b_content = await asyncio.wait_for( reader.read(contentLength), timeout=timeout) asyncio.run_coroutine_threadsafe(self.transferData( remote=remote, writer=writer, data=b_content), loop=self.ioloop)