npc.go 1.5 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/lib/lg"
  8. "os"
  9. "strings"
  10. "time"
  11. )
  12. const VERSION = "v0.0.15"
  13. var (
  14. serverAddr = flag.String("server", "", "Server addr (ip:port)")
  15. configPath = flag.String("config", "npc.conf", "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. 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. lg.InitLogFile("npc", true, common.GetLogPath())
  37. } else {
  38. lg.InitLogFile("npc", false, common.GetLogPath())
  39. }
  40. if *verifyKey != "" && *serverAddr != "" {
  41. for {
  42. client.NewRPClient(*serverAddr, *verifyKey, *connType, *proxyUrl).Start()
  43. lg.Println("It will be reconnected in five seconds")
  44. time.Sleep(time.Second * 5)
  45. }
  46. } else {
  47. client.StartFromFile(*configPath)
  48. }
  49. }