Home  >  Article  >  Backend Development  >  Persistent hidden services in golang

Persistent hidden services in golang

WBOY
WBOYforward
2024-02-09 12:33:09884browse

Persistent hidden services in golang

php editor Xinyi will take you to explore the persistent hidden services in golang. Golang is an efficient programming language known for its excellent performance and concurrency. In this fast-paced Internet era, hidden services have become an important part of many applications. They provide a safe and reliable way to keep applications running for long periods of time while hiding their internal details. This article will introduce how to implement persistent hidden services in golang, and explore its application scenarios and advantages in practical applications. Whether you are a beginner or an experienced developer, you will gain useful knowledge and practical experience. Let’s explore this interesting topic together!

Question content

I'm trying to host a hidden service in golang using github.com/cretz/bine/tor. But every time I start the program, a new hidden service (with a new .onion address) is started instead of the previous hidden service.

This is the code I use

package main

import (
    "context"
    "fmt"
    "github.com/cretz/bine/tor"
    "log"
    "net/http"
    "time"
)

func main() {
    // Start tor with default config
    fmt.Println("Starting and registering onion service, please wait a couple of minutes...")
    t, err := tor.Start(nil, nil)
    if err != nil {
        log.Panicf("Unable to start Tor: %v", err)
    }
    defer t.Close()
    // Wait at most a few minutes to publish the service
    listenCtx, listenCancel := context.WithTimeout(context.Background(), 3*time.Minute)
    defer listenCancel()
    // Create a v3 onion service
    onion, err := t.Listen(listenCtx, &tor.ListenConf{Version3: true, RemotePorts: []int{80}})
    if err != nil {
        log.Panicf("Unable to create onion service: %v", err)
    }
    defer onion.Close()
    fmt.Printf("Open Tor browser and navigate to http://%v.onion\n", onion.ID)
    fmt.Println("Press enter to exit")
    // Serve the current folder from HTTP
    errCh := make(chan error, 1)
    go func() { errCh <- http.Serve(onion, http.FileServer(http.Dir("."))) }()
    // End when enter is pressed
    go func() {
        fmt.Scanln()
        errCh <- nil
    }()
    if err = <-errCh; err != nil {
        log.Panicf("Failed serving: %v", err)
    }
}


I tried forcing the program to use the same DataDir

t, err := tor.Start(nil, &tor.StartConf{DataDir: "data-dir"})

But this doesn't work.

Workaround

You need to create and store a key and then pass it to ListenConf:

See: https://github.com/cretz /bine/blob/master/tor/listen.go#L56

Notice:

The library you are using provides functions to do this.

See: https://github.com /cretz/bine/blob/b9d31d9c786616742e39a121b60522e803e96731/torutil/ed25519/ed25519.go#L132

Do this once and save privateKeyString:

keyPair, _ := ed25519.GenerateKey()
privateKeyString := hex.EncodeToString(keyPair.PrivateKey())

Then in your server code:

privateKeyBytes, _ := hex.DecodeString(privateKeyString)
privateKey := ed25519.PrivateKey(privateKeyBytes)
lc := &tor.ListenConf{
    Key: privateKey,
    Version3: true,
    RemotePorts: []int{80}
}

The above is the detailed content of Persistent hidden services in golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete