登录
首页 >  Golang >  Go问答

Golang中实现的持久化隐蔽服务

来源:stackoverflow

时间:2024-02-07 15:36:24 151浏览 收藏

今天golang学习网给大家带来了《Golang中实现的持久化隐蔽服务》,其中涉及到的知识点包括等等,无论你是小白还是老手,都适合看一看哦~有好的建议也欢迎大家在评论留言,若是看完有所收获,也希望大家能多多点赞支持呀!一起加油学习~

问题内容

我正在尝试使用 github.com/cretz/bine/tor 在 golang 中托管一个隐藏服务。 但每次我启动程序时,都会启动一个新的隐藏服务(带有新的 .onion 地址),而不是之前的隐藏服务。

这是我使用的代码

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)
    }
}


我尝试强制程序使用相同的 DataDir

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

但这行不通。


正确答案


您需要创建并存储一个密钥,然后将其传递给 ListenConf:

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

注意:

您正在使用的库提供了执行此操作的函数。

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

执行此操作一次并保存 privateKeyString

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

然后在您的服务器代码中:

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

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>