Beispiel #1
0
def run_server(config: lsConfig.Config):
    loop = asyncio.get_event_loop()

    listenAddr = net.Address(config.localAddr, config.localPort)
    remoteAddr = net.Address(config.serverAddr, config.serverPort)
    server = LsLocal(loop=loop,
                     password=config.password,
                     listenAddr=listenAddr,
                     remoteAddr=remoteAddr)

    def didListen(address):
        print('Listen to %s:%d\n' % address)

    asyncio.ensure_future(server.listen(didListen))
    loop.run_forever()
Beispiel #2
0
    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)
Beispiel #3
0
class TestLsLocal(unittest.TestCase):
    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 tearDown(self):
        self.remoteServer.close()
        self.loop.close()

    def test_run(self):
        def didListen(address):

            self.assertEqual(address[0], self.listenAddr.ip)
            self.assertEqual(address[1], self.listenAddr.port)

            user_client = socket.create_connection(self.listenAddr)
            user_client.send(b'hello world')
            user_client.close()

            async def call_later():
                conn, _ = await self.loop.sock_accept(self.remoteServer)
                received_msg = await self.loop.sock_recv(conn, 1024)
                conn.close()

                self.assertEqual(received_msg, self.encrypted_msg)

                await asyncio.sleep(0.1)
                self.loop.stop()

            asyncio.ensure_future(call_later(), loop=self.loop)

        with self.assertRaises(RuntimeError):
            self.loop.run_until_complete(self.local.listen(didListen))