Skip to content

Commit

Permalink
portmap loss & android ipv6 failed & public ip detect
Browse files Browse the repository at this point in the history
  • Loading branch information
TenderIronh committed Nov 21, 2024
1 parent 3616768 commit 77bfa45
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
3 changes: 2 additions & 1 deletion app/app/src/main/java/cn/openp2p/OpenP2PService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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!!)
Expand Down
2 changes: 1 addition & 1 deletion core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 21 additions & 5 deletions core/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion core/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion core/underlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 77bfa45

Please sign in to comment.