diff --git a/cmd/reseed.go b/cmd/reseed.go index 8b95fab..e28fc92 100644 --- a/cmd/reseed.go +++ b/cmd/reseed.go @@ -2,15 +2,18 @@ package cmd import ( "fmt" + "io/ioutil" "log" "net" + "os" "runtime" + "strconv" "time" - "strconv" "github.com/MDrollette/i2p-tools/reseed" - "github.com/cretz/bine/tor" "github.com/codegangsta/cli" + "github.com/cretz/bine/tor" + "github.com/cretz/bine/torutil/ed25519" ) func NewReseedCommand() cli.Command { @@ -31,6 +34,15 @@ func NewReseedCommand() cli.Command { Name: "onion", Usage: "Present an onionv3 address", }, + cli.BoolFlag{ + Name: "singleOnion", + Usage: "Use a faster, but non-anonymous single-hop onion", + }, + cli.StringFlag{ + Name: "onionKey", + Value: "onion.key", + Usage: "Specify a path to an ed25519 private key for onion", + }, cli.StringFlag{ Name: "key", Usage: "Path to your su3 signing private key", @@ -186,13 +198,49 @@ func reseedAction(c *cli.Context) { }() } - if c.Bool("onion") { - port, err := strconv.Atoi(c.String("port")) - if err != nil { - log.Fatalln(err.Error()) - } - log.Fatalln(server.ListenAndServeOnion(nil, &tor.ListenConf{LocalPort: port, RemotePorts: []int{80}})) - }else if tlsHost != "" && tlsCert != "" && tlsKey != "" { + if c.Bool("onion") { + port, err := strconv.Atoi(c.String("port")) + if err != nil { + log.Fatalln(err.Error()) + } + if _, err := os.Stat(c.String("onionKey")); err == nil { + ok, err := ioutil.ReadFile(c.String("onionKey")) + if err != nil { + log.Fatalln(err.Error()) + } else { + log.Fatalln( + server.ListenAndServeOnion( + nil, + &tor.ListenConf{ + LocalPort: port, + Key: ed25519.PrivateKey(ok), + RemotePorts: []int{80}, + Version3: true, + NonAnonymous: c.Bool("singleOnion"), + DiscardKey: false, + }, + c.String("onionKey"), + ), + ) + } + } else if os.IsNotExist(err) { + log.Fatalln( + server.ListenAndServeOnion( + nil, + &tor.ListenConf{ + LocalPort: port, + RemotePorts: []int{80}, + Version3: true, + NonAnonymous: c.Bool("singleOnion"), + DiscardKey: false, + }, + c.String("onionKey"), + ), + ) + } else { + + } + } else if tlsHost != "" && tlsCert != "" && tlsKey != "" { log.Printf("HTTPS server started on %s\n", server.Addr) log.Fatalln(server.ListenAndServeTLS(tlsCert, tlsKey)) } else { diff --git a/reseed/server.go b/reseed/server.go index 82d1ef3..687042e 100644 --- a/reseed/server.go +++ b/reseed/server.go @@ -5,6 +5,7 @@ import ( "context" "crypto/tls" "io" + "io/ioutil" "log" "net" "net/http" @@ -13,6 +14,7 @@ import ( "time" "github.com/cretz/bine/tor" + "github.com/cretz/bine/torutil/ed25519" "github.com/gorilla/handlers" "github.com/justinas/alice" "gopkg.in/throttled/throttled.v2" @@ -114,7 +116,7 @@ func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error { return srv.Serve(tlsListener) } -func (srv *Server) ListenAndServeOnion(startConf *tor.StartConf, listenConf *tor.ListenConf) error { +func (srv *Server) ListenAndServeOnion(startConf *tor.StartConf, listenConf *tor.ListenConf, onionKey string) error { log.Println("Starting and registering onion service, please wait a couple of minutes...") tor, err := tor.Start(nil, startConf) if err != nil { @@ -128,6 +130,10 @@ func (srv *Server) ListenAndServeOnion(startConf *tor.StartConf, listenConf *tor if err != nil { return err } + err = ioutil.WriteFile(onionKey, []byte(srv.OnionListener.Key.(ed25519.KeyPair).PrivateKey()), 0644) + if err != nil { + return err + } log.Printf("Onionv3 server started on http://%v.onion\n", srv.OnionListener.ID) return srv.Serve(srv.OnionListener) }