my_boyfriend.py
def manage_connections(listen_sock, processing_q, connection_id):
    """
    Handle logic required to set up and maintain connections. The ability to hack this
    is the main selling point of alsanna - for instance if you need to send a plaintext
    message from the server to the client prior to TLS negotiation, you can do it here.

    So that we can have all the protocol logic in one place, we handle both directions
    of traffic here. A more specialized proxy might benefit in modularity from having
    a child process for each direction and using blocking sockets for each.

    The forward() method handles setting up the socket we use to forward data to the
    server. This is ugly as heck, but handles the common case where the server kills
    your connection if you take too long editing something.
    """

    ###########################################################
    # Initialize queues and set up TLS on listener if need be #
    ###########################################################

    q_manager = multiprocessing.Manager()
    c_result_q = q_manager.Queue()
    processing_q.put((str(connection_id) + "c", c_result_q))
    s_result_q = q_manager.Queue()
    processing_q.put((str(connection_id) + "s", s_result_q))

    with listen_sock:
        forward_sock = None

        ############################################################################
        # Shuffle bytes back and forth, setting up forwarder on first sent message #
        ############################################################################
        client_done, server_done = False, False
        try:
            # We check forward_sock to handle the situation where we never update
            # server_done because we errored out trying to send from client to server
            while not client_done and (not server_done or forward_sock is None):
                # Die if orphaned
                if os.getppid() == 1:
                    return
                # Send try to forward data in each direction
                if not client_done:
                    try:
                        client_done = forward(listen_sock, forward_sock,
                                              processing_q, c_result_q,
                                              str(connection_id) + "c")
                        if  isinstance(client_done, socket.socket):
                            forward_sock = client_done
                            client_done = False
                    except:
                        processing_q.put(("Err", "Error forwarding data to server.\n"
                                          + traceback.format_exc()))
                        break
                if not server_done and forward_sock is not None:
                    try:
                        server_done = forward(forward_sock, listen_sock,
                                              processing_q, s_result_q,
                                              str(connection_id) + "s")
                    except:
                        processing_q.put(("Err", "Error forwarding data to client.\n"
                                          + traceback.format_exc()))
                        break
        finally:  # No context manager because it's None sometimes; manual cleanup
            if forward_sock is not None:
                forward_sock.close()

    Handle logic required to set up and maintain connections. The ability to hack this
    is the main selling point of alsanna - for instance if you need to send a plaintext
    message from the server to the client prior to TLS negotiation, you can do it here.

    So that we can have all the protocol logic in one place, we handle both directions
    of traffic here. A more specialized proxy might benefit in modularity from having
    a child process for each direction and using blocking sockets for each.

    The forward() method handles setting up the socket we use to forward data to the
    server. This is ugly as heck, but handles the common case where the server kills
    your connection if you take too long editing something.
    
Handle logic required to set up and maintain connections. The ability to hack this
is the main selling point of alsanna - for instance if you need to send a plaintext
message from the server to the client prior to TLS negotiation, you can do it here.

So that we can have all the protocol logic in one place, we handle both directions
of traffic here. A more specialized proxy might benefit in modularity from having
a child process for each direction and using blocking sockets for each.

The forward() method handles setting up the socket we use to forward data to the
server. This is ugly as heck, but handles the common case where the server kills
your connection if you take too long editing something.
Handle logic required to set up and maintain connections . The ability to hack this is the main selling point of alsanna - for instance if you need to send a plaintext message from the server to the client prior to TLS negotiation you can do it here . So that we can have all the protocol logic in one place we handle both directions of traffic here . A more specialized proxy might benefit in modularity from having a child process for each direction and using blocking sockets for each . The forward () method handles setting up the socket we use to forward data to the server . This is ugly as heck but handles the common case where the server kills your connection if you take too long editing something .
def manage_connections(listen_sock, processing_q, connection_id):
    q_manager = multiprocessing.Manager()
    c_result_q = q_manager.Queue()
    processing_q.put((str(connection_id) + "c", c_result_q))
    s_result_q = q_manager.Queue()
    processing_q.put((str(connection_id) + "s", s_result_q))
    with listen_sock:
        forward_sock = None
        #################
        #################
        client_done, server_done = False, False
        try:
            while not client_done and (not server_done or forward_sock is None):
                if os.getppid() == 1:
                    return
                if not client_done:
                    try:
                        client_done = forward(listen_sock, forward_sock,
                                              processing_q, c_result_q,
                                              str(connection_id) + "c")
                        if  isinstance(client_done, socket.socket):
                            forward_sock = client_done
                            client_done = False
                    except:
                        processing_q.put(("Err", "Error forwarding data to server.\n"
                                          + traceback.format_exc()))
                        break
                if not server_done and forward_sock is not None:
                    try:
                        server_done = forward(forward_sock, listen_sock,
                                              processing_q, s_result_q,
                                              str(connection_id) + "s")
                    except:
                        processing_q.put(("Err", "Error forwarding data to client.\n"
                                          + traceback.format_exc()))
                        break
        finally:  
            if forward_sock is not None:
                forward_sock.close()
def manage_connections ( listen_sock , processing_q , connection_id ) : q_manager = multiprocessing . Manager ( ) c_result_q = q_manager . Queue ( ) processing_q . put ( ( str ( connection_id ) + "c" , c_result_q ) ) s_result_q = q_manager . Queue ( ) processing_q . put ( ( str ( connection_id ) + "s" , s_result_q ) ) with listen_sock : forward_sock = None client_done , server_done = False , False try : while not client_done and ( not server_done or forward_sock is None ) : if os . getppid ( ) == 1 : return if not client_done : try : client_done = forward ( listen_sock , forward_sock , processing_q , c_result_q , str ( connection_id ) + "c" ) if isinstance ( client_done , socket . socket ) : forward_sock = client_done client_done = False except : processing_q . put ( ( "Err" , "Error forwarding data to server.\n" + traceback . format_exc ( ) ) ) break if not server_done and forward_sock is not None : try : server_done = forward ( forward_sock , listen_sock , processing_q , s_result_q , str ( connection_id ) + "s" ) except : processing_q . put ( ( "Err" , "Error forwarding data to client.\n" + traceback . format_exc ( ) ) ) break finally : if forward_sock is not None : forward_sock . close ( )
Handle logic required to set up and maintain connections.
Handle logic required to set up and maintain connections .
"""
    Handle logic required to set up and maintain connections. The ability to hack this
    is the main selling point of alsanna - for instance if you need to send a plaintext
    message from the server to the client prior to TLS negotiation, you can do it here.

    So that we can have all the protocol logic in one place, we handle both directions
    of traffic here. A more specialized proxy might benefit in modularity from having
    a child process for each direction and using blocking sockets for each.

    The forward() method handles setting up the socket we use to forward data to the
    server. This is ugly as heck, but handles the common case where the server kills
    your connection if you take too long editing something.
    """ ########################################################### # Initialize queues and set up TLS on listener if need be # ########################################################### ############################################################################ # Shuffle bytes back and forth, setting up forwarder on first sent message # ############################################################################ # We check forward_sock to handle the situation where we never update # server_done because we errored out trying to send from client to server # Die if orphaned # Send try to forward data in each direction # No context manager because it's None sometimes; manual cleanup
listen_sock
processing_q
connection_id