npc.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. if *logType == "stdout" {
  36. logs.SetLogger(logs.AdapterConsole, `{"level":`+*logLevel+`,"color":true}`)
  37. } else {
  38. logs.SetLogger(logs.AdapterFile, `{"level":`+*logLevel+`,"filename":"npc_log.log"}`)
  39. }
  40. if *verifyKey != "" && *serverAddr != "" {
  41. for {
  42. client.NewRPClient(*serverAddr, *verifyKey, *connType, *proxyUrl).Start()
  43. logs.Info("It will be reconnected in five seconds")
  44. time.Sleep(time.Second * 5)
  45. }
  46. } else {
  47. client.StartFromFile(*configPath)
  48. }
  49. }