[Python] The remote host breaks the connection

Discussão em 'Python' iniciado por Stack, Janeiro 9, 2025.

  1. Stack

    Stack Membro Participativo

    I want to make my proxy server on VDS, set up a proxy on my computer, and write a script on the most remote server to process and forward requests. At the stage of forwarding requests to the site (like google.com or youtube.com ) errors occur, if you run the program on a local computer (127.0.0.0:65432) , you will get the error [WinError 10054] , but if you run it on VDS, you will get the error [Errno 104] Connection reset by peer . However, in both cases, a response with an error of 400 is received from the end resources.

    Proxy:

    [​IMG]

    VPS:

    [​IMG]

    Local:

    [​IMG]

    Here's my code, I've changed it many times (because I'm just starting out and I make a lot of mistakes, but I can't handle this one)

    import socket
    import sys
    import threading


    HOST = "127.0.0.1" # Standard loopback interface address (localhost)
    PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
    BUFFER_SIZE = 10000


    def main():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind((HOST, PORT))
    s.listen(10)
    print(f"Подняли сервер на порту: {PORT}")

    while True:
    try:
    conn, addr = s.accept()
    d = threading.Thread(target=conn_string, args=(conn, addr))
    d.setDaemon(True)
    d.start()
    except Exception as e:
    print(e)
    sys.exit(1)
    s.close()


    def conn_string(conn, addr):
    try:
    data = conn.recv(BUFFER_SIZE)
    #print(f"Connection: {conn}")
    server = data.decode("utf-8")
    first_line = server.split('\n')[0]
    url = first_line.split(" ")[1]
    http_pos = url.find("://")
    if http_pos==-1:
    temp=url
    else:
    temp = url[(http_pos+3):]
    port_pos = temp.find(":")
    webserver_pos = temp.find("/")
    if webserver_pos == -1:
    webserver_pos = len(temp)

    webserver =""
    port = -1
    if (port_pos == -1 or webserver_pos < port_pos):
    port = 80
    webserver = temp[:webserver_pos]
    else:
    port = int((temp[(port_pos+1):])[:webserver_pos-port_pos-1])
    webserver = temp[:port_pos]
    decode_data = decode(data)
    #print(f"Address: {addr}")
    #print(f"Data: {data}")
    #print(f"Webserver: {webserver}")
    proxy_server(webserver, port, conn, addr, decode_data)
    except Exception as e:
    print(e)
    conn.close()
    sys.exit(2)


    def decode(data):
    return bytes(data)


    def get_vpn():
    return ("127.0.0.1", 12345)


    def proxy_server(url, host_port, conn, addr, decode_data):
    print(url, host_port)
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.settimeout(60.0)
    client.connect((url, int(host_port)))
    client.send(decode_data)
    while True:
    response = client.recv(BUFFER_SIZE)
    print(response.decode('utf-8'))
    if len(response.decode('utf-8')) > 0:
    conn.send(response)
    dar = float(len(response))
    dar = float(dar/1024)
    dar = f"{dar} KB"
    print(f"[*] Requests Done: {addr[0]} => {dar} <=")
    else:
    break
    print(f"Закрыл соединение с {url}")
    client.close()
    conn.close()


    main()

    Continue reading...

Compartilhe esta Página