npc.go 6.5 KB

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