From fb8c0cac2746317afa7e3b7ee47277dc184f177e Mon Sep 17 00:00:00 2001 From: erinn Date: Tue, 9 Oct 2018 10:14:28 -0700 Subject: [PATCH] tidying up code paths and making detport selection a little better --- application/examples/v3/main.go | 4 ++-- ricochet.go | 2 +- utils/networkresolver.go | 40 +++++++++++++++++++++++---------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/application/examples/v3/main.go b/application/examples/v3/main.go index 0dac8cd..b127f75 100644 --- a/application/examples/v3/main.go +++ b/application/examples/v3/main.go @@ -13,10 +13,10 @@ import ( // An example of how to setup a v3 onion service in go func main() { cpubk, cprivk, _ := ed25519.GenerateKey(rand.Reader) - l, err := application.SetupOnionV3("127.0.0.1:9051", "tcp4", "", cprivk, 9878) + l, err := application.SetupOnionV3("127.0.0.1:9051", "tcp4", "", cprivk, "", 9878) utils.CheckError(err) log.Printf("Got Listener %v", l.Addr().String()) decodedPub, err := base32.StdEncoding.DecodeString(strings.ToUpper(l.Addr().String()[:56])) log.Printf("Decoded Public Key: %x %v", decodedPub[:32], err) log.Printf("ed25519 Public Key: %x", cpubk) -} +} \ No newline at end of file diff --git a/ricochet.go b/ricochet.go index ad9a269..cb74cfb 100644 --- a/ricochet.go +++ b/ricochet.go @@ -5,7 +5,7 @@ import ( "git.openprivacy.ca/openprivacy/libricochet-go/utils" "io" "net" -) + ) // Open establishes a protocol session on an established net.Conn, and returns a new // OpenConnection instance representing this connection. On error, the connection diff --git a/utils/networkresolver.go b/utils/networkresolver.go index f5c064f..0e7be34 100644 --- a/utils/networkresolver.go +++ b/utils/networkresolver.go @@ -5,6 +5,8 @@ import ( "golang.org/x/net/proxy" "net" "strings" + "fmt" + "log" ) const ( @@ -54,28 +56,42 @@ func (nr *NetworkResolver) Resolve(hostname string) (net.Conn, string, error) { conn, err := torDialer.Dial("tcp", resolvedHostname+".onion:9878") if err != nil { - NewNym("127.0.0.1:9051", "tcp4", "", 9878) - conn, err = torDialer.Dial("tcp", resolvedHostname+".onion:9878") + torc, err := bulb.Dial("tcp4", "127.0.0.1:9051") + if err != nil { + log.Printf("%v\n", err) + return nil, "", err + } + err = torc.Authenticate("") if err != nil { return nil, "", err } + + NewNym(torc) + conn, err = torDialer.Dial("tcp", resolvedHostname+".onion:9878") + return nil, "", err } return conn, resolvedHostname, nil } + +func GetTorVersion(c *bulb.Conn) (string, error) { + resp, err := c.Request("GETINFO version") + if err != nil { + fmt.Printf("error getting tor version: %v\n", err) + return "", nil + } + if len(resp.Data) > 0 { + return resp.Data[0], nil + } + return "", nil +} + // runs SIGNAL NEWNYM on the tor control port to flush the onion descriptors cache -func NewNym(torControlAddress string, torControlSocketType string, authentication string, onionport uint16) error { - c, err := bulb.Dial(torControlSocketType, torControlAddress) +func NewNym(c *bulb.Conn) error { + _, err := c.Request("SIGNAL NEWNYM") if err != nil { - return err + c.Close() } - - err = c.Authenticate(authentication) - if err != nil { - return err - } - - _, err = c.Request("SIGNAL NEWNYM") return err }