コード例 #1
0
 def __init__(self, server=("0.0.0.0", 0)):
     sucky = socket(AF_INET, SOCK_DGRAM)
     logging.debug("Hostname: {0} ({1})".format(
         gethostname(), gethostbyname(gethostname())))
     sucky.bind(server)
     self.addr = sucky.getsockname()
     logging.debug("Socket bind: {0}".format(self.addr))
     self.sock = UDPSocket(sucky)
     self.server = server
コード例 #2
0
ファイル: networkio.py プロジェクト: jsam/jollyroger
class Sender(object):
    def __init__(self, send_dest, recv_dest, uuid):
        self.recv_dest = recv_dest
        self.send_dest = send_dest  ## hardening, tcp over udp magic

        sucky = socket(AF_INET, SOCK_DGRAM)
        if len(send_dest) > 2:
            sucky.connect((send_dest[0], send_dest[1]))
        else:
            sucky.connect(send_dest)
        self.sock = UDPSocket(sucky)
        self.uuid = uuid

    def send_query(self, data, query_type, rpc_no):
        msg = {
            "data": data,
            "type": "query",
            "query_type": query_type,
            "rpc_no": rpc_no,
            "uuid": self.uuid,
            "recv_dest": self.recv_dest,
            "send_dest": self.send_dest,
        }

        # logging.debug("Sending packet {0}".format(msg))
        self.sock.send(msg, self.send_dest)

    def send_error(self, err, rpc_no):
        msg = {
            "data": err,
            "type": "err",
            "rpc_no": rpc_no,
            "uuid": self.uuid,
            "recv_dest": self.recv_dest,
            "send_dest": self.send_dest,
        }

        self.sock.send(msg, self.send_dest)

    def send_resp(self, resp, rpc_no):
        msg = {
            "data": resp,
            "type": "resp",
            "rpc_no": rpc_no,
            "uuid": self.uuid,
            "recv_dest": self.recv_dest,
            "send_dest": self.send_dest,
        }
        logging.debug("Sending response {} to {}".format(msg, self.send_dest))
        yield self.sock.send(msg, self.send_dest)

    def send_file(self, filename):
        import tftpy

        client = tftpy.TftpClient(self.send_dest[0], self.send_dest[1] + 1)
        client.upload(filename, basename(filename))
コード例 #3
0
class Sender(object):
    def __init__(self, send_dest, recv_dest, uuid):
        self.recv_dest = recv_dest
        self.send_dest = send_dest  ## hardening, tcp over udp magic

        sucky = socket(AF_INET, SOCK_DGRAM)
        if len(send_dest) > 2:
            sucky.connect((send_dest[0], send_dest[1]))
        else:
            sucky.connect(send_dest)
        self.sock = UDPSocket(sucky)
        self.uuid = uuid

    def send_query(self, data, query_type, rpc_no):
        msg = {
            "data": data,
            "type": "query",
            "query_type": query_type,
            "rpc_no": rpc_no,
            "uuid": self.uuid,
            "recv_dest": self.recv_dest,
            "send_dest": self.send_dest
        }

        #logging.debug("Sending packet {0}".format(msg))
        self.sock.send(msg, self.send_dest)

    def send_error(self, err, rpc_no):
        msg = {
            "data": err,
            "type": "err",
            "rpc_no": rpc_no,
            "uuid": self.uuid,
            "recv_dest": self.recv_dest,
            "send_dest": self.send_dest
        }

        self.sock.send(msg, self.send_dest)

    def send_resp(self, resp, rpc_no):
        msg = {
            "data": resp,
            "type": "resp",
            "rpc_no": rpc_no,
            "uuid": self.uuid,
            "recv_dest": self.recv_dest,
            "send_dest": self.send_dest
        }
        logging.debug("Sending response {} to {}".format(msg, self.send_dest))
        yield self.sock.send(msg, self.send_dest)

    def send_file(self, filename):
        import tftpy
        client = tftpy.TftpClient(self.send_dest[0], self.send_dest[1] + 1)
        client.upload(filename, basename(filename))
