示例#1
0
class Server:
    def __init__(self, cip, cport, sip, sport, lbip, lbport):
        # create an INET, STREAMing socket
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sip = sip
        self.sport = sport
        self.cip = cip
        self.cport = cport
        self.lbip = lbip
        self.lbport = lbport
        self.store = KeyStore((self.lbip, self.lbport), (self.cip, self.cport),
                              (self.sip, sport))  # KeyStore

    def start(self):
        threading.Thread(target=self.store.start).start()
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.socket.bind((self.cip, self.cport))
        self.socket.listen(constants.LEADER_QUEUE_SIZE)
        while 1:
            (clientsocket, address) = self.socket.accept()

            # parts = request_parser.ProtoParser.parse(msg)
            # t = threading.Thread(target=self.__process,
            #                     args=(clientsocket, parts))

            t = threading.Thread(target=self.__process_block,
                                 args=(clientsocket, ))
            t.start()

    def __process_block(self, clientsocket):
        msg = clientsocket.recv(constants.BUFFER_SIZE)
        print msg
        if msg[:3] == "set":
            while msg.count("\r\n") < 2:
                msg = msg + clientsocket.recv(constants.BUFFER_SIZE)
            parts = msg.split("\r\n")
            key = parts[0].split(" ")[1]
            val = parts[1]
            print msg, key, val
            self.set(key, val)
            clientsocket.send("STORED\r\n")
        elif msg[:3] == "get":
            while (msg.find("\r\n") == -1):
                msg = msg + clientsocket.recv(constants.BUFFER_SIZE)
            res = ""
            parts = msg.strip().split(" ")
            for part in parts[1:]:
                value = self.get(part)
                if value is not None:
                    res = res + "VALUE " + part + " 0 " + str(
                        len(value)) + "\r\n" + str(value) + "\r\n"
            print "get res ", res
            res += "END\r\n"
            clientsocket.send(res)
        clientsocket.close()

    def stop(self):
        pass

    # Calls Keystores API
    def get(self, key):
        return self.store.get(key)

    # Same
    def set(self, key, value):
        self.store.set(key, value)
示例#2
0
文件: framework.py 项目: Pynon/apt2
 def displayKbMenu(self):
     searchString = ""
     depth = 0
     searches = {0: ""}
     self.display.output()
     self.display.output("---------------------------------------")
     self.display.output("Browse Knowledgebase")
     results = {}
     while True:
         self.display.output("[ " + searchString + " ]")
         if (searchString != ""):
             results = kb.get(searchString)
             i = 0
             for option in results:
                 self.display.output(str(i) + ". " + option)
                 i += 1
         else:
             self.display.output()
             self.display.output("0. host")
             self.display.output("1. service")
             self.display.output("2. domain")
             results = ["host", "service", "domain"]
             i = 3  # Keep selection filter from breaking
         self.display.output()
         self.display.output(
             "Choose From Above Or: (a)dd, (d)elete, (b)ack, (m)ain menu, (i)mport, write to (t)emp file"
         )
         self.display.output()
         search = self.display.input(
             "Select option or enter custom search path: ")
         if search == "m":
             break
         elif search == "b":
             if depth > 0:
                 depth -= 1
             searchString = searches[depth]
         elif search == "a":
             text = self.display.input("Input new record: ")
             kb.add(searchString + "/" + text.replace("/", "|"))
         elif search == "d":
             choice = self.display.input("Choose record to remove: ")
             try:
                 if int(choice) in range(i):
                     kb.rm(searchString + "/" + results[int(choice)])
                 else:
                     self.display.error("%s - Not a valid option" %
                                        (choice))
             except ValueError:
                 self.display.error("Not a valid option")
         elif search == "i":
             self.display.error("Not implemented yet")
         elif search == "t":
             tempPath = self.config[
                 "tmpDir"] + "KBRESULTS-" + Utils.getRandStr(10) + ".txt"
             text = ""
             for line in results:
                 text = text + line + "\n"
             Utils.writeFile(text, tempPath)
             self.display.output("Results written to: %s" % (tempPath))
         elif re.match("([a-zA-Z0-9.\*]*/)+([a-zA-Z0-9.\*]*)",
                       search) != None:
             # Input in form of a/b/c/d, search keystore
             searchString = search
             depth = 0
             searches[depth] = searchString
         else:
             try:
                 if int(search) in range(i):
                     if searchString == "":
                         searchString = results[int(search)]
                     else:
                         searchString = searchString + "/" + results[int(
                             search)]
                     depth += 1
                     searches[depth] = searchString
                 else:
                     self.display.error("%s - Not a valid option" %
                                        (search))
             except ValueError:
                 self.display.error("%s - Not a valid option" % (search))
示例#3
0
 def displayKbMenu(self):
     searchString = ""
     depth = 0
     searches = {0: ""}
     self.display.output()
     self.display.output("---------------------------------------")
     self.display.output("Browse Knowledgebase")
     results = {}
     while True:
         self.display.output("[ " + searchString + " ]")
         if (searchString != ""):
             results = kb.get(searchString)
             i = 0
             for option in results:
                 self.display.output(str(i) + ". " + option)
                 i += 1
         else:
             self.display.output()
             self.display.output("0. host")
             self.display.output("1. service")
             self.display.output("2. domain")
             self.display.output("3. osint")
             results = ["host", "service", "domain", "osint"]
             i = 4  # Keep selection filter from breaking
         self.display.output()
         self.display.output(
             "Choose From Above Or: (a)dd, (d)elete, (b)ack, (m)ain menu, (i)mport, write to (t)emp file")
         self.display.output()
         search = self.display.input("Select option or enter custom search path: ")
         if search == "m":
             break
         elif search == "b":
             if depth > 0:
                 depth -= 1
             searchString = searches[depth]
         elif search == "a":
             text = self.display.input("Input new record: ")
             kb.add(searchString + "/" + text.replace("/", "|"))
         elif search == "d":
             choice = self.display.input("Choose record to remove: ")
             try:
                 if int(choice) in range(i):
                     kb.rm(searchString + "/" + results[int(choice)])
                 else:
                     self.display.error("%s - Not a valid option" % (choice))
             except ValueError:
                 self.display.error("Not a valid option")
         elif search == "i":
             self.display.error("Not implemented yet")
         elif search == "t":
             tempPath = self.config["tmpDir"] + "KBRESULTS-" + Utils.getRandStr(10) + ".txt"
             text = ""
             for line in results:
                 text = text + line + "\n"
             Utils.writeFile(text, tempPath)
             self.display.output("Results written to: %s" % (tempPath))
         elif re.match("([a-zA-Z0-9.\*]*/)+([a-zA-Z0-9.\*]*)", search) != None:
             # Input in form of a/b/c/d, search keystore
             searchString = search
             depth = 0
             searches[depth] = searchString
         else:
             try:
                 if int(search) in range(i):
                     if searchString == "":
                         searchString = results[int(search)]
                     else:
                         searchString = searchString + "/" + results[int(search)]
                     depth += 1
                     searches[depth] = searchString
                 else:
                     self.display.error("%s - Not a valid option" % (search))
             except ValueError:
                 self.display.error("%s - Not a valid option" % (search))