예제 #1
0
    def recv(self, client: socket.socket):
        while True:
            try:
                recvData = client.recv(1024).decode('utf-8')

            except:
                print('%s 下线' % (client.getpeername()))
                break
            else:
                if len(recvData) != 0:
                    print(client.getpeername(), '接受数据为:', recvData)
                    temp = recvData.splitlines()[-1]
                    print('post 请求的数据:', temp)

                    print('请求注册')
                    # end=re.fullmatch('userName=(.*?)&userPwd=(.*?)',temp)
                    # end=re.fullmatch('text1=(.*?)&text2=(.*?)',temp)
                    end = re.fullmatch('userName=(.*?)&pwd=(.*?)&text1=(.*)',
                                       temp)

                    print("用户名:", end.groups()[0])
                    print("密码:", end.groups()[1])

                    print(end.groups())

                    time.sleep(1)
                    client.close()
                    break
                else:
                    client.close()
                    break
예제 #2
0
def forward_tcp_to_tcp(source: socket.socket, destination: socket.socket):
    try:
        log.debug("Ongoing connection route: %s ---> %s ---> %s" %
                  (source.getpeername(), source.getsockname(),
                   destination.getpeername()))
    except:
        # Do nothing
        log.debug("Socket closed maybe??")
    message = ' '
    while message:
        message = source.recv(tcp_bufsize)
        if message:
            destination.sendall(message)
        else:
            try:
                source.shutdown(socket.SHUT_RD)
            except:
                # traceback.print_exc()
                pass  # Do nothing then
            try:
                destination.shutdown(socket.SHUT_WR)
            except:
                # traceback.print_exc()
                pass  # Do nothing then
            break
예제 #3
0
 def _onUnknownReadable(self, conn : socket):
     if conn is self.__target:
         try:
             nconn, addr = conn.accept()
             self.__clients[str(addr)] = nconn
             self._addSocket(nconn)
         except Exception as ex:
             self._err(conn, ex)
             return
         packet = Packet()
         packet.op = Packet.OP_USER_CONN
         packet.host, packet.port = addr
         packet.send(self.__source)
     else:
         data = conn.recv(TCP_RECV_BUFF)
         packet = Packet()
         packet.host, packet.port = conn.getpeername()
         if data:
             packet.op = Packet.OP_USER_MSG
             packet.data = data
         else:
             packet.op = Packet.OP_USER_DISCONN
             del self.__clients[str(conn.getpeername())]
             self._removeSocket(conn)
         packet.send(self.__source)
예제 #4
0
 def send_body(self, conn: socket.socket):
     """Send body into [conn]."""
     if self._content_io and (not self.request
                              or self.request.command != "HEAD"):
         logging.debug('%s <- sending body', conn.getpeername())
         with suppress(BrokenPipeError):
             conn.sendfile(self._content_io)
             logging.info('%s <- body sent - ok', conn.getpeername())
    def serve(self, sd: socket.socket) -> None:
        """ Handle the peer request

		:param sd: the socket descriptor used for read the request
		:return None
		"""

        try:
            packet = sd.recv(300).decode()
        except socket.error as e:
            self.log.write_red(
                f'Unable to read the packet from the socket: {e}')
            sd.close()
            return

        # log the packet received
        socket_ip_sender = sd.getpeername()[0]
        if ipaddress.IPv6Address(socket_ip_sender).ipv4_mapped is None:
            socket_ip_sender = ipaddress.IPv6Address(
                socket_ip_sender).compressed
        else:
            socket_ip_sender = ipaddress.IPv6Address(
                socket_ip_sender).ipv4_mapped.compressed

        socket_port_sender = sd.getpeername()[1]
        self.log.write_green(f'{socket_ip_sender} [{socket_port_sender}] -> ',
                             end='')
        self.log.write(f'{packet}')

        command = packet[0:4]

        if command == "AQUE":

            if len(packet) != 212:
                self.log.write_red(
                    f'Invalid packet received: {packet}\nUnable to reply.')
                return

            pktid = packet[4:20]
            ip_peer = packet[20:75]
            port_peer = int(packet[75:80])
            file_md5 = packet[80:112]
            filename = packet[112:212]

            if pktid != LocalData.get_sent_net_quer_packet():
                return

            if not LocalData.exist_net_peer_file(ip_peer, port_peer, file_md5,
                                                 filename):
                LocalData.add_net_peer_file(ip_peer, port_peer, file_md5,
                                            filename)

        else:
            self.log.write_red(
                f'Invalid packet received: {packet}\nUnable to reply.')

        return
