torgo: github.com/wybiral/torgo Index | Examples | Files

package torgo

import "github.com/wybiral/torgo"

Package torgo provides a Golang controller interface for Tor.

Index

Examples

Package Files

client.go controller.go ed25519.go onion.go rsa.go torgo.go

func NewClient Uses

func NewClient(addr string) (*http.Client, error)

NewClient return new HTTP client that uses a Tor SOCKS proxy. `addr` is the host:port address of SOCKS proxy (usually "127.0.0.1:9050")

Create a new Tor SOCKS HTTP client and request current IP from httpbin.org.

Code:

// Create client from SOCKS proxy address
client, err := torgo.NewClient("127.0.0.1:9050")
if err != nil {
    log.Fatal(err)
}
// Perform HTTP GET request
resp, err := client.Get("http://httpbin.org/ip")
if err != nil {
    log.Fatal(err)
}
// Copy response to Stdout
io.Copy(os.Stdout, resp.Body)

func ServiceIDFromEd25519 Uses

func ServiceIDFromEd25519(pub ed25519.PublicKey) (string, error)

ServiceIDFromEd25519 calculates a Tor service ID from an ed25519.PublicKey.

Calculate Tor service ID from ed25519.PublicKey.

Code:

// Calculate service ID
serviceID, err := torgo.ServiceIDFromEd25519(publicKeyEd25519)
if err != nil {
    log.Fatal(err)
}
fmt.Println(serviceID)

func ServiceIDFromRSA Uses

func ServiceIDFromRSA(pub *rsa.PublicKey) (string, error)

ServiceIDFromRSA calculates a Tor service ID from an *rsa.PublicKey.

Calculate Tor service ID from *rsa.PublicKey.

Code:

// Calculate service ID
serviceID, err := torgo.ServiceIDFromRSA(publicKeyRSA)
if err != nil {
    log.Fatal(err)
}
fmt.Println(serviceID)

type Controller Uses

type Controller struct {
    // Array of available authentication methods.
    AuthMethods []string
    // Cookie file path (empty if not available).
    CookieFile string
    // Text is a textproto.Conn to the control port.
    Text *textproto.Conn
}

A Controller instance is a control port connection that provides methods for communicating with Tor.

func NewController Uses

func NewController(addr string) (*Controller, error)

NewController returns a new Controller instance connecting to the control port at addr.

Return a new Controller interface.

Code:

// Address of control port
addr := "127.0.0.1:9051"
controller, err := torgo.NewController(addr)
if err != nil {
    log.Fatal(err)
}
fmt.Println(controller)

func (*Controller) AddOnion Uses

func (c *Controller) AddOnion(onion *Onion) error

AddOnion adds Onion hidden service. If no private key is supplied one will be generated and the PrivateKeyType and PrivateKey properties will be set with the newly generated one. The hidden service will use port mapping contained in Ports map supplied. ServiceID will be assigned based on the private key and will be the address of this hidden service (without the ".onion" ending).

Add onion and generate private key.

Code:

// Define onion that maps virtual port 80 to local port 8080
onion := &torgo.Onion{Ports: map[int]string{
    80: "127.0.0.1:8080",
}}
// Add onion to controller
err := controller.AddOnion(onion)
if err != nil {
    log.Fatal(err)
}
// Print onion ID (address without ".onion" ending)
fmt.Println(onion.ServiceID)

Add onion and generate private key (using ED25519-V3 key if supported).

Code:

// Define onion that maps virtual port 80 to local port 8080
onion := &torgo.Onion{
    Ports: map[int]string{
        80: "127.0.0.1:8080",
    },
    PrivateKeyType: "NEW",
    PrivateKey:     "ED25519-V3",
}
// Add onion to controller
err := controller.AddOnion(onion)
if err != nil {
    log.Fatal(err)
}
// Print onion ID (address without ".onion" ending)
fmt.Println(onion.ServiceID)

func (*Controller) AuthenticateCookie Uses

func (c *Controller) AuthenticateCookie() error

AuthenticateCookie authenticate to controller with cookie from current CookieFile path.

Authenticate with cookie file.

Code:

err := controller.AuthenticateCookie()
if err != nil {
    log.Fatal(err)
}

func (*Controller) AuthenticateNone Uses

func (c *Controller) AuthenticateNone() error

AuthenticateNone authenticate to controller without password or cookie.

Authenticate without password or cookie file.

Code:

err := controller.AuthenticateNone()
if err != nil {
    log.Fatal(err)
}

func (*Controller) AuthenticatePassword Uses

func (c *Controller) AuthenticatePassword(password string) error

AuthenticatePassword authenticate to controller with password.

Authenticate with password.

Code:

err := controller.AuthenticatePassword("pa55w0rd")
if err != nil {
    log.Fatal(err)
}

