Beispiel #1
0
def forward_incoming_req_pkt(ba_req, ba_len, host_host, host_port):
    '''Forward incoming req packet to host.'''
    req = packet.DataReq(ba_req)
    if req.cek_valid() == False:
        LOG.fatal("Bad DATA-REQ packet")
        return
    
    ses_id = req.get_sesid()
    req_data = req.get_data()
    #req_data = rewrite_req(req_data, host_host, host_port)
    LOG.debug("ses_id=%d" % ses_id)
    
    try:
        h_conn = HOST_CONNS_DICT[ses_id]
        LOG.debug("use old connection.ses_id = %d" % h_conn.ses_id)
    except KeyError:
        h_conn = HostConn(ses_id)
        h_conn.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        _, err = mysock.connect(h_conn.sock, (host_host, host_port))
        if err != None:
            LOG.fatal("Connection to %s port %d failed." % (host_host, host_port))
            LOG.fatal("Please check your local web server")
            return LOCAL_STATUS_DOWN, ses_id
        HOST_CONNS_DICT[ses_id] = h_conn
    
    h_sock = h_conn.sock
    written, err = mysock.send_all(h_sock, req_data)
    if err != None:
        print "error forward"
        del HOST_CONNS_DICT[ses_id]
        sys.exit(-1)
    
    if written != len(req_data):
        print "PARTIAL FORWARD to host"
        print "FATAL UNHANDLED COND"
        sys.exit(-1)
    
    return LOCAL_STATUS_OK, ses_id
Beispiel #2
0
def client_loop(server, port, user, passwd, host_host, host_port):
    """Main client loop."""
    #connect to server
    server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    mysock.setkeepalives(server_sock)
    ret, err = mysock.connect(server_sock, (server, port))
    
    if err != None:
        LOG.error("can't connect to server")
        return
    
    if do_auth(user, passwd, server_sock) == False:
        sys.exit(1)
        
    LOG.info("Authentication successfull")
    
    client = Client(server_sock)
    
    #start looping
    while True:
        #cek last_ping
        client.cek_ping_req()
        
        #server_sock select()
        wsock = []
        if client.any_pkt_to_server() or any_host_response():
            wsock.append(server_sock)
            
        to_read, to_write, _ = select.select([server_sock],
            wsock, [], 1)
        
        if len(to_read) > 0:
            #read sock
            ba_pkt, err = packet.get_all_data_pkt(server_sock)
            if ba_pkt is None or err != None:
                LOG.error("Connection to server closed")
                break
            
            #request packet
            if ba_pkt[0] == packet.TYPE_DATA_REQ:
                local_status, ses_id = forward_incoming_req_pkt(ba_pkt, len(ba_pkt), host_host, host_port)
                if local_status == LOCAL_STATUS_DOWN:
                    client.send_ctrl_local_down(ses_id)
            
            #ping rsp
            elif ba_pkt[0] == packet.TYPE_PING_RSP:
                #print "PING-RSP ", datetime.datetime.now()
                client.handle_ping_rsp(ba_pkt)
            
            #ctrl packet
            elif ba_pkt[0] == packet.TYPE_CTRL:
                LOG.debug("CTRL Packet. subtype = %d" % ba_pkt[1])
                client.handle_ctrl_pkt(ba_pkt)
            else:
                LOG.error("Unknown packet.type = %d" % ba_pkt[0])
                LOG.fatal("exiting...")
                sys.exit(-1)
        
        if len(to_write) > 0:
            forward_host_rsp(server_sock)
            if client.send_to_server_pkt() == False:
                break
           
        clean_host_conn()
        
        #select() untuk host sock
        rlist = []
        for h_conn in HOST_CONNS_DICT.itervalues():
            if h_conn.sock != None and h_conn.ended == False:
                rlist.append(h_conn.sock)
        
        if len(rlist) > 0:      
            h_read, _, _ = select.select(rlist, [], [], 0.5)
        
            if len(h_read) > 0:
                for sock in h_read:
                    accept_host_rsp(sock)
        
        #cek ping rsp
        if client.cek_ping_rsp() == False:
            LOG.error("PING-RSP timeout")
            break
                
    LOG.error("Client disconnected")