mirror of
https://github.com/ayflying/p2p.git
synced 2026-03-04 17:29:22 +00:00
关闭公共节点的链接方式,只连接本地私有节点
This commit is contained in:
@@ -2,15 +2,13 @@ package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/ayflying/p2p/internal/controller/p2p"
|
||||
"github.com/ayflying/p2p/internal/service"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gcmd"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/os/gtimer"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -32,47 +30,68 @@ var (
|
||||
g.Log().Debug(ctx, "开始执行main")
|
||||
|
||||
parser, err = gcmd.Parse(g.MapStrBool{
|
||||
"w,ws": true,
|
||||
"g,gateway": true,
|
||||
"p,port": true,
|
||||
"p,port": true,
|
||||
})
|
||||
addr := g.Cfg().MustGet(ctx, "ws.address").String()
|
||||
ws := parser.GetOpt("ws", addr).String()
|
||||
//port := parser.GetOpt("port", 0).Int()
|
||||
port := parser.GetOpt("port", "23333").Int()
|
||||
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(ghttp.MiddlewareHandlerResponse)
|
||||
group.Bind(
|
||||
p2p.NewV1(),
|
||||
)
|
||||
})
|
||||
h, _ := service.P2P().CreateLibp2pHost(gctx.New(), port)
|
||||
err = service.P2P().DHTStart(h, nil)
|
||||
if err != nil {
|
||||
g.Log().Error(ctx, err)
|
||||
}
|
||||
|
||||
//启动p2p服务端网关
|
||||
s.Group("/ws", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(ghttp.MiddlewareHandlerResponse)
|
||||
err = service.P2P().GatewayStart(ctx, group)
|
||||
if err != nil {
|
||||
g.Log().Error(ctx, err)
|
||||
}
|
||||
})
|
||||
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)
|
||||
if err = service.P2P().StoreToDHT(gctx.New(), validKey, dataValue); err != nil {
|
||||
g.Log().Debugf(ctx, "❌ 存储失败: %v\n", err)
|
||||
} else {
|
||||
g.Log().Debugf(ctx, "✅ 存储成功\nKey: %s\nValue: %s\n", validKey, dataValue)
|
||||
}
|
||||
|
||||
//s.SetPort(port)
|
||||
|
||||
// 延迟启动
|
||||
gtimer.SetTimeout(ctx, time.Second*5, func(ctx context.Context) {
|
||||
g.Log().Debug(ctx, "开始执行客户端")
|
||||
// 启动p2p客户端
|
||||
err = service.P2P().Start(ctx, ws)
|
||||
|
||||
g.Log().Debugf(ctx, "当前监听端口:%v", s.GetListenedPort())
|
||||
//addrs, _ := net.InterfaceAddrs()
|
||||
//for _, addr := range addrs {
|
||||
// if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && ipnet.IP.To4() != nil {
|
||||
// g.Log().Infof(ctx, "访问地址:http://%v:%d", ipnet.IP.String(), s.GetListenedPort())
|
||||
// }
|
||||
//}
|
||||
|
||||
})
|
||||
//parser, err = gcmd.Parse(g.MapStrBool{
|
||||
// "w,ws": true,
|
||||
// "g,gateway": true,
|
||||
// "p,port": true,
|
||||
//})
|
||||
//addr := g.Cfg().MustGet(ctx, "ws.address").String()
|
||||
//ws := parser.GetOpt("ws", addr).String()
|
||||
////port := parser.GetOpt("port", 0).Int()
|
||||
//
|
||||
//s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
// group.Middleware(ghttp.MiddlewareHandlerResponse)
|
||||
// group.Bind(
|
||||
// p2p.NewV1(),
|
||||
// )
|
||||
//})
|
||||
//
|
||||
////启动p2p服务端网关
|
||||
//s.Group("/ws", func(group *ghttp.RouterGroup) {
|
||||
// group.Middleware(ghttp.MiddlewareHandlerResponse)
|
||||
// err = service.P2P().GatewayStart(ctx, group)
|
||||
// if err != nil {
|
||||
// g.Log().Error(ctx, err)
|
||||
// }
|
||||
//})
|
||||
//
|
||||
////s.SetPort(port)
|
||||
//
|
||||
//// 延迟启动
|
||||
//gtimer.SetTimeout(ctx, time.Second*5, func(ctx context.Context) {
|
||||
// g.Log().Debug(ctx, "开始执行客户端")
|
||||
// // 启动p2p客户端
|
||||
// err = service.P2P().Start(ws)
|
||||
//
|
||||
// g.Log().Debugf(ctx, "当前监听端口:%v", s.GetListenedPort())
|
||||
// //addrs, _ := net.InterfaceAddrs()
|
||||
// //for _, addr := range addrs {
|
||||
// // if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && ipnet.IP.To4() != nil {
|
||||
// // g.Log().Infof(ctx, "访问地址:http://%v:%d", ipnet.IP.String(), s.GetListenedPort())
|
||||
// // }
|
||||
// //}
|
||||
//
|
||||
//})
|
||||
|
||||
s.Run()
|
||||
return nil
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -77,12 +77,12 @@ var (
|
||||
//addr := "/ip4/192.168.50.173/tcp/51888/p2p/12D3KooWJKBB9bF9MjqgsFYUUsPBG249FDq7a3ZdaYc9iw8G78JQ"
|
||||
//addrs := "WyIvaXA0LzEyNy4wLjAuMS90Y3AvNTE4ODgiLCIvaXA0LzE5Mi4xNjguNTAuMTczL3RjcC81MTg4OCJd"
|
||||
wsStr := "ws://192.168.50.173:51888/ws"
|
||||
err = service.P2P().Start(ctx, wsStr)
|
||||
err = service.P2P().Start(wsStr)
|
||||
case "dht":
|
||||
g.Log().Debug(ctx, "开始执行dht")
|
||||
h, _ := service.P2P().CreateLibp2pHost(ctx, 0)
|
||||
h, _ := service.P2P().CreateLibp2pHost(ctx, 23333)
|
||||
|
||||
err := service.P2P().DHTStart(ctx, h, nil)
|
||||
err := service.P2P().DHTStart(h, nil)
|
||||
if err != nil {
|
||||
g.Log().Error(ctx, err)
|
||||
}
|
||||
@@ -98,17 +98,23 @@ var (
|
||||
|
||||
case "dht2":
|
||||
g.Log().Debug(ctx, "开始执行dht2")
|
||||
h, _ := service.P2P().CreateLibp2pHost(ctx, 0)
|
||||
h, _ := service.P2P().CreateLibp2pHost(ctx, 23333)
|
||||
|
||||
addr := []string{
|
||||
//"/ip4/192.168.50.173/tcp/23333/p2p/12D3KooWQsb1137nCzqbMMCzwHsyU8aaCZeFnBUBTkWVsfp8gs26",
|
||||
//"/ip4/192.168.50.173/udp/23333/quic-v1/p2p/12D3KooWQsb1137nCzqbMMCzwHsyU8aaCZeFnBUBTkWVsfp8gs26",
|
||||
//"/ip4/114.132.176.115/tcp/23333/p2p/12D3KooWJQMiYyptqSrx4PPsGLY9hjLbaDdxmBXmGtKmSWuiP79D",
|
||||
//"/ip4/114.132.176.115/udp/23333/quic-v1/p2p/12D3KooWJQMiYyptqSrx4PPsGLY9hjLbaDdxmBXmGtKmSWuiP79D",
|
||||
}
|
||||
//addr := []string{
|
||||
// "/ip4/192.168.50.243/tcp/23333/p2p/12D3KooWESZtrm6AfqhC3oj5FsAbcSmePwHFFip3F2MPExrxHxwy",
|
||||
// "/ip4/192.168.50.243/udp/23333/quic-v1/p2p/12D3KooWESZtrm6AfqhC3oj5FsAbcSmePwHFFip3F2MPExrxHxwy",
|
||||
//
|
||||
// "/ip4/192.168.50.173/tcp/23333/p2p/12D3KooWKgW8WxncYzZ2h5erMbK3GfLGhNHFapPvhUc1KVmdZeRg",
|
||||
// "/ip4/192.168.50.173/udp/23333/quic-v1/p2p/12D3KooWKgW8WxncYzZ2h5erMbK3GfLGhNHFapPvhUc1KVmdZeRg",
|
||||
//
|
||||
// //"/ip4/192.168.50.173/tcp/23333/p2p/12D3KooWQsb1137nCzqbMMCzwHsyU8aaCZeFnBUBTkWVsfp8gs26",
|
||||
// //"/ip4/192.168.50.173/udp/23333/quic-v1/p2p/12D3KooWQsb1137nCzqbMMCzwHsyU8aaCZeFnBUBTkWVsfp8gs26",
|
||||
// //"/ip4/114.132.176.115/tcp/23333/p2p/12D3KooWJQMiYyptqSrx4PPsGLY9hjLbaDdxmBXmGtKmSWuiP79D",
|
||||
// //"/ip4/114.132.176.115/udp/23333/quic-v1/p2p/12D3KooWJQMiYyptqSrx4PPsGLY9hjLbaDdxmBXmGtKmSWuiP79D",
|
||||
//}
|
||||
|
||||
id := gcmd.GetOpt("id").String()
|
||||
err := service.P2P().DHTStart(ctx, h, addr)
|
||||
err := service.P2P().DHTStart(h, nil)
|
||||
if err != nil {
|
||||
g.Log().Error(ctx, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user