关闭公共节点的链接方式,只连接本地私有节点

This commit is contained in:
2025-10-21 18:47:30 +08:00
parent ab2f099826
commit f4bf11c136
8 changed files with 222 additions and 137 deletions

View File

@@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"time"
"github.com/ayflying/p2p/internal/service"
"github.com/gogf/gf/v2/frame/g"
@@ -28,15 +29,16 @@ var (
parser, err = gcmd.Parse(g.MapStrBool{
"p,port": true,
})
port := parser.GetOpt("port", "0").Int()
port := parser.GetOpt("port", "23333").Int()
h, _ := service.P2P().CreateLibp2pHost(ctx, port)
err = service.P2P().DHTStart(ctx, h, nil)
err = service.P2P().DHTStart(h, nil)
if err != nil {
g.Log().Error(ctx, err)
}
go func() {
time.Sleep(5 * time.Second)
publicIp, _ := service.P2P().GetIPv4PublicIP()
validKey := fmt.Sprintf("%v/ip", h.ID())
dataValue := fmt.Sprintf("来自节点 %s 的数据:%v", h.ID().ShortString(), publicIp)