npc.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package main
  2. import (
  3. "ehang.io/nps/client"
  4. "ehang.io/nps/lib/common"
  5. "ehang.io/nps/lib/config"
  6. "ehang.io/nps/lib/file"
  7. "ehang.io/nps/lib/install"
  8. "ehang.io/nps/lib/version"
  9. "flag"
  10. "fmt"
  11. "github.com/astaxie/beego/logs"
  12. "github.com/ccding/go-stun/stun"
  13. "github.com/kardianos/service"
  14. "os"
  15. "runtime"
  16. "strings"
  17. "time"
  18. )
  19. var (
  20. serverAddr = flag.String("server", "", "Server addr (ip:port)")
  21. configPath = flag.String("config", "", "Configuration file path")
  22. verifyKey = flag.String("vkey", "", "Authentication key")
  23. logType = flag.String("log", "stdout", "Log output mode(stdout|file)")
  24. connType = flag.String("type", "tcp", "Connection type with the server(kcp|tcp)")
  25. proxyUrl = flag.String("proxy", "", "proxy socks5 url(eg:socks5://111:222@127.0.0.1:9007)")
  26. logLevel = flag.String("log_level", "7", "log level 0~7")
  27. registerTime = flag.Int("time", 2, "register time long /h")
  28. localPort = flag.Int("local_port", 2000, "p2p local port")
  29. password = flag.String("password", "", "p2p password flag")
  30. target = flag.String("target", "", "p2p target")
  31. localType = flag.String("local_type", "p2p", "p2p target")
  32. logPath = flag.String("log_path", "", "npc log path")
  33. debug = flag.Bool("debug", true, "npc debug")
  34. pprofAddr = flag.String("pprof", "", "PProf debug addr (ip:port)")
  35. )
  36. const systemdScript = `[Unit]
  37. Description={{.Description}}
  38. ConditionFileIsExecutable={{.Path|cmdEscape}}
  39. {{range $i, $dep := .Dependencies}}
  40. {{$dep}} {{end}}
  41. [Service]
  42. LimitNOFILE=65536
  43. StartLimitInterval=5
  44. StartLimitBurst=10
  45. ExecStart={{.Path|cmdEscape}}{{range .Arguments}} {{.|cmd}}{{end}}
  46. {{if .ChRoot}}RootDirectory={{.ChRoot|cmd}}{{end}}
  47. {{if .WorkingDirectory}}WorkingDirectory={{.WorkingDirectory|cmdEscape}}{{end}}
  48. {{if .UserName}}User={{.UserName}}{{end}}
  49. {{if .ReloadSignal}}ExecReload=/bin/kill -{{.ReloadSignal}} "$MAINPID"{{end}}
  50. {{if .PIDFile}}PIDFile={{.PIDFile|cmd}}{{end}}
  51. {{if and .LogOutput .HasOutputFileSupport -}}
  52. StandardOutput=file:/var/log/{{.Name}}.out
  53. StandardError=file:/var/log/{{.Name}}.err
  54. {{- end}}
  55. Restart=always
  56. RestartSec=120
  57. [Install]
  58. WantedBy=multi-user.target
  59. `
  60. func main() {
  61. flag.Parse()
  62. logs.Reset()
  63. logs.EnableFuncCallDepth(true)
  64. logs.SetLogFuncCallDepth(3)
  65. if *logPath == "" {
  66. *logPath = common.GetNpcLogPath()
  67. }
  68. if common.IsWindows() {
  69. *logPath = strings.Replace(*logPath, "\\", "\\\\", -1)
  70. }
  71. if *debug {
  72. logs.SetLogger(logs.AdapterConsole, `{"level":`+*logLevel+`,"color":true}`)
  73. } else {
  74. logs.SetLogger(logs.AdapterFile, `{"level":`+*logLevel+`,"filename":"`+*logPath+`","daily":false,"maxlines":100000,"color":true}`)
  75. }
  76. // init service
  77. options := make(service.KeyValue)
  78. svcConfig := &service.Config{
  79. Name: "Npc",
  80. DisplayName: "nps内网穿透客户端",
  81. Description: "一款轻量级、功能强大的内网穿透代理服务器。支持tcp、udp流量转发,支持内网http代理、内网socks5代理,同时支持snappy压缩、站点保护、加密传输、多路复用、header修改等。支持web图形化管理,集成多用户模式。",
  82. Option: options,
  83. }
  84. if !common.IsWindows() {
  85. svcConfig.Dependencies = []string{
  86. "Requires=network.target",
  87. "After=network-online.target syslog.target"}
  88. svcConfig.Option["SystemdScript"] = systemdScript
  89. }
  90. for _, v := range os.Args[1:] {
  91. switch v {
  92. case "install", "start", "stop", "uninstall", "restart":
  93. continue
  94. }
  95. if !strings.Contains(v, "-service=") && !strings.Contains(v, "-debug=") {
  96. svcConfig.Arguments = append(svcConfig.Arguments, v)
  97. }
  98. }
  99. svcConfig.Arguments = append(svcConfig.Arguments, "-debug=false")
  100. prg := &npc{
  101. exit: make(chan struct{}),
  102. }
  103. s, err := service.New(prg, svcConfig)
  104. if err != nil {
  105. logs.Error(err)
  106. return
  107. }
  108. if len(os.Args) >= 2 {
  109. switch os.Args[1] {
  110. case "status":
  111. if len(os.Args) > 2 {
  112. path := strings.Replace(os.Args[2], "-config=", "", -1)
  113. client.GetTaskStatus(path)
  114. }
  115. case "register":
  116. flag.CommandLine.Parse(os.Args[2:])
  117. client.RegisterLocalIp(*serverAddr, *verifyKey, *connType, *proxyUrl, *registerTime)
  118. case "update":
  119. install.UpdateNpc()
  120. return
  121. case "nat":
  122. nat, host, err := stun.NewClient().Discover()
  123. if err != nil || host == nil {
  124. logs.Error("get nat type error", err)
  125. return
  126. }
  127. fmt.Printf("nat type: %s \npublic address: %s\n", nat.String(), host.String())
  128. os.Exit(0)
  129. case "install", "start", "stop", "uninstall", "restart":
  130. if os.Args[1] == "install" {
  131. service.Control(s, "stop")
  132. service.Control(s, "uninstall")
  133. install.InstallNpc()
  134. }
  135. err := service.Control(s, os.Args[1])
  136. if err != nil {
  137. logs.Error("Valid actions: %q\n%s", service.ControlAction, err.Error())
  138. }
  139. return
  140. }
  141. }
  142. s.Run()
  143. }
  144. type npc struct {
  145. exit chan struct{}
  146. }
  147. func (p *npc) Start(s service.Service) error {
  148. go p.run()
  149. return nil
  150. }
  151. func (p *npc) Stop(s service.Service) error {
  152. close(p.exit)
  153. if service.Interactive() {
  154. os.Exit(0)
  155. }
  156. return nil
  157. }
  158. func (p *npc) run() error {
  159. defer func() {
  160. if err := recover(); err != nil {
  161. const size = 64 << 10
  162. buf := make([]byte, size)
  163. buf = buf[:runtime.Stack(buf, false)]
  164. logs.Warning("npc: panic serving %v: %v\n%s", err, string(buf))
  165. }
  166. }()
  167. common.InitPProfFromArg(*pprofAddr)
  168. //p2p or secret command
  169. if *password != "" {
  170. commonConfig := new(config.CommonConfig)
  171. commonConfig.Server = *serverAddr
  172. commonConfig.VKey = *verifyKey
  173. commonConfig.Tp = *connType
  174. localServer := new(config.LocalServer)
  175. localServer.Type = *localType
  176. localServer.Password = *password
  177. localServer.Target = *target
  178. localServer.Port = *localPort
  179. commonConfig.Client = new(file.Client)
  180. commonConfig.Client.Cnf = new(file.Config)
  181. go client.StartLocalServer(localServer, commonConfig)
  182. return nil
  183. }
  184. env := common.GetEnvMap()
  185. if *serverAddr == "" {
  186. *serverAddr, _ = env["NPC_SERVER_ADDR"]
  187. }
  188. if *verifyKey == "" {
  189. *verifyKey, _ = env["NPC_SERVER_VKEY"]
  190. }
  191. logs.Info("the version of client is %s, the core version of client is %s", version.VERSION, version.GetVersion())
  192. if *verifyKey != "" && *serverAddr != "" && *configPath == "" {
  193. go func() {
  194. for {
  195. client.NewRPClient(*serverAddr, *verifyKey, *connType, *proxyUrl, nil).Start()
  196. logs.Info("It will be reconnected in five seconds")
  197. time.Sleep(time.Second * 5)
  198. }
  199. }()
  200. } else {
  201. if *configPath == "" {
  202. *configPath = "conf/npc.conf"
  203. }
  204. go client.StartFromFile(*configPath)
  205. }
  206. select {
  207. case <-p.exit:
  208. logs.Warning("stop...")
  209. }
  210. return nil
  211. }