From eab5708b33a1fb6f5586db98c3a0d146a58e979f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B2=B3?= Date: Mon, 16 Dec 2019 22:50:34 +0800 Subject: [PATCH] change md and register npc as a service --- README.md | 2 ++ cmd/npc/npc.go | 30 +++++++++++++++------------- docs/run.md | 2 ++ docs/use.md | 53 +++++++++++++++++++++++++++++--------------------- 4 files changed, 51 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index e12f47a..d1f8958 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,8 @@ nps是一款轻量级、高性能、功能强大的**内网穿透**代理服务 - 点击web管理中客户端前的+号,复制启动命令 - 执行启动命令,linux直接执行即可,windows将./npc换成npc.exe用cmd执行 +如果需要注册到系统服务可查看[注册到系统服务](https://cnlh.github.io/nps/#/use?id=注册到系统服务) + ### 配置 - 客户端连接后,在web中配置对应穿透服务即可 - 更多高级用法见[完整文档](https://cnlh.github.io/nps/) diff --git a/cmd/npc/npc.go b/cmd/npc/npc.go index d4ec4bd..ddce289 100644 --- a/cmd/npc/npc.go +++ b/cmd/npc/npc.go @@ -32,8 +32,7 @@ var ( target = flag.String("target", "", "p2p target") localType = flag.String("local_type", "p2p", "p2p target") logPath = flag.String("log_path", "", "npc log path") - debug = flag.Bool("debug", false, "npc debug") - srv = flag.String("service", "", "service option") + debug = flag.Bool("debug", true, "npc debug") ) func main() { @@ -69,10 +68,15 @@ func main() { "After=network-online.target syslog.target"} } for _, v := range os.Args[1:] { - if !strings.Contains(v, "-service=") { + switch v { + case "install", "start", "stop", "uninstall", "restart": + continue + } + if !strings.Contains(v, "-service=") && !strings.Contains(v, "-debug=") { svcConfig.Arguments = append(svcConfig.Arguments, v) } } + svcConfig.Arguments = append(svcConfig.Arguments, "-debug=false") prg := &npc{ exit: make(chan struct{}), } @@ -102,19 +106,17 @@ func main() { } fmt.Printf("nat type: %s \npublic address: %s\n", nat.String(), host.String()) os.Exit(0) + case "install", "start", "stop", "uninstall", "restart": + if os.Args[1] == "install" { + install.InstallNpc() + } + err := service.Control(s, os.Args[1]) + if err != nil { + logs.Error("Valid actions: %q\n", service.ControlAction, err.Error()) + } + return } } - - if *srv != "" { - if *srv == "install" { - install.InstallNpc() - } - err := service.Control(s, *srv) - if err != nil { - logs.Error("Valid actions: %q\n", service.ControlAction, err.Error()) - } - return - } s.Run() } diff --git a/docs/run.md b/docs/run.md index f3e566b..bc79756 100644 --- a/docs/run.md +++ b/docs/run.md @@ -26,6 +26,8 @@ - 点击web管理中客户端前的+号,复制启动命令 - 执行启动命令,linux直接执行即可,windows将./npc换成npc.exe用cmd执行 +如果需要注册到系统服务可查看[注册到系统服务](/use?id=注册到系统服务) + ## 配置 - 客户端连接后,在web中配置对应穿透服务即可 - 可以查看[使用示例](/example) diff --git a/docs/use.md b/docs/use.md index 183d41e..deeda30 100644 --- a/docs/use.md +++ b/docs/use.md @@ -2,34 +2,43 @@ ## 无配置文件模式 此模式的各种配置在服务端web管理中完成,客户端除运行一条命令外无需任何其他设置 ``` - ./npc -server=ip:port -vkey=web界面中显示的密钥 + ./npc -debug=true -server=ip:port -vkey=web界面中显示的密钥 ``` +## 注册到系统服务 +对于linux、darwin +- 注册:`sudo ./npc install 其他参数(例如-server=xx -vkey=xx或者-config=xxx)` +- 启动:`sudo ./npc start` +- 停止:`sudo ./npc stop` +- 如果需要更换命令内容需要先卸载`./npc -service=uninstall`,再重新注册 + +对于windows,使用管理员身份运行cmd + +- 注册:`npc.exe install 其他参数(例如-server=xx -vkey=xx或者-config=xxx)` +- 启动:`npc.exe start` +- 停止:`npc.exe stop` +- 如果需要更换命令内容需要先卸载`npc.exe -service=uninstall`,再重新注册 + +## 客户端更新 +首先进入到对于的客户端二进制文件目录 + +请首先执行`sudo ./npc stop`或者`nps.exe stop`停止运行,然后 + +对于linux +```shell + sudo ./npc-update update +``` +对于windows +```shell +npc-update.exe update +``` + +更新完成后,执行执行`sudo nps start`或者`nps.exe start`重新运行即可完成升级 + ## 配置文件模式 此模式使用nps的公钥或者客户端私钥验证,各种配置在客户端完成,同时服务端web也可以进行管理 ``` ./npc -config=npc配置文件路径 ``` -可自行添加systemd service,例如:`npc.service` -``` -[Unit] -Description=npc - convenient proxy server client -Documentation=https://github.com/cnlh/nps/ -After=network-online.target remote-fs.target nss-lookup.target -Wants=network-online.target - -[Service] -Type=simple -KillMode=process -Restart=always -RestartSec=15s -StandardOutput=append:/var/log/nps/npc.log -ExecStartPre=/bin/echo 'Starting npc' -ExecStopPost=/bin/echo 'Stopping npc' -ExecStart=/absolutely path to/npc -server=ip:port -vkey=web界面中显示的密钥 - -[Install] -WantedBy=multi-user.target -``` ## 配置文件说明 [示例配置文件](https://github.com/cnlh/nps/tree/master/conf/npc.conf) #### 全局配置