def sendOnSSL(self,item,trafic_type,sendtoip):
     # sends the packet on the ssl tunnel with wraping the msg in trafic type header
     # item: pickle/flat version of data to sent
     # trafic_type: UDP or TCP
     # sendto: ip to send
     # isClient: True if Client is sending a packet else false. it implies if client is sending a packet he can open a new session if needed but server cannot a open session so skip the send msg
     # The method checks the avalibility of session, if active sends the packet, else create a new session and send the packet
     if not(self.isSession(sendtoip)):#Create a new session for a request
         if self.isClient:
             status=self.createSession(sendtoip)
             if status==False:
                 return
         else:
             print 'I am server cannot open session for client'
             return    
     target_Session=self.getSession(sendtoip)
     sock=target_Session.wrapSock
     pkey = item.pkey
     item.pkey = None
     if trafic_type=='UDP':
         data_pickle = b64encode(pickle.dumps( item.data, pickle.HIGHEST_PROTOCOL))
         
         sslPacket=(trafic_type,crypto.encrypt(pkey, data_pickle))
         data = pickle.dumps( sslPacket, pickle.HIGHEST_PROTOCOL )
     else:
         
         data_pickle = b64encode(pickle.dumps( item, pickle.HIGHEST_PROTOCOL ))
         try:
             sslPacket = (trafic_type,crypto.encrypt( pkey, data_pickle ))
         except:
             print 'SendonSSL: Encryption error'
             return
         data=pickle.dumps( sslPacket, pickle.HIGHEST_PROTOCOL )
     try:
         SizeStruct = struct.Struct( "!I" )
         sock.sendall(struct.pack("!I",len(data)))
         start=0
         end=5000# Sending 5000 bytes
         size=len(data)
         while True:
             if end < size:
                 sock.sendall(data[start:end])
                 start=end
                 end=end+5000
             else:
                 sock.sendall(data[start:])
                 break
     except socket.error, msg:
             print msg
             if self.isSession(id):
                 self.delSession(id)
             return
Exemplo n.º 2
0
    def do_work( self, item ):
        if self.sslClientOnly==True:
            if item.type.startswith( "S_P2P" ):
                trafic_type='UDP'
                sendtoip=item.addr[0]
            else:
                trafic_type='TCP'
                sendtoip=item.to_ip[0]
            self.sslComModule.sendOnSSL(item, trafic_type, sendtoip) 
            
        elif item.type.startswith( "S_P2P" ):
            #@Ali
            if self.isSSL==1:
                if int(item.addr[2])==self.sslComModule.hostSSLport:
                    trafic_type='UDP'
                    sendtoip=item.addr[0]
                    self.sslComModule.sendOnSSL(item, trafic_type, sendtoip)
                    return
            send_to=( item.addr[0], int(item.addr[2]))
            if ipMap.has_key(item.addr) or item.addr[0]==myPubaddr[0]: #@Ali same network, chk ipmap for mapping or requeue the msg and wait for relay response
                if ipMap.has_key(item.addr):
                    send_to=( ipMap[item.addr], int(item.addr[2]))#@Ali send on local/private address
                else:#@Ali Requeue the item
                    self.main_mng.add(item)
                    return
            try:
                key = item.pkey
            except:
                print "unable to obtain public key for p2p traffic"    
            #print item.data
            #print item.addr[0]
            data = pickle.dumps( item.data, pickle.HIGHEST_PROTOCOL )
            #print key
            data = crypto.encrypt(key, data) #p2p message encryption
            try:
                self.udpSSock.sendto( data, send_to ) #@Ali Transmit the data on random port
            except:
                self.main_mng.add(item)
                print 'udp socket error'
                print traceback.print_exc()
                
            #
        else:
            #@Ali
            if self.isSSL==1:
                if int(item.to_ip[1])==self.sslComModule.hostSSLport:
                    trafic_type='TCP'
                    sendtoip=item.to_ip[0]
                    self.sslComModule.sendOnSSL(item, trafic_type, sendtoip)
                    return
            send_to=(item.to_ip[0],int(item.to_ip[1]))
            if ipMap.has_key(item.to_ip) or item.to_ip[0]==myPubaddr[0]: #@Ali same network, chk ipmap for mapping or requeue the msg and wait for relay response
                if ipMap.has_key(item.to_ip):
                    send_to=( ipMap[item.to_ip], int(item.to_ip[1]))#@Ali send on local/private address
                else:#@Ali Requeue the item
                    self.main_mng.add(item)
                    return
            #
            pkey = item.pkey
            item.pkey = None

            logging.info( "Communication manager is sending {0.type} to {1}".format( item, send_to ) )
            try:
                sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
                sock.connect( send_to )
                #item.myIP=(myPubaddr[0],myPubaddr[1],myPubaddr[2])

                SizeStruct = struct.Struct( "!I" )
                data = pickle.dumps( item, pickle.HIGHEST_PROTOCOL )
                en_data = crypto.encrypt( pkey, data )
                sock.sendall(struct.pack("!I",len(en_data)))
                start=0
                end=5000# Sending 1 MB
                size=len(en_data)
                while True:
                    if end < size:
                        sock.sendall(en_data[start:end])
                        start=end
                        end=end+5000
                    else:
                        sock.sendall(en_data[start:])
                        break
                sock.close()
            except socket.error as er:
                logging.info( "Socket error: {0}".format( er ) )
                print 'Socket Error'
                print item.type
                print send_to
                print item.to_ip
#                print ipMap
                print traceback.print_exc()
#                print 'Item added to the Queue for re-attempt'
#                item.pkey=pkey
#                self.main_mng.add(item)
                sock.close()
                return
            except:
#                item.pkey=pkey
#                self.main_mng.add(item)
                print traceback.print_exc()
#                print 'Item added to the Queue for re-attempt'
                sock.close()
                return