npc.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "time"
  8. "github.com/astaxie/beego/logs"
  9. "github.com/ccding/go-stun/stun"
  10. "github.com/cnlh/nps/client"
  11. "github.com/cnlh/nps/lib/common"
  12. "github.com/cnlh/nps/lib/config"
  13. "github.com/cnlh/nps/lib/daemon"
  14. "github.com/cnlh/nps/lib/file"
  15. "github.com/cnlh/nps/lib/version"
  16. )
  17. var (
  18. serverAddr = flag.String("server", "", "Server addr (ip:port)")
  19. configPath = flag.String("config", "", "Configuration file path")
  20. verifyKey = flag.String("vkey", "", "Authentication key")
  21. logType = flag.String("log", "stdout", "Log output mode(stdout|file)")
  22. connType = flag.String("type", "tcp", "Connection type with the server(kcp|tcp)")
  23. proxyUrl = flag.String("proxy", "", "proxy socks5 url(eg:socks5://111:222@127.0.0.1:9007)")
  24. logLevel = flag.String("log_level", "7", "log level 0~7")
  25. registerTime = flag.Int("time", 2, "register time long /h")
  26. localPort = flag.Int("local_port", 2000, "p2p local port")
  27. password = flag.String("password", "", "p2p password flag")
  28. target = flag.String("target", "", "p2p target")
  29. localType = flag.String("local_type", "p2p", "p2p target")
  30. logPath = flag.String("log_path", "npc.log", "npc log path")
  31. )
  32. func main() {
  33. flag.Parse()
  34. if len(os.Args) >= 2 {
  35. switch os.Args[1] {
  36. case "status":
  37. if len(os.Args) > 2 {
  38. path := strings.Replace(os.Args[2], "-config=", "", -1)
  39. client.GetTaskStatus(path)
  40. }
  41. case "register":
  42. flag.CommandLine.Parse(os.Args[2:])
  43. client.RegisterLocalIp(*serverAddr, *verifyKey, *connType, *proxyUrl, *registerTime)
  44. case "nat":
  45. nat, host, err := stun.NewClient().Discover()
  46. if err != nil || host == nil {
  47. logs.Error("get nat type error", err)
  48. return
  49. }
  50. fmt.Printf("nat type: %s \npublic address: %s\n", nat.String(), host.String())
  51. os.Exit(0)
  52. }
  53. }
  54. daemon.InitDaemon("npc", common.GetRunPath(), common.GetTmpPath())
  55. logs.EnableFuncCallDepth(true)
  56. logs.SetLogFuncCallDepth(3)
  57. if *logType == "stdout" {
  58. logs.SetLogger(logs.AdapterConsole, `{"level":`+*logLevel+`,"color":true}`)
  59. } else {
  60. logs.SetLogger(logs.AdapterFile, `{"level":`+*logLevel+`,"filename":"`+*logPath+`","daily":false,"maxlines":100000,"color":true}`)
  61. }
  62. //p2p or secret command
  63. if *password != "" {
  64. commonConfig := new(config.CommonConfig)
  65. commonConfig.Server = *serverAddr
  66. commonConfig.VKey = *verifyKey
  67. commonConfig.Tp = *connType
  68. localServer := new(config.LocalServer)
  69. localServer.Type = *localType
  70. localServer.Password = *password
  71. localServer.Target = *target
  72. localServer.Port = *localPort
  73. commonConfig.Client = new(file.Client)
  74. commonConfig.Client.Cnf = new(file.Config)
  75. client.StartLocalServer(localServer, commonConfig)
  76. return
  77. }
  78. env := common.GetEnvMap()
  79. if *serverAddr == "" {
  80. *serverAddr, _ = env["NPC_SERVER_ADDR"]
  81. }
  82. if *verifyKey == "" {
  83. *verifyKey, _ = env["NPC_SERVER_VKEY"]
  84. }
  85. logs.Info("the version of client is %s, the core version of client is %s", version.VERSION, version.GetVersion())
  86. if *verifyKey != "" && *serverAddr != "" && *configPath == "" {
  87. for {
  88. client.NewRPClient(*serverAddr, *verifyKey, *connType, *proxyUrl, nil).Start()
  89. logs.Info("It will be reconnected in five seconds")
  90. time.Sleep(time.Second * 5)
  91. }
  92. } else {
  93. if *configPath == "" {
  94. *configPath = "npc.conf"
  95. }
  96. client.StartFromFile(*configPath)
  97. }
  98. }