From 3ec790d98d9a04fb6b2f16a2b55a8aef00b0cec7 Mon Sep 17 00:00:00 2001 From: ffdfgdfg Date: Tue, 18 Feb 2020 22:53:37 +0800 Subject: [PATCH] run without service #407 --- cmd/npc/npc.go | 24 +++++++++++++++++------- cmd/nps/nps.go | 36 +++++++++++++++++++++++------------- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/cmd/npc/npc.go b/cmd/npc/npc.go index 781dc1f0..67c12546 100644 --- a/cmd/npc/npc.go +++ b/cmd/npc/npc.go @@ -15,6 +15,7 @@ import ( "os" "runtime" "strings" + "sync" "time" ) @@ -107,7 +108,12 @@ func main() { } s, err := service.New(prg, svcConfig) if err != nil { - logs.Error(err) + logs.Error(err, "service function disabled") + run() + // run without service + wg := sync.WaitGroup{} + wg.Add(1) + wg.Wait() return } if len(os.Args) >= 2 { @@ -172,6 +178,15 @@ func (p *npc) run() error { logs.Warning("npc: panic serving %v: %v\n%s", err, string(buf)) } }() + run() + select { + case <-p.exit: + logs.Warning("stop...") + } + return nil +} + +func run() { common.InitPProfFromArg(*pprofAddr) //p2p or secret command if *password != "" { @@ -187,7 +202,7 @@ func (p *npc) run() error { commonConfig.Client = new(file.Client) commonConfig.Client.Cnf = new(file.Config) go client.StartLocalServer(localServer, commonConfig) - return nil + return } env := common.GetEnvMap() if *serverAddr == "" { @@ -211,9 +226,4 @@ func (p *npc) run() error { } go client.StartFromFile(*configPath) } - select { - case <-p.exit: - logs.Warning("stop...") - } - return nil } diff --git a/cmd/nps/nps.go b/cmd/nps/nps.go index 93640ae7..7746db78 100644 --- a/cmd/nps/nps.go +++ b/cmd/nps/nps.go @@ -1,26 +1,27 @@ package main import ( + "ehang.io/nps/lib/crypt" + "ehang.io/nps/lib/file" "ehang.io/nps/lib/install" + "ehang.io/nps/lib/version" + "ehang.io/nps/server" + "ehang.io/nps/server/connection" + "ehang.io/nps/server/tool" + "ehang.io/nps/web/routers" "flag" "log" "os" "path/filepath" "runtime" "strings" + "sync" "ehang.io/nps/lib/common" - "ehang.io/nps/lib/crypt" "ehang.io/nps/lib/daemon" - "ehang.io/nps/lib/file" - "ehang.io/nps/lib/version" - "ehang.io/nps/server" - "ehang.io/nps/server/connection" - "ehang.io/nps/server/tool" "github.com/astaxie/beego" "github.com/astaxie/beego/logs" - "ehang.io/nps/web/routers" "github.com/kardianos/service" ) @@ -97,7 +98,12 @@ func main() { prg.exit = make(chan struct{}) s, err := service.New(prg, svcConfig) if err != nil { - logs.Error(err) + logs.Error(err, "service function disabled") + run() + // run without service + wg := sync.WaitGroup{} + wg.Add(1) + wg.Wait() return } if len(os.Args) > 1 && os.Args[1] != "service" { @@ -166,6 +172,15 @@ func (p *nps) run() error { logs.Warning("nps: panic serving %v: %v\n%s", err, string(buf)) } }() + run() + select { + case <-p.exit: + logs.Warning("stop...") + } + return nil +} + +func run() { routers.Init() task := &file.Tunnel{ Mode: "webServer", @@ -181,9 +196,4 @@ func (p *nps) run() error { tool.InitAllowPort() tool.StartSystemInfo() go server.StartNewServer(bridgePort, task, beego.AppConfig.String("bridge_type")) - select { - case <-p.exit: - logs.Warning("stop...") - } - return nil }