func (*Controller) DeleteOnion Uses

func (c *Controller) DeleteOnion(serviceID string) error

DeleteOnion deletes an onion by its serviceID (stop hidden service created by this controller).

Delete an onion by its ServiceID

Code:

err := controller.DeleteOnion(onion.ServiceID)
if err != nil {
    log.Fatal(err)
}

func (*Controller) GetAddress Uses

func (c *Controller) GetAddress() (string, error)

GetAddress returns the current external IP address.

Print external IP address.

Code:

address, err := controller.GetAddress()
if err != nil {
    log.Fatal(err)
}
fmt.Println(address)

func (*Controller) GetBytesRead Uses

func (c *Controller) GetBytesRead() (int, error)

GetBytesRead returns total bytes downloaded.

Print total bytes read (downloaded).

Code:

n, err := controller.GetBytesRead()
if err != nil {
    log.Fatal(err)
}
fmt.Println(n)

func (*Controller) GetBytesWritten Uses

func (c *Controller) GetBytesWritten() (int, error)

GetBytesWritten returns total bytes uploaded.

Print total bytes written (uploaded).

Code:

n, err := controller.GetBytesWritten()
if err != nil {
    log.Fatal(err)
}
fmt.Println(n)

func (*Controller) GetConfigFile Uses

func (c *Controller) GetConfigFile() (string, error)

GetConfigFile return path to Tor config file.

Print path to Tor configuration file.

Code:

config, err := controller.GetConfigFile()
if err != nil {
    log.Fatal(err)
}
fmt.Println(config)

func (*Controller) GetTorPid Uses

func (c *Controller) GetTorPid() (int, error)

GetTorPid returns PID for current Tor process.

Print PID of Tor process.

Code:

pid, err := controller.GetTorPid()
if err != nil {
    log.Fatal(err)
}
fmt.Println(pid)

func (*Controller) GetVersion Uses

func (c *Controller) GetVersion() (string, error)

GetVersion returns version of Tor server.

Return version of Tor server.

Code:

version, err := controller.GetVersion()
if err != nil {
    log.Fatal(err)
}
fmt.Println(version)

func (*Controller) Signal Uses

func (c *Controller) Signal(signal string) error

Signal sends a signal to the server. Tor documentations defines the following signals :

* RELOAD
* SHUTDOWN
* DUMP
* DEBUG
* HALT
* CLEARDNSCACHE
* NEWNYM
* HEARTBEAT
* DORMANT
* ACTIVE

Send NEWSYM signal to switch to new clean circuits.

Code:

err := controller.Signal("NEWNYM")
if err != nil {
    log.Fatal(err)
}

Send signal to reload configuration.

Code:

err := controller.Signal("RELOAD")
if err != nil {
    log.Fatal(err)
}

type Onion Uses

type Onion struct {
    // Ports maps virtual ports for the hidden service to local addresses.
    Ports map[int]string
    // ServiceID is the unique hidden service address (without ".onion" ending).
    ServiceID string
    // Base64 encoded private key for the hidden service.
    PrivateKey string
    // Type of private key (RSA1024 or ED25519-V3).
    PrivateKeyType string
}

Onion represents a hidden service.

func OnionFromEd25519 Uses

func OnionFromEd25519(pri ed25519.PrivateKey) (*Onion, error)

OnionFromEd25519 returns an Onion instance from an ED25519 private key which can be used to start a hidden service with controller.AddOnion.

Create an Onion and start hidden service from an ed25519.PrivateKey.

Code:

// Create Onion from private key (does not start hidden service)
onion, err := torgo.OnionFromEd25519(privateKeyEd25519)
if err != nil {
    log.Fatal(err)
}
// Set port mapping from hidden service 80 to localhost:8080
onion.Ports[80] = "localhost:8080"
// Print service ID for Onion
fmt.Println(onion.ServiceID)
// Start hidden service
controller.AddOnion(onion)

func OnionFromRSA Uses

func OnionFromRSA(pri *rsa.PrivateKey) (*Onion, error)

OnionFromRSA returns an Onion instance from a 1024 bit RSA private key which can be used to start a hidden service with controller.AddOnion.

Create an Onion and start hidden service from an rsa.PrivateKey.

Code:

// Create Onion from private key (does not start hidden service)
onion, err := torgo.OnionFromRSA(privateKeyRSA)
if err != nil {
    log.Fatal(err)
}
// Set port mapping from hidden service 80 to localhost:8080
onion.Ports[80] = "localhost:8080"
// Print service ID for Onion
fmt.Println(onion.ServiceID)
// Start hidden service
controller.AddOnion(onion)

Package torgo imports 19 packages (graph) and is imported by 3 packages. Updated 2019-04-13. Refresh now. Tools for package owners.