ipsn / go-libtor

Self-contained Tor from Go
BSD 3-Clause "New" or "Revised" License
544 stars 46 forks source link

How to keep same onion url ? #14

Closed aayushsinha44 closed 4 years ago

aayushsinha44 commented 4 years ago

Each time i run the code, i get different onion url. How to keep the onion url same?

Code:

package demo

import ( "context" "fmt" "log" "net/http" "os" "time"

"github.com/cretz/bine/process"
"github.com/cretz/bine/tor"
"github.com/ipsn/go-libtor"

)

var creator = libtor.Creator

type LibTorWrapper struct{}

func (LibTorWrapper) New(ctx context.Context, args ...string) (process.Process, error) { return creator.New(ctx, args...) }

// Run starts up an embedded Tor process, starts a hidden onion service on a new // goroutine and returns the onion address. We're cheating here and not caring // about actually cleaning up after ourselves. func Run(datadir string) string { // Start tor with some defaults + elevated verbosity fmt.Println("Starting and registering onion service, please wait a bit...") t, err := tor.Start(nil, &tor.StartConf{ProcessCreator: LibTorWrapper{}, DebugWriter: os.Stderr, DataDir: datadir}) if err != nil { log.Panicf("Failed to start tor: %v", err) } // Wait at most a few minutes to publish the service ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) defer cancel()

// Create an onion service to listen on any port but show as 80
onion, err := t.Listen(ctx, &tor.ListenConf{RemotePorts: []int{80}})
if err != nil {
    log.Panicf("Failed to create onion service: %v", err)
}
fmt.Printf("Please open a Tor capable browser and navigate to http://%v.onion\n", onion.ID)

// Run a Hello-World HTTP service until terminated
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, Tor! This is Android!!!")
})
go http.Serve(onion, nil)

return fmt.Sprintf("http://%v.onion", onion.ID)

}

karalabe commented 4 years ago

You can check how I did it in https://github.com/ipsn/go-torfluxdb/blob/master/proxy/proxy.go#L26

TL;DR: In tor.ListenConf you can specify the private key to use, which will cause the same URL to be generated every time. Without the key, Tor just generates a new ephemeral random key that it discards afterwards.