def test_dumps_and_loads_succeed(self): password = randomPassword() string = dumpsPassword(password) loaded_password = loadsPassword(string) self.assertEqual(password, loaded_password)
def setUp(self): self.ls_local, self.ls_server = socket.socketpair() password = randomPassword() self.loop = asyncio.new_event_loop() self.cipher = Cipher.NewCipher(password) self.securesocket = SecureSocket(loop=self.loop, cipher=self.cipher) self.msg = bytearray(b'hello world') self.encripted_msg = self.msg.copy() self.cipher.encode(self.encripted_msg)
def setUp(self): self.listenAddr = net.Address('127.0.0.1', getValidAddr()[1]) password = randomPassword() self.cipher = Cipher.NewCipher(password) self.loop = asyncio.new_event_loop() self.server = LsServer(loop=self.loop, password=password, listenAddr=self.listenAddr)
def test_dumps_and_loads_fail(self): password = randomPassword() password[random.randint(1, 255)] = 0 with self.assertRaises(InvalidPasswordError): dumpsPassword(password) string = base64.encodebytes(password).decode('utf8', errors='strict') with self.assertRaises(InvalidPasswordError): loadsPassword(string) password = randomPassword() password = password[:-2] with self.assertRaises(InvalidPasswordError): dumpsPassword(password) string = dumpsPassword(IDENTITY_PASSWORD) string = string[:-3] with self.assertRaises(InvalidPasswordError): loadsPassword(string)
def test_encryption(self): password = randomPassword() original_data = bytearray() for _ in range(0xffff): original_data.append(random.randint(0, 255)) cipher = Cipher.NewCipher(password) data = original_data.copy() cipher.encode(data) self.assertNotEqual(data, original_data) cipher.decode(data) self.assertEqual(data, original_data)
def setUp(self): self.listenAddr = net.Address('127.0.0.1', random.randint(10000, 20000)) self.dstServer = socket.socket() self.dstServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.dstServer.bind(('127.0.0.1', 44444)) self.dstServer.listen(1) password = randomPassword() self.cipher = Cipher.NewCipher(password) self.loop = asyncio.new_event_loop() self.server = LsServer(loop=self.loop, password=password, listenAddr=self.listenAddr)
def setUp(self): self.listenAddr = net.Address('127.0.0.1', 11111) self.remoteAddr = net.Address('127.0.0.1', 22222) self.remoteServer = socket.socket() self.remoteServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.remoteServer.bind(self.remoteAddr) self.remoteServer.listen(1) password = randomPassword() self.cipher = Cipher.NewCipher(password) self.loop = asyncio.new_event_loop() self.local = LsLocal(loop=self.loop, password=password, listenAddr=self.listenAddr, remoteAddr=self.remoteAddr) self.msg = bytearray(b'hello world') self.encrypted_msg = self.msg.copy() self.cipher.encode(self.encrypted_msg)
def test_randomPassword(self): for idx in range(0xff): with self.subTest(idx): password = randomPassword() isValid = validatePassword(password) self.assertTrue(isValid)
def main(): parser = argparse.ArgumentParser( description='A light tunnel proxy that helps you bypass firewalls') parser.add_argument('--version', action='store_true', default=False, help='show version information') proxy_options = parser.add_argument_group('Proxy options') proxy_options.add_argument('--save', metavar='CONFIG', help='path to dump config') proxy_options.add_argument('-c', metavar='CONFIG', help='path to config file') proxy_options.add_argument('-s', metavar='SERVER_ADDR', help='server address, default: 0.0.0.0') proxy_options.add_argument('-p', metavar='SERVER_PORT', type=int, help='server port, default: 8388') proxy_options.add_argument('-k', metavar='PASSWORD', help='password') proxy_options.add_argument('--random', action='store_true', default=False, help='generate a random password to use') args = parser.parse_args() if args.version: print('lightsocks 0.1.0') sys.exit(0) config = lsConfig.Config(None, None, None, None, None) if args.c: try: with open(args.c, encoding='utf-8') as f: file_config = lsConfig.load(f) except lsConfig.InvalidFileError: parser.print_usage() print(f'invalid config file {args.c!r}') sys.exit(1) except FileNotFoundError: parser.print_usage() print(f'config file {args.c!r} not found') sys.exit(1) config = config._replace(**file_config._asdict()) if args.s: serverAddr = args.s # TODO: 验证 serverAddr 有效性 config = config._replace(serverAddr=serverAddr) if args.p: serverPort = args.p # TODO: 验证 serverPort 有效性 config = config._replace(serverPort=serverPort) if args.k: try: password = loadsPassword(args.k) config = config._replace(password=password) except InvalidPasswordError: parser.print_usage() print('invalid password') sys.exit(1) if config.serverAddr is None: config = config._replace(serverAddr='0.0.0.0') if config.serverPort is None: config = config._replace(serverPort=8388) if config.password is None and not args.random: parser.print_usage() print('need PASSWORD, please use [-k PASSWORD] or ' 'use [--random] to generate a random password') sys.exit(1) if args.random: print('generate random password') config = config._replace(password=randomPassword()) if args.save: print(f'dump config file into {args.save!r}') with open(args.save, 'w', encoding='utf-8') as f: lsConfig.dump(f, config) run_server(config)