コード例 #4
0
    def __init__(self, send_dest, recv_dest, uuid):
        self.recv_dest = recv_dest
        self.send_dest = send_dest  ## hardening, tcp over udp magic

        sucky = socket(AF_INET, SOCK_DGRAM)
        if len(send_dest) > 2:
            sucky.connect((send_dest[0], send_dest[1]))
        else:
            sucky.connect(send_dest)
        self.sock = UDPSocket(sucky)
        self.uuid = uuid
コード例 #5
0
ファイル: networkio.py プロジェクト: jsam/jollyroger
 def __init__(self, server=("0.0.0.0", 0)):
     sucky = socket(AF_INET, SOCK_DGRAM)
     logging.debug("Hostname: {0} ({1})".format(gethostname(), gethostbyname(gethostname())))
     sucky.bind(server)
     self.addr = sucky.getsockname()
     logging.debug("Socket bind: {0}".format(self.addr))
     self.sock = UDPSocket(sucky)
     self.server = server
コード例 #6
0
ファイル: networkio.py プロジェクト: jsam/jollyroger
    def __init__(self, send_dest, recv_dest, uuid):
        self.recv_dest = recv_dest
        self.send_dest = send_dest  ## hardening, tcp over udp magic

        sucky = socket(AF_INET, SOCK_DGRAM)
        if len(send_dest) > 2:
            sucky.connect((send_dest[0], send_dest[1]))
        else:
            sucky.connect(send_dest)
        self.sock = UDPSocket(sucky)
        self.uuid = uuid
コード例 #7
0
class Receiver(object):
    def __init__(self, server=("0.0.0.0", 0)):
        sucky = socket(AF_INET, SOCK_DGRAM)
        logging.debug("Hostname: {0} ({1})".format(
            gethostname(), gethostbyname(gethostname())))
        sucky.bind(server)
        self.addr = sucky.getsockname()
        logging.debug("Socket bind: {0}".format(self.addr))
        self.sock = UDPSocket(sucky)
        self.server = server

    def recv(self):
        packet, addr = yield self.sock.recv()
        logging.debug("Received packet {0} from {1}.".format(packet, addr))

        routing_table = kBucket(cache["addr"], cache["port"], cache["uuid"])
        routing_table.insert(packet["recv_dest"][0], packet["recv_dest"][1],
                             packet["uuid"])

        logging.debug("Routing table updated: ({0}, {1}, {2})".format(
            packet["recv_dest"][0], packet["recv_dest"][1], packet["uuid"]))

        if packet["type"] == "query":
            ## unpack => (resp, addr, rpc_no, uuid)

            if packet["query_type"] == "shellcmd":
                unpack = (self.handle_shellcmd(packet), packet["recv_dest"],
                          packet["rpc_no"], packet["uuid"])
                yield unpack

            if packet["query_type"] == "ping":
                # ping(): pong
                resp = "pong"
                unpack = (resp, packet["recv_dest"], packet["rpc_no"],
                          packet["uuid"])
                yield unpack

            if packet["query_type"] == "store":
                # store(key, value h_key): True
                resp = True
                try:
                    cache["values"].update({
                        packet["data"][2]:
                        (packet["data"][0], packet["data"][1]),
                        "author":
                        packet["data"][3]
                    })
                    logging.debug("Values stored: {}".format(cache["values"]))
                except Exception:
                    resp = False

                unpack = (resp, packet["recv_dest"], packet["rpc_no"],
                          packet["uuid"])
                yield unpack

            if packet["query_type"] == "find_node":
                # find_node(hkey): k_nearest | node
                nearest = routing_table.get_nearest_nodes(packet["data"])

                if nearest[0][2] == packet["data"]:
                    resp = nearest[0]
                else:
                    resp = nearest
                unpack = (resp, packet["recv_dest"], packet["rpc_no"],
                          packet["uuid"])
                yield unpack

            if packet["query_type"] == "find_value":
                # find_value(hkey): k_nearest | value
                resp = cache["values"].get(packet["data"], None)
                if resp is None:
                    resp = routing_table.get_nearest_nodes(packet["data"])

                unpack = (resp, packet["recv_dest"], packet["rpc_no"],
                          packet["uuid"])

                yield unpack

        if packet["type"] == "err":
            yield packet, "err", addr, packet["rpc_no"], packet["uuid"]

        if packet["type"] == "resp":
            yield packet, addr, packet["rpc_no"], packet["uuid"]

    def handle_shellcmd(self, data):
        try:
            proc = subprocess.Popen(data["data"],
                                    shell=True,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE,
                                    stdin=subprocess.PIPE)
            resp = proc.stdout.read() + proc.stderr.read()
        except:
            logging.error("Subprocess Popen execution error")

        return resp

    def get_addr(self):
        return self.addr

    def get_ip(self):
        return self.addr[0]

    def get_port(self):
        return self.addr[1]