예제 #6
0
    def __read_socket(self, s: socket.socket):
        """
        Handles sockets that has data to be read/incomming connections
        """
        if s is self.__socket:  # If socket is servers own socket, we have a new client connecting
            connection, addr = s.accept()  # Accept client
            connection.setblocking(0)  # Asynchronous input

            # Store info about client
            self.__sockets_read.append(connection)
            self.__response_queues[connection] = queue.Queue()
            self.on_client_connected(addr)

        else:  # A clients socket has data to be read
            header = self.__header_buffer.get(
                s.getpeername(), None
            )  # Has this client sent data before? Get the previous data (header) sent
            n = header.size if header is not None else PacketHeader.SIZE  # Get the expected size to read (as stated in the header) or (if the header doesnt exist, which means this is the first data from that client to be received) set the size to header size

            avail = self._available_bytes(
                s.fileno())  # Get size of the data awaiting to be read
            if avail < n:  # Available data size is not (yet) sufficient
                if avail <= 0:  # If we have data request with no size, the client has disconnected
                    self.on_client_disconnected(s.getpeername())
                    self.__close_socket(s)

                return  # If size of the data is less than expected, wait for more in the next call

            data = s.recv(n) if n > 0 else None  # Receive the acctual data
            if header is None:  # If we don't have previous data form this client
                if data is None:  # This is not expected
                    return

                header = PacketHeader._make(
                    struct.unpack(PacketHeader.FORMAT,
                                  data))  # Unpack header data
                self.__header_buffer[s.getpeername(
                )] = header  # Associente this header to client
                if header.size > 0:  # If expecting more data receive it in the next call
                    return

            # Here we are guaranteed to have a header, but not necessarily any data after that (i.e: header != None and (data == None or data != None))
            if self.on_data_received(header, data):  # Call callback
                response = struct.pack(PacketHeader.FORMAT, int(PacketID.TRUE),
                                       0)  # Success packet to be sent back
            else:
                response = struct.pack(PacketHeader.FORMAT,
                                       int(PacketID.FALSE),
                                       0)  # Fail packet to be sent back

            self.__response_queues[s].put(response)  # Queue response packet
            if s not in self.__sockets_write:
                self.__sockets_write.append(
                    s)  # Append cliet socket to write queue

            del self.__header_buffer[s.getpeername(
            )]  # Remove associated header from client as it's already processed
예제 #7
0
    def transfer_from_tunnel(self, r_conn: socket_t, mask: int,
                             buf: List[deque]) -> None:
        """receive data from tunnel and store in buffer"""
        w_conn = self.work_pool.inv.get(r_conn)
        if w_conn is None:
            self._sel.unregister(r_conn)
            r_conn.close()
            return

        data = b''
        need_close = False

        try:
            data = r_conn.recv(BUF_SIZE)
        except ConnectionError:
            need_close = True

        if data == b'' or need_close:
            try:
                peer = r_conn.getpeername()
                logger.info(
                    f'closing tunnel connection from {format_addr(peer)}'
                )  # noqa
            except OSError as e:
                logger.warn(e)
            self._sel.unregister(r_conn)
            r_conn.close()
            buf[NEG].append(sentinel)
            del self.work_pool.inv[r_conn]
            return

        buf[NEG].append(data)
예제 #8
0
파일: server.py 프로젝트: yairp03/CodeNames
 def recv(sock: socket.socket) -> Message:
     try:
         req = sock.recv(1024).decode()
     except ConnectionResetError:
         return Message()
     if req:
         try:
             msg = parse_message(req)
         except ValueError as e:
             server_print(f"Bad received: {req.encode()}", sock.getpeername())
             msg = Message(ResponseCodes.BAD_MESSAGE, str(e))
         else:
             server_print(f"Received: {msg}", sock.getpeername())
         return msg
     else:
         return Message()
예제 #9
0
 def __init__(self, sock: socket.socket, q: Queue):
     self.sock = sock
     self.client = sock.getpeername()
     self.q = q
     self.event = threading.Event()
     self.home = HOME
     self.current = self.home
예제 #10
0
def manage_client(conn: socket.socket, cmd: str) -> None:
    """Manage client connections by facilitating commands.

    Args:
        conn: Client connection.
        cmd: Command to execute.
    """
    peername = conn.getpeername()[:2]

    try:
        handler = {"LIST": cmd_list, "GRAB": cmd_grab, "PUSH": cmd_push}[cmd]
    except KeyError as e:
        raise exc.BadRequest("invalid command") from e
    failure = True
    try:
        results = ["OK", handler(conn)]
        failure = False
    except ConnectionResetError:
        raise
    except (OSError, IOError) as e:
        results = ["KO", e.args[0]]
    except Exception as e:
        results = ["KO", e.message]
    finally:
        conn.send(utils.encode(results))

    print("[*] {} {} {} {}".format(*peername, ["OK", "KO"][failure], cmd))
