diff --git a/.travis.yml b/.travis.yml index f076c5c..d5c3457 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ script: os: - linux before_deploy: - - chmod +x ./build.sh && ./build.sh + - chmod +x ./build.sh && chmod +x ./build.android.sh && ./build.sh deploy: provider: releases @@ -44,14 +44,15 @@ deploy: - linux_mipsle_server.tar.gz - linux_mips_client.tar.gz - linux_mips_server.tar.gz - - macos_client.tar.gz - - macos_server.tar.gz - - win_386_client.tar.gz - - win_386_server.tar.gz - - win_amd64_client.tar.gz - - win_amd64_server.tar.gz + - darwin_amd64_client.tar.gz + - darwin_amd64_server.tar.gz + - windows_386_client.tar.gz + - windows_386_server.tar.gz + - windows_amd64_client.tar.gz + - windows_amd64_server.tar.gz - npc_syno.spk - npc_sdk.tar.gz + - android_client.apk on: tags: true all_branches: true diff --git a/README.md b/README.md index 8ef2b90..d1f8958 100644 --- a/README.md +++ b/README.md @@ -28,19 +28,32 @@ nps是一款轻量级、高性能、功能强大的**内网穿透**代理服务 下载对应的系统版本即可,服务端和客户端是单独的 ### 服务端启动 -1. 进入服务端启动 -```shell - ./nps -``` -如有错误修改配置文件相应端口,无错误可继续进行下去 +下载完服务器压缩包后,解压,然后进入解压后的文件夹 -2. 访问服务端ip:web服务端口(默认为8024) -3. 使用用户名和密码登陆(默认admin/123,正式使用一定要更改) -4. 在web中创建客户端 +- 执行安装命令 + +对于linux|darwin ```sudo ./nps install``` + +对于windows,管理员身份运行cmd,进入安装目录 ```nps.exe install``` + +- 启动 + +对于linux|darwin ```sudo nps start``` + +对于windows,管理员身份运行cmd,进入程序目录 ```nps.exe start``` + +停止和重启可用,stop和restart + +**如果发现没有启动成功,可以查看日志(Windows日志文件位于当前运行目录下,linux和darwin位于/var/log/nps.log)** +- 访问服务端ip:web服务端口(默认为8080) +- 使用用户名和密码登陆(默认admin/123,正式使用一定要更改) +- 创建客户端 ### 客户端连接 -1. 点击web管理中客户端前的+号,复制启动命令 -2. 执行启动命令,linux直接执行即可,windows将./npc换成npc.exe用cmd执行 +- 点击web管理中客户端前的+号,复制启动命令 +- 执行启动命令,linux直接执行即可,windows将./npc换成npc.exe用cmd执行 + +如果需要注册到系统服务可查看[注册到系统服务](https://cnlh.github.io/nps/#/use?id=注册到系统服务) ### 配置 - 客户端连接后,在web中配置对应穿透服务即可 diff --git a/build.android.sh b/build.android.sh new file mode 100644 index 0000000..cae6d93 --- /dev/null +++ b/build.android.sh @@ -0,0 +1,17 @@ +#/bin/bash +#sudo apt-get install libgl1-mesa-dev xorg-dev +#go get github.com/ffdfgdfg/fyne-cross +#fyne-cross --targets=linux/amd64,windows/amd64,darwin/amd64 gui/npc/npc.go + +mkdir -p /go/src/github.com/cnlh/nps +cp -R * /go/src/github.com/cnlh/nps +cd /go/src/github.com/cnlh/nps +go get -u fyne.io/fyne fyne.io/fyne/cmd/fyne +go mod vendor +cd vendor +cp -R * /go/src +cd .. +rm -rf vendor +cd gui/npc +fyne package -os android -appID org.nps.client -icon ../../docs/logo.png +mv npc.apk /app/android_client.apk diff --git a/build.bash b/build.bash deleted file mode 100755 index b7296ca..0000000 --- a/build.bash +++ /dev/null @@ -1,136 +0,0 @@ -#!/bin/bash -CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go -upx npc -tar -czvf linux_amd64_client.tar.gz npc conf/npc.conf - -CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go -upx npc -tar -czvf linux_386_client.tar.gz npc conf/npc.conf - -CGO_ENABLED=0 GOOS=freebsd GOARCH=386 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go -upx npc -tar -czvf freebsd_386_client.tar.gz npc conf/npc.conf - -CGO_ENABLED=0 GOOS=freebsd GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go -upx npc -tar -czvf freebsd_amd64_client.tar.gz npc conf/npc.conf - -CGO_ENABLED=0 GOOS=freebsd GOARCH=arm go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go -upx npc -tar -czvf freebsd_arm_client.tar.gz npc conf/npc.conf - -CGO_ENABLED=0 GOOS=linux GOARCH=arm go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go -upx npc -tar -czvf linux_arm_client.tar.gz npc conf/npc.conf - - -CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go -upx npc -tar -czvf linux_arm64_client.tar.gz npc conf/npc.conf - - -CGO_ENABLED=0 GOOS=linux GOARCH=mips64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go -upx npc -tar -czvf linux_mips64_client.tar.gz npc conf/npc.conf - - -CGO_ENABLED=0 GOOS=linux GOARCH=mips64le go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go -upx npc -tar -czvf linux_mips64le_client.tar.gz npc conf/npc.conf - - -CGO_ENABLED=0 GOOS=linux GOARCH=mipsle go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go -upx npc -tar -czvf linux_mipsle_client.tar.gz npc conf/npc.conf - - -CGO_ENABLED=0 GOOS=linux GOARCH=mips go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go -upx npc -tar -czvf linux_mips_client.tar.gz npc conf/npc.conf - - -CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go -upx npc -tar -czvf win_386_client.tar.gz npc.exe conf/npc.conf - - -CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go -upx npc -tar -czvf win_amd64_client.tar.gz npc.exe conf/npc.conf - - -CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go -upx npc -tar -czvf macos_client.tar.gz npc conf/npc.conf - - - -CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go -upx nps -tar -czvf linux_amd64_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps - -CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go -upx nps -tar -czvf linux_386_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps - -CGO_ENABLED=0 GOOS=linux GOARCH=arm go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go -upx nps -tar -czvf linux_arm_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps - - -CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go -upx nps -tar -czvf linux_arm64_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps - - -CGO_ENABLED=0 GOOS=freebsd GOARCH=arm go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go -upx nps -tar -czvf freebsd_arm_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps - - -CGO_ENABLED=0 GOOS=freebsd GOARCH=386 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go -upx nps -tar -czvf freebsd_386_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps - - -CGO_ENABLED=0 GOOS=freebsd GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go -upx nps -tar -czvf freebsd_amd64_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps - - - -CGO_ENABLED=0 GOOS=linux GOARCH=mips go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go -upx nps -tar -czvf linux_mips_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps - - -CGO_ENABLED=0 GOOS=linux GOARCH=mips64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go -upx nps -tar -czvf linux_mips64_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps - - -CGO_ENABLED=0 GOOS=linux GOARCH=mips64le go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go -upx nps -tar -czvf linux_mips64le_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps - - -CGO_ENABLED=0 GOOS=linux GOARCH=mipsle go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go -upx nps -tar -czvf linux_mipsle_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps - - - -CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go -upx nps -tar -czvf macos_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps - - -CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go -upx nps.exe -tar -czvf win_amd64_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps.exe - - -CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go -upx nps.exe -tar -czvf win_386_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps.exe - diff --git a/build.sh b/build.sh old mode 100644 new mode 100755 index df967c4..56e77f9 --- a/build.sh +++ b/build.sh @@ -1,5 +1,5 @@ #/bash/sh -export VERSION=0.25.1 +export VERSION=0.25.2 sudo apt-get install gcc-mingw-w64-i686 env GOOS=windows GOARCH=386 CGO_ENABLED=1 CC=i686-w64-mingw32-gcc go build -ldflags "-s -w -extldflags -static -extldflags -static" -buildmode=c-shared -o npc_sdk.dll cmd/npc/sdk.go @@ -69,17 +69,17 @@ tar -czvf linux_mips_client.tar.gz npc conf/npc.conf conf/multi_account.conf CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go -tar -czvf win_386_client.tar.gz npc.exe conf/npc.conf conf/multi_account.conf +tar -czvf windows_386_client.tar.gz npc.exe conf/npc.conf conf/multi_account.conf CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go -tar -czvf win_amd64_client.tar.gz npc.exe conf/npc.conf conf/multi_account.conf +tar -czvf windows_amd64_client.tar.gz npc.exe conf/npc.conf conf/multi_account.conf CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go -tar -czvf macos_client.tar.gz npc conf/npc.conf conf/multi_account.conf +tar -czvf darwin_amd64_client.tar.gz npc conf/npc.conf conf/multi_account.conf CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go @@ -146,23 +146,24 @@ tar -czvf linux_mipsle_server.tar.gz conf/nps.conf conf/tasks.json conf/clients. CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go -tar -czvf macos_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps +tar -czvf darwin_amd64_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go -tar -czvf win_amd64_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps.exe +tar -czvf windows_amd64_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps.exe CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go -tar -czvf win_386_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps.exe +tar -czvf windows_386_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps.exe curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" sudo apt-get update sudo apt-get -y -o Dpkg::Options::="--force-confnew" install docker-ce docker --version +docker run --rm -i -w /app -v $(pwd):/app -e ANDROID_HOME=/usr/local/android_sdk ffdfgdfg/fyne-cross:android /app/build.android.sh git clone https://github.com/cnlh/spksrc.git ~/spksrc mkdir ~/spksrc/nps && cp -rf ./* ~/spksrc/nps/ docker run -itd --name spksrc --env VERSION=$VERSION -v ~/spksrc:/spksrc synocommunity/spksrc /bin/bash diff --git a/cmd/npc/npc.go b/cmd/npc/npc.go index fe5b3f5..ddce289 100644 --- a/cmd/npc/npc.go +++ b/cmd/npc/npc.go @@ -3,18 +3,19 @@ package main import ( "flag" "fmt" - "os" - "strings" - "time" - "github.com/astaxie/beego/logs" "github.com/ccding/go-stun/stun" "github.com/cnlh/nps/client" "github.com/cnlh/nps/lib/common" "github.com/cnlh/nps/lib/config" - "github.com/cnlh/nps/lib/daemon" "github.com/cnlh/nps/lib/file" + "github.com/cnlh/nps/lib/install" "github.com/cnlh/nps/lib/version" + "github.com/kardianos/service" + "os" + "runtime" + "strings" + "time" ) var ( @@ -30,11 +31,60 @@ var ( password = flag.String("password", "", "p2p password flag") target = flag.String("target", "", "p2p target") localType = flag.String("local_type", "p2p", "p2p target") - logPath = flag.String("log_path", "npc.log", "npc log path") + logPath = flag.String("log_path", "", "npc log path") + debug = flag.Bool("debug", true, "npc debug") ) func main() { flag.Parse() + logs.Reset() + logs.EnableFuncCallDepth(true) + logs.SetLogFuncCallDepth(3) + if *logPath == "" { + *logPath = common.GetNpcLogPath() + } + if common.IsWindows() { + *logPath = strings.Replace(*logPath, "\\", "\\\\", -1) + } + if *debug { + logs.SetLogger(logs.AdapterConsole, `{"level":`+*logLevel+`,"color":true}`) + } else { + logs.SetLogger(logs.AdapterFile, `{"level":`+*logLevel+`,"filename":"`+*logPath+`","daily":false,"maxlines":100000,"color":true}`) + } + + // init service + options := make(service.KeyValue) + options["Restart"] = "on-success" + options["SuccessExitStatus"] = "1 2 8 SIGKILL" + svcConfig := &service.Config{ + Name: "Npc", + DisplayName: "nps内网穿透客户端", + Description: "一款轻量级、功能强大的内网穿透代理服务器。支持tcp、udp流量转发,支持内网http代理、内网socks5代理,同时支持snappy压缩、站点保护、加密传输、多路复用、header修改等。支持web图形化管理,集成多用户模式。", + Option: options, + } + if !common.IsWindows() { + svcConfig.Dependencies = []string{ + "Requires=network.target", + "After=network-online.target syslog.target"} + } + for _, v := range os.Args[1:] { + 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{}), + } + s, err := service.New(prg, svcConfig) + if err != nil { + logs.Error(err) + return + } if len(os.Args) >= 2 { switch os.Args[1] { case "status": @@ -45,6 +95,9 @@ func main() { case "register": flag.CommandLine.Parse(os.Args[2:]) client.RegisterLocalIp(*serverAddr, *verifyKey, *connType, *proxyUrl, *registerTime) + case "update": + install.UpdateNpc() + return case "nat": nat, host, err := stun.NewClient().Discover() if err != nil || host == nil { @@ -53,16 +106,45 @@ 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 } } - daemon.InitDaemon("npc", common.GetRunPath(), common.GetTmpPath()) - logs.EnableFuncCallDepth(true) - logs.SetLogFuncCallDepth(3) - if *logType == "stdout" { - logs.SetLogger(logs.AdapterConsole, `{"level":`+*logLevel+`,"color":true}`) - } else { - logs.SetLogger(logs.AdapterFile, `{"level":`+*logLevel+`,"filename":"`+*logPath+`","daily":false,"maxlines":100000,"color":true}`) + s.Run() +} + +type npc struct { + exit chan struct{} +} + +func (p *npc) Start(s service.Service) error { + go p.run() + return nil +} +func (p *npc) Stop(s service.Service) error { + close(p.exit) + if service.Interactive() { + os.Exit(0) } + return nil +} + +func (p *npc) run() error { + defer func() { + if err := recover(); err != nil { + const size = 64 << 10 + buf := make([]byte, size) + buf = buf[:runtime.Stack(buf, false)] + logs.Warning("npc: panic serving %v: %v\n%s", err, string(buf)) + } + }() //p2p or secret command if *password != "" { commonConfig := new(config.CommonConfig) @@ -76,8 +158,8 @@ func main() { localServer.Port = *localPort commonConfig.Client = new(file.Client) commonConfig.Client.Cnf = new(file.Config) - client.StartLocalServer(localServer, commonConfig) - return + go client.StartLocalServer(localServer, commonConfig) + return nil } env := common.GetEnvMap() if *serverAddr == "" { @@ -88,15 +170,22 @@ func main() { } logs.Info("the version of client is %s, the core version of client is %s", version.VERSION, version.GetVersion()) if *verifyKey != "" && *serverAddr != "" && *configPath == "" { - for { - client.NewRPClient(*serverAddr, *verifyKey, *connType, *proxyUrl, nil).Start() - logs.Info("It will be reconnected in five seconds") - time.Sleep(time.Second * 5) - } + go func() { + for { + client.NewRPClient(*serverAddr, *verifyKey, *connType, *proxyUrl, nil).Start() + logs.Info("It will be reconnected in five seconds") + time.Sleep(time.Second * 5) + } + }() } else { if *configPath == "" { - *configPath = "npc.conf" + *configPath = "conf/npc.conf" } - client.StartFromFile(*configPath) + 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 98f13af..95ddf9c 100644 --- a/cmd/nps/nps.go +++ b/cmd/nps/nps.go @@ -2,9 +2,12 @@ package main import ( "flag" + "github.com/cnlh/nps/lib/install" "log" "os" "path/filepath" + "runtime" + "strings" "github.com/astaxie/beego" "github.com/astaxie/beego/logs" @@ -12,14 +15,13 @@ import ( "github.com/cnlh/nps/lib/crypt" "github.com/cnlh/nps/lib/daemon" "github.com/cnlh/nps/lib/file" - "github.com/cnlh/nps/lib/install" "github.com/cnlh/nps/lib/version" "github.com/cnlh/nps/server" "github.com/cnlh/nps/server/connection" - "github.com/cnlh/nps/server/test" "github.com/cnlh/nps/server/tool" "github.com/cnlh/nps/web/routers" + "github.com/kardianos/service" ) var ( @@ -29,20 +31,9 @@ var ( func main() { flag.Parse() - beego.LoadAppConfig("ini", filepath.Join(common.GetRunPath(), "conf", "nps.conf")) - routers.Init() - if len(os.Args) > 1 { - switch os.Args[1] { - case "test": - test.TestServerConfig() - log.Println("test ok, no error") - return - case "start", "restart", "stop", "status", "reload": - daemon.InitDaemon("nps", common.GetRunPath(), common.GetTmpPath()) - case "install": - install.InstallNps() - return - } + // init log + if err := beego.LoadAppConfig("ini", filepath.Join(common.GetRunPath(), "conf", "nps.conf")); err != nil { + log.Fatalln("load config file error", err.Error()) } if level = beego.AppConfig.String("log_level"); level == "" { level = "7" @@ -50,11 +41,102 @@ func main() { logs.Reset() logs.EnableFuncCallDepth(true) logs.SetLogFuncCallDepth(3) - if *logType == "stdout" { - logs.SetLogger(logs.AdapterConsole, `{"level":`+level+`,"color":true}`) - } else { - logs.SetLogger(logs.AdapterFile, `{"level":`+level+`,"filename":"`+beego.AppConfig.String("log_path")+`","daily":false,"maxlines":100000,"color":true}`) + logPath := beego.AppConfig.String("log_path") + if logPath == "" { + logPath = common.GetLogPath() } + if common.IsWindows() { + logPath = strings.Replace(logPath, "\\", "\\\\", -1) + } + logs.SetLogger(logs.AdapterFile, `{"level":`+level+`,"filename":"`+logPath+`","daily":false,"maxlines":100000,"color":true}`) + // init service + options := make(service.KeyValue) + options["Restart"] = "on-success" + options["SuccessExitStatus"] = "1 2 8 SIGKILL" + svcConfig := &service.Config{ + Name: "Nps", + DisplayName: "nps内网穿透代理服务器", + Description: "一款轻量级、功能强大的内网穿透代理服务器。支持tcp、udp流量转发,支持内网http代理、内网socks5代理,同时支持snappy压缩、站点保护、加密传输、多路复用、header修改等。支持web图形化管理,集成多用户模式。", + Option: options, + } + if !common.IsWindows() { + svcConfig.Dependencies = []string{ + "Requires=network.target", + "After=network-online.target syslog.target"} + } + prg := &nps{} + prg.exit = make(chan struct{}) + s, err := service.New(prg, svcConfig) + if err != nil { + logs.Error(err) + return + } + if len(os.Args) > 1 { + switch os.Args[1] { + case "debug": + logs.SetLogger(logs.AdapterConsole, `{"level":`+level+`,"color":true}`) + case "reload": + daemon.InitDaemon("nps", common.GetRunPath(), common.GetTmpPath()) + return + case "install": + // uninstall before + service.Control(s, "uninstall") + + binPath := install.InstallNps() + svcConfig.Executable = binPath + s, err := service.New(prg, svcConfig) + if err != nil { + logs.Error(err) + return + } + err = service.Control(s, os.Args[1]) + if err != nil { + logs.Error("Valid actions: %q\n", service.ControlAction, err.Error()) + } + return + case "start", "restart", "stop", "uninstall": + err := service.Control(s, os.Args[1]) + if err != nil { + logs.Error("Valid actions: %q\n", service.ControlAction, err.Error()) + } + return + case "update": + install.UpdateNps() + return + default: + logs.Error("command is not support") + return + } + } + s.Run() +} + +type nps struct { + exit chan struct{} +} + +func (p *nps) Start(s service.Service) error { + p.run() + return nil +} +func (p *nps) Stop(s service.Service) error { + close(p.exit) + if service.Interactive() { + os.Exit(0) + } + return nil +} + +func (p *nps) run() error { + defer func() { + if err := recover(); err != nil { + const size = 64 << 10 + buf := make([]byte, size) + buf = buf[:runtime.Stack(buf, false)] + logs.Warning("nps: panic serving %v: %v\n%s", err, string(buf)) + } + }() + routers.Init() task := &file.Tunnel{ Mode: "webServer", } @@ -68,5 +150,10 @@ func main() { crypt.InitTls(filepath.Join(common.GetRunPath(), "conf", "server.pem"), filepath.Join(common.GetRunPath(), "conf", "server.key")) tool.InitAllowPort() tool.StartSystemInfo() - server.StartNewServer(bridgePort, task, beego.AppConfig.String("bridge_type")) + go server.StartNewServer(bridgePort, task, beego.AppConfig.String("bridge_type")) + select { + case <-p.exit: + logs.Warning("stop...") + } + return nil } diff --git a/conf/nps.conf b/conf/nps.conf index 8c22ab1..01f3e27 100755 --- a/conf/nps.conf +++ b/conf/nps.conf @@ -26,7 +26,7 @@ public_vkey=123 # log level LevelEmergency->0 LevelAlert->1 LevelCritical->2 LevelError->3 LevelWarning->4 LevelNotice->5 LevelInformational->6 LevelDebug->7 log_level=7 -log_path=nps.log +#log_path=nps.log #Whether to restrict IP access, true or false or ignore #ip_limit=true @@ -42,6 +42,9 @@ web_password=123 web_port = 8080 web_ip=0.0.0.0 web_base_url= +web_open_ssl=false +web_cert_file=conf/server.pem +web_key_file=conf/server.key # if web under proxy use sub path. like http://host/nps need this. #web_base_url=/nps diff --git a/docs/_sidebar.md b/docs/_sidebar.md index cfb58fa..a17588d 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -18,7 +18,8 @@ * [功能](feature.md) * [说明](description.md) * [web api](api.md) - + * [sdk](npc_sdk.md) +git * 其他 * [贡献](contribute.md) diff --git a/docs/description.md b/docs/description.md index 0520ad6..584f5b0 100644 --- a/docs/description.md +++ b/docs/description.md @@ -24,3 +24,6 @@ 默认情况下linux对连接数量有限制,对于性能好的机器完全可以调整内核参数以处理更多的连接。 `tcp_max_syn_backlog` `somaxconn` 酌情调整参数,增强网络性能 + +## web管理保护 +当一个ip连续登陆失败次数超过10次,将在一分钟内禁止该ip再次尝试。 diff --git a/docs/example.md b/docs/example.md index e37c1f6..acaab93 100644 --- a/docs/example.md +++ b/docs/example.md @@ -25,7 +25,7 @@ 现在访问(http|https://)`a.proxy.com`,`b.proxy.com`即可成功 -**https:** 如需使用https请进行相关配置,详见 [使用https](##使用https) +**https:** 如需使用https请进行相关配置,详见 [使用https](/nps_extend?id=使用https) ## tcp隧道 @@ -100,7 +100,7 @@ ## p2p服务 -**适用范围:** 大流量传输场景,流量不经过公网服务器,但是由于p2p穿透和nat类型关系较大,不保证100%成功,支持大部分nat类型。[nat类型检测](##nat类型检测) +**适用范围:** 大流量传输场景,流量不经过公网服务器,但是由于p2p穿透和nat类型关系较大,不保证100%成功,支持大部分nat类型。[nat类型检测](/npc_extend?id=nat类型检测) **假设场景:** diff --git a/docs/index.html b/docs/index.html index c86cf55..caaab9b 100644 --- a/docs/index.html +++ b/docs/index.html @@ -11,7 +11,10 @@
+ + -