コード例 #8
0
ファイル: networkio.py プロジェクト: jsam/jollyroger
class Receiver(object):
    def __init__(self, server=("0.0.0.0", 0)):
        sucky = socket(AF_INET, SOCK_DGRAM)
        logging.debug("Hostname: {0} ({1})".format(gethostname(), gethostbyname(gethostname())))
        sucky.bind(server)
        self.addr = sucky.getsockname()
        logging.debug("Socket bind: {0}".format(self.addr))
        self.sock = UDPSocket(sucky)
        self.server = server

    def recv(self):
        packet, addr = yield self.sock.recv()
        logging.debug("Received packet {0} from {1}.".format(packet, addr))

        routing_table = kBucket(cache["addr"], cache["port"], cache["uuid"])
        routing_table.insert(packet["recv_dest"][0], packet["recv_dest"][1], packet["uuid"])

        logging.debug(
            "Routing table updated: ({0}, {1}, {2})".format(
                packet["recv_dest"][0], packet["recv_dest"][1], packet["uuid"]
            )
        )

        if packet["type"] == "query":
            ## unpack => (resp, addr, rpc_no, uuid)

            if packet["query_type"] == "shellcmd":
                unpack = (self.handle_shellcmd(packet), packet["recv_dest"], packet["rpc_no"], packet["uuid"])
                yield unpack

            if packet["query_type"] == "ping":
                # ping(): pong
                resp = "pong"
                unpack = (resp, packet["recv_dest"], packet["rpc_no"], packet["uuid"])
                yield unpack

            if packet["query_type"] == "store":
                # store(key, value h_key): True
                resp = True
                try:
                    cache["values"].update(
                        {packet["data"][2]: (packet["data"][0], packet["data"][1]), "author": packet["data"][3]}
                    )
                    logging.debug("Values stored: {}".format(cache["values"]))
                except Exception:
                    resp = False

                unpack = (resp, packet["recv_dest"], packet["rpc_no"], packet["uuid"])
                yield unpack

            if packet["query_type"] == "find_node":
                # find_node(hkey): k_nearest | node
                nearest = routing_table.get_nearest_nodes(packet["data"])

                if nearest[0][2] == packet["data"]:
                    resp = nearest[0]
                else:
                    resp = nearest
                unpack = (resp, packet["recv_dest"], packet["rpc_no"], packet["uuid"])
                yield unpack

            if packet["query_type"] == "find_value":
                # find_value(hkey): k_nearest | value
                resp = cache["values"].get(packet["data"], None)
                if resp is None:
                    resp = routing_table.get_nearest_nodes(packet["data"])

                unpack = (resp, packet["recv_dest"], packet["rpc_no"], packet["uuid"])

                yield unpack

        if packet["type"] == "err":
            yield packet, "err", addr, packet["rpc_no"], packet["uuid"]

        if packet["type"] == "resp":
            yield packet, addr, packet["rpc_no"], packet["uuid"]

    def handle_shellcmd(self, data):
        try:
            proc = subprocess.Popen(
                data["data"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE
            )
            resp = proc.stdout.read() + proc.stderr.read()
        except:
            logging.error("Subprocess Popen execution error")

        return resp

    def get_addr(self):
        return self.addr

    def get_ip(self):
        return self.addr[0]

    def get_port(self):
        return self.addr[1]