예제 #11
0
def recvfrom_blob(
        sock: socket.socket,
        timeout: int = SOCKET_OPERATION_TIMEOUT) -> Tuple[bytes, str]:
    """
    Receive DNS message from TCP/UDP socket.
    """

    # deadline is always time.monotonic
    deadline = time.monotonic() + timeout

    while True:
        try:
            if sock.type & socket.SOCK_DGRAM:
                handle_socket_timeout(sock, deadline)
                data, addr = sock.recvfrom(RECEIVE_MESSAGE_SIZE)
            elif sock.type & socket.SOCK_STREAM:
                # First 2 bytes of TCP packet are the size of the message
                # See https://tools.ietf.org/html/rfc1035#section-4.2.2
                data = recv_n_bytes_from_tcp(sock, 2, deadline)
                msg_len = struct.unpack_from("!H", data)[0]
                data = recv_n_bytes_from_tcp(sock, msg_len, deadline)
                addr = sock.getpeername()[0]
            else:
                raise NotImplementedError(
                    "[recvfrom_blob]: unknown socket type '%i'" % sock.type)
            return data, addr
        except socket.timeout as ex:
            raise RuntimeError("Server took too long to respond") from ex
        except OSError as ex:
            if ex.errno == errno.ENOBUFS:
                time.sleep(0.1)
            else:
                raise
예제 #12
0
def channel_thread(channel: socket.socket):
    host, port = channel.getpeername()[0:2]

    while True:
        size_buffer = channel.recv(1)
        if len(size_buffer) == 0:
            break
        size = size_buffer[0]
        print('(pending: {})'.format(size))

        if size == 0:
            print('Closing connection to [{}]:{}'.format(host, port))
            break

        buffer = bytes()
        while len(buffer) < size:
            buffer_size = min([size - len(buffer), 64])
            buffer = buffer + channel.recv(buffer_size)

        print('Received message from [{}]:{}.'.format(host, port))
        print(buffer.decode('utf-8'))

        channel.send(bytes([
            size,
        ]))
        channel.send(buffer)

    send_john_cena(channel)
    channel.shutdown(socket.SHUT_WR)
    channel.close()
예제 #13
0
 def __init__(self, app_handler, client_socket: socket.socket,
              print_lock: Lock):
     Thread.__init__(self)
     self.app_handler = app_handler
     self.client_socket = client_socket
     self.client_addr = client_socket.getpeername()
     self.log_lock = print_lock
예제 #14
0
    def address(self, client: socket = None) -> Tuple[str, int]:
        """
        Gets both host and port from this server or from one of its clients.

        ---
        Arguments
        ---

            client (socket, None)
        A client socket object from which the address will be obtained, if not
        from the server.

        ---
        Returns
        ---

            Tuple(str, int)
        Both the host and the port, respectively.
        """

        # If the client socket was not provided,...
        if client is None:

            # ... checks whether the socket is already open...
            self.check_connection()

            # ... and returns the socket address of this server.
            return self.get_socket().getsockname()

        # Checks the client connection.
        self.check_client(client)

        # Returns the client address.
        return client.getpeername()
예제 #15
0
    def rec(self, conn: socket.socket):  # 在select线程中运行的
        data = conn.recv(1024)
        logging.info(data.decode(encoding='cp936'))

        if data.strip() == b'quit' or data.strip() == b'':
            self.selector.unregister(conn)  # 关闭之前,注销,理解为之前的从字典中移除socket对象
            conn.close()
            return

        for key in self.selector.get_map().values():
            s = key.fileobj
            # if key.fileobj is self.sock:  # 方法一
            #     continue
            # if key == self.accept_key:  # 方法二
            #     continue
            # if key.fd == self.accept_fd:  # 方法三
            #     continue
            # msg = 'Your msg = {} form {}'.format(data.decode(encoding='cp936'), conn.getpeername())
            # s.send(msg.encode(encoding='cp936'))
            # print(key.data)
            # print(self.rec)
            # print(1, key.data is self.rec)  # False
            # print(2, key.data == self.rec)  # True
            if key.data == self.rec:  # 方法四
                msg = 'Your msg = {} form {}'.format(data.decode(encoding='cp936'), conn.getpeername())
                s.send(msg.encode(encoding='cp936'))
