ipsn / go-libtor

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

How is it possible to preserve .onion address #19

Closed simonfrey closed 4 years ago

simonfrey commented 4 years ago

As I would like to keep the same tor .onion address even after restart of the server, I would need to preserve the private key. Digging trough go-libtor & bine I did not find out how to do it. Could you plese be so kind and explain me with a code example how to do that?

Thanks!

aayushsinha44 commented 4 years ago

While generating onion url you have to provide the private key. If you preserve the onion address by supplying same key.

Here _key is your key in string format. key, err := ParseRsaPrivateKeyFromPemStr(_key) onion, err = t.Listen(ctx, &tor.ListenConf{RemotePorts: []int{80}, LocalPort: goServerPort, Key: key})

For generating private key and storing it as string for later reference.

func GenerateKey() string { key, err := rsa.GenerateKey(rand.Reader, 1024) if err != nil { return err.Error() } return ExportRsaPrivateKeyAsPemStr(key) }

func ExportRsaPrivateKeyAsPemStr(privkey *rsa.PrivateKey) string { privkey_bytes := x509.MarshalPKCS1PrivateKey(privkey) privkey_pem := pem.EncodeToMemory( &pem.Block{ Type: "RSA PRIVATE KEY", Bytes: privkey_bytes, }, ) return string(privkey_pem) }

func ParseRsaPrivateKeyFromPemStr(privPEM string) (*rsa.PrivateKey, error) { block, _ := pem.Decode([]byte(privPEM)) if block == nil { return nil, errors.New("failed to parse PEM block containing the key") } priv, err := x509.ParsePKCS1PrivateKey(block.Bytes) if err != nil { return nil, err } return priv, nil }

qwertyqop commented 2 years ago

after trying to write a tor V3 address to the _key variable, i was having some issues _key = "== ed25519v1-secret: ".... it returns: "panic: runtime error: invalid memory address or nil pointer dereference" then it segfaults. is there a similar function to ParseRsaPrivateKeyFromPemStr but for V3 addresses? i couldn't find anything in the documentation