From 6c1551d951a9b00cddc509f88e51f1b05b501e67 Mon Sep 17 00:00:00 2001 From: TenderIronh Date: Tue, 14 Dec 2021 15:25:38 +0800 Subject: [PATCH] auto adjust server and local timestamp for totp --- config.go | 2 +- daemon.go | 57 ++++++++++++++++++++++++++++++++------------------- openp2p.go | 24 ++++++++++------------ p2pnetwork.go | 19 +++++++++-------- protocol.go | 4 ++-- 5 files changed, 61 insertions(+), 45 deletions(-) diff --git a/config.go b/config.go index bc3a72c..a680c4a 100644 --- a/config.go +++ b/config.go @@ -77,7 +77,7 @@ type NetworkConfig struct { os string publicIP string natType int - shareBandwidth int + ShareBandwidth int // server info ServerHost string ServerPort int diff --git a/daemon.go b/daemon.go index 81c7e38..16f5339 100644 --- a/daemon.go +++ b/daemon.go @@ -129,6 +129,27 @@ func install() { shareBandwidth := installFlag.Int("sharebandwidth", 10, "N mbps share bandwidth limit, private node no limit") // logLevel := installFlag.Int("loglevel", 1, "0:debug 1:info 2:warn 3:error") installFlag.Parse(os.Args[2:]) + checkParams(*node, *user, *password) + gConf.Network.ServerHost = *serverHost + gConf.Network.User = *user + gConf.Network.Node = *node + gConf.Network.Password = *password + gConf.Network.ServerPort = 27182 + gConf.Network.UDPPort1 = 27182 + gConf.Network.UDPPort2 = 27183 + gConf.Network.NoShare = *noShare + gConf.Network.ShareBandwidth = *shareBandwidth + config := AppConfig{} + config.PeerNode = *peerNode + config.PeerUser = *peerUser + config.PeerPassword = *peerPassword + config.DstHost = *dstIP + config.DstPort = *dstPort + config.SrcPort = *srcPort + config.Protocol = *protocol + gConf.add(config) + os.Chdir(defaultInstallPath) + gConf.save() // copy files os.MkdirAll(defaultInstallPath, 0775) @@ -153,27 +174,6 @@ func install() { } src.Close() dst.Close() - gConf.Network.ServerHost = *serverHost - gConf.Network.User = *user - gConf.Network.Node = *node - gConf.Network.Password = *password - gConf.Network.ServerPort = 27182 - gConf.Network.UDPPort1 = 27182 - gConf.Network.UDPPort2 = 27183 - gConf.Network.NoShare = *noShare - gConf.Network.shareBandwidth = *shareBandwidth - config := AppConfig{} - config.PeerNode = *peerNode - config.PeerUser = *peerUser - config.PeerPassword = *peerPassword - config.DstHost = *dstIP - config.DstPort = *dstPort - config.SrcPort = *srcPort - config.Protocol = *protocol - gConf.add(config) - // TODO other params - os.Chdir(defaultInstallPath) - gConf.save() // install system service d := daemon{} @@ -210,3 +210,18 @@ func uninstall() { os.Rename(binPath, binPath+"0") os.RemoveAll(defaultInstallPath) } + +func checkParams(node, user, password string) { + if len(node) < 8 { + gLog.Println(LevelERROR, "node name too short, it must >=8 charaters") + os.Exit(9) + } + if len(user) < 8 { + gLog.Println(LevelERROR, "user name too short, it must >=8 charaters") + os.Exit(9) + } + if len(password) < 8 { + gLog.Println(LevelERROR, "password too short, it must >=8 charaters") + os.Exit(9) + } +} diff --git a/openp2p.go b/openp2p.go index 27e2d68..0620448 100644 --- a/openp2p.go +++ b/openp2p.go @@ -22,6 +22,14 @@ func main() { case "update": gLog = InitLogger(filepath.Dir(os.Args[0]), "openp2p", LevelDEBUG, 1024*1024, LogFileAndConsole) update() + targetPath := filepath.Join(defaultInstallPath, defaultBinName) + d := daemon{} + err := d.Control("restart", targetPath, []string{"-d", "-f"}) + if err != nil { + gLog.Println(LevelERROR, "restart service error:", err) + } else { + gLog.Println(LevelINFO, "restart service ok.") + } return case "install": install() @@ -50,6 +58,7 @@ func main() { byDaemon := flag.Bool("bydaemon", false, "start by daemon") logLevel := flag.Int("loglevel", 1, "0:debug 1:info 2:warn 3:error") flag.Parse() + gLog = InitLogger(filepath.Dir(os.Args[0]), "openp2p", LogLevel(*logLevel), 1024*1024, LogFileAndConsole) gLog.Println(LevelINFO, "openp2p start. version: ", OpenP2PVersion) if *daemonMode { @@ -59,18 +68,7 @@ func main() { } if !*configFile { // validate cmd params - if *node == "" { - gLog.Println(LevelERROR, "node name not set", os.Args, len(os.Args), os.Args[0]) - return - } - if *user == "" { - gLog.Println(LevelERROR, "user name not set") - return - } - if *password == "" { - gLog.Println(LevelERROR, "password not set") - return - } + checkParams(*node, *user, *password) if *peerNode != "" { if *dstPort == 0 { gLog.Println(LevelERROR, "dstPort not set") @@ -109,7 +107,7 @@ func main() { UDPPort1: 27182, UDPPort2: 27183, ipv6: "240e:3b7:621:def0:fda4:dd7f:36a1:2803", // TODO: detect real ipv6 - shareBandwidth: *shareBandwidth, + ShareBandwidth: *shareBandwidth, } } // gConf.save() // not change config file diff --git a/p2pnetwork.go b/p2pnetwork.go index 58f4bef..0f9376c 100644 --- a/p2pnetwork.go +++ b/p2pnetwork.go @@ -30,7 +30,8 @@ type P2PNetwork struct { restartCh chan bool wg sync.WaitGroup writeMtx sync.Mutex - serverTs uint64 + serverTs int64 + localTs int64 // msgMap sync.Map msgMap map[uint64]chan []byte //key: nodeID msgMapMtx sync.Mutex @@ -48,7 +49,7 @@ func P2PNetworkInstance(config *NetworkConfig) *P2PNetwork { online: false, running: true, msgMap: make(map[uint64]chan []byte), - limiter: newBandwidthLimiter(config.shareBandwidth), + limiter: newBandwidthLimiter(config.ShareBandwidth), } instance.msgMap[0] = make(chan []byte) // for gateway if config != nil { @@ -249,7 +250,7 @@ func (pn *P2PNetwork) AddApp(config AppConfig) error { PeerUser: config.PeerUser, PeerNatType: peerNatType, PeerIP: peerIP, - ShareBandwidth: pn.config.shareBandwidth, + ShareBandwidth: pn.config.ShareBandwidth, RelayNode: relayNode, Version: OpenP2PVersion, } @@ -391,7 +392,6 @@ func (pn *P2PNetwork) init() error { q.Add("password", pn.config.Password) q.Add("version", OpenP2PVersion) q.Add("nattype", fmt.Sprintf("%d", pn.config.natType)) - q.Add("timestamp", fmt.Sprintf("%d", time.Now().Unix())) noShareStr := "false" if pn.config.NoShare { @@ -465,8 +465,9 @@ func (pn *P2PNetwork) handleMessage(t int, msg []byte) { gLog.Printf(LevelERROR, "login error:%d, detail:%s", rsp.Error, rsp.Detail) pn.running = false } else { - gLog.Printf(LevelINFO, "login ok. Server ts=%d, local ts=%d", rsp.Ts, time.Now().Unix()) pn.serverTs = rsp.Ts + pn.localTs = time.Now().Unix() + gLog.Printf(LevelINFO, "login ok. Server ts=%d, local ts=%d", rsp.Ts, pn.localTs) } case MsgHeartbeat: gLog.Printf(LevelDEBUG, "P2PNetwork heartbeat ok") @@ -609,7 +610,9 @@ func (pn *P2PNetwork) handlePush(subType uint16, msg []byte) error { gLog.Printf(LevelINFO, "%s is connecting...", req.From) gLog.Println(LevelDEBUG, "push connect response to ", req.From) // verify token or name&password - if VerifyTOTP(req.Token, pn.config.User, pn.config.Password, time.Now().Unix()) || (req.User == pn.config.User && req.Password == pn.config.Password) { + if VerifyTOTP(req.Token, pn.config.User, pn.config.Password, time.Now().Unix()+(pn.serverTs-pn.localTs)) || // localTs may behind, auto adjust ts + VerifyTOTP(req.Token, pn.config.User, pn.config.Password, time.Now().Unix()) || + (req.User == pn.config.User && req.Password == pn.config.Password) { gLog.Printf(LevelINFO, "Access Granted\n") config := AppConfig{} config.peerNatType = req.NatType @@ -618,8 +621,8 @@ func (pn *P2PNetwork) handlePush(subType uint16, msg []byte) error { config.PeerNode = req.From // share relay node will limit bandwidth if req.User != pn.config.User || req.Password != pn.config.Password { - gLog.Printf(LevelINFO, "set share bandwidth %d mbps", pn.config.shareBandwidth) - config.shareBandwidth = pn.config.shareBandwidth + gLog.Printf(LevelINFO, "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) diff --git a/protocol.go b/protocol.go index 22b4978..de4575b 100644 --- a/protocol.go +++ b/protocol.go @@ -11,7 +11,7 @@ import ( "time" ) -const OpenP2PVersion = "0.96.1" +const OpenP2PVersion = "0.97.0" const ProducnName string = "openp2p" type openP2PHeader struct { @@ -198,7 +198,7 @@ type PushRsp struct { type LoginRsp struct { Error int `json:"error,omitempty"` Detail string `json:"detail,omitempty"` - Ts uint64 `json:"ts,omitempty"` + Ts int64 `json:"ts,omitempty"` } type NatDetectReq struct {