Пример #1
0
    def run(self):
        if self.outbound:
            proxy = parrot.Socket(self)
            self.log("Creating outbound proxy mapping from %s:%d --> %s:%d" % (
                self.interfaces["eth0"]["ip"], self.proxy_port, 
                self.server_ip, self.server_port))
        else:
            proxy = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            proxy.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            self.log("Creating inbound proxy mapping from localhost:%d --> %s:%d" % (self.proxy_port, self.server_ip, self.server_port))
        proxy.bind(('', self.proxy_port))
        proxy.listen(5)

        while not self.done:
            if self.outbound:
                client_socket = proxy.accept()
                server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            else:
                client_socket = proxy.accept()[0]
                client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                server_socket = parrot.Socket(self)
            server_socket.connect((self.server_ip, self.server_port))
            thread.start_new_thread(self.forward, (client_socket, server_socket))
            thread.start_new_thread(self.forward, (server_socket, client_socket))
Пример #2
0
 def relay_set(self, full_key, value):
     addr, key = self.path_to_address(full_key)
     sock = parrot.Socket(self)
     sock.connect((addr, house_gateway.NODE_PORT))
     sock.send('write %s %s' % (key, value))
     response = sock.recv().strip()
     msg = response.split(' ', 1)
     if msg[0] != 'ok':
         self.log("error in relay_set: %s" % response)
Пример #3
0
 def run(self):
     server = parrot.Socket(self, type=parrot.SOCK_DGRAM)
     server.bind(("0.0.0.0", 4321))
     #while not self.done:    # Run until cancelled
     fd_set = parrot.select([server])
     message, client = server.recvfrom(256)  # <=256 byte datagram
     self.log("Client connected: %s" % str(client))
     self.log("Echoing message")
     server.sendto(message, client)
Пример #4
0
 def run(self):
     time.sleep(1)
     sock = parrot.Socket(self)
     sock.connect((self.server, self.port))
     self.log("connecting the server")
     sock.send("GET / HTTP/1.0\r\n\r\n")
     content = sock.recv()
     self.log("receiving: \n%s" % content)
     sock.close()
Пример #5
0
 def __init__(self,
              node,
              server_address,
              RequestHandlerClass,
              bind_and_activate=True):
     SocketServer.BaseServer.__init__(self, server_address,
                                      RequestHandlerClass)
     self.socket = parrot.Socket(node, type=self.socket_type)
     if bind_and_activate:
         self.server_bind()
         self.server_activate()
Пример #6
0
 def relay_get(self, full_key):
     addr, key = self.path_to_address(full_key)
     sock = parrot.Socket(self)
     sock.connect((addr, house_gateway.NODE_PORT))
     sock.send('read %s' % key)
     response = sock.recv().strip()
     msg = response.split(' ', 1)
     if msg[0] == 'ok':
         return msg[1]
     else:
         self.log("error in relay_get: %s" % response)
         return None
Пример #7
0
def access_socket_main(self):
    server = parrot.Socket(self)
    server.bind(("0.0.0.0", 1234))
    server.listen()
    inputs = [server]
    while not self.done:
        inputs_ready = parrot.select(inputs)
        for s in inputs_ready:
            if s == server:
                client = server.accept()
                # self.log("client %s accepted" % client)
                inputs.append(client)
            else:
                # self.log("receiving client request...")
                data = s.recv()
                if not data:
                    s.close()
                    inputs.remove(s)
                    continue

                data = data.rstrip(None)  # Remove trailing whitespace
                content = data.split(' ', 2)
                # Check availability and access rights of property

                if content[0] == 'read':
                    if len(content) != 2:
                        s.send("error malformed command '%s'\n" % data)
                        continue
                    key = content[1]
                    access = access_rights(self, key)
                    if (access and 'r' in access) or (key == 'capabilities'):
                        s.send("ok %s\n" % self.get(key))
                    else:
                        s.send("error read access denied for '%s'\n" % key)
                elif content[0] == 'write':
                    if len(content) != 3:
                        s.send("error malformed command '%s'\n" % data)
                        continue
                    key = content[1]
                    access = access_rights(self, key)
                    if not access or not 'w' in access:
                        s.send("error write access denied for '%s'\n" % key)
                        continue
                    if not self.set(key, content[2]):
                        s.send("error setting value failed for '%s'\n" % key)
                    else:
                        s.send("ok\n")
                else:
                    s.send("error unexpected command '%s'\n" % content[0])
    self.comm_chan.close()
Пример #8
0
    def run(self):
        time.sleep(2) # Wait for server to start
        messout = 'Hello over UDP'
        address = ("10.1.2.2", 4321)

        sock = parrot.Socket(self, type=parrot.SOCK_DGRAM)

        # sock.bind(("0.0.0.0", 2137))
        sock.sendto(messout, address)
        messin, peer = sock.recvfrom(256) # <=256 byte datagram

        if messin != messout:
            self.log("Failed to receive identical message")
        self.log("Received: %s" % messin)
        # sock.close()
        self.done = True
Пример #9
0
 def run(self):
     server = parrot.Socket(self)
     server.bind(("0.0.0.0", 80))
     server.listen()
     self.log("listening 0.0.0.0:80")
     inputs = [server]
     #time.sleep(5)
     while not self.done:
         inputs_ready = parrot.select(inputs)
         for s in inputs_ready:
             if s == server:
                 client = server.accept()
                 self.log("client %s accepted" % client)
                 inputs.append(client)
             else:
                 content = s.recv()
                 if content:
                     self.log("receiving from client: %s" % str(content))
                     s.send("<html>Hello World!</html>")
                     s.close()
                 else:
                     self.log("Client closed down!")
                     inputs.remove(s)