0

First post here as I've just started using Tor. I have a server running on a machine with a hidden service on port 50001. I'm trying to connect to it from my laptop using the server hidden service onion address and send it json objects to retrieve data. I am able to do this fine over my local network using sockets (not using tor or onion address) but haven't been able to get it to work over tor.

I'm able to create a tor instance but I'm trying to use sockets with the 127.0.0.1:9050 proxy but how do I specify the onion address I want to communicate with? The onion address doesn't contain an IP that sockets needs. So not sure how to do this. Showing what I have so far below.

socks_proxy = ('127.0.0.1', 9050)

onion_service_address = 'my server onion address'
onion_service_port = 50001

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    # Connect to the Tor SOCKS proxy
    s.connect(socks_proxy)

    # Send the Tor-specific handshake to establish a connection
    s.send(b'\x05\x01\x00')

    # Receive the response to the handshake
    response = s.recv(2)

    # Check if the response indicates a successful connection
    if response == b'\x05\x00':
        # JSON request to send to the onion service
        json_data= {"id": 1, "jsonrpc": "2.0", "method": method, "params": []}

        # Construct the onion service address with its port
        onion_service_address_with_port = f"{onion_service_address}:{onion_service_port}"

        # Connect to the onion service through the Tor SOCKS proxy
        s.send(f'\x05\x01\x00\x03{len(onion_service_address)}{onion_service_address_with_port}'.encode())

        # Receive the response to the connection request
        response = s.recv(10)

        # Check if the response indicates a successful connection
        if response == b'\x05\x00\x00\x03':
            # Serialize the JSON data to bytes
            json_bytes = json.dumps(json_data).encode("utf-8")

            # Send the JSON data
            s.send(json_bytes)

            # Receive and print the response
            response = s.recv(4096)
            print(response.decode("utf-8"))

        else:
            print("Failed to connect to the onion service.")

    else:
        print("Failed to connect to the Tor SOCKS proxy.")

0

You must log in to answer this question.

Browse other questions tagged .