2019-01-09 12:33:00 +00:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"flag"
|
2019-04-08 09:01:08 +00:00
|
|
|
|
"fmt"
|
2019-08-10 03:15:25 +00:00
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/astaxie/beego/logs"
|
|
|
|
|
"github.com/ccding/go-stun/stun"
|
2019-02-03 04:40:43 +00:00
|
|
|
|
"github.com/cnlh/nps/client"
|
2019-02-12 19:54:00 +00:00
|
|
|
|
"github.com/cnlh/nps/lib/common"
|
2019-04-08 09:01:08 +00:00
|
|
|
|
"github.com/cnlh/nps/lib/config"
|
2019-02-09 09:07:47 +00:00
|
|
|
|
"github.com/cnlh/nps/lib/daemon"
|
2019-04-08 09:01:08 +00:00
|
|
|
|
"github.com/cnlh/nps/lib/file"
|
2019-03-15 06:03:49 +00:00
|
|
|
|
"github.com/cnlh/nps/lib/version"
|
2019-01-09 12:33:00 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2019-02-16 12:43:26 +00:00
|
|
|
|
serverAddr = flag.String("server", "", "Server addr (ip:port)")
|
2019-03-07 10:07:53 +00:00
|
|
|
|
configPath = flag.String("config", "", "Configuration file path")
|
2019-02-16 12:43:26 +00:00
|
|
|
|
verifyKey = flag.String("vkey", "", "Authentication key")
|
|
|
|
|
logType = flag.String("log", "stdout", "Log output mode(stdout|file)")
|
|
|
|
|
connType = flag.String("type", "tcp", "Connection type with the server(kcp|tcp)")
|
|
|
|
|
proxyUrl = flag.String("proxy", "", "proxy socks5 url(eg:socks5://111:222@127.0.0.1:9007)")
|
2019-02-23 15:29:48 +00:00
|
|
|
|
logLevel = flag.String("log_level", "7", "log level 0~7")
|
2019-02-16 12:43:26 +00:00
|
|
|
|
registerTime = flag.Int("time", 2, "register time long /h")
|
2019-04-08 09:01:08 +00:00
|
|
|
|
localPort = flag.Int("local_port", 2000, "p2p local port")
|
|
|
|
|
password = flag.String("password", "", "p2p password flag")
|
|
|
|
|
target = flag.String("target", "", "p2p target")
|
|
|
|
|
localType = flag.String("local_type", "p2p", "p2p target")
|
|
|
|
|
logPath = flag.String("log_path", "npc.log", "npc log path")
|
2019-01-09 12:33:00 +00:00
|
|
|
|
)
|
2019-03-07 10:07:53 +00:00
|
|
|
|
|
2019-01-09 12:33:00 +00:00
|
|
|
|
func main() {
|
|
|
|
|
flag.Parse()
|
2019-04-08 09:01:08 +00:00
|
|
|
|
if len(os.Args) >= 2 {
|
2019-02-12 19:54:00 +00:00
|
|
|
|
switch os.Args[1] {
|
|
|
|
|
case "status":
|
2019-04-08 09:01:08 +00:00
|
|
|
|
if len(os.Args) > 2 {
|
|
|
|
|
path := strings.Replace(os.Args[2], "-config=", "", -1)
|
|
|
|
|
client.GetTaskStatus(path)
|
|
|
|
|
}
|
2019-02-16 12:43:26 +00:00
|
|
|
|
case "register":
|
|
|
|
|
flag.CommandLine.Parse(os.Args[2:])
|
|
|
|
|
client.RegisterLocalIp(*serverAddr, *verifyKey, *connType, *proxyUrl, *registerTime)
|
2019-04-08 09:01:08 +00:00
|
|
|
|
case "nat":
|
|
|
|
|
nat, host, err := stun.NewClient().Discover()
|
|
|
|
|
if err != nil || host == nil {
|
|
|
|
|
logs.Error("get nat type error", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fmt.Printf("nat type: %s \npublic address: %s\n", nat.String(), host.String())
|
|
|
|
|
os.Exit(0)
|
2019-02-12 19:54:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
daemon.InitDaemon("npc", common.GetRunPath(), common.GetTmpPath())
|
2019-02-24 05:17:43 +00:00
|
|
|
|
logs.EnableFuncCallDepth(true)
|
|
|
|
|
logs.SetLogFuncCallDepth(3)
|
2019-02-03 04:40:43 +00:00
|
|
|
|
if *logType == "stdout" {
|
2019-02-23 15:29:48 +00:00
|
|
|
|
logs.SetLogger(logs.AdapterConsole, `{"level":`+*logLevel+`,"color":true}`)
|
2019-02-02 16:54:43 +00:00
|
|
|
|
} else {
|
2019-04-21 15:03:58 +00:00
|
|
|
|
logs.SetLogger(logs.AdapterFile, `{"level":`+*logLevel+`,"filename":"`+*logPath+`","daily":false,"maxlines":100000,"color":true}`)
|
2019-04-08 09:01:08 +00:00
|
|
|
|
}
|
|
|
|
|
//p2p or secret command
|
|
|
|
|
if *password != "" {
|
|
|
|
|
commonConfig := new(config.CommonConfig)
|
|
|
|
|
commonConfig.Server = *serverAddr
|
|
|
|
|
commonConfig.VKey = *verifyKey
|
|
|
|
|
commonConfig.Tp = *connType
|
|
|
|
|
localServer := new(config.LocalServer)
|
|
|
|
|
localServer.Type = *localType
|
|
|
|
|
localServer.Password = *password
|
|
|
|
|
localServer.Target = *target
|
|
|
|
|
localServer.Port = *localPort
|
|
|
|
|
commonConfig.Client = new(file.Client)
|
|
|
|
|
commonConfig.Client.Cnf = new(file.Config)
|
|
|
|
|
client.StartLocalServer(localServer, commonConfig)
|
|
|
|
|
return
|
2019-01-12 16:09:12 +00:00
|
|
|
|
}
|
2019-03-07 10:07:53 +00:00
|
|
|
|
env := common.GetEnvMap()
|
|
|
|
|
if *serverAddr == "" {
|
2019-03-15 06:03:49 +00:00
|
|
|
|
*serverAddr, _ = env["NPC_SERVER_ADDR"]
|
2019-03-07 10:07:53 +00:00
|
|
|
|
}
|
|
|
|
|
if *verifyKey == "" {
|
2019-03-15 06:03:49 +00:00
|
|
|
|
*verifyKey, _ = env["NPC_SERVER_VKEY"]
|
2019-03-07 10:07:53 +00:00
|
|
|
|
}
|
2019-04-08 09:01:08 +00:00
|
|
|
|
logs.Info("the version of client is %s, the core version of client is %s", version.VERSION, version.GetVersion())
|
2019-03-07 10:07:53 +00:00
|
|
|
|
if *verifyKey != "" && *serverAddr != "" && *configPath == "" {
|
2019-02-16 12:43:26 +00:00
|
|
|
|
for {
|
2019-03-15 06:03:49 +00:00
|
|
|
|
client.NewRPClient(*serverAddr, *verifyKey, *connType, *proxyUrl, nil).Start()
|
2019-02-23 15:29:48 +00:00
|
|
|
|
logs.Info("It will be reconnected in five seconds")
|
2019-02-16 12:43:26 +00:00
|
|
|
|
time.Sleep(time.Second * 5)
|
|
|
|
|
}
|
2019-02-12 19:54:00 +00:00
|
|
|
|
} else {
|
2019-03-07 10:07:53 +00:00
|
|
|
|
if *configPath == "" {
|
2019-04-01 15:58:23 +00:00
|
|
|
|
*configPath = "npc.conf"
|
2019-03-07 10:07:53 +00:00
|
|
|
|
}
|
2019-02-12 19:54:00 +00:00
|
|
|
|
client.StartFromFile(*configPath)
|
2019-01-31 18:39:28 +00:00
|
|
|
|
}
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|