예제 #16
0
def rec(conn: socket.socket, msk: int):
    data = conn.recv(1024)
    print(data)
    print(msk)

    msg = 'Your msg {} form {}'.format(data.decode(), conn.getpeername())
    conn.send(msg.encode())
예제 #17
0
    def transfer_from_dest(self, r_conn: socket_t, mask: int,
                           buf: List[deque]) -> None:
        """receive data from dest and store in buffer"""
        w_conn = self.working_pool.inv.get(r_conn)
        if w_conn is None:
            self._sel.unregister(r_conn)
            r_conn.close()
            return

        data = b''
        need_close = False

        try:
            data = r_conn.recv(BUF_SIZE)
        except ConnectionError:
            need_close = True

        if data == b'' or need_close:
            try:
                peer = r_conn.getpeername()
                logger.info(
                    f'closing dest connection from {format_addr(peer)}')
            except OSError as e:
                logger.warn(e)
            self._sel.unregister(r_conn)
            self._sel.modify(w_conn, selectors.EVENT_WRITE,
                             partial(self.send_to_tunnel, buf=buf))
            r_conn.close()
            buf[NEG].append(sentinel)
            del self.working_pool.inv[r_conn]
            return

        buf[NEG].append(data)
예제 #18
0
    def del_client(self, conn: socket.socket):
        addr = conn.getpeername()
        if addr not in self.clients.keys():
            raise ClientNotExistException()

        self.server.remove_handler(conn.fileno())
        del self.clients[addr]
예제 #19
0
    def listen_to_client_messages(self, conn: socket.socket):
        '''
            This function is ran under a new thread where it 
            constantly listens to the ip if the client is sending
            any messages and if any message is recieved the message
            is broadcasted to other clients
        '''

        # Getting the IP and PORT of the Client
        IP, PORT = conn.getpeername()

        # The first message recieved by the client is the name
        # of the client. This is mapped using the `client_names`
        name = conn.recv(1024).decode()
        # client_names[(IP, PORT)] = name

        # Log to the server
        print(f"{IP}:{PORT} => {name}")

        # This Loop keeps on reading messages from the client
        while True:

            try:
                msg = conn.recv(1024).decode()
                self.broadcast_message(f"{name} => {msg}")

            except Exception:

                # client_names.pop((IP, PORT))
                # clients.remove(conn)

                conn.close()
                threading.currentThread()._stop()
예제 #20
0
 def  start_listener(self, conn:socket.socket, index):
     s=socket.socket()
     print("Master>>On thread",index,"peered with ", conn.getpeername())
     while True :
         if self.SLAVES_LIST[index].uploading :
             self.remove_slave()
             if self.slaves_num== 0 :
                 self.WORKING=False
                 print("Master>>Ready to receive upload")
                 self.receive_upload(conn,s)
                 self.close_listener()
             else :
                 print("Master>>Ready to receive upload")
                 self.receive_upload(conn,s)
             break
         else:
             msg=conn.recv(23).decode().split("|")
             print("Master>>Received message : ",msg)
             if msg[0]=="hi" :
                 self.SLAVES_LIST[index].address=msg[1]
                 self.SLAVES_LIST[index].port=int(msg[2].replace("?",""))
                 print(self.SLAVES_LIST[index].address,",",self.SLAVES_LIST[index].port,"is saying hi !")
                 s.connect((self.SLAVES_LIST[index].address, self.SLAVES_LIST[index].port))
                 print(self.SLAVES_LIST[index].address, self.SLAVES_LIST[index].port , " says hi !")
             
             print("Master>>Affecting job to", self.SLAVES_LIST[index].address, self.SLAVES_LIST[index].port)
             self.affect_job(s, index)
             print("Master>>Job affected to slave", self.SLAVES_LIST[index].address, self.SLAVES_LIST[index].port)
예제 #21
0
 def __init__(self, sock: socket.socket) -> None:
     self.endpoint_addr = Address(*sock.getpeername())
     self._endpoint_pubkey: Optional[RSA.RsaKey] = None
     self._connected = False
     self._closed = False
     self._sent_pubkey = False
     self._sock = sock
예제 #22
0
def handle_client(sock: socket.socket):
    while True:
        data = sock.recv(1024)
        if not data:
            break
        sock.sendall(data.decode("utf-8").upper().encode())
    print("Disconnected from", sock.getpeername())
    sock.close()
예제 #23
0
파일: protocols.py 프로젝트: unazed/henrio
async def connect(protocol_factory: Callable[..., ConnectionBase],
                  address: str = None,
                  port: int = None,
                  family=AF_INET,
                  type=SOCK_STREAM,
                  proto=0,
                  fileno=None,
                  bufsize: int = 1024,
                  sock: socket = None):
    if sock is not None:
        if fileno is not None:
            raise ValueError("You cannot specify a fileno AND a socket!")
        try:
            sock.getpeername()
            connected = False
        # We want to check if the sock is connected already
        except OSError:  # It'll raise an OSError if we try to getpeername of an unconnected sock
            if address is not None or port is not None:
                raise ValueError(
                    "You cannot specify both an address/port AND a connected socket!"
                ) from None
            connected = True
    else:
        sock = socket(family=family, type=type, proto=proto, fileno=fileno)
        connected = False

    connection = protocol_factory(
        socket=sock, host=(address, port),
        bufsize=bufsize)  # All protos need these args

    if not connected:
        await connection._connect(
        )  # If the sock isn't connected, connect "asynchronously"

    loop = await get_loop()
    if not isinstance(loop, IOCPLoop):
        await create_writer(sock, connection._writer_callback
                            )  # Create our reader and writer
    else:
        if sock.fileno() not in loop._open_ports:
            _overlapped.CreateIoCompletionPort(sock.fileno(), loop._port, 0, 0)
            loop._open_ports.append(sock.fileno())
    await create_reader(sock, connection._reader_callback)

    return connection
예제 #24
0
    def add_client(self, conn: socket.socket):
        addr = conn.getpeername()
        if addr in self.clients.keys():
            raise ClientExistException()

        client = Client(self, conn)

        self.server.add_handler(conn.fileno(), client.recv, EVENT_READ)
        self.clients[addr] = client
예제 #25
0
파일: udp.py 프로젝트: osauldmy/BI-PSI
def recv_and_log(sock: socket.socket) -> Tuple[int, int, int, int, bytes]:
    """
    Simple wrapper to log each responset to stdout after receiving it.
    """
    return log_pipe(
        *decode_packet(sock.recv(MAX_PACKET_SIZE)),
        "RECV",
        sock.getpeername()[0],
    )
예제 #26
0
 def __init__(self, sock: socket.socket) -> None:
     self.endpoint_addr = Address(*sock.getpeername())
     self._sock = sock
     self._buffer = b""
     self._headers_len: Optional[int] = None
     self._headers: Optional[MessageHeaders] = None
     self._content: Optional[bytes] = None
     self._request_reading = True
     self._closed = False
예제 #27
0
def bind_remote_address_to_logger(sock: socket.socket):  # type:ignore
    try:
        bind_contextvars(remote=sock.getpeername())
    except socket.error:
        pass

    try:
        yield
    finally:
        unbind_contextvars("remote")
예제 #28
0
파일: server.py 프로젝트: yairp03/CodeNames
    def send(sock: socket.socket, res: Message):
        json_data = json.dumps(res.data)
        msg = (
            str(res.code).rjust(Consts.CODE_LEN, "0")
            + str(len(json_data)).rjust(Consts.LENGTH_LEN, "0")
            + json_data
        )

        sock.send(msg.encode())
        server_print(f"Sent:     {res}", sock.getpeername())
예제 #29
0
    def recv(self, sock: socket.socket):
        while not self.event.is_set():
            try:
                data = sock.recv(1024)  # blocked,bytes
                logging.info(data)
            except Exception as e:
                logging.error(e)
                data = b'quit'
                print('fenshi')
            if data == b'quit':
                self.clients.pop(sock.getpeername())
                sock.close()
                break

            msg = "ack{}. {} {}".format(
                sock.getpeername(),
                datetime.datetime.now().strftime("%Y/%m/%d-%H:%M:%S"),
                data.decode()).encode()
            for s in self.clients.values():
                s.send(msg)
예제 #30
0
    def __init__(self, id: str, conn: socket.socket, manager):
        super().__init__(name=id)

        self.conn = conn
        self.addr = conn.getpeername()
        self.server = manager
        self.closed = False
        self.info = {}
        self.shared = {}
        self._lock_sharing = [False]
        self._buffer = bytearray()
        log.debug("Finished ClientWorker initialization.")
예제 #31
0
 def original_addr(self, csock: socket.socket):
     ip, port = csock.getpeername()[:2]
     ip = re.sub(r"^::ffff:(?=\d+.\d+.\d+.\d+$)", "", ip)
     ip = ip.split("%", 1)[0]
     with self.lock:
         try:
             write((ip, port), self.wfile)
             addr = read(self.rfile)
             if addr is None:
                 raise RuntimeError("Cannot resolve original destination.")
             return tuple(addr)
         except (EOFError, socket.error):
             self._connect()
             return self.original_addr(csock)