From 77bfa4517279a5e1a35b6206bc4199a3abf088ce Mon Sep 17 00:00:00 2001 From: TenderIronh Date: Thu, 21 Nov 2024 10:31:07 +0800 Subject: [PATCH] portmap loss & android ipv6 failed & public ip detect --- .../main/java/cn/openp2p/OpenP2PService.kt | 3 ++- core/config.go | 2 +- core/nat.go | 26 +++++++++++++++---- core/protocol.go | 8 +++++- core/underlay.go | 2 +- 5 files changed, 32 insertions(+), 9 deletions(-) diff --git a/app/app/src/main/java/cn/openp2p/OpenP2PService.kt b/app/app/src/main/java/cn/openp2p/OpenP2PService.kt index 6660496..88de756 100644 --- a/app/app/src/main/java/cn/openp2p/OpenP2PService.kt +++ b/app/app/src/main/java/cn/openp2p/OpenP2PService.kt @@ -203,7 +203,8 @@ class OpenP2PService : VpnService() { val network = Network(id, name, gateway, nodeList) println(network) Log.i(OpenP2PService.LOG_TAG, "onBind"); - builder.addDnsServer("8.8.8.8") + builder.addDnsServer("223.5.5.5") + builder.addDnsServer("2400:3200::1") // alicloud dns v6 & v4 builder.addRoute("10.2.3.0", 24) // builder.addRoute("0.0.0.0", 0); builder.setSession(LOG_TAG!!) diff --git a/core/config.go b/core/config.go index 8f6a8c7..15ad725 100644 --- a/core/config.go +++ b/core/config.go @@ -239,7 +239,7 @@ func (c *Config) delete(app AppConfig) { defer c.save() for i := 0; i < len(c.Apps); i++ { if (app.SrcPort != 0 && c.Apps[i].Protocol == app.Protocol && c.Apps[i].SrcPort == app.SrcPort) || // normal app - (app.SrcPort == 0 && c.Apps[i].PeerNode == app.PeerNode) { // memapp + (app.SrcPort == 0 && c.Apps[i].SrcPort == 0 && c.Apps[i].PeerNode == app.PeerNode) { // memapp if i == len(c.Apps)-1 { c.Apps = c.Apps[:i] } else { diff --git a/core/nat.go b/core/nat.go index 2a4e3de..3748d25 100644 --- a/core/nat.go +++ b/core/nat.go @@ -66,7 +66,7 @@ func natTest(serverHost string, serverPort int, localPort int) (publicIP string, } // The connection can write data to the desired address. - msg, err := newMessage(MsgNATDetect, 0, nil) + msg, err := newMessage(MsgNATDetect, MsgNAT, nil) _, err = conn.WriteTo(msg, dst) if err != nil { return "", 0, err @@ -164,17 +164,33 @@ func publicIPTest(publicIP string, echoPort int) (hasPublicIP int, hasUPNPorNATP break } defer conn.Close() - dst, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", publicIP, echoPort)) + dst, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", gConf.Network.ServerHost, gConf.Network.ServerPort)) if err != nil { break } - conn.WriteTo([]byte("echo"), dst) + + // The connection can write data to the desired address. + msg, _ := newMessage(MsgNATDetect, MsgPublicIP, NatDetectReq{EchoPort: echoPort}) + _, err = conn.WriteTo(msg, dst) + if err != nil { + return + } buf := make([]byte, 1600) // wait for echo testing conn.SetReadDeadline(time.Now().Add(PublicIPEchoTimeout)) - _, _, err = conn.ReadFromUDP(buf) - if err == nil { + nRead, _, err := conn.ReadFromUDP(buf) + if err != nil { + gLog.Println(LvERROR, "PublicIP detect error:", err) + break + } + natRsp := NatDetectRsp{} + err = json.Unmarshal(buf[openP2PHeaderSize:nRead], &natRsp) + if err != nil { + gLog.Println(LvERROR, "PublicIP detect error:", err) + break + } + if natRsp.Port == echoPort { if i == 1 { gLog.Println(LvDEBUG, "UPNP or NAT-PMP:YES") hasUPNPorNATPMP = 1 diff --git a/core/protocol.go b/core/protocol.go index c9ca1bd..6519ab2 100644 --- a/core/protocol.go +++ b/core/protocol.go @@ -10,7 +10,7 @@ import ( "time" ) -const OpenP2PVersion = "3.21.8" +const OpenP2PVersion = "3.21.10" const ProductName string = "openp2p" const LeastSupportVersion = "3.0.0" const SyncServerTimeVersion = "3.9.0" @@ -217,6 +217,12 @@ const ( MsgSDWANInfoRsp ) +// MsgNATDetect +const ( + MsgNAT = iota + MsgPublicIP +) + func newMessage(mainType uint16, subType uint16, packet interface{}) ([]byte, error) { data, err := json.Marshal(packet) if err != nil { diff --git a/core/underlay.go b/core/underlay.go index 5bb9997..0c0a42f 100644 --- a/core/underlay.go +++ b/core/underlay.go @@ -27,7 +27,7 @@ func DefaultReadBuffer(ul underlay) (*openP2PHeader, []byte, error) { return nil, nil, err } head, err := decodeHeader(headBuf) - if err != nil { + if err != nil || head.MainType > 16 { return nil, nil, err } dataBuf := make([]byte, head.DataLen)