mirror of
https://github.com/openp2p-cn/openp2p.git
synced 2026-05-08 15:03:32 +08:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e21adebc26 | ||
|
|
b2a7619bd6 | ||
|
|
fe4022ba6c | ||
|
|
82c74b4f85 | ||
|
|
0af65b7204 | ||
|
|
46b4f78010 |
@@ -100,5 +100,8 @@ sudo /usr/local/openp2p/openp2p uninstall
|
||||
|
||||
## Docker运行
|
||||
```
|
||||
# 把YOUR-TOKEN和YOUR-NODE-NAME替换成自己的
|
||||
docker run -d --net host --name openp2p-client -e OPENP2P_TOKEN=YOUR-TOKEN -e OPENP2P_NODE=YOUR-NODE-NAME openp2pcn/openp2p-client:latest
|
||||
OR
|
||||
docker run -d --net host --name openp2p-client openp2pcn/openp2p-client:latest -token YOUR-TOKEN -node YOUR-NODE-NAME
|
||||
```
|
||||
|
||||
3
USAGE.md
3
USAGE.md
@@ -102,5 +102,8 @@ sudo /usr/local/openp2p/openp2p uninstall
|
||||
|
||||
## Run with Docker
|
||||
```
|
||||
# Replace YOUR-TOKEN and YOUR-NODE-NAME with yours
|
||||
docker run -d --net host --name openp2p-client -e OPENP2P_TOKEN=YOUR-TOKEN -e OPENP2P_NODE=YOUR-NODE-NAME openp2pcn/openp2p-client:latest
|
||||
OR
|
||||
docker run -d --net host --name openp2p-client openp2pcn/openp2p-client:latest -token YOUR-TOKEN -node YOUR-NODE-NAME
|
||||
```
|
||||
@@ -137,8 +137,7 @@ func netInfo() *NetInfo {
|
||||
continue
|
||||
}
|
||||
rsp := NetInfo{}
|
||||
err = json.Unmarshal(buf[:n], &rsp)
|
||||
if err != nil {
|
||||
if err = json.Unmarshal(buf[:n], &rsp); err != nil {
|
||||
gLog.Printf(LvERROR, "wrong NetInfo:%s", err)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -3,8 +3,10 @@ package openp2p
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -13,14 +15,15 @@ var gConf Config
|
||||
|
||||
type AppConfig struct {
|
||||
// required
|
||||
AppName string
|
||||
Protocol string
|
||||
SrcPort int
|
||||
PeerNode string
|
||||
DstPort int
|
||||
DstHost string
|
||||
PeerUser string
|
||||
Enabled int // default:1
|
||||
AppName string
|
||||
Protocol string
|
||||
Whitelist string
|
||||
SrcPort int
|
||||
PeerNode string
|
||||
DstPort int
|
||||
DstHost string
|
||||
PeerUser string
|
||||
Enabled int // default:1
|
||||
// runtime info
|
||||
peerVersion string
|
||||
peerToken uint64
|
||||
@@ -41,6 +44,10 @@ type AppConfig struct {
|
||||
isUnderlayServer int // TODO: bool?
|
||||
}
|
||||
|
||||
func (c *AppConfig) ID() string {
|
||||
return fmt.Sprintf("%s%d", c.Protocol, c.SrcPort)
|
||||
}
|
||||
|
||||
// TODO: add loglevel, maxlogfilesize
|
||||
type Config struct {
|
||||
Network NetworkConfig `json:"network"`
|
||||
@@ -201,6 +208,7 @@ func parseParams(subCommand string) {
|
||||
node := fset.String("node", "", "node name. 8-31 characters. if not set, it will be hostname")
|
||||
peerNode := fset.String("peernode", "", "peer node name that you want to connect")
|
||||
dstIP := fset.String("dstip", "127.0.0.1", "destination ip ")
|
||||
whiteList := fset.String("whitelist", "", "whitelist for p2pApp ")
|
||||
dstPort := fset.Int("dstport", 0, "destination port ")
|
||||
srcPort := fset.Int("srcport", 0, "source port ")
|
||||
tcpPort := fset.Int("tcpport", 0, "tcp port for upnp or publicip")
|
||||
@@ -210,7 +218,7 @@ func parseParams(subCommand string) {
|
||||
daemonMode := fset.Bool("d", false, "daemonMode")
|
||||
notVerbose := fset.Bool("nv", false, "not log console")
|
||||
newconfig := fset.Bool("newconfig", false, "not load existing config.json")
|
||||
logLevel := fset.Int("loglevel", 0, "0:info 1:warn 2:error 3:debug")
|
||||
logLevel := fset.Int("loglevel", 1, "0:debug 1:info 2:warn 3:error")
|
||||
if subCommand == "" { // no subcommand
|
||||
fset.Parse(os.Args[1:])
|
||||
} else {
|
||||
@@ -220,6 +228,7 @@ func parseParams(subCommand string) {
|
||||
config := AppConfig{Enabled: 1}
|
||||
config.PeerNode = *peerNode
|
||||
config.DstHost = *dstIP
|
||||
config.Whitelist = *whiteList
|
||||
config.DstPort = *dstPort
|
||||
config.SrcPort = *srcPort
|
||||
config.Protocol = *protocol
|
||||
@@ -253,17 +262,17 @@ func parseParams(subCommand string) {
|
||||
gConf.setToken(*token)
|
||||
}
|
||||
})
|
||||
|
||||
// set default value
|
||||
if gConf.Network.ServerHost == "" {
|
||||
gConf.Network.ServerHost = *serverHost
|
||||
}
|
||||
if *node != "" {
|
||||
if len(*node) < MinNodeNameLen {
|
||||
gLog.Println(LvERROR, ErrNodeTooShort)
|
||||
os.Exit(9)
|
||||
}
|
||||
gConf.Network.Node = *node
|
||||
} else {
|
||||
envNode := os.Getenv("OPENP2P_NODE")
|
||||
if envNode != "" {
|
||||
gConf.Network.Node = envNode
|
||||
}
|
||||
if gConf.Network.Node == "" { // if node name not set. use os.Hostname
|
||||
gConf.Network.Node = defaultNodeName()
|
||||
}
|
||||
@@ -275,7 +284,14 @@ func parseParams(subCommand string) {
|
||||
}
|
||||
gConf.Network.TCPPort = *tcpPort
|
||||
}
|
||||
|
||||
if *token == 0 {
|
||||
envToken := os.Getenv("OPENP2P_TOKEN")
|
||||
if envToken != "" {
|
||||
if n, err := strconv.ParseUint(envToken, 10, 64); n != 0 && err == nil {
|
||||
gConf.setToken(n)
|
||||
}
|
||||
}
|
||||
}
|
||||
gConf.Network.ServerPort = *serverPort
|
||||
gConf.Network.UDPPort1 = UDPPort1
|
||||
gConf.Network.UDPPort2 = UDPPort2
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/openp2p-cn/totp"
|
||||
@@ -22,62 +23,10 @@ func handlePush(pn *P2PNetwork, subType uint16, msg []byte) error {
|
||||
gLog.Printf(LvDEBUG, "handle push msg type:%d, push header:%+v", subType, pushHead)
|
||||
switch subType {
|
||||
case MsgPushConnectReq: // TODO: handle a msg move to a new function
|
||||
req := PushConnectReq{}
|
||||
err := json.Unmarshal(msg[openP2PHeaderSize+PushHeaderSize:], &req)
|
||||
if err != nil {
|
||||
gLog.Printf(LvERROR, "wrong MsgPushConnectReq:%s", err)
|
||||
return err
|
||||
}
|
||||
gLog.Printf(LvDEBUG, "%s is connecting...", req.From)
|
||||
gLog.Println(LvDEBUG, "push connect response to ", req.From)
|
||||
if compareVersion(req.Version, LeastSupportVersion) == LESS {
|
||||
gLog.Println(LvERROR, ErrVersionNotCompatible.Error(), ":", req.From)
|
||||
rsp := PushConnectRsp{
|
||||
Error: 10,
|
||||
Detail: ErrVersionNotCompatible.Error(),
|
||||
To: req.From,
|
||||
From: pn.config.Node,
|
||||
}
|
||||
pn.push(req.From, MsgPushConnectRsp, rsp)
|
||||
return ErrVersionNotCompatible
|
||||
}
|
||||
// verify totp token or token
|
||||
t := totp.TOTP{Step: totp.RelayTOTPStep}
|
||||
if t.Verify(req.Token, pn.config.Token, time.Now().Unix()-pn.dt) { // localTs may behind, auto adjust ts
|
||||
gLog.Printf(LvINFO, "Access Granted\n")
|
||||
config := AppConfig{}
|
||||
config.peerNatType = req.NatType
|
||||
config.peerConeNatPort = req.ConeNatPort
|
||||
config.peerIP = req.FromIP
|
||||
config.PeerNode = req.From
|
||||
config.peerVersion = req.Version
|
||||
config.fromToken = req.Token
|
||||
config.peerIPv6 = req.IPv6
|
||||
config.hasIPv4 = req.HasIPv4
|
||||
config.hasUPNPorNATPMP = req.HasUPNPorNATPMP
|
||||
config.linkMode = req.LinkMode
|
||||
config.isUnderlayServer = req.IsUnderlayServer
|
||||
// share relay node will limit bandwidth
|
||||
if req.Token != pn.config.Token {
|
||||
gLog.Printf(LvINFO, "set share bandwidth %d mbps", pn.config.ShareBandwidth)
|
||||
config.shareBandwidth = pn.config.ShareBandwidth
|
||||
}
|
||||
// go pn.AddTunnel(config, req.ID)
|
||||
go pn.addDirectTunnel(config, req.ID)
|
||||
break
|
||||
}
|
||||
gLog.Println(LvERROR, "Access Denied:", req.From)
|
||||
rsp := PushConnectRsp{
|
||||
Error: 1,
|
||||
Detail: fmt.Sprintf("connect to %s error: Access Denied", pn.config.Node),
|
||||
To: req.From,
|
||||
From: pn.config.Node,
|
||||
}
|
||||
pn.push(req.From, MsgPushConnectRsp, rsp)
|
||||
err = handleConnectReq(pn, subType, msg)
|
||||
case MsgPushRsp:
|
||||
rsp := PushRsp{}
|
||||
err := json.Unmarshal(msg[openP2PHeaderSize:], &rsp)
|
||||
if err != nil {
|
||||
if err = json.Unmarshal(msg[openP2PHeaderSize:], &rsp); err != nil {
|
||||
gLog.Printf(LvERROR, "wrong pushRsp:%s", err)
|
||||
return err
|
||||
}
|
||||
@@ -88,9 +37,8 @@ func handlePush(pn *P2PNetwork, subType uint16, msg []byte) error {
|
||||
}
|
||||
case MsgPushAddRelayTunnelReq:
|
||||
req := AddRelayTunnelReq{}
|
||||
err := json.Unmarshal(msg[openP2PHeaderSize+PushHeaderSize:], &req)
|
||||
if err != nil {
|
||||
gLog.Printf(LvERROR, "wrong RelayNodeRsp:%s", err)
|
||||
if err = json.Unmarshal(msg[openP2PHeaderSize+PushHeaderSize:], &req); err != nil {
|
||||
gLog.Printf(LvERROR, "wrong %v:%s", reflect.TypeOf(req), err)
|
||||
return err
|
||||
}
|
||||
config := AppConfig{}
|
||||
@@ -106,9 +54,8 @@ func handlePush(pn *P2PNetwork, subType uint16, msg []byte) error {
|
||||
}(req)
|
||||
case MsgPushAPPKey:
|
||||
req := APPKeySync{}
|
||||
err := json.Unmarshal(msg[openP2PHeaderSize+PushHeaderSize:], &req)
|
||||
if err != nil {
|
||||
gLog.Printf(LvERROR, "wrong APPKeySync:%s", err)
|
||||
if err = json.Unmarshal(msg[openP2PHeaderSize+PushHeaderSize:], &req); err != nil {
|
||||
gLog.Printf(LvERROR, "wrong %v:%s", reflect.TypeOf(req), err)
|
||||
return err
|
||||
}
|
||||
SaveKey(req.AppID, req.AppKey)
|
||||
@@ -133,123 +80,16 @@ func handlePush(pn *P2PNetwork, subType uint16, msg []byte) error {
|
||||
os.Exit(0)
|
||||
return err
|
||||
case MsgPushReportApps:
|
||||
gLog.Println(LvINFO, "MsgPushReportApps")
|
||||
req := ReportApps{}
|
||||
gConf.mtx.Lock()
|
||||
defer gConf.mtx.Unlock()
|
||||
for _, config := range gConf.Apps {
|
||||
appActive := 0
|
||||
relayNode := ""
|
||||
relayMode := ""
|
||||
linkMode := LinkModeUDPPunch
|
||||
i, ok := pn.apps.Load(fmt.Sprintf("%s%d", config.Protocol, config.SrcPort))
|
||||
if ok {
|
||||
app := i.(*p2pApp)
|
||||
if app.isActive() {
|
||||
appActive = 1
|
||||
}
|
||||
relayNode = app.relayNode
|
||||
relayMode = app.relayMode
|
||||
linkMode = app.tunnel.linkModeWeb
|
||||
}
|
||||
appInfo := AppInfo{
|
||||
AppName: config.AppName,
|
||||
Error: config.errMsg,
|
||||
Protocol: config.Protocol,
|
||||
SrcPort: config.SrcPort,
|
||||
RelayNode: relayNode,
|
||||
RelayMode: relayMode,
|
||||
LinkMode: linkMode,
|
||||
PeerNode: config.PeerNode,
|
||||
DstHost: config.DstHost,
|
||||
DstPort: config.DstPort,
|
||||
PeerUser: config.PeerUser,
|
||||
PeerIP: config.peerIP,
|
||||
PeerNatType: config.peerNatType,
|
||||
RetryTime: config.retryTime.Local().Format("2006-01-02T15:04:05-0700"),
|
||||
ConnectTime: config.connectTime.Local().Format("2006-01-02T15:04:05-0700"),
|
||||
IsActive: appActive,
|
||||
Enabled: config.Enabled,
|
||||
}
|
||||
req.Apps = append(req.Apps, appInfo)
|
||||
}
|
||||
pn.write(MsgReport, MsgReportApps, &req)
|
||||
err = handleReportApps(pn, subType, msg)
|
||||
case MsgPushReportLog:
|
||||
gLog.Println(LvDEBUG, "MsgPushReportLog")
|
||||
req := ReportLogReq{}
|
||||
err := json.Unmarshal(msg[openP2PHeaderSize:], &req)
|
||||
if err != nil {
|
||||
gLog.Printf(LvERROR, "wrong MsgPushReportLog:%s %s", err, string(msg[openP2PHeaderSize:]))
|
||||
return err
|
||||
}
|
||||
if req.FileName == "" {
|
||||
req.FileName = "openp2p.log"
|
||||
}
|
||||
f, err := os.Open(filepath.Join("log", req.FileName))
|
||||
if err != nil {
|
||||
gLog.Println(LvERROR, "read log file error:", err)
|
||||
break
|
||||
}
|
||||
fi, err := f.Stat()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
if req.Offset == 0 && fi.Size() > 4096 {
|
||||
req.Offset = fi.Size() - 4096
|
||||
}
|
||||
if req.Len <= 0 {
|
||||
req.Len = 4096
|
||||
}
|
||||
f.Seek(req.Offset, 0)
|
||||
if req.Len > 1024*1024 { // too large
|
||||
break
|
||||
}
|
||||
buff := make([]byte, req.Len)
|
||||
readLength, err := f.Read(buff)
|
||||
f.Close()
|
||||
if err != nil {
|
||||
gLog.Println(LvERROR, "read log content error:", err)
|
||||
break
|
||||
}
|
||||
rsp := ReportLogRsp{}
|
||||
rsp.Content = string(buff[:readLength])
|
||||
rsp.FileName = req.FileName
|
||||
rsp.Total = fi.Size()
|
||||
rsp.Len = req.Len
|
||||
pn.write(MsgReport, MsgPushReportLog, &rsp)
|
||||
err = handleLog(pn, subType, msg)
|
||||
case MsgPushEditApp:
|
||||
gLog.Println(LvINFO, "MsgPushEditApp")
|
||||
newApp := AppInfo{}
|
||||
err := json.Unmarshal(msg[openP2PHeaderSize:], &newApp)
|
||||
if err != nil {
|
||||
gLog.Printf(LvERROR, "wrong MsgPushEditApp:%s %s", err, string(msg[openP2PHeaderSize:]))
|
||||
return err
|
||||
}
|
||||
oldConf := AppConfig{Enabled: 1}
|
||||
// protocol0+srcPort0 exist, delApp
|
||||
oldConf.AppName = newApp.AppName
|
||||
oldConf.Protocol = newApp.Protocol0
|
||||
oldConf.SrcPort = newApp.SrcPort0
|
||||
oldConf.PeerNode = newApp.PeerNode
|
||||
oldConf.DstHost = newApp.DstHost
|
||||
oldConf.DstPort = newApp.DstPort
|
||||
|
||||
gConf.delete(oldConf)
|
||||
// AddApp
|
||||
newConf := oldConf
|
||||
newConf.Protocol = newApp.Protocol
|
||||
newConf.SrcPort = newApp.SrcPort
|
||||
gConf.add(newConf, false)
|
||||
pn.DeleteApp(oldConf) // DeleteApp may cost some times, execute at the end
|
||||
// autoReconnect will auto AddApp
|
||||
// pn.AddApp(config)
|
||||
// TODO: report result
|
||||
err = handleEditApp(pn, subType, msg)
|
||||
case MsgPushEditNode:
|
||||
gLog.Println(LvINFO, "MsgPushEditNode")
|
||||
req := EditNode{}
|
||||
err := json.Unmarshal(msg[openP2PHeaderSize:], &req)
|
||||
if err != nil {
|
||||
gLog.Printf(LvERROR, "wrong MsgPushEditNode:%s %s", err, string(msg[openP2PHeaderSize:]))
|
||||
if err = json.Unmarshal(msg[openP2PHeaderSize:], &req); err != nil {
|
||||
gLog.Printf(LvERROR, "wrong %v:%s %s", reflect.TypeOf(req), err, string(msg[openP2PHeaderSize:]))
|
||||
return err
|
||||
}
|
||||
gConf.setNode(req.NewName)
|
||||
@@ -259,9 +99,8 @@ func handlePush(pn *P2PNetwork, subType uint16, msg []byte) error {
|
||||
case MsgPushSwitchApp:
|
||||
gLog.Println(LvINFO, "MsgPushSwitchApp")
|
||||
app := AppInfo{}
|
||||
err := json.Unmarshal(msg[openP2PHeaderSize:], &app)
|
||||
if err != nil {
|
||||
gLog.Printf(LvERROR, "wrong MsgPushSwitchApp:%s %s", err, string(msg[openP2PHeaderSize:]))
|
||||
if err = json.Unmarshal(msg[openP2PHeaderSize:], &app); err != nil {
|
||||
gLog.Printf(LvERROR, "wrong %v:%s %s", reflect.TypeOf(app), err, string(msg[openP2PHeaderSize:]))
|
||||
return err
|
||||
}
|
||||
config := AppConfig{Enabled: app.Enabled, SrcPort: app.SrcPort, Protocol: app.Protocol}
|
||||
@@ -273,19 +112,195 @@ func handlePush(pn *P2PNetwork, subType uint16, msg []byte) error {
|
||||
}
|
||||
case MsgPushDstNodeOnline:
|
||||
gLog.Println(LvINFO, "MsgPushDstNodeOnline")
|
||||
app := PushDstNodeOnline{}
|
||||
err := json.Unmarshal(msg[openP2PHeaderSize:], &app)
|
||||
if err != nil {
|
||||
gLog.Printf(LvERROR, "wrong MsgPushDstNodeOnline:%s %s", err, string(msg[openP2PHeaderSize:]))
|
||||
req := PushDstNodeOnline{}
|
||||
if err = json.Unmarshal(msg[openP2PHeaderSize:], &req); err != nil {
|
||||
gLog.Printf(LvERROR, "wrong %v:%s %s", reflect.TypeOf(req), err, string(msg[openP2PHeaderSize:]))
|
||||
return err
|
||||
}
|
||||
gLog.Println(LvINFO, "retry peerNode ", app.Node)
|
||||
gConf.retryApp(app.Node)
|
||||
gLog.Println(LvINFO, "retry peerNode ", req.Node)
|
||||
gConf.retryApp(req.Node)
|
||||
default:
|
||||
pn.msgMapMtx.Lock()
|
||||
ch := pn.msgMap[pushHead.From]
|
||||
pn.msgMapMtx.Unlock()
|
||||
ch <- msg
|
||||
}
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
func handleEditApp(pn *P2PNetwork, subType uint16, msg []byte) (err error) {
|
||||
gLog.Println(LvINFO, "MsgPushEditApp")
|
||||
newApp := AppInfo{}
|
||||
if err = json.Unmarshal(msg[openP2PHeaderSize:], &newApp); err != nil {
|
||||
gLog.Printf(LvERROR, "wrong %v:%s %s", reflect.TypeOf(newApp), err, string(msg[openP2PHeaderSize:]))
|
||||
return err
|
||||
}
|
||||
oldConf := AppConfig{Enabled: 1}
|
||||
// protocol0+srcPort0 exist, delApp
|
||||
oldConf.AppName = newApp.AppName
|
||||
oldConf.Protocol = newApp.Protocol0
|
||||
oldConf.Whitelist = newApp.Whitelist
|
||||
oldConf.SrcPort = newApp.SrcPort0
|
||||
oldConf.PeerNode = newApp.PeerNode
|
||||
oldConf.DstHost = newApp.DstHost
|
||||
oldConf.DstPort = newApp.DstPort
|
||||
|
||||
gConf.delete(oldConf)
|
||||
// AddApp
|
||||
newConf := oldConf
|
||||
newConf.Protocol = newApp.Protocol
|
||||
newConf.SrcPort = newApp.SrcPort
|
||||
gConf.add(newConf, false)
|
||||
pn.DeleteApp(oldConf) // DeleteApp may cost some times, execute at the end
|
||||
return nil
|
||||
// autoReconnect will auto AddApp
|
||||
// pn.AddApp(config)
|
||||
// TODO: report result
|
||||
}
|
||||
|
||||
func handleConnectReq(pn *P2PNetwork, subType uint16, msg []byte) (err error) {
|
||||
req := PushConnectReq{}
|
||||
if err = json.Unmarshal(msg[openP2PHeaderSize+PushHeaderSize:], &req); err != nil {
|
||||
gLog.Printf(LvERROR, "wrong %v:%s", reflect.TypeOf(req), err)
|
||||
return err
|
||||
}
|
||||
gLog.Printf(LvDEBUG, "%s is connecting...", req.From)
|
||||
gLog.Println(LvDEBUG, "push connect response to ", req.From)
|
||||
if compareVersion(req.Version, LeastSupportVersion) == LESS {
|
||||
gLog.Println(LvERROR, ErrVersionNotCompatible.Error(), ":", req.From)
|
||||
rsp := PushConnectRsp{
|
||||
Error: 10,
|
||||
Detail: ErrVersionNotCompatible.Error(),
|
||||
To: req.From,
|
||||
From: pn.config.Node,
|
||||
}
|
||||
pn.push(req.From, MsgPushConnectRsp, rsp)
|
||||
return ErrVersionNotCompatible
|
||||
}
|
||||
// verify totp token or token
|
||||
t := totp.TOTP{Step: totp.RelayTOTPStep}
|
||||
if t.Verify(req.Token, pn.config.Token, time.Now().Unix()-pn.dt/int64(time.Second)) { // localTs may behind, auto adjust ts
|
||||
gLog.Printf(LvINFO, "Access Granted\n")
|
||||
config := AppConfig{}
|
||||
config.peerNatType = req.NatType
|
||||
config.peerConeNatPort = req.ConeNatPort
|
||||
config.peerIP = req.FromIP
|
||||
config.PeerNode = req.From
|
||||
config.peerVersion = req.Version
|
||||
config.fromToken = req.Token
|
||||
config.peerIPv6 = req.IPv6
|
||||
config.hasIPv4 = req.HasIPv4
|
||||
config.hasUPNPorNATPMP = req.HasUPNPorNATPMP
|
||||
config.linkMode = req.LinkMode
|
||||
config.isUnderlayServer = req.IsUnderlayServer
|
||||
// share relay node will limit bandwidth
|
||||
if req.Token != pn.config.Token {
|
||||
gLog.Printf(LvINFO, "set share bandwidth %d mbps", pn.config.ShareBandwidth)
|
||||
config.shareBandwidth = pn.config.ShareBandwidth
|
||||
}
|
||||
// go pn.AddTunnel(config, req.ID)
|
||||
go pn.addDirectTunnel(config, req.ID)
|
||||
return nil
|
||||
}
|
||||
gLog.Println(LvERROR, "Access Denied:", req.From)
|
||||
rsp := PushConnectRsp{
|
||||
Error: 1,
|
||||
Detail: fmt.Sprintf("connect to %s error: Access Denied", pn.config.Node),
|
||||
To: req.From,
|
||||
From: pn.config.Node,
|
||||
}
|
||||
return pn.push(req.From, MsgPushConnectRsp, rsp)
|
||||
}
|
||||
|
||||
func handleReportApps(pn *P2PNetwork, subType uint16, msg []byte) (err error) {
|
||||
gLog.Println(LvINFO, "MsgPushReportApps")
|
||||
req := ReportApps{}
|
||||
gConf.mtx.Lock()
|
||||
defer gConf.mtx.Unlock()
|
||||
for _, config := range gConf.Apps {
|
||||
appActive := 0
|
||||
relayNode := ""
|
||||
relayMode := ""
|
||||
linkMode := LinkModeUDPPunch
|
||||
i, ok := pn.apps.Load(config.ID())
|
||||
if ok {
|
||||
app := i.(*p2pApp)
|
||||
if app.isActive() {
|
||||
appActive = 1
|
||||
}
|
||||
relayNode = app.relayNode
|
||||
relayMode = app.relayMode
|
||||
linkMode = app.tunnel.linkModeWeb
|
||||
}
|
||||
appInfo := AppInfo{
|
||||
AppName: config.AppName,
|
||||
Error: config.errMsg,
|
||||
Protocol: config.Protocol,
|
||||
Whitelist: config.Whitelist,
|
||||
SrcPort: config.SrcPort,
|
||||
RelayNode: relayNode,
|
||||
RelayMode: relayMode,
|
||||
LinkMode: linkMode,
|
||||
PeerNode: config.PeerNode,
|
||||
DstHost: config.DstHost,
|
||||
DstPort: config.DstPort,
|
||||
PeerUser: config.PeerUser,
|
||||
PeerIP: config.peerIP,
|
||||
PeerNatType: config.peerNatType,
|
||||
RetryTime: config.retryTime.Local().Format("2006-01-02T15:04:05-0700"),
|
||||
ConnectTime: config.connectTime.Local().Format("2006-01-02T15:04:05-0700"),
|
||||
IsActive: appActive,
|
||||
Enabled: config.Enabled,
|
||||
}
|
||||
req.Apps = append(req.Apps, appInfo)
|
||||
}
|
||||
return pn.write(MsgReport, MsgReportApps, &req)
|
||||
}
|
||||
|
||||
func handleLog(pn *P2PNetwork, subType uint16, msg []byte) (err error) {
|
||||
gLog.Println(LvDEBUG, "MsgPushReportLog")
|
||||
const defaultLen = 1024 * 128
|
||||
const maxLen = 1024 * 1024
|
||||
req := ReportLogReq{}
|
||||
if err = json.Unmarshal(msg[openP2PHeaderSize:], &req); err != nil {
|
||||
gLog.Printf(LvERROR, "wrong %v:%s %s", reflect.TypeOf(req), err, string(msg[openP2PHeaderSize:]))
|
||||
return err
|
||||
}
|
||||
if req.FileName == "" {
|
||||
req.FileName = "openp2p.log"
|
||||
}
|
||||
f, err := os.Open(filepath.Join("log", req.FileName))
|
||||
if err != nil {
|
||||
gLog.Println(LvERROR, "read log file error:", err)
|
||||
return err
|
||||
}
|
||||
fi, err := f.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if req.Offset > fi.Size() {
|
||||
req.Offset = fi.Size() - defaultLen
|
||||
}
|
||||
// verify input parameters
|
||||
if req.Offset < 0 {
|
||||
req.Offset = 0
|
||||
}
|
||||
if req.Len <= 0 || req.Len > maxLen {
|
||||
req.Len = defaultLen
|
||||
}
|
||||
|
||||
f.Seek(req.Offset, 0)
|
||||
buff := make([]byte, req.Len)
|
||||
readLength, err := f.Read(buff)
|
||||
f.Close()
|
||||
if err != nil {
|
||||
gLog.Println(LvERROR, "read log content error:", err)
|
||||
return err
|
||||
}
|
||||
rsp := ReportLogRsp{}
|
||||
rsp.Content = string(buff[:readLength])
|
||||
rsp.FileName = req.FileName
|
||||
rsp.Total = fi.Size()
|
||||
rsp.Len = req.Len
|
||||
return pn.write(MsgReport, MsgPushReportLog, &rsp)
|
||||
}
|
||||
|
||||
155
core/iptree.go
Normal file
155
core/iptree.go
Normal file
@@ -0,0 +1,155 @@
|
||||
package openp2p
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/emirpasic/gods/trees/avltree"
|
||||
"github.com/emirpasic/gods/utils"
|
||||
)
|
||||
|
||||
type IPTree struct {
|
||||
tree *avltree.Tree
|
||||
treeMtx sync.RWMutex
|
||||
}
|
||||
|
||||
// add 120k cost 0.5s
|
||||
func (iptree *IPTree) AddIntIP(minIP uint32, maxIP uint32) bool {
|
||||
if minIP > maxIP {
|
||||
return false
|
||||
}
|
||||
iptree.treeMtx.Lock()
|
||||
defer iptree.treeMtx.Unlock()
|
||||
newMinIP := minIP
|
||||
newMaxIP := maxIP
|
||||
cur := iptree.tree.Root
|
||||
for {
|
||||
if cur == nil {
|
||||
break
|
||||
}
|
||||
curMaxIP := cur.Value.(uint32)
|
||||
curMinIP := cur.Key.(uint32)
|
||||
|
||||
// newNode all in existNode, treat as inserted.
|
||||
if newMinIP >= curMinIP && newMaxIP <= curMaxIP {
|
||||
return true
|
||||
}
|
||||
// has no interset
|
||||
if newMinIP > curMaxIP {
|
||||
cur = cur.Children[1]
|
||||
continue
|
||||
}
|
||||
if newMaxIP < curMinIP {
|
||||
cur = cur.Children[0]
|
||||
continue
|
||||
}
|
||||
// has interset, rm it and Add the new merged ip segment
|
||||
iptree.tree.Remove(curMinIP)
|
||||
if curMinIP < newMinIP {
|
||||
newMinIP = curMinIP
|
||||
}
|
||||
if curMaxIP > newMaxIP {
|
||||
newMaxIP = curMaxIP
|
||||
}
|
||||
cur = iptree.tree.Root
|
||||
}
|
||||
// put in the tree
|
||||
iptree.tree.Put(newMinIP, newMaxIP)
|
||||
return true
|
||||
}
|
||||
|
||||
func (iptree *IPTree) Add(minIPStr string, maxIPStr string) bool {
|
||||
var minIP, maxIP uint32
|
||||
binary.Read(bytes.NewBuffer(net.ParseIP(minIPStr).To4()), binary.BigEndian, &minIP)
|
||||
binary.Read(bytes.NewBuffer(net.ParseIP(maxIPStr).To4()), binary.BigEndian, &maxIP)
|
||||
return iptree.AddIntIP(minIP, maxIP)
|
||||
}
|
||||
|
||||
func (iptree *IPTree) Contains(ipStr string) bool {
|
||||
var ip uint32
|
||||
binary.Read(bytes.NewBuffer(net.ParseIP(ipStr).To4()), binary.BigEndian, &ip)
|
||||
return iptree.ContainsInt(ip)
|
||||
}
|
||||
|
||||
func (iptree *IPTree) ContainsInt(ip uint32) bool {
|
||||
iptree.treeMtx.RLock()
|
||||
defer iptree.treeMtx.RUnlock()
|
||||
if iptree.tree == nil {
|
||||
return false
|
||||
}
|
||||
n := iptree.tree.Root
|
||||
for n != nil {
|
||||
curMaxIP := n.Value.(uint32)
|
||||
curMinIP := n.Key.(uint32)
|
||||
switch {
|
||||
case ip >= curMinIP && ip <= curMaxIP: // hit
|
||||
return true
|
||||
case ip < curMinIP:
|
||||
n = n.Children[0]
|
||||
default:
|
||||
n = n.Children[1]
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (iptree *IPTree) Size() int {
|
||||
iptree.treeMtx.RLock()
|
||||
defer iptree.treeMtx.RUnlock()
|
||||
return iptree.tree.Size()
|
||||
}
|
||||
|
||||
func (iptree *IPTree) Print() {
|
||||
iptree.treeMtx.RLock()
|
||||
defer iptree.treeMtx.RUnlock()
|
||||
log.Println("size:", iptree.Size())
|
||||
log.Println(iptree.tree.String())
|
||||
}
|
||||
|
||||
func (iptree *IPTree) Clear() {
|
||||
iptree.treeMtx.Lock()
|
||||
defer iptree.treeMtx.Unlock()
|
||||
iptree.tree.Clear()
|
||||
}
|
||||
|
||||
// input format 127.0.0.1,192.168.1.0/24,10.1.1.30-10.1.1.50
|
||||
// 127.0.0.1
|
||||
// 192.168.1.0/24
|
||||
// 192.168.1.1-192.168.1.10
|
||||
func NewIPTree(ips string) *IPTree {
|
||||
iptree := &IPTree{
|
||||
tree: avltree.NewWith(utils.UInt32Comparator),
|
||||
}
|
||||
ipArr := strings.Split(ips, ",")
|
||||
for _, ip := range ipArr {
|
||||
if strings.Contains(ip, "/") { // x.x.x.x/24
|
||||
_, ipNet, err := net.ParseCIDR(ip)
|
||||
if err != nil {
|
||||
fmt.Println("Error parsing CIDR:", err)
|
||||
continue
|
||||
}
|
||||
minIP := ipNet.IP.Mask(ipNet.Mask).String()
|
||||
maxIP := calculateMaxIP(ipNet).String()
|
||||
iptree.Add(minIP, maxIP)
|
||||
} else if strings.Contains(ip, "-") { // x.x.x.x-y.y.y.y
|
||||
minAndMax := strings.Split(ip, "-")
|
||||
iptree.Add(minAndMax[0], minAndMax[1])
|
||||
} else { // single ip
|
||||
iptree.Add(ip, ip)
|
||||
}
|
||||
}
|
||||
return iptree
|
||||
}
|
||||
func calculateMaxIP(ipNet *net.IPNet) net.IP {
|
||||
maxIP := make(net.IP, len(ipNet.IP))
|
||||
copy(maxIP, ipNet.IP)
|
||||
for i := range maxIP {
|
||||
maxIP[i] |= ^ipNet.Mask[i]
|
||||
}
|
||||
return maxIP
|
||||
}
|
||||
171
core/iptree_test.go
Normal file
171
core/iptree_test.go
Normal file
@@ -0,0 +1,171 @@
|
||||
package openp2p
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func wrapTestContains(t *testing.T, iptree *IPTree, ip string, result bool) {
|
||||
if iptree.Contains(ip) == result {
|
||||
// t.Logf("compare version %s %s ok\n", v1, v2)
|
||||
} else {
|
||||
t.Errorf("test %s fail\n", ip)
|
||||
}
|
||||
}
|
||||
func wrapBenchmarkContains(t *testing.B, iptree *IPTree, ip string, result bool) {
|
||||
if iptree.Contains(ip) == result {
|
||||
// t.Logf("compare version %s %s ok\n", v1, v2)
|
||||
} else {
|
||||
t.Errorf("test %s fail\n", ip)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllInputFormat(t *testing.T) {
|
||||
iptree := NewIPTree("219.137.185.70,127.0.0.1,127.0.0.0/8,192.168.1.0/24,192.168.3.100-192.168.3.255,192.168.100.0-192.168.200.255")
|
||||
wrapTestContains(t, iptree, "127.0.0.1", true)
|
||||
wrapTestContains(t, iptree, "127.0.0.2", true)
|
||||
wrapTestContains(t, iptree, "127.1.1.1", true)
|
||||
wrapTestContains(t, iptree, "219.137.185.70", true)
|
||||
wrapTestContains(t, iptree, "219.137.185.71", false)
|
||||
wrapTestContains(t, iptree, "192.168.1.2", true)
|
||||
wrapTestContains(t, iptree, "192.168.2.2", false)
|
||||
wrapTestContains(t, iptree, "192.168.3.1", false)
|
||||
wrapTestContains(t, iptree, "192.168.3.100", true)
|
||||
wrapTestContains(t, iptree, "192.168.3.255", true)
|
||||
wrapTestContains(t, iptree, "192.168.150.1", true)
|
||||
wrapTestContains(t, iptree, "192.168.250.1", false)
|
||||
}
|
||||
|
||||
func TestSingleIP(t *testing.T) {
|
||||
iptree := NewIPTree("")
|
||||
iptree.Add("219.137.185.70", "219.137.185.70")
|
||||
wrapTestContains(t, iptree, "219.137.185.70", true)
|
||||
wrapTestContains(t, iptree, "219.137.185.71", false)
|
||||
}
|
||||
|
||||
func TestWrongSegment(t *testing.T) {
|
||||
iptree := NewIPTree("")
|
||||
inserted := iptree.Add("87.251.75.0", "82.251.75.255")
|
||||
if inserted {
|
||||
t.Errorf("TestWrongSegment failed\n")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSegment2(t *testing.T) {
|
||||
iptree := NewIPTree("")
|
||||
iptree.Clear()
|
||||
iptree.Add("10.1.5.50", "10.1.5.100")
|
||||
iptree.Add("10.1.1.50", "10.1.1.100")
|
||||
iptree.Add("10.1.2.50", "10.1.2.100")
|
||||
iptree.Add("10.1.6.50", "10.1.6.100")
|
||||
iptree.Add("10.1.7.50", "10.1.7.100")
|
||||
iptree.Add("10.1.3.50", "10.1.3.100")
|
||||
iptree.Add("10.1.1.1", "10.1.1.10") // no interset
|
||||
iptree.Add("10.1.1.200", "10.1.1.250") // no interset
|
||||
iptree.Print()
|
||||
|
||||
iptree.Add("10.1.1.80", "10.1.1.90") // all in
|
||||
iptree.Add("10.1.1.40", "10.1.1.60") // interset
|
||||
iptree.Print()
|
||||
iptree.Add("10.1.1.90", "10.1.1.110") // interset
|
||||
iptree.Print()
|
||||
t.Logf("blocklist size:%d\n", iptree.Size())
|
||||
wrapTestContains(t, iptree, "10.1.1.40", true)
|
||||
wrapTestContains(t, iptree, "10.1.5.50", true)
|
||||
wrapTestContains(t, iptree, "10.1.6.50", true)
|
||||
wrapTestContains(t, iptree, "10.1.7.50", true)
|
||||
wrapTestContains(t, iptree, "10.1.2.50", true)
|
||||
wrapTestContains(t, iptree, "10.1.3.50", true)
|
||||
wrapTestContains(t, iptree, "10.1.1.60", true)
|
||||
wrapTestContains(t, iptree, "10.1.1.90", true)
|
||||
wrapTestContains(t, iptree, "10.1.1.110", true)
|
||||
wrapTestContains(t, iptree, "10.1.1.250", true)
|
||||
wrapTestContains(t, iptree, "10.1.2.60", true)
|
||||
wrapTestContains(t, iptree, "10.1.100.30", false)
|
||||
wrapTestContains(t, iptree, "10.1.200.30", false)
|
||||
|
||||
iptree.Add("10.0.0.0", "10.255.255.255") // will merge all segment
|
||||
iptree.Print()
|
||||
if iptree.Size() != 1 {
|
||||
t.Errorf("merge ip segment error\n")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func BenchmarkBuildBlockList20k(t *testing.B) {
|
||||
iptree := NewIPTree("")
|
||||
iptree.Clear()
|
||||
iptree.Add("10.1.5.50", "10.1.5.100")
|
||||
iptree.Add("10.1.1.50", "10.1.1.100")
|
||||
iptree.Add("10.1.2.50", "10.1.2.100")
|
||||
iptree.Add("10.1.6.50", "10.1.6.100")
|
||||
iptree.Add("10.1.7.50", "10.1.7.100")
|
||||
iptree.Add("10.1.3.50", "10.1.3.100")
|
||||
iptree.Add("10.1.1.1", "10.1.1.10") // no interset
|
||||
iptree.Add("10.1.1.200", "10.1.1.250") // no interset
|
||||
iptree.Add("10.1.1.80", "10.1.1.90") // all in
|
||||
iptree.Add("10.1.1.40", "10.1.1.60") // interset
|
||||
iptree.Add("10.1.1.90", "10.1.1.110") // interset
|
||||
var minIP uint32
|
||||
binary.Read(bytes.NewBuffer(net.ParseIP("10.1.1.1").To4()), binary.BigEndian, &minIP)
|
||||
|
||||
// insert 10k block ip single
|
||||
nodeNum := uint32(10000 * 1)
|
||||
gap := uint32(10)
|
||||
for i := minIP; i < minIP+nodeNum*gap; i += gap {
|
||||
iptree.AddIntIP(i, i)
|
||||
// t.Logf("blocklist size:%d\n", iptree.Size())
|
||||
}
|
||||
binary.Read(bytes.NewBuffer(net.ParseIP("100.1.1.1").To4()), binary.BigEndian, &minIP)
|
||||
// insert 100k block ip segment
|
||||
for i := minIP; i < minIP+nodeNum*gap; i += gap {
|
||||
iptree.AddIntIP(i, i+5)
|
||||
}
|
||||
t.Logf("blocklist size:%d\n", iptree.Size())
|
||||
iptree.Clear()
|
||||
t.Logf("clear. blocklist size:%d\n", iptree.Size())
|
||||
}
|
||||
func BenchmarkQuery(t *testing.B) {
|
||||
iptree := NewIPTree("")
|
||||
iptree.Clear()
|
||||
iptree.Add("10.1.5.50", "10.1.5.100")
|
||||
iptree.Add("10.1.1.50", "10.1.1.100")
|
||||
iptree.Add("10.1.2.50", "10.1.2.100")
|
||||
iptree.Add("10.1.6.50", "10.1.6.100")
|
||||
iptree.Add("10.1.7.50", "10.1.7.100")
|
||||
iptree.Add("10.1.3.50", "10.1.3.100")
|
||||
iptree.Add("10.1.1.1", "10.1.1.10") // no interset
|
||||
iptree.Add("10.1.1.200", "10.1.1.250") // no interset
|
||||
iptree.Add("10.1.1.80", "10.1.1.90") // all in
|
||||
iptree.Add("10.1.1.40", "10.1.1.60") // interset
|
||||
iptree.Add("10.1.1.90", "10.1.1.110") // interset
|
||||
var minIP uint32
|
||||
binary.Read(bytes.NewBuffer(net.ParseIP("10.1.1.1").To4()), binary.BigEndian, &minIP)
|
||||
|
||||
// insert 10k block ip single
|
||||
nodeNum := uint32(10000 * 100)
|
||||
gap := uint32(10)
|
||||
for i := minIP; i < minIP+nodeNum*gap; i += gap {
|
||||
iptree.AddIntIP(i, i)
|
||||
// t.Logf("blocklist size:%d\n", iptree.Size())
|
||||
}
|
||||
binary.Read(bytes.NewBuffer(net.ParseIP("100.1.1.1").To4()), binary.BigEndian, &minIP)
|
||||
// insert 100k block ip segment
|
||||
for i := minIP; i < minIP+nodeNum*gap; i += gap {
|
||||
iptree.AddIntIP(i, i+5)
|
||||
}
|
||||
t.Logf("blocklist size:%d\n", iptree.Size())
|
||||
t.ResetTimer()
|
||||
queryNum := 100 * 10000
|
||||
for i := 0; i < queryNum; i++ {
|
||||
iptree.ContainsInt(minIP + uint32(i))
|
||||
wrapBenchmarkContains(t, iptree, "10.1.5.55", true)
|
||||
wrapBenchmarkContains(t, iptree, "10.1.1.1", true)
|
||||
wrapBenchmarkContains(t, iptree, "10.1.5.200", false)
|
||||
wrapBenchmarkContains(t, iptree, "200.1.1.1", false)
|
||||
}
|
||||
t.Logf("query list:%d\n", queryNum*4)
|
||||
|
||||
}
|
||||
@@ -84,7 +84,7 @@ func natTest(serverHost string, serverPort int, localPort int) (publicIP string,
|
||||
return "", 0, err
|
||||
}
|
||||
natRsp := NatDetectRsp{}
|
||||
err = json.Unmarshal(buffer[openP2PHeaderSize:nRead], &natRsp)
|
||||
json.Unmarshal(buffer[openP2PHeaderSize:nRead], &natRsp)
|
||||
|
||||
return natRsp.IP, natRsp.Port, nil
|
||||
}
|
||||
@@ -121,6 +121,7 @@ func publicIPTest(publicIP string, echoPort int) (hasPublicIP int, hasUPNPorNATP
|
||||
echoConn, err = net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4zero, Port: echoPort})
|
||||
if err != nil {
|
||||
gLog.Println(LvERROR, "echo server listen error:", err)
|
||||
wg.Done()
|
||||
return
|
||||
}
|
||||
buf := make([]byte, 1600)
|
||||
@@ -135,7 +136,10 @@ func publicIPTest(publicIP string, echoPort int) (hasPublicIP int, hasUPNPorNATP
|
||||
echoConn.WriteToUDP(buf[0:n], addr)
|
||||
gLog.Println(LvDEBUG, "echo server end")
|
||||
}()
|
||||
wg.Wait() // wait echo udp
|
||||
wg.Wait() // wait echo udp
|
||||
if echoConn == nil { // listen error
|
||||
return
|
||||
}
|
||||
defer echoConn.Close()
|
||||
// testing for public ip
|
||||
for i := 0; i < 2; i++ {
|
||||
|
||||
@@ -21,7 +21,6 @@ func Run() {
|
||||
fmt.Println(OpenP2PVersion)
|
||||
return
|
||||
case "update":
|
||||
gLog = NewLogger(baseDir, ProductName, LvDEBUG, 1024*1024, LogFileAndConsole)
|
||||
targetPath := filepath.Join(defaultInstallPath, defaultBinName)
|
||||
d := daemon{}
|
||||
err := d.Control("restart", targetPath, nil)
|
||||
|
||||
@@ -17,6 +17,7 @@ type p2pApp struct {
|
||||
listener net.Listener
|
||||
listenerUDP *net.UDPConn
|
||||
tunnel *P2PTunnel
|
||||
iptree *IPTree
|
||||
rtid uint64 // relay tunnelID
|
||||
relayNode string
|
||||
relayMode string
|
||||
@@ -64,6 +65,15 @@ func (app *p2pApp) listenTCP() error {
|
||||
}
|
||||
break
|
||||
}
|
||||
// check white list
|
||||
if app.config.Whitelist != "" {
|
||||
remoteIP := strings.Split(conn.RemoteAddr().String(), ":")[0]
|
||||
if !app.iptree.Contains(remoteIP) {
|
||||
conn.Close()
|
||||
gLog.Printf(LvERROR, "%s not in whitelist, access denied", remoteIP)
|
||||
continue
|
||||
}
|
||||
}
|
||||
oConn := overlayConn{
|
||||
tunnel: app.tunnel,
|
||||
connTCP: conn,
|
||||
|
||||
@@ -3,6 +3,7 @@ package openp2p
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
@@ -10,6 +11,7 @@ import (
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -112,30 +114,18 @@ func (pn *P2PNetwork) runAll() {
|
||||
allApps := gConf.Apps // read a copy, other thread will modify the gConf.Apps
|
||||
|
||||
for _, config := range allApps {
|
||||
if config.nextRetryTime.After(time.Now()) {
|
||||
continue
|
||||
}
|
||||
if config.Enabled == 0 {
|
||||
if config.nextRetryTime.After(time.Now()) || config.Enabled == 0 || config.retryNum >= retryLimit {
|
||||
continue
|
||||
}
|
||||
if config.AppName == "" {
|
||||
config.AppName = fmt.Sprintf("%s%d", config.Protocol, config.SrcPort)
|
||||
config.AppName = config.ID()
|
||||
}
|
||||
appExist := false
|
||||
i, ok := pn.apps.Load(fmt.Sprintf("%s%d", config.Protocol, config.SrcPort))
|
||||
if ok {
|
||||
app := i.(*p2pApp)
|
||||
appExist = true
|
||||
if app.isActive() {
|
||||
if i, ok := pn.apps.Load(config.ID()); ok {
|
||||
if app := i.(*p2pApp); app.isActive() {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if appExist {
|
||||
pn.DeleteApp(*config)
|
||||
}
|
||||
if config.retryNum >= retryLimit {
|
||||
continue
|
||||
}
|
||||
|
||||
if config.retryNum > 0 { // first time not show reconnect log
|
||||
gLog.Printf(LvINFO, "detect app %s disconnect, reconnecting the %d times...", config.AppName, config.retryNum)
|
||||
@@ -160,6 +150,7 @@ func (pn *P2PNetwork) runAll() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (pn *P2PNetwork) autorunApp() {
|
||||
gLog.Println(LvINFO, "autorunApp start")
|
||||
pn.wgReconnect.Add(1)
|
||||
@@ -181,9 +172,7 @@ func (pn *P2PNetwork) addRelayTunnel(config AppConfig) (*P2PTunnel, uint64, stri
|
||||
return nil, 0, "", errors.New("read MsgRelayNodeRsp error")
|
||||
}
|
||||
rsp := RelayNodeRsp{}
|
||||
err := json.Unmarshal(body, &rsp)
|
||||
if err != nil {
|
||||
gLog.Printf(LvERROR, "wrong RelayNodeRsp:%s", err)
|
||||
if err := json.Unmarshal(body, &rsp); err != nil {
|
||||
return nil, 0, "", errors.New("unmarshal MsgRelayNodeRsp error")
|
||||
}
|
||||
if rsp.RelayName == "" || rsp.RelayToken == 0 {
|
||||
@@ -216,9 +205,7 @@ func (pn *P2PNetwork) addRelayTunnel(config AppConfig) (*P2PTunnel, uint64, stri
|
||||
return nil, 0, "", errors.New("read MsgPushAddRelayTunnelRsp error")
|
||||
}
|
||||
rspID := TunnelMsg{}
|
||||
err = json.Unmarshal(body, &rspID)
|
||||
if err != nil {
|
||||
gLog.Printf(LvERROR, "wrong RelayNodeRsp:%s", err)
|
||||
if err = json.Unmarshal(body, &rspID); err != nil {
|
||||
return nil, 0, "", errors.New("unmarshal MsgRelayNodeRsp error")
|
||||
}
|
||||
return t, rspID.ID, rsp.Mode, err
|
||||
@@ -233,7 +220,7 @@ func (pn *P2PNetwork) AddApp(config AppConfig) error {
|
||||
}
|
||||
// check if app already exist?
|
||||
appExist := false
|
||||
_, ok := pn.apps.Load(fmt.Sprintf("%s%d", config.Protocol, config.SrcPort))
|
||||
_, ok := pn.apps.Load(config.ID())
|
||||
if ok {
|
||||
appExist = true
|
||||
}
|
||||
@@ -299,11 +286,13 @@ func (pn *P2PNetwork) AddApp(config AppConfig) error {
|
||||
key: appKey,
|
||||
tunnel: t,
|
||||
config: config,
|
||||
iptree: NewIPTree(config.Whitelist),
|
||||
rtid: rtid,
|
||||
relayNode: relayNode,
|
||||
relayMode: relayMode,
|
||||
hbTime: time.Now()}
|
||||
pn.apps.Store(fmt.Sprintf("%s%d", config.Protocol, config.SrcPort), &app)
|
||||
pn.apps.Store(config.ID(), &app)
|
||||
gLog.Printf(LvINFO, "%s use tunnel %d", app.config.AppName, app.tunnel.id)
|
||||
if err == nil {
|
||||
go app.listen()
|
||||
}
|
||||
@@ -314,16 +303,37 @@ func (pn *P2PNetwork) DeleteApp(config AppConfig) {
|
||||
gLog.Printf(LvINFO, "DeleteApp %s%d start", config.Protocol, config.SrcPort)
|
||||
defer gLog.Printf(LvINFO, "DeleteApp %s%d end", config.Protocol, config.SrcPort)
|
||||
// close the apps of this config
|
||||
i, ok := pn.apps.Load(fmt.Sprintf("%s%d", config.Protocol, config.SrcPort))
|
||||
i, ok := pn.apps.Load(config.ID())
|
||||
if ok {
|
||||
app := i.(*p2pApp)
|
||||
gLog.Printf(LvINFO, "app %s exist, delete it", fmt.Sprintf("%s%d", config.Protocol, config.SrcPort))
|
||||
gLog.Printf(LvINFO, "app %s exist, delete it", app.config.AppName)
|
||||
app.close()
|
||||
pn.apps.Delete(fmt.Sprintf("%s%d", config.Protocol, config.SrcPort))
|
||||
pn.apps.Delete(config.ID())
|
||||
}
|
||||
}
|
||||
|
||||
func (pn *P2PNetwork) addDirectTunnel(config AppConfig, tid uint64) (*P2PTunnel, error) {
|
||||
func (pn *P2PNetwork) findTunnel(config *AppConfig) (t *P2PTunnel) {
|
||||
// find existing tunnel to peer
|
||||
pn.allTunnels.Range(func(id, i interface{}) bool {
|
||||
tmpt := i.(*P2PTunnel)
|
||||
if tmpt.config.PeerNode == config.PeerNode {
|
||||
gLog.Println(LvINFO, "tunnel already exist ", config.PeerNode)
|
||||
isActive := tmpt.checkActive()
|
||||
// inactive, close it
|
||||
if !isActive {
|
||||
gLog.Println(LvINFO, "but it's not active, close it ", config.PeerNode)
|
||||
tmpt.close()
|
||||
} else {
|
||||
t = tmpt
|
||||
}
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
return t
|
||||
}
|
||||
|
||||
func (pn *P2PNetwork) addDirectTunnel(config AppConfig, tid uint64) (t *P2PTunnel, err error) {
|
||||
gLog.Printf(LvDEBUG, "addDirectTunnel %s%d to %s:%s:%d start", config.Protocol, config.SrcPort, config.PeerNode, config.DstHost, config.DstPort)
|
||||
defer gLog.Printf(LvDEBUG, "addDirectTunnel %s%d to %s:%s:%d end", config.Protocol, config.SrcPort, config.PeerNode, config.DstHost, config.DstPort)
|
||||
isClient := false
|
||||
@@ -332,64 +342,34 @@ func (pn *P2PNetwork) addDirectTunnel(config AppConfig, tid uint64) (*P2PTunnel,
|
||||
tid = rand.Uint64()
|
||||
isClient = true
|
||||
}
|
||||
exist := false
|
||||
// find existing tunnel to peer
|
||||
var t *P2PTunnel
|
||||
pn.allTunnels.Range(func(id, i interface{}) bool {
|
||||
t = i.(*P2PTunnel)
|
||||
if t.config.PeerNode == config.PeerNode {
|
||||
// server side force close existing tunnel
|
||||
if !isClient {
|
||||
t.close()
|
||||
return false
|
||||
}
|
||||
|
||||
// client side checking
|
||||
gLog.Println(LvINFO, "tunnel already exist ", config.PeerNode)
|
||||
isActive := t.checkActive()
|
||||
// inactive, close it
|
||||
if !isActive {
|
||||
gLog.Println(LvINFO, "but it's not active, close it ", config.PeerNode)
|
||||
t.close()
|
||||
} else {
|
||||
// active
|
||||
exist = true
|
||||
}
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
if exist {
|
||||
if t = pn.findTunnel(&config); t != nil {
|
||||
return t, nil
|
||||
}
|
||||
// create tunnel if not exist
|
||||
t = &P2PTunnel{pn: pn,
|
||||
config: config,
|
||||
id: tid,
|
||||
}
|
||||
|
||||
pn.msgMapMtx.Lock()
|
||||
pn.msgMap[nodeNameToID(config.PeerNode)] = make(chan []byte, 50)
|
||||
pn.msgMapMtx.Unlock()
|
||||
// server side
|
||||
if !isClient {
|
||||
err := pn.newTunnel(t, tid, isClient)
|
||||
t, err = pn.newTunnel(config, tid, isClient)
|
||||
return t, err // always return
|
||||
}
|
||||
// client side
|
||||
// peer info
|
||||
initErr := t.requestPeerInfo()
|
||||
initErr := pn.requestPeerInfo(&config)
|
||||
if initErr != nil {
|
||||
gLog.Println(LvERROR, "init error:", initErr)
|
||||
|
||||
return nil, initErr
|
||||
}
|
||||
var err error
|
||||
// try TCP6
|
||||
if IsIPv6(t.config.peerIPv6) && IsIPv6(t.pn.config.publicIPv6) {
|
||||
if IsIPv6(config.peerIPv6) && IsIPv6(pn.config.publicIPv6) {
|
||||
gLog.Println(LvINFO, "try TCP6")
|
||||
t.config.linkMode = LinkModeTCP6
|
||||
t.config.isUnderlayServer = 0
|
||||
if err = pn.newTunnel(t, tid, isClient); err == nil {
|
||||
config.linkMode = LinkModeTCP6
|
||||
config.isUnderlayServer = 0
|
||||
if t, err = pn.newTunnel(config, tid, isClient); err == nil {
|
||||
return t, nil
|
||||
}
|
||||
}
|
||||
@@ -397,59 +377,74 @@ func (pn *P2PNetwork) addDirectTunnel(config AppConfig, tid uint64) (*P2PTunnel,
|
||||
// TODO: try UDP6
|
||||
|
||||
// try TCP4
|
||||
if t.config.hasIPv4 == 1 || t.pn.config.hasIPv4 == 1 || t.config.hasUPNPorNATPMP == 1 || t.pn.config.hasUPNPorNATPMP == 1 {
|
||||
if config.hasIPv4 == 1 || pn.config.hasIPv4 == 1 || config.hasUPNPorNATPMP == 1 || pn.config.hasUPNPorNATPMP == 1 {
|
||||
gLog.Println(LvINFO, "try TCP4")
|
||||
t.config.linkMode = LinkModeTCP4
|
||||
if t.config.hasIPv4 == 1 || t.config.hasUPNPorNATPMP == 1 {
|
||||
t.config.isUnderlayServer = 0
|
||||
config.linkMode = LinkModeTCP4
|
||||
if config.hasIPv4 == 1 || config.hasUPNPorNATPMP == 1 {
|
||||
config.isUnderlayServer = 0
|
||||
} else {
|
||||
t.config.isUnderlayServer = 1
|
||||
config.isUnderlayServer = 1
|
||||
}
|
||||
if err = pn.newTunnel(t, tid, isClient); err == nil {
|
||||
if t, err = pn.newTunnel(config, tid, isClient); err == nil {
|
||||
return t, nil
|
||||
}
|
||||
}
|
||||
// TODO: try UDP4
|
||||
|
||||
// try TCPPunch
|
||||
if t.config.peerNatType == NATCone && t.pn.config.natType == NATCone { // TODO: support c2s
|
||||
gLog.Println(LvINFO, "try TCP4 Punch")
|
||||
t.config.linkMode = LinkModeTCPPunch
|
||||
t.config.isUnderlayServer = 0
|
||||
if err = pn.newTunnel(t, tid, isClient); err == nil {
|
||||
gLog.Println(LvINFO, "TCP4 Punch ok")
|
||||
return t, nil
|
||||
for i := 0; i < Cone2ConePunchMaxRetry; i++ { // when both 2 nats has restrict firewall, simultaneous punching needs to be very precise, it takes a few tries
|
||||
if config.peerNatType == NATCone && pn.config.natType == NATCone { // TODO: support c2s
|
||||
gLog.Println(LvINFO, "try TCP4 Punch")
|
||||
config.linkMode = LinkModeTCPPunch
|
||||
config.isUnderlayServer = 0
|
||||
if t, err = pn.newTunnel(config, tid, isClient); err == nil {
|
||||
gLog.Println(LvINFO, "TCP4 Punch ok")
|
||||
return t, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// try UDPPunch
|
||||
if t.config.peerNatType == NATCone || t.pn.config.natType == NATCone {
|
||||
gLog.Println(LvINFO, "try UDP4 Punch")
|
||||
t.config.linkMode = LinkModeUDPPunch
|
||||
t.config.isUnderlayServer = 0
|
||||
if err = pn.newTunnel(t, tid, isClient); err == nil {
|
||||
return t, nil
|
||||
for i := 0; i < Cone2ConePunchMaxRetry; i++ { // when both 2 nats has restrict firewall, simultaneous punching needs to be very precise, it takes a few tries
|
||||
if config.peerNatType == NATCone || pn.config.natType == NATCone {
|
||||
gLog.Println(LvINFO, "try UDP4 Punch")
|
||||
config.linkMode = LinkModeUDPPunch
|
||||
config.isUnderlayServer = 0
|
||||
if t, err = pn.newTunnel(config, tid, isClient); err == nil {
|
||||
return t, nil
|
||||
}
|
||||
}
|
||||
if !(config.peerNatType == NATCone && pn.config.natType == NATCone) { // not cone2cone, no more try
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil, ErrorHandshake // only ErrorHandshake will try relay
|
||||
}
|
||||
|
||||
func (pn *P2PNetwork) newTunnel(t *P2PTunnel, tid uint64, isClient bool) error {
|
||||
func (pn *P2PNetwork) newTunnel(config AppConfig, tid uint64, isClient bool) (t *P2PTunnel, err error) {
|
||||
if existTunnel := pn.findTunnel(&config); existTunnel != nil {
|
||||
return existTunnel, nil
|
||||
}
|
||||
t = &P2PTunnel{pn: pn,
|
||||
config: config,
|
||||
id: tid,
|
||||
}
|
||||
t.initPort()
|
||||
if isClient {
|
||||
if err := t.connect(); err != nil {
|
||||
if err = t.connect(); err != nil {
|
||||
gLog.Println(LvERROR, "p2pTunnel connect error:", err)
|
||||
return err
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if err := t.listen(); err != nil {
|
||||
if err = t.listen(); err != nil {
|
||||
gLog.Println(LvERROR, "p2pTunnel listen error:", err)
|
||||
return err
|
||||
return
|
||||
}
|
||||
}
|
||||
// store it when success
|
||||
gLog.Printf(LvDEBUG, "store tunnel %d", tid)
|
||||
pn.allTunnels.Store(tid, t)
|
||||
return nil
|
||||
return
|
||||
}
|
||||
func (pn *P2PNetwork) init() error {
|
||||
gLog.Println(LvINFO, "init start")
|
||||
@@ -480,7 +475,12 @@ func (pn *P2PNetwork) init() error {
|
||||
gLog.Println(LvDEBUG, "detect NAT type:", pn.config.natType, " publicIP:", pn.config.publicIP)
|
||||
gatewayURL := fmt.Sprintf("%s:%d", pn.config.ServerHost, pn.config.ServerPort)
|
||||
uri := "/api/v1/login"
|
||||
config := tls.Config{InsecureSkipVerify: true} // let's encrypt root cert "DST Root CA X3" expired at 2021/09/29. many old system(windows server 2008 etc) will not trust our cert
|
||||
caCertPool := x509.NewCertPool()
|
||||
caCertPool.AppendCertsFromPEM([]byte(rootCA))
|
||||
|
||||
config := tls.Config{
|
||||
RootCAs: caCertPool,
|
||||
InsecureSkipVerify: false} // let's encrypt root cert "DST Root CA X3" expired at 2021/09/29. many old system(windows server 2008 etc) will not trust our cert
|
||||
websocket.DefaultDialer.TLSClientConfig = &config
|
||||
u := url.URL{Scheme: "wss", Host: gatewayURL, Path: uri}
|
||||
q := u.Query()
|
||||
@@ -552,9 +552,8 @@ func (pn *P2PNetwork) handleMessage(t int, msg []byte) {
|
||||
case MsgLogin:
|
||||
// gLog.Println(LevelINFO,string(msg))
|
||||
rsp := LoginRsp{}
|
||||
err = json.Unmarshal(msg[openP2PHeaderSize:], &rsp)
|
||||
if err != nil {
|
||||
gLog.Printf(LvERROR, "wrong login response:%s", err)
|
||||
if err = json.Unmarshal(msg[openP2PHeaderSize:], &rsp); err != nil {
|
||||
gLog.Printf(LvERROR, "wrong %v:%s", reflect.TypeOf(rsp), err)
|
||||
return
|
||||
}
|
||||
if rsp.Error != 0 {
|
||||
@@ -746,3 +745,30 @@ func (pn *P2PNetwork) refreshIPv6(force bool) {
|
||||
}
|
||||
pn.config.publicIPv6 = string(buf[:n])
|
||||
}
|
||||
|
||||
func (pn *P2PNetwork) requestPeerInfo(config *AppConfig) error {
|
||||
// request peer info
|
||||
pn.write(MsgQuery, MsgQueryPeerInfoReq, &QueryPeerInfoReq{config.peerToken, config.PeerNode})
|
||||
head, body := pn.read("", MsgQuery, MsgQueryPeerInfoRsp, UnderlayConnectTimeout)
|
||||
if head == nil {
|
||||
return ErrNetwork // network error, should not be ErrPeerOffline
|
||||
}
|
||||
rsp := QueryPeerInfoRsp{}
|
||||
if err := json.Unmarshal(body, &rsp); err != nil {
|
||||
return ErrMsgFormat
|
||||
}
|
||||
if rsp.Online == 0 {
|
||||
return ErrPeerOffline
|
||||
}
|
||||
if compareVersion(rsp.Version, LeastSupportVersion) == LESS {
|
||||
return ErrVersionNotCompatible
|
||||
}
|
||||
config.peerVersion = rsp.Version
|
||||
config.hasIPv4 = rsp.HasIPv4
|
||||
config.peerIP = rsp.IPv4
|
||||
config.peerIPv6 = rsp.IPv6
|
||||
config.hasUPNPorNATPMP = rsp.HasUPNPorNATPMP
|
||||
config.peerNatType = rsp.NatType
|
||||
///
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"reflect"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -32,34 +33,6 @@ type P2PTunnel struct {
|
||||
punchTs uint64
|
||||
}
|
||||
|
||||
func (t *P2PTunnel) requestPeerInfo() error {
|
||||
// request peer info
|
||||
t.pn.write(MsgQuery, MsgQueryPeerInfoReq, &QueryPeerInfoReq{t.config.peerToken, t.config.PeerNode})
|
||||
head, body := t.pn.read("", MsgQuery, MsgQueryPeerInfoRsp, UnderlayConnectTimeout)
|
||||
if head == nil {
|
||||
return ErrNetwork // network error, should not be ErrPeerOffline
|
||||
}
|
||||
rsp := QueryPeerInfoRsp{}
|
||||
err := json.Unmarshal(body, &rsp)
|
||||
if err != nil {
|
||||
gLog.Printf(LvERROR, "wrong QueryPeerInfoRsp:%s", err)
|
||||
return ErrMsgFormat
|
||||
}
|
||||
if rsp.Online == 0 {
|
||||
return ErrPeerOffline
|
||||
}
|
||||
if compareVersion(rsp.Version, LeastSupportVersion) == LESS {
|
||||
return ErrVersionNotCompatible
|
||||
}
|
||||
t.config.peerVersion = rsp.Version
|
||||
t.config.hasIPv4 = rsp.HasIPv4
|
||||
t.config.peerIP = rsp.IPv4
|
||||
t.config.peerIPv6 = rsp.IPv6
|
||||
t.config.hasUPNPorNATPMP = rsp.HasUPNPorNATPMP
|
||||
t.config.peerNatType = rsp.NatType
|
||||
///
|
||||
return nil
|
||||
}
|
||||
func (t *P2PTunnel) initPort() {
|
||||
t.running = true
|
||||
t.hbMtx.Lock()
|
||||
@@ -118,9 +91,8 @@ func (t *P2PTunnel) connect() error {
|
||||
return errors.New("connect error")
|
||||
}
|
||||
rsp := PushConnectRsp{}
|
||||
err := json.Unmarshal(body, &rsp)
|
||||
if err != nil {
|
||||
gLog.Printf(LvERROR, "wrong MsgPushConnectRsp:%s", err)
|
||||
if err := json.Unmarshal(body, &rsp); err != nil {
|
||||
gLog.Printf(LvERROR, "wrong %v:%s", reflect.TypeOf(rsp), err)
|
||||
return err
|
||||
}
|
||||
// gLog.Println(LevelINFO, rsp)
|
||||
@@ -135,7 +107,7 @@ func (t *P2PTunnel) connect() error {
|
||||
t.config.peerConeNatPort = rsp.ConeNatPort
|
||||
t.config.peerIP = rsp.FromIP
|
||||
t.punchTs = rsp.PunchTs
|
||||
err = t.start()
|
||||
err := t.start()
|
||||
if err != nil {
|
||||
gLog.Println(LvERROR, "handshake error:", err)
|
||||
err = ErrorHandshake
|
||||
@@ -165,14 +137,10 @@ func (t *P2PTunnel) isActive() bool {
|
||||
}
|
||||
|
||||
func (t *P2PTunnel) checkActive() bool {
|
||||
hbt := time.Now()
|
||||
t.hbMtx.Lock()
|
||||
if t.hbTime.Before(time.Now().Add(-TunnelHeartbeatTime)) {
|
||||
t.hbMtx.Unlock()
|
||||
if t.conn == nil {
|
||||
return false
|
||||
}
|
||||
t.hbMtx.Unlock()
|
||||
// hbtime within TunnelHeartbeatTime, check it now
|
||||
hbt := time.Now()
|
||||
t.conn.WriteBytes(MsgP2P, MsgTunnelHeartbeat, nil)
|
||||
isActive := false
|
||||
// wait at most 5s
|
||||
@@ -184,6 +152,7 @@ func (t *P2PTunnel) checkActive() bool {
|
||||
t.hbMtx.Unlock()
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
}
|
||||
gLog.Printf(LvINFO, "checkActive %t. hbtime=%d", isActive, t.hbTime)
|
||||
return isActive
|
||||
}
|
||||
|
||||
@@ -451,6 +420,7 @@ func (t *P2PTunnel) readLoop() {
|
||||
}
|
||||
switch head.SubType {
|
||||
case MsgTunnelHeartbeat:
|
||||
t.hbTime = time.Now()
|
||||
t.conn.WriteBytes(MsgP2P, MsgTunnelHeartbeatAck, nil)
|
||||
gLog.Printf(LvDEBUG, "%d read tunnel heartbeat", t.id)
|
||||
case MsgTunnelHeartbeatAck:
|
||||
@@ -492,9 +462,8 @@ func (t *P2PTunnel) readLoop() {
|
||||
t.pn.relay(tunnelID, body[8:])
|
||||
case MsgRelayHeartbeat:
|
||||
req := RelayHeartbeat{}
|
||||
err := json.Unmarshal(body, &req)
|
||||
if err != nil {
|
||||
gLog.Printf(LvERROR, "wrong RelayHeartbeat:%s", err)
|
||||
if err := json.Unmarshal(body, &req); err != nil {
|
||||
gLog.Printf(LvERROR, "wrong %v:%s", reflect.TypeOf(req), err)
|
||||
continue
|
||||
}
|
||||
gLog.Printf(LvDEBUG, "got MsgRelayHeartbeat from %d:%d", req.RelayTunnelID, req.AppID)
|
||||
@@ -514,9 +483,8 @@ func (t *P2PTunnel) readLoop() {
|
||||
t.pn.updateAppHeartbeat(req.AppID)
|
||||
case MsgOverlayConnectReq:
|
||||
req := OverlayConnectReq{}
|
||||
err := json.Unmarshal(body, &req)
|
||||
if err != nil {
|
||||
gLog.Printf(LvERROR, "wrong MsgOverlayConnectReq:%s", err)
|
||||
if err := json.Unmarshal(body, &req); err != nil {
|
||||
gLog.Printf(LvERROR, "wrong %v:%s", reflect.TypeOf(req), err)
|
||||
continue
|
||||
}
|
||||
// app connect only accept token(not relay totp token), avoid someone using the share relay node's token
|
||||
@@ -559,9 +527,8 @@ func (t *P2PTunnel) readLoop() {
|
||||
go oConn.run()
|
||||
case MsgOverlayDisconnectReq:
|
||||
req := OverlayDisconnectReq{}
|
||||
err := json.Unmarshal(body, &req)
|
||||
if err != nil {
|
||||
gLog.Printf(LvERROR, "wrong OverlayDisconnectRequest:%s", err)
|
||||
if err := json.Unmarshal(body, &req); err != nil {
|
||||
gLog.Printf(LvERROR, "wrong %v:%s", reflect.TypeOf(req), err)
|
||||
continue
|
||||
}
|
||||
overlayID := req.ID
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const OpenP2PVersion = "3.9.1"
|
||||
const OpenP2PVersion = "3.10.3"
|
||||
const ProductName string = "openp2p"
|
||||
const LeastSupportVersion = "3.0.0"
|
||||
const SyncServerTimeVersion = "3.9.0"
|
||||
@@ -135,17 +135,18 @@ const (
|
||||
const (
|
||||
ReadBuffLen = 4096 // for UDP maybe not enough
|
||||
NetworkHeartbeatTime = time.Second * 30 // TODO: server no response hb, save flow
|
||||
TunnelHeartbeatTime = time.Second * 15
|
||||
TunnelHeartbeatTime = time.Second * 10 // some nat udp session expired time less than 15s. change to 10s
|
||||
TunnelIdleTimeout = time.Minute
|
||||
SymmetricHandshakeNum = 800 // 0.992379
|
||||
// SymmetricHandshakeNum = 1000 // 0.999510
|
||||
SymmetricHandshakeInterval = time.Millisecond
|
||||
HandshakeTimeout = time.Second * 5
|
||||
PeerAddRelayTimeount = HandshakeTimeout * 2
|
||||
PeerAddRelayTimeount = time.Second * 30 // peer need times
|
||||
CheckActiveTimeout = time.Second * 5
|
||||
PaddingSize = 16
|
||||
AESKeySize = 16
|
||||
MaxRetry = 10
|
||||
Cone2ConePunchMaxRetry = 1
|
||||
RetryInterval = time.Second * 30
|
||||
PublicIPEchoTimeout = time.Second * 1
|
||||
NatTestTimeout = time.Second * 5
|
||||
@@ -350,9 +351,10 @@ type AppInfo struct {
|
||||
AppName string `json:"appName,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Protocol string `json:"protocol,omitempty"`
|
||||
Whitelist string `json:"whitelist,omitempty"`
|
||||
SrcPort int `json:"srcPort,omitempty"`
|
||||
Protocol0 string `json:"protocol0,omitempty"`
|
||||
SrcPort0 int `json:"srcPort0,omitempty"`
|
||||
SrcPort0 int `json:"srcPort0,omitempty"` // srcport+protocol is uneque, use as old app id
|
||||
NatType int `json:"natType,omitempty"`
|
||||
PeerNode string `json:"peerNode,omitempty"`
|
||||
DstPort int `json:"dstPort,omitempty"`
|
||||
@@ -439,3 +441,25 @@ type QueryPeerInfoRsp struct {
|
||||
IPv6 string `json:"IPv6,omitempty"` // if public relay node, ipv6 not set
|
||||
HasUPNPorNATPMP int `json:"hasUPNPorNATPMP,omitempty"`
|
||||
}
|
||||
|
||||
const rootCA = `-----BEGIN CERTIFICATE-----
|
||||
MIIDhTCCAm0CFHm0cd8dnGCbUW/OcS56jf0gvRk7MA0GCSqGSIb3DQEBCwUAMH4x
|
||||
CzAJBgNVBAYTAkNOMQswCQYDVQQIDAJHRDETMBEGA1UECgwKb3BlbnAycC5jbjET
|
||||
MBEGA1UECwwKb3BlbnAycC5jbjETMBEGA1UEAwwKb3BlbnAycC5jbjEjMCEGCSqG
|
||||
SIb3DQEJARYUb3BlbnAycC5jbkBnbWFpbC5jb20wIBcNMjMwODAxMDkwMjMwWhgP
|
||||
MjEyMzA3MDgwOTAyMzBaMH4xCzAJBgNVBAYTAkNOMQswCQYDVQQIDAJHRDETMBEG
|
||||
A1UECgwKb3BlbnAycC5jbjETMBEGA1UECwwKb3BlbnAycC5jbjETMBEGA1UEAwwK
|
||||
b3BlbnAycC5jbjEjMCEGCSqGSIb3DQEJARYUb3BlbnAycC5jbkBnbWFpbC5jb20w
|
||||
ggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDWg8wPy5hBLUaY4WOXayKu
|
||||
+magEz1LAY0krzXYSZaSCvGMwA0cervwAqgKfiiZEhho5UNA5iVOJ6bO1RL9H7Vp
|
||||
4HuW9BttDU/NQHguD8pyqx06Kaosz5LRw8USz1BCWWFdmi8Mv4I0omtd7m6lbWnY
|
||||
nrjQKLYPahPW481jUfJPqR6wUTnBuBMr2ZAGqmFR4Lhqs9B1P9GeBfDWNwVApJUC
|
||||
VEhbElukRJxdUvWeJ5+HMENKQcHCTTgmQbmDLMobHXs3Xf7fT9qC76wOe9LFHI6L
|
||||
dAww9gryQhxWauQl1NO8aGJTFu+3wgnKBdTMJmF/1iuZYXJOCR1solwqU1hCgBsj
|
||||
AgMBAAEwDQYJKoZIhvcNAQELBQADggEBADp153YNVN8p6/3PLnXxHBDeDViAfeQd
|
||||
VJmy8eH1LTq/xtUY71HGSpL7iIBNoQdDTHfsg3c6ZANBCxbO/7AhFAzPt1aK8eHy
|
||||
XuEiW0Z6R8np1Khh3alCOfD15tKcjok//Wxisbz+YItlbDus/eWRbLGB3HGrzn4l
|
||||
GB18jw+G7o4U3rGX8agHqVGQEd06gk1ZaprASpTGwSsv4A5ehosjT1d7re8Z5eD4
|
||||
RVtXS+DplMClQ5QSlv3StwcWOsjyiAimNfLEU5xoEfq17yOJUTU1OTL4YOt16QUc
|
||||
C1tnzFr3k/ioqFR7cnyzNrbjlfPOmO9l2WReEbMP3bvaSHm6EcpJKS8=
|
||||
-----END CERTIFICATE-----`
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"archive/zip"
|
||||
"compress/gzip"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -19,9 +20,13 @@ import (
|
||||
func update(host string, port int) {
|
||||
gLog.Println(LvINFO, "update start")
|
||||
defer gLog.Println(LvINFO, "update end")
|
||||
caCertPool := x509.NewCertPool()
|
||||
caCertPool.AppendCertsFromPEM([]byte(rootCA))
|
||||
|
||||
c := http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
TLSClientConfig: &tls.Config{RootCAs: caCertPool,
|
||||
InsecureSkipVerify: false},
|
||||
},
|
||||
Timeout: time.Second * 30,
|
||||
}
|
||||
@@ -43,8 +48,7 @@ func update(host string, port int) {
|
||||
return
|
||||
}
|
||||
updateInfo := UpdateInfo{}
|
||||
err = json.Unmarshal(rspBuf, &updateInfo)
|
||||
if err != nil {
|
||||
if err = json.Unmarshal(rspBuf, &updateInfo); err != nil {
|
||||
gLog.Println(LvERROR, rspBuf, " update info decode error:", err)
|
||||
return
|
||||
}
|
||||
@@ -69,7 +73,7 @@ func updateFile(url string, checksum string, dst string) error {
|
||||
return err
|
||||
}
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: false},
|
||||
}
|
||||
client := &http.Client{Transport: tr}
|
||||
response, err := client.Get(url)
|
||||
|
||||
Reference in New Issue
Block a user