Skip to content

Instantly share code, notes, and snippets.

@giannoug
Created August 1, 2021 22:59
Show Gist options
  • Save giannoug/e11fcdc94be2ca873dc2660b57bcdb37 to your computer and use it in GitHub Desktop.
Save giannoug/e11fcdc94be2ca873dc2660b57bcdb37 to your computer and use it in GitHub Desktop.
Read Tor node's traffic information using Go (can be turned into a telegraf plugin)
package main
//https://gitweb.torproject.org/torspec.git/tree/control-spec.txt
import (
"fmt"
"net"
"net/textproto"
"os"
"time"
"github.com/cretz/bine/control"
)
func main() {
if err := run(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func run() error {
println("Starting")
tconn, err := net.DialTimeout("tcp", "127.0.0.1:9051", 2*time.Minute)
if err != nil {
return err
}
conn := textproto.NewConn(tconn)
controlConn := control.NewConn(conn)
err = controlConn.Authenticate("penis")
if err != nil {
return err
}
info, err := controlConn.GetInfo("version")
if err != nil {
return err
}
fmt.Printf("Got info, %v: %v\n", info[0].Key, info[0].Val)
taswe, err := controlConn.GetInfo("traffic/read", "traffic/written")
if err != nil {
return err
}
fmt.Printf("Got traffic/read, %v: %v\n", taswe[0].Key, taswe[0].Val)
fmt.Printf("Got traffic/written, %v: %v\n", taswe[1].Key, taswe[1].Val)
_ = controlConn.Close()
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment