예제 #1
0
 def send(self, msg, log = False):
     """ Send a message using socket s, and log it. """
     if len(msg) <= 0: return
     msgs = [i for i in msg.split() if len(i) > 0]
     if len(msgs) > 0 and msgs[0] == 'ShowCommands':
         self.send('ShowCommands')
         for extra in EXTRA:
             print (Fore.YELLOW if hasColor else "")+EXTRA[extra]+(Style.RESET_ALL if hasColor else "")
     elif len(msgs) > 0 and msgs[0] == 'Command':
         if msgs[1] in EXTRA:
             print (Fore.YELLOW if hasColor else "")+EXTRA[msgs[1]]+(Style.RESET_ALL if hasColor else "")
         else:
             self.send('Command '+msgs[1])
     elif len(msgs) > 0 and msgs[0] == 'Find':
         self.send('Search '+self.ip+' '+str(self.udpport)+' F?T?0?1?'+'$'.join(msgs[1:]))
     elif len(msgs) > 0 and msgs[0] == 'DownloadById':
         num = int(msgs[1])
         self.searchMutex.acquire()
         cc = ClientConnection(self.username, self.ip, -1, True, dict(self.searchResults[num]))
         self.searchMutex.release()
         cc.start()
         self.searchMutex.acquire()
         self.s.send(FUNCTIONS['ConnectToMe'](*((self.searchResults[num]['nick'], self.ip, cc.port))))
         self.searchMutex.release()
         cc.join() 
     elif len(msgs) > 0 and msgs[0] == 'ConnectToMe':
         cc = ClientConnection(self.username, msgs[2], -1, True)
         cc.start()
         self.s.send(FUNCTIONS[msgs[0]](*((msgs[1], msgs[2], cc.port))))
         cc.join()
     elif len(msgs) > 0 and msgs[0] in FUNCTIONS:
         resp = FUNCTIONS[msgs[0]](*(tuple(msgs[1:])))
         if resp: self.s.send(resp)
         # if a search, clear search results, they're going to be repopulated !!
         if 'Search' in msgs[0]:
             self.search = msgs[3].strip()
             self.searchMutex.acquire()
             self.searchResults = dict()
             self.searchMutex.release()
     elif len(msgs) > 0 and msgs[0] == 'Show':
         self.show()
     elif len(msgs) > 0 and msgs[0] == 'ShowSearchResults':
         self.showSearchResults()
     elif len(msgs) > 0 and msgs[0] == 'ClientConnection':
         # This is the part where the main loop halts and we enter a client connection loop (passive)
         cc = ClientConnection(self.username, msgs[1], int(msgs[2]))
         cc.start()
         cc.join()
     else:
         self.s.send('$'+msg+'|')
     if log:
         print (SENT if not hasColor else Fore.GREEN) + msg + ("" if not hasColor else Style.RESET_ALL)
예제 #2
0
 def addConnection(self, sock, ip_addr):
     fileno = sock.fileno()
     conn = ClientConnection(self, sock, ip_addr)
     if self.connections.has_key(sock.fileno()):
         self.connections[fileno].sock = sock
     else:
         self.connections[fileno] = conn
     self.epoll.modify(fileno, select.EPOLLOUT)
예제 #3
0
 def __init__(self):
     super(AgentClient, self).__init__()
     self.config = AgentClientConfig()
     self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     self.ip_addr = self.config.server_ip
     self.port = self.config.server_port
     self.receiver = MessageReceiver(self.sock)
     self.handler = ScanMessageHandler()
     self.connection = ClientConnection(self, self.sock, self.ip_addr)
     self.checker = HeartBeatChecker(self.connection,
                                     self.config.heartbeat_interval)
     self.epoll = select.epoll()
     self.dispatch_queue = Queue.Queue()
     self.dispatcher = Dispatcher(self.dispatch_queue)
예제 #4
0
 def start(self):
     """
     This method is called to start a client or server service. This method allow to initialize and configure a
     client or server service and create a socket connection between several client to a server.
     """
     if self.configuration['role'] == "client":
         print("")
         self.client = ClientConnection.ClientConnection(self.configuration)
         self.client.connection()
         self.client.client()
     elif self.configuration['role'] == "server":
         print("")
         self.server = ServerConnection.ServerConnection(self.configuration)
         self.server.connection()
         self.server.server()
예제 #5
0
    def ConnectToServer(self):
        serverList = Configuration.GetServerList()

        print("Connecting with %i different servers" % len(serverList))

        for entry in serverList:
            if entry == "":
                continue
            # TODO: this does not seem like a healthy way to split.
            entry = entry.split(sep=", ")
            address = str(entry[0][2:len(entry[0]) - 1])
            root = str(entry[1][1:len(entry[1]) - 3])
            connection = ClientConnection(self, address, root)
            connection.start()
            self.connections.append(connection)
예제 #6
0
    def login(self):
        attempts = 0
        while True:
            if attempts == 3:
                print("Too many failed attempts.")
                break
            username = input("Username: "******"Password: "******"Welcome " + self.user.getFirstname())
                    conn = ClientConnection(('127.0.0.1', 64010))
                    with conn as s:
                        #conn.__enter__() executes: connection open
                        s.send(username)

                    break
                else:
                    attempts += 1
                    print("Login Invalid!")
            else:
                print("Login Invalid!")