mirror of
https://github.com/ayflying/p2p.git
synced 2026-03-04 17:29:22 +00:00
服务端节点会给客户端追加公网ip
This commit is contained in:
@@ -178,6 +178,7 @@ func (s *sP2P) register() error {
|
||||
addrs[i] = addr.String()
|
||||
}
|
||||
|
||||
ports, _ := s.getLocalTCPPorts(s.client.host)
|
||||
// 构建注册消息
|
||||
msg := GatewayMessage{
|
||||
Type: MsgTypeRegister,
|
||||
@@ -185,6 +186,7 @@ func (s *sP2P) register() error {
|
||||
Data: gjson.MustEncode(g.Map{
|
||||
"peer_id": s.client.host.ID().String(),
|
||||
"addrs": addrs,
|
||||
"ports": ports,
|
||||
}),
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -104,6 +105,7 @@ func (s *sP2P) handleRegister(ctx context.Context, conn *websocket.Conn, msg *Ga
|
||||
var data struct {
|
||||
PeerID string `json:"peer_id"`
|
||||
Addrs []string `json:"addrs"`
|
||||
Ports []int `json:"ports"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(msg.Data, &data); err != nil {
|
||||
@@ -111,18 +113,20 @@ func (s *sP2P) handleRegister(ctx context.Context, conn *websocket.Conn, msg *Ga
|
||||
return
|
||||
}
|
||||
|
||||
//// 追加公网ip
|
||||
//publicIp, _, _ := net.SplitHostPort(conn.RemoteAddr().String())
|
||||
//ParseIP := net.ParseIP(publicIp)
|
||||
//var ipType string
|
||||
//if ParseIP.To4() != nil {
|
||||
// ipType = "ip4"
|
||||
//} else {
|
||||
// ipType = "ip6"
|
||||
//}
|
||||
// 追加公网ip
|
||||
publicIp, _, _ := net.SplitHostPort(conn.RemoteAddr().String())
|
||||
ParseIP := net.ParseIP(publicIp)
|
||||
var ipType string
|
||||
if ParseIP.To4() != nil {
|
||||
ipType = "ip4"
|
||||
} else {
|
||||
ipType = "ip6"
|
||||
}
|
||||
//port2 := 53533
|
||||
//data.Addrs = append(data.Addrs, fmt.Sprintf("/%s/%s/tcp/%d", ipType, publicIp, port2))
|
||||
//data.Addrs = append(data.Addrs, fmt.Sprintf("/%s/%s/udp/%d/quic-v1", ipType, publicIp, port2))
|
||||
for _, port2 := range data.Ports {
|
||||
data.Addrs = append(data.Addrs, fmt.Sprintf("/%s/%s/tcp/%d", ipType, publicIp, port2))
|
||||
data.Addrs = append(data.Addrs, fmt.Sprintf("/%s/%s/udp/%d/quic-v1", ipType, publicIp, port2))
|
||||
}
|
||||
|
||||
// 过滤回环地址
|
||||
data.Addrs = s.filterLoopbackAddrs(data.Addrs)
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -80,46 +79,6 @@ func init() {
|
||||
ip, _ = service.P2P().GetIPv4PublicIP()
|
||||
}
|
||||
|
||||
// 获取公网IP并判断类型(ipv4/ipv6)
|
||||
func (s *sP2P) getPublicIPAndType() (ip string, ipType string, err error) {
|
||||
// 公网IP查询接口(多个备用)
|
||||
|
||||
client := http.Client{Timeout: 5 * time.Second}
|
||||
for _, api := range ipAPIs {
|
||||
resp, err := client.Get(api)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 读取响应(纯IP字符串)
|
||||
buf := make([]byte, 128)
|
||||
n, err := resp.Body.Read(buf)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
ip = strings.TrimSpace(string(buf[:n]))
|
||||
if ip == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
// 判断IP类型
|
||||
parsedIP := net.ParseIP(ip)
|
||||
if parsedIP == nil {
|
||||
continue // 无效IP格式
|
||||
}
|
||||
|
||||
if parsedIP.To4() != nil {
|
||||
return ip, "ipv4", nil // IPv4
|
||||
} else if parsedIP.To16() != nil {
|
||||
return ip, "ipv6", nil // IPv6
|
||||
}
|
||||
}
|
||||
|
||||
return "", "", fmt.Errorf("所有公网IP查询接口均失败")
|
||||
}
|
||||
|
||||
// 只获取IPv4公网IP(过滤IPv6结果)
|
||||
func (s *sP2P) GetIPv4PublicIP() (string, error) {
|
||||
ctx := gctx.New()
|
||||
|
||||
Reference in New Issue
Block a user