Beispiel #1
0
def handle_request(client_sock, server_addr: str, server_port: int):
    try:
        # receive data from client(i.e. browser)
        head_data = client_sock.recv(BUFFER_SIZE)
        if not head_data:
            client_sock.close()
            return
        parse_head(head_data)  # show debug message
        encrypted_data = encrypt(head_data)  # encrypt data
        target_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # send encrypted data to server
        target_sock.connect((server_addr, server_port))
        target_sock.send(encrypted_data)
        read_write(client_sock, target_sock)  # async communication
        target_sock.close()  # close socket
    except TimeoutError:
        logging.warning('[WARN] [{}] {:>61}'.format(get_time_str(), "link to " + server_addr + ' time out.'))
    except ConnectionAbortedError:
        logging.warning('[WARN] [{}] {:>61}'.format(get_time_str(), "link to " + server_addr + ' was aborted by client.'))
    except ConnectionResetError:
        logging.warning('[WARN] [{}] {:>61}'.format(get_time_str(), "link to " + server_addr + ' was reseted.'))
    except ConnectionRefusedError:
        logging.warning('[WARN] [{}] {:>61}'.format(get_time_str(), "link to " + server_addr + ' was refused.'))
    except socket.gaierror:
        logging.error('[ERR]  [{}] {:>61}'.format(get_time_str(), "can't CONNECT to server!"))
    finally:
        client_sock.close()
Beispiel #2
0
def handle_request(client_sock):
    try:
        head_str = decrypt(client_sock.recv(BUFFER_SIZE))  # receive data from client
        method, path, protocol = parse_head(head_str)  # analyze data
        target_sock = _get_target_sock(method, path, client_sock, head_str)
        read_write(client_sock, target_sock)  # async communication
    except TimeoutError:
        logging.warning("[WARN] [{}] {:7} {:>53}".format(get_time_str(), method, short_str(path, 31) + " time out."))
    except ConnectionAbortedError:
        logging.warning(
            "[WARN] [{}] {:7} {:>53}".format(get_time_str(), method, short_str(path, 22) + " aborted by client.")
        )
    except ConnectionResetError:
        logging.warning("[WARN] [{}] {:7} {:>53}".format(get_time_str(), method, short_str(path, 28) + " was reseted."))
    except ConnectionRefusedError:
        logging.warning("[WARN] [{}] {:7} {:>53}".format(get_time_str(), method, short_str(path, 28) + " was refused."))
    except socket.gaierror:
        logging.error("[ERR ] [{}] {:>53}".format(get_time_str(), "can't CONNECT to server!"))
    finally:
        client_sock.close()
Beispiel #3
0
def parse_head(head_str: bytes):
    method, path, protocol = head_str.split(b'\r\n')[0].split(b' ')
    # assert str(type(method)) == "<class 'bytes'>"
    # assert str(type(protocol)) == "<class 'bytes'>"
    # assert str(type(path)) == "<class 'bytes'>"
    method = method.decode()
    path = path.decode()
    protocol = protocol.decode()
    # assert str(type(method)) == "<class 'str'>"
    # assert str(type(path)) == "<class 'str'>"
    # assert str(type(protocol)) == "<class 'str'>"
    logging.info('[INFO] [{}] {:7} {:44} {}'.format(get_time_str(), method, short_str(path, PATH_LEN), protocol))
    logging.debug('[DEBUG] [{} in {} running threads]'.format(threading.current_thread().getName(), threading.active_count()))
    logging.debug(head_str)
    logging.debug('')
    return method, path, protocol