web console

This commit is contained in:
TenderIronh
2022-02-03 23:43:28 +08:00
parent 7686af39e0
commit 95b46f51d0
17 changed files with 406 additions and 343 deletions
+64 -47
View File
@@ -8,8 +8,6 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"syscall"
"time"
)
@@ -30,10 +28,10 @@ func handlePush(pn *P2PNetwork, 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()+(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) {
// verify totp token or token
if VerifyTOTP(req.Token, pn.config.Token, time.Now().Unix()+(pn.serverTs-pn.localTs)) || // localTs may behind, auto adjust ts
VerifyTOTP(req.Token, pn.config.Token, time.Now().Unix()) ||
(req.FromToken == pn.config.Token) {
gLog.Printf(LevelINFO, "Access Granted\n")
config := AppConfig{}
config.peerNatType = req.NatType
@@ -41,7 +39,7 @@ func handlePush(pn *P2PNetwork, subType uint16, msg []byte) error {
config.peerIP = req.FromIP
config.PeerNode = req.From
// share relay node will limit bandwidth
if req.User != pn.config.User || req.Password != pn.config.Password {
if req.FromToken != pn.config.Token {
gLog.Printf(LevelINFO, "set share bandwidth %d mbps", pn.config.ShareBandwidth)
config.shareBandwidth = pn.config.ShareBandwidth
}
@@ -79,9 +77,6 @@ func handlePush(pn *P2PNetwork, subType uint16, msg []byte) error {
config := AppConfig{}
config.PeerNode = req.RelayName
config.peerToken = req.RelayToken
// set user password, maybe the relay node is your private node
config.PeerUser = pn.config.User
config.PeerPassword = pn.config.Password
go func(r AddRelayTunnelReq) {
t, errDt := pn.addDirectTunnel(config, 0)
if errDt == nil {
@@ -93,23 +88,25 @@ func handlePush(pn *P2PNetwork, subType uint16, msg []byte) error {
}(req)
case MsgPushUpdate:
gLog.Println(LevelINFO, "MsgPushUpdate")
update() // download new version first, then exec ./openp2p update
targetPath := filepath.Join(defaultInstallPath, defaultBinName)
args := []string{"update"}
env := os.Environ()
// Windows does not support exec syscall.
if runtime.GOOS == "windows" {
cmd := exec.Command(targetPath, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Env = env
err := cmd.Run()
if err == nil {
os.Exit(0)
}
return err
cmd := exec.Command(targetPath, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Env = env
err := cmd.Run()
if err == nil {
os.Exit(0)
}
return syscall.Exec(targetPath, args, env)
return err
case MsgPushRestart:
gLog.Println(LevelINFO, "MsgPushRestart")
os.Exit(0)
return err
case MsgPushReportApps:
gLog.Println(LevelINFO, "MsgPushReportApps")
req := ReportApps{}
@@ -117,6 +114,14 @@ func handlePush(pn *P2PNetwork, subType uint16, msg []byte) error {
gConf.mtx.Lock()
defer gConf.mtx.Unlock()
for _, config := range gConf.Apps {
appActive := 0
i, ok := pn.apps.Load(fmt.Sprintf("%s%d", config.Protocol, config.SrcPort))
if ok {
app := i.(*p2pApp)
if app.isActive() {
appActive = 1
}
}
appInfo := AppInfo{
AppName: config.AppName,
Protocol: config.Protocol,
@@ -129,29 +134,11 @@ func handlePush(pn *P2PNetwork, subType uint16, msg []byte) error {
PeerIP: config.peerIP,
PeerNatType: config.peerNatType,
RetryTime: config.retryTime.String(),
IsActive: 1,
IsActive: appActive,
Enabled: config.Enabled,
}
req.Apps = append(req.Apps, appInfo)
}
// pn.apps.Range(func(_, i interface{}) bool {
// app := i.(*p2pApp)
// appInfo := AppInfo{
// AppName: app.config.AppName,
// Protocol: app.config.Protocol,
// SrcPort: app.config.SrcPort,
// RelayNode: app.relayNode,
// PeerNode: app.config.PeerNode,
// DstHost: app.config.DstHost,
// DstPort: app.config.DstPort,
// PeerUser: app.config.PeerUser,
// PeerIP: app.config.peerIP,
// PeerNatType: app.config.peerNatType,
// RetryTime: app.config.retryTime.String(),
// IsActive: 1,
// }
// req.Apps = append(req.Apps, appInfo)
// return true
// })
pn.write(MsgReport, MsgReportApps, &req)
case MsgPushEditApp:
gLog.Println(LevelINFO, "MsgPushEditApp")
@@ -161,7 +148,7 @@ func handlePush(pn *P2PNetwork, subType uint16, msg []byte) error {
gLog.Printf(LevelERROR, "wrong MsgPushEditApp:%s %s", err, string(msg[openP2PHeaderSize:]))
return err
}
var oldConf AppConfig
oldConf := AppConfig{Enabled: 1}
// protocol0+srcPort0 exist, delApp
oldConf.AppName = newApp.AppName
oldConf.Protocol = newApp.Protocol0
@@ -175,12 +162,42 @@ func handlePush(pn *P2PNetwork, subType uint16, msg []byte) error {
newConf := oldConf
newConf.Protocol = newApp.Protocol
newConf.SrcPort = newApp.SrcPort
gConf.add(newConf)
gConf.save()
pn.DeleteApp(oldConf) // save quickly for the next request reportApplist
gConf.add(newConf, false)
gConf.save() // save quickly for the next request reportApplist
pn.DeleteApp(oldConf) // DeleteApp may cost some times, execute at the end
// autoReconnect will auto AddApp
// pn.AddApp(config)
// TODO: report result
case MsgPushEditNode:
gLog.Println(LevelINFO, "MsgPushEditNode")
req := EditNode{}
err := json.Unmarshal(msg[openP2PHeaderSize:], &req)
if err != nil {
gLog.Printf(LevelERROR, "wrong MsgPushEditNode:%s %s", err, string(msg[openP2PHeaderSize:]))
return err
}
gConf.mtx.Lock()
gConf.Network.Node = req.NewName
gConf.Network.ShareBandwidth = req.Bandwidth
gConf.mtx.Unlock()
gConf.save()
// TODO: hot reload
os.Exit(0)
case MsgPushSwitchApp:
gLog.Println(LevelINFO, "MsgPushSwitchApp")
app := AppInfo{}
err := json.Unmarshal(msg[openP2PHeaderSize:], &app)
if err != nil {
gLog.Printf(LevelERROR, "wrong MsgPushSwitchApp:%s %s", err, string(msg[openP2PHeaderSize:]))
return err
}
config := AppConfig{Enabled: app.Enabled, SrcPort: app.SrcPort, Protocol: app.Protocol}
gLog.Println(LevelINFO, app.AppName, " switch to ", app.Enabled)
gConf.switchApp(config, app.Enabled)
if app.Enabled == 0 {
// disable APP
pn.DeleteApp(config)
}
default:
pn.msgMapMtx.Lock()
ch := pn.msgMap[pushHead.From]