Example #1
0
def client_auth(sock, addr):
    """Authenticate the client."""
    ba_req, err = mysock.recv(sock, BUF_LEN)
    if err != None:
        LOG.warning("can't recv auth req %s:%s" % addr)
        return None, AUTH_RES_UNKNOWN_ERR
    
    auth_req = packet.AuthReq(ba_req)
    if not auth_req.cek_valid():
        LOG.fatal("Bad AuthReq packet")
        return None, AUTH_RES_PKT_ERR
    
    auth_rsp = packet.AuthRsp()
    auth_res = AUTH_RES_OK
    
    user, password = auth_req.get_userpassword()
    if client_auth_rpc(user, password) != True:
        LOG.debug("auth rpc failed for user %s at %s" % (user, addr))
        auth_rsp.build(packet.AUTH_RSP_BAD_USERPASS)
        auth_res = AUTH_RES_UNKNOWN_ERR
    else:
        auth_rsp.build(packet.AUTH_RSP_OK)
        
    written, err = mysock.send_all(sock, auth_rsp.payload)
    if err != None or written < len(auth_rsp.payload):
        LOG.error("send auth reply failed.%s:%s" % addr)
        return user, AUTH_RES_UNKNOWN_ERR
    
    return user, auth_res
Example #2
0
def _send_rsp_pkt_to_server(rsp_pkt, server_sock):
    '''Send response packet to server.'''
    written, err = mysock.send_all(server_sock, rsp_pkt.payload)
    if err != None:
        print "error sending packet to server"
        sys.exit(-1)
    
    if written != len(rsp_pkt.payload):
        print "partial write to server"
        sys.exit(-1)
Example #3
0
 def ping_rsp_send(self):
     '''Send PING-RSP to client.'''
     if self.wait_ping_rsp == False:
         return True
     
     p_rsp = packet.PingRsp()
     
     written, err = mysock.send_all(self.sock, p_rsp.payload)
     
     if err != None or (len(p_rsp.payload) != written):
         LOG.error("error sending PING-RSP to %s" % self.user)
         return False
     
     self.wait_ping_rsp = False
Example #4
0
 def send_ctrl_pkt(self):
     '''send ctrl packet to client.'''
     if len(self.ctrl_pkt) == 0:
         return True
     
     LOG.debug("send ctrl pkt to client:%s" % self.user)
     
     pkt = self.ctrl_pkt.pop(0)
     written, err = mysock.send_all(self.sock, pkt.payload)
     if written != len(pkt.payload) or err != None:
         LOG.error("failed to send ctrl_pkt to:%s.type = %d" %
                   self.user, pkt.payload[1])
         self.dead = True
         return False
     return True
Example #5
0
 def req_pkt_fwd(self):
     '''Forward request packet to client.'''
     if len(self.req_pkt) == 0:
         return
     
     req = self.req_pkt.pop(0)
     
     req_pkt = packet.DataReq()
     req_pkt.build(req.payload, req.ses_id)
     
     written, err = mysock.send_all(self.sock, req_pkt.payload)
     
     if written != len(req_pkt.payload) or err != None:
         LOG.error("failed to send req pkt to client:%s" % self.user)
         self.dead = True
         return False
Example #6
0
 def send_to_server_pkt(self):
     '''Send a packet in to_server queue'''
     if len(self.to_server_pkt) == 0:
         return True
     
     pkt = self.to_server_pkt.pop(0)
     
     written, err = mysock.send_all(self.server_sock, pkt.payload)
     if (err != None) or (written != len(pkt.payload)):
         LOG.error("can't send pkt to server")
         return False
     
     if pkt.payload[0] == packet.TYPE_PING_REQ:
         self.last_ping = time.time()
         self.wait_ping_rsp = True
         
     return True
Example #7
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
Example #8
0
def handle_peer(sock, addr):
    '''Peer connection handler.'''
    #LOG.debug("new_peer.addr = %s %s" % addr)
    
    #get request
    ba_req, err = mysock.recv(sock, BUF_LEN)
    if err != None or len(ba_req) == 0:
        return
    
    #get client name
    client_name = get_client_name(ba_req, BASE_DOMAIN)
    
    if client_name is None:
        LOG.warning("client name not found.aaddr = %s:%s" % addr)
        sock.close()
        return
    
    #check client status
    client_status = authstat.client_status(client_name) 
    if client_status != authstat.RH_STATUS_OK:
        LOG.info("Access denied. Client status for %s is %d." % (client_name, client_status))
        mysock.send_all(sock, authstat.client_status_msg(client_status, client_name))
        return
    
    #get pointer to client mq
    client_mq = get_client_mq(client_name)
    
    if client_mq == None:
        handle_client_offline(client_name, sock)
        return
    
    #register peer to client
    peer = Peer(sock, addr, client_name, client_mq)
    peer.ses_id = register_peer(peer)
    
    if peer.ses_id == None:
        LOG.error("can't add peer. MAX_CONN REACHED %s" % client_name)
        peer.close()
        return
    
    #forward request packet to client
    forward_reqpkt_to_client(client_mq, peer.ses_id, ba_req)
    peer.trf_req += len(ba_req)
    
    while True:
        #fetch response packet
        try:
            for _ in xrange(0, Peer.RSP_FORWARD_NUM):
                msg = peer.in_mq.get_nowait()
                rsp = msg['pkt']
                peer.rsp_list.append(rsp)
        except gevent.queue.Empty:
            pass
        
        #select() sock utk write jika ada RSP-pkt
        wlist = []
        if len(peer.rsp_list) > 0:
            wlist.append(sock)
            
        rsocks, wsocks, _ = select.select([sock], wlist , [], 0.1)
        
        #rsocks can be read
        if len(rsocks) > 0:
            ba_req, err = mysock.recv(sock, BUF_LEN)
            if len(ba_req) == 0:
                peer.ended = True
                break    
            elif len(ba_req) > 0:
                forward_reqpkt_to_client(client_mq, peer.ses_id, ba_req)
                peer.trf_req += len(ba_req)
        
        if len(wsocks) > 0:
            is_ok = peer.forward_rsp_pkt()
        
        if peer.ended == True:
            break
        
        gevent.sleep(0)
    
    peer.close()
    
    #send usage to server
    authstat.report_usage(client_name, peer.trf_req, peer.trf_rsp)
Example #9
0
def handle_local_down(client_name, sock):
    LOG.info("Send 'local down' message to peer. Client = %s " % client_name)
    str_err = local_down_str(client_name)
    mysock.send_all(sock, str_err)
Example #10
0
def handle_client_offline(client_name, sock):
    '''User not found handler.'''
    LOG.info("Send 'client not online' message to peer. Client = %s " % client_name)
    str_err = user_offline_str(client_name)
    mysock.send_all(sock, str_err)