npc.go 2.1 KB

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