1
0

npc.go 2.2 KB

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