2019-01-09 12:33:00 +00:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2020-01-08 13:57:14 +00:00
|
|
|
|
"ehang.io/nps/client"
|
|
|
|
|
"ehang.io/nps/lib/common"
|
|
|
|
|
"ehang.io/nps/lib/config"
|
|
|
|
|
"ehang.io/nps/lib/file"
|
|
|
|
|
"ehang.io/nps/lib/install"
|
|
|
|
|
"ehang.io/nps/lib/version"
|
2019-01-09 12:33:00 +00:00
|
|
|
|
"flag"
|
2019-04-08 09:01:08 +00:00
|
|
|
|
"fmt"
|
2019-08-10 03:15:25 +00:00
|
|
|
|
"github.com/astaxie/beego/logs"
|
|
|
|
|
"github.com/ccding/go-stun/stun"
|
2019-12-15 18:28:31 +00:00
|
|
|
|
"github.com/kardianos/service"
|
|
|
|
|
"os"
|
2020-02-27 16:31:17 +00:00
|
|
|
|
"os/exec"
|
2019-12-15 18:28:31 +00:00
|
|
|
|
"runtime"
|
|
|
|
|
"strings"
|
2020-02-18 14:53:37 +00:00
|
|
|
|
"sync"
|
2019-12-15 18:28:31 +00:00
|
|
|
|
"time"
|
2019-01-09 12:33:00 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2019-02-16 12:43:26 +00:00
|
|
|
|
serverAddr = flag.String("server", "", "Server addr (ip:port)")
|
2019-03-07 10:07:53 +00:00
|
|
|
|
configPath = flag.String("config", "", "Configuration file path")
|
2019-02-16 12:43:26 +00:00
|
|
|
|
verifyKey = flag.String("vkey", "", "Authentication key")
|
|
|
|
|
logType = flag.String("log", "stdout", "Log output mode(stdout|file)")
|
|
|
|
|
connType = flag.String("type", "tcp", "Connection type with the server(kcp|tcp)")
|
|
|
|
|
proxyUrl = flag.String("proxy", "", "proxy socks5 url(eg:socks5://111:222@127.0.0.1:9007)")
|
2019-02-23 15:29:48 +00:00
|
|
|
|
logLevel = flag.String("log_level", "7", "log level 0~7")
|
2019-02-16 12:43:26 +00:00
|
|
|
|
registerTime = flag.Int("time", 2, "register time long /h")
|
2019-04-08 09:01:08 +00:00
|
|
|
|
localPort = flag.Int("local_port", 2000, "p2p local port")
|
|
|
|
|
password = flag.String("password", "", "p2p password flag")
|
|
|
|
|
target = flag.String("target", "", "p2p target")
|
|
|
|
|
localType = flag.String("local_type", "p2p", "p2p target")
|
2019-12-15 18:28:31 +00:00
|
|
|
|
logPath = flag.String("log_path", "", "npc log path")
|
2019-12-16 14:50:34 +00:00
|
|
|
|
debug = flag.Bool("debug", true, "npc debug")
|
2020-02-09 08:54:40 +00:00
|
|
|
|
pprofAddr = flag.String("pprof", "", "PProf debug addr (ip:port)")
|
2020-02-22 17:49:41 +00:00
|
|
|
|
stunAddr = flag.String("stun_addr", "stun.stunprotocol.org:3478", "stun server address (eg:stun.stunprotocol.org:3478)")
|
2020-03-05 15:49:24 +00:00
|
|
|
|
ver = flag.Bool("version", false, "show current version")
|
2019-01-09 12:33:00 +00:00
|
|
|
|
)
|
2019-03-07 10:07:53 +00:00
|
|
|
|
|
2019-01-09 12:33:00 +00:00
|
|
|
|
func main() {
|
|
|
|
|
flag.Parse()
|
2019-12-15 18:28:31 +00:00
|
|
|
|
logs.Reset()
|
|
|
|
|
logs.EnableFuncCallDepth(true)
|
|
|
|
|
logs.SetLogFuncCallDepth(3)
|
2020-03-05 15:49:24 +00:00
|
|
|
|
if *ver {
|
|
|
|
|
common.PrintVersion()
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-12-15 18:28:31 +00:00
|
|
|
|
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)
|
|
|
|
|
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"}
|
2020-02-27 16:31:17 +00:00
|
|
|
|
svcConfig.Option["SystemdScript"] = install.SystemdScript
|
|
|
|
|
svcConfig.Option["SysvScript"] = install.SysvScript
|
2019-12-15 18:28:31 +00:00
|
|
|
|
}
|
|
|
|
|
for _, v := range os.Args[1:] {
|
2019-12-16 14:50:34 +00:00
|
|
|
|
switch v {
|
|
|
|
|
case "install", "start", "stop", "uninstall", "restart":
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(v, "-service=") && !strings.Contains(v, "-debug=") {
|
2019-12-15 18:28:31 +00:00
|
|
|
|
svcConfig.Arguments = append(svcConfig.Arguments, v)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-16 14:50:34 +00:00
|
|
|
|
svcConfig.Arguments = append(svcConfig.Arguments, "-debug=false")
|
2019-12-15 18:28:31 +00:00
|
|
|
|
prg := &npc{
|
|
|
|
|
exit: make(chan struct{}),
|
|
|
|
|
}
|
|
|
|
|
s, err := service.New(prg, svcConfig)
|
|
|
|
|
if err != nil {
|
2020-02-18 14:53:37 +00:00
|
|
|
|
logs.Error(err, "service function disabled")
|
|
|
|
|
run()
|
|
|
|
|
// run without service
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
wg.Wait()
|
2019-12-15 18:28:31 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2019-04-08 09:01:08 +00:00
|
|
|
|
if len(os.Args) >= 2 {
|
2019-02-12 19:54:00 +00:00
|
|
|
|
switch os.Args[1] {
|
|
|
|
|
case "status":
|
2019-04-08 09:01:08 +00:00
|
|
|
|
if len(os.Args) > 2 {
|
|
|
|
|
path := strings.Replace(os.Args[2], "-config=", "", -1)
|
|
|
|
|
client.GetTaskStatus(path)
|
|
|
|
|
}
|
2019-02-16 12:43:26 +00:00
|
|
|
|
case "register":
|
|
|
|
|
flag.CommandLine.Parse(os.Args[2:])
|
|
|
|
|
client.RegisterLocalIp(*serverAddr, *verifyKey, *connType, *proxyUrl, *registerTime)
|
2019-12-16 09:15:05 +00:00
|
|
|
|
case "update":
|
|
|
|
|
install.UpdateNpc()
|
|
|
|
|
return
|
2019-04-08 09:01:08 +00:00
|
|
|
|
case "nat":
|
2020-02-22 17:49:41 +00:00
|
|
|
|
c := stun.NewClient()
|
|
|
|
|
c.SetServerAddr(*stunAddr)
|
|
|
|
|
nat, host, err := c.Discover()
|
2019-04-08 09:01:08 +00:00
|
|
|
|
if err != nil || host == nil {
|
|
|
|
|
logs.Error("get nat type error", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fmt.Printf("nat type: %s \npublic address: %s\n", nat.String(), host.String())
|
|
|
|
|
os.Exit(0)
|
2020-02-27 16:31:17 +00:00
|
|
|
|
case "start", "stop", "restart":
|
|
|
|
|
// support busyBox and sysV, for openWrt
|
|
|
|
|
if service.Platform() == "unix-systemv" {
|
|
|
|
|
logs.Info("unix-systemv service")
|
|
|
|
|
cmd := exec.Command("/etc/init.d/"+svcConfig.Name, os.Args[1])
|
|
|
|
|
err := cmd.Run()
|
|
|
|
|
if err != nil {
|
|
|
|
|
logs.Error(err)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
err := service.Control(s, os.Args[1])
|
|
|
|
|
if err != nil {
|
|
|
|
|
logs.Error("Valid actions: %q\n%s", service.ControlAction, err.Error())
|
2019-12-16 14:50:34 +00:00
|
|
|
|
}
|
2020-02-27 16:31:17 +00:00
|
|
|
|
return
|
|
|
|
|
case "install":
|
|
|
|
|
service.Control(s, "stop")
|
|
|
|
|
service.Control(s, "uninstall")
|
|
|
|
|
install.InstallNpc()
|
2019-12-16 14:50:34 +00:00
|
|
|
|
err := service.Control(s, os.Args[1])
|
|
|
|
|
if err != nil {
|
2020-01-07 02:02:58 +00:00
|
|
|
|
logs.Error("Valid actions: %q\n%s", service.ControlAction, err.Error())
|
2019-12-16 14:50:34 +00:00
|
|
|
|
}
|
2020-02-27 16:31:17 +00:00
|
|
|
|
if service.Platform() == "unix-systemv" {
|
|
|
|
|
logs.Info("unix-systemv service")
|
|
|
|
|
confPath := "/etc/init.d/" + svcConfig.Name
|
|
|
|
|
os.Symlink(confPath, "/etc/rc.d/S90"+svcConfig.Name)
|
|
|
|
|
os.Symlink(confPath, "/etc/rc.d/K02"+svcConfig.Name)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
case "uninstall":
|
|
|
|
|
err := service.Control(s, os.Args[1])
|
|
|
|
|
if err != nil {
|
|
|
|
|
logs.Error("Valid actions: %q\n%s", service.ControlAction, err.Error())
|
|
|
|
|
}
|
|
|
|
|
if service.Platform() == "unix-systemv" {
|
|
|
|
|
logs.Info("unix-systemv service")
|
|
|
|
|
os.Remove("/etc/rc.d/S90" + svcConfig.Name)
|
|
|
|
|
os.Remove("/etc/rc.d/K02" + svcConfig.Name)
|
|
|
|
|
}
|
2019-12-16 14:50:34 +00:00
|
|
|
|
return
|
2019-02-12 19:54:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-15 18:28:31 +00:00
|
|
|
|
s.Run()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type npc struct {
|
|
|
|
|
exit chan struct{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *npc) Start(s service.Service) error {
|
2019-12-16 09:15:05 +00:00
|
|
|
|
go p.run()
|
2019-12-15 18:28:31 +00:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (p *npc) Stop(s service.Service) error {
|
|
|
|
|
close(p.exit)
|
|
|
|
|
if service.Interactive() {
|
|
|
|
|
os.Exit(0)
|
2019-04-08 09:01:08 +00:00
|
|
|
|
}
|
2019-12-15 18:28:31 +00:00
|
|
|
|
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))
|
|
|
|
|
}
|
|
|
|
|
}()
|
2020-02-18 14:53:37 +00:00
|
|
|
|
run()
|
|
|
|
|
select {
|
|
|
|
|
case <-p.exit:
|
|
|
|
|
logs.Warning("stop...")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func run() {
|
2020-02-09 08:54:40 +00:00
|
|
|
|
common.InitPProfFromArg(*pprofAddr)
|
2019-04-08 09:01:08 +00:00
|
|
|
|
//p2p or secret command
|
|
|
|
|
if *password != "" {
|
|
|
|
|
commonConfig := new(config.CommonConfig)
|
|
|
|
|
commonConfig.Server = *serverAddr
|
|
|
|
|
commonConfig.VKey = *verifyKey
|
|
|
|
|
commonConfig.Tp = *connType
|
|
|
|
|
localServer := new(config.LocalServer)
|
|
|
|
|
localServer.Type = *localType
|
|
|
|
|
localServer.Password = *password
|
|
|
|
|
localServer.Target = *target
|
|
|
|
|
localServer.Port = *localPort
|
|
|
|
|
commonConfig.Client = new(file.Client)
|
|
|
|
|
commonConfig.Client.Cnf = new(file.Config)
|
2019-12-15 18:28:31 +00:00
|
|
|
|
go client.StartLocalServer(localServer, commonConfig)
|
2020-04-21 12:52:48 +00:00
|
|
|
|
return
|
2019-01-12 16:09:12 +00:00
|
|
|
|
}
|
2019-03-07 10:07:53 +00:00
|
|
|
|
env := common.GetEnvMap()
|
|
|
|
|
if *serverAddr == "" {
|
2019-03-15 06:03:49 +00:00
|
|
|
|
*serverAddr, _ = env["NPC_SERVER_ADDR"]
|
2019-03-07 10:07:53 +00:00
|
|
|
|
}
|
|
|
|
|
if *verifyKey == "" {
|
2019-03-15 06:03:49 +00:00
|
|
|
|
*verifyKey, _ = env["NPC_SERVER_VKEY"]
|
2019-03-07 10:07:53 +00:00
|
|
|
|
}
|
2019-04-08 09:01:08 +00:00
|
|
|
|
logs.Info("the version of client is %s, the core version of client is %s", version.VERSION, version.GetVersion())
|
2019-03-07 10:07:53 +00:00
|
|
|
|
if *verifyKey != "" && *serverAddr != "" && *configPath == "" {
|
2019-12-15 18:28:31 +00:00
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}()
|
2019-02-12 19:54:00 +00:00
|
|
|
|
} else {
|
2019-03-07 10:07:53 +00:00
|
|
|
|
if *configPath == "" {
|
2019-12-15 18:28:31 +00:00
|
|
|
|
*configPath = "conf/npc.conf"
|
2019-03-07 10:07:53 +00:00
|
|
|
|
}
|
2019-12-15 18:28:31 +00:00
|
|
|
|
go client.StartFromFile(*configPath)
|
|
|
|
|
}
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|