Need help creating a TCP relay between two sockets

I have the following situation:

SomeServer(S) <-> (C)MyApp(S) <-> (C)User

(S) represents a server socket
(C) represents a client socket

Essentially, MyApp initiates a connection with SomeServer ( SomeServer (S) ↔ (C) MyApp ) and after some authentication procedures, MyApp (S) successfully starts to wait ( C) for the user to connect. As soon as the User connects, MyApp transfers the data from SomeServer to the User . This happens in both directions.

My SomeServer (S) ↔ (C) MyApp works fine, but I can not get MyApp (S) ↔ (C) User . I get to the User connecting to MyApp (S) , but can not get relayed data!

Ok, I hope some of them are clear;) Now let me show my code for MyApp . The BTW implementation of SomeServer and the User is not suitable for solving my question, since none of them can be changed.

I commented on my code indicating where I am having problems. Oh, I should also mention that I have no problem breaking the entire “Server Section” for some other code, if necessary. This is POC, so the focus is on working with functionality, rather than writing efficient code. Thank you for your time.

''' MyApp.py module '''

import asyncore, socket
import SSL

# Client Section
# Connects to SomeServer

class MyAppClient(asyncore.dispatcher):

    def __init__(self, host, port):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect((host, port))

    connectionPhase = 1
    def handle_read(self):
        print "connectionPhase =", self.connectionPhase

    # The following IF statements may not make sense
    # as I have removed code irrelevant to this question

    if self.connectionPhase < 3: # authentication phase
            data = self.recv(1024)
            print 'Received:', data

            # Client/Server authentication is handled here
            # Everything from this point on happens over
            # an encrypted socket using SSL

            # Start the RelayServer listening on localhost 8080
            # self.socket is encrypted and is the socket communicating
            # with SomeServer

            rs = RelayServer(('localhost', 8080), self.socket)
            print 'RelayServer started'

        # connectionPhase = 3 when this IF loop is done

        elif self.connectionPhase == 3: # receiving data for User
            data = self.recv(1024)

            print 'Received data - forward to User:', data

            # Forward this data to User
            # Don't understand why data is being read here
            # when the RelayServer was instantiated above

# Server Section
# Connects to User

class RelayConnection(asyncore.dispatcher):
    def __init__(self, client, sock):
        asyncore.dispatcher.__init__(self)
        self.client = client
        print "connecting to %s..." % str(sock)

    def handle_connect(self):
        print "connected."
        # Allow reading once the connection
        # on the other side is open.
        self.client.is_readable = True


    # For some reason this never runs, i.e. data from SomeServer
    # isn't read here, but instead in MyAppClient.handle_read()
    # don't know how to make it arrive here instead as it should
    # be relayed to User

    def handle_read(self):
        self.client.send(self.recv(1024))

class RelayClient(asyncore.dispatcher):
    def __init__(self, server, client, sock):
        asyncore.dispatcher.__init__(self, client)
        self.is_readable = False
        self.server = server
        self.relay = RelayConnection(self, sock)

    def handle_read(self):
        self.relay.send(self.recv(1024))

    def handle_close(self):
        print "Closing relay..."
        # If the client disconnects, close the
        # relay connection as well.
        self.relay.close()
        self.close()

    def readable(self):
        return self.is_readable

class RelayServer(asyncore.dispatcher):
    def __init__(self, bind_address, MyAppClient_sock):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.bind(bind_address)
        self.MyAppClient_sock = MyAppClient_sock
        print self.MyAppClient_sock
        self.listen(1)

    def handle_accept(self):
        conn, addr = self.accept()
        RelayClient(self, conn, self.MyAppClient_sock)

if __name__ == "__main__":
    # Connect to host
    # First connection stage
    connectionPhase = 1
    c = MyAppClient('host', port) # SomeServer host and port

    asyncore.loop()

EDIT:

@samplebias I replaced my complete module with your code (not shown), and I re-added all the bits and pieces that I need for authentication, etc.

At this point, I get the same result as my own code above. I mean that MyApp (or the Server in your code) is connected to SomeServer and transfers data back and forth. So far so good. When a user (or client application) connects to localhost 8080, this code runs:

if not self.listener:
    self.listener = Listener(self.listener_addr, self)

BUT this is not fulfilled

  # if user is attached, send data
    elif self.user:
        print 'self.user'
        self.user.send(data)

, . User, , , init - . handle_read() .

?

+3
1

, , . , handle_read() MyAppClient self.socket RelayServer. MyAppClient, RelayConnection, .

, , , , , . , IMAP-, , ( , close() ..).

  • "someserver". .
  • 8080 1 , . , .
  • . , .

:

import asyncore
import socket

class User(asyncore.dispatcher_with_send):

    def __init__(self, sock, server):
        asyncore.dispatcher_with_send.__init__(self, sock)
        self.server = server

    def handle_read(self):
        data = self.recv(4096)
        # parse User auth protocol here, authenticate, set phase flag, etc.
        # if authenticated, send data to server
        if self.server:
            self.server.send(data)

    def handle_close(self):
        if self.server:
            self.server.close()
        self.close()

class Listener(asyncore.dispatcher_with_send):

    def __init__(self, listener_addr, server):
        asyncore.dispatcher_with_send.__init__(self)
        self.server = server
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.bind(listener_addr)
        self.listen(1)

    def handle_accept(self):
        conn, addr = self.accept()
        # this listener only accepts 1 client. while it is serving 1 client
        # it will reject all other clients.
        if not self.server.user:
            self.server.user = User(conn, self.server)
        else:
            conn.close()

class Server(asyncore.dispatcher_with_send):

    def __init__(self, server_addr, listener_addr):
        asyncore.dispatcher_with_send.__init__(self)
        self.server_addr = server_addr
        self.listener_addr = listener_addr
        self.listener = None
        self.user = None

    def start(self):
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect(self.server_addr)

    def handle_error(self, *n):
        self.close()

    def handle_read(self):
        data = self.recv(4096)
        # parse SomeServer auth protocol here, set phase flag, etc.
        if not self.listener:
            self.listener = Listener(self.listener_addr, self)
        # if user is attached, send data
        elif self.user:
            self.user.send(data)

    def handle_close(self):
        if self.user:
            self.user.server = None
            self.user.close()
            self.user = None
        if self.listener:
            self.listener.close()
            self.listener = None
        self.close()
        self.start()

if __name__ == '__main__':
    app = Server(('someserver', 143), ('localhost', 8080))
    app.start()
    asyncore.loop()
+3

All Articles