npc.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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", "npc.conf", "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. if *verifyKey != "" && *serverAddr != "" {
  43. for {
  44. client.NewRPClient(*serverAddr, *verifyKey, *connType, *proxyUrl).Start()
  45. logs.Info("It will be reconnected in five seconds")
  46. time.Sleep(time.Second * 5)
  47. }
  48. } else {
  49. client.StartFromFile(*configPath)
  50. }
  51. }