scherzma / Skunk

P2P Tor Chat App
GNU General Public License v3.0
4 stars 0 forks source link

Script to Start Tor for a Peer #14

Closed JavaHammes closed 4 months ago

JavaHammes commented 6 months ago

We need to make it so that a peer can automatically connect to the Tor network and route its traffic through it, and that it can be found as a hidden service on the tor network.

The script should have the functionality to create an onion service directory, as well as to configure which port tor should run on and on which port the hidden service should run. All this requires a torrc file that configures the things mentioned. The script could have an interface that offers the following functionalities:

config := TorConfig{
    TorrcPath:         "TORRCPATH",
    OnionServiceDir:   "ONIONSERVICEDIR",
    HiddenServicePort: "PORT",
}

hiddenSericeHostname, err := startTor(&config)
if err != nil {
   log.Fatal("Error starting tor %w", err)
}

stopTor()

The startTor function could then offer the following functionalities

func startTor(config *TorConfig) (string, error) {
    err := createOnionServiceDir(config.OnionServiceDir)
    if err != nil {
        return nil, err
    }

    err := writeTorrc(&config)
    if err != nil {
        return nil, err
    }

    cmd := exec.Command("tor", "-f", config.TorrcPath)
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr

    err := cmd.Start()
    if err != nil {
        return nil, err
    }

    hostname, err := readOnionHostname(config.OnionServiceDir)
    if err  != nil {
        return nil, err
    }
    return hostname, nil
}

Of course, management of the started process should also be available in order to be able to stop the Tor process again with stopTor()

However, there are some problems when it comes to testability. The problem is that each peer must have its own onion service directory and tor can only be started once on a machine. So it would probably be smart to write a separate script for testing purposes that automatically creates the required onion service directories and reads the information for each peer from them. However, this is really only needed if it is specifically about testing the Tor functionalities of a peer. Theoretically, since a peer also works fine over the clearnet, many of the peer's functionalities can probably be tested without the darknet connections.