npc.go 7.3 KB

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