npc.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package main
  2. import (
  3. "flag"
  4. "github.com/cnlh/nps/client"
  5. "github.com/cnlh/nps/lib/common"
  6. "github.com/cnlh/nps/lib/daemon"
  7. "github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
  8. "os"
  9. "strings"
  10. "time"
  11. )
  12. var (
  13. serverAddr = flag.String("server", "", "Server addr (ip:port)")
  14. configPath = flag.String("config", "", "Configuration file path")
  15. verifyKey = flag.String("vkey", "", "Authentication key")
  16. logType = flag.String("log", "stdout", "Log output mode(stdout|file)")
  17. connType = flag.String("type", "tcp", "Connection type with the server(kcp|tcp)")
  18. proxyUrl = flag.String("proxy", "", "proxy socks5 url(eg:socks5://111:222@127.0.0.1:9007)")
  19. logLevel = flag.String("log_level", "7", "log level 0~7")
  20. registerTime = flag.Int("time", 2, "register time long /h")
  21. )
  22. func main() {
  23. flag.Parse()
  24. if len(os.Args) > 2 {
  25. switch os.Args[1] {
  26. case "status":
  27. path := strings.Replace(os.Args[2], "-config=", "", -1)
  28. client.GetTaskStatus(path)
  29. case "register":
  30. flag.CommandLine.Parse(os.Args[2:])
  31. client.RegisterLocalIp(*serverAddr, *verifyKey, *connType, *proxyUrl, *registerTime)
  32. }
  33. }
  34. daemon.InitDaemon("npc", common.GetRunPath(), common.GetTmpPath())
  35. logs.EnableFuncCallDepth(true)
  36. logs.SetLogFuncCallDepth(3)
  37. if *logType == "stdout" {
  38. logs.SetLogger(logs.AdapterConsole, `{"level":`+*logLevel+`,"color":true}`)
  39. } else {
  40. logs.SetLogger(logs.AdapterFile, `{"level":`+*logLevel+`,"filename":"npc_log.log"}`)
  41. }
  42. env := common.GetEnvMap()
  43. if *serverAddr == "" {
  44. *serverAddr, _ = env["NPS_SERVER_ADDR"]
  45. }
  46. if *verifyKey == "" {
  47. *verifyKey, _ = env["NPS_SERVER_VKEY"]
  48. }
  49. if *verifyKey != "" && *serverAddr != "" && *configPath == "" {
  50. for {
  51. client.NewRPClient(*serverAddr, *verifyKey, *connType, *proxyUrl).Start()
  52. logs.Info("It will be reconnected in five seconds")
  53. time.Sleep(time.Second * 5)
  54. }
  55. } else {
  56. if *configPath == "" {
  57. *configPath = "npc.conf"
  58. }
  59. client.StartFromFile(*configPath)
  60. }
  61. }