Skip to content

Commit

Permalink
auto adjust server and local timestamp for totp
Browse files Browse the repository at this point in the history
  • Loading branch information
TenderIronh committed Dec 14, 2021
1 parent dd3d87c commit 6c1551d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 45 deletions.
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type NetworkConfig struct {
os string
publicIP string
natType int
shareBandwidth int
ShareBandwidth int
// server info
ServerHost string
ServerPort int
Expand Down
57 changes: 36 additions & 21 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{}
Expand Down Expand Up @@ -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)
}
}
24 changes: 11 additions & 13 deletions openp2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down
19 changes: 11 additions & 8 deletions p2pnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"time"
)

const OpenP2PVersion = "0.96.1"
const OpenP2PVersion = "0.97.0"
const ProducnName string = "openp2p"

type openP2PHeader struct {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6c1551d

Please sign in to comment.