mirror of
https://github.com/ehang-io/nps.git
synced 2025-09-02 11:56:53 +00:00
客户端配置,端口白名单等
This commit is contained in:
@@ -3,33 +3,81 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"github.com/cnlh/nps/client"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/config"
|
||||
"github.com/cnlh/nps/lib/daemon"
|
||||
"github.com/cnlh/nps/lib/lg"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const VERSION = "v0.0.13"
|
||||
const VERSION = "v0.0.15"
|
||||
|
||||
var (
|
||||
serverAddr = flag.String("server", "", "服务器地址ip:端口")
|
||||
verifyKey = flag.String("vkey", "", "验证密钥")
|
||||
logType = flag.String("log", "stdout", "日志输出方式(stdout|file)")
|
||||
connType = flag.String("type", "tcp", "与服务端建立连接方式(kcp|tcp)")
|
||||
serverAddr = flag.String("server", "", "Server addr (ip:port)")
|
||||
configPath = flag.String("config", "npc.conf", "Configuration file path")
|
||||
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)")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
daemon.InitDaemon("npc", common.GetRunPath(), common.GetPidPath())
|
||||
if len(os.Args) > 2 {
|
||||
switch os.Args[1] {
|
||||
case "status":
|
||||
path := strings.Replace(os.Args[2], "-config=", "", -1)
|
||||
cnf, err := config.NewConfig(path)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
c, err := client.NewConn(cnf.CommonConfig.Tp, cnf.CommonConfig.VKey, cnf.CommonConfig.Server, common.WORK_CONFIG)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
if _, err := c.Write([]byte(common.WORK_STATUS)); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
if f, err := common.ReadAllFromFile(filepath.Join(common.GetTmpPath(), "npc_vkey.txt")); err != nil {
|
||||
log.Fatalln(err)
|
||||
} else if _, err := c.Write([]byte(string(f))); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
if l, err := c.GetLen(); err != nil {
|
||||
log.Fatalln(err)
|
||||
} else if b, err := c.ReadLen(l); err != nil {
|
||||
lg.Fatalln(err)
|
||||
} else {
|
||||
arr := strings.Split(string(b), common.CONN_DATA_SEQ)
|
||||
for _, v := range cnf.Hosts {
|
||||
if common.InArr(arr, v.Remark) {
|
||||
log.Println(v.Remark, "ok")
|
||||
} else {
|
||||
log.Println(v.Remark, "not running")
|
||||
}
|
||||
}
|
||||
for _, v := range cnf.Tasks {
|
||||
if common.InArr(arr, v.Remark) {
|
||||
log.Println(v.Remark, "ok")
|
||||
} else {
|
||||
log.Println(v.Remark, "not running")
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
daemon.InitDaemon("npc", common.GetRunPath(), common.GetTmpPath())
|
||||
if *logType == "stdout" {
|
||||
lg.InitLogFile("npc", true, common.GetLogPath())
|
||||
} else {
|
||||
lg.InitLogFile("npc", false, common.GetLogPath())
|
||||
}
|
||||
stop := make(chan int)
|
||||
for _, v := range strings.Split(*verifyKey, ",") {
|
||||
lg.Println("客户端启动,连接:", *serverAddr, " 验证令牌:", v)
|
||||
go client.NewRPClient(*serverAddr, v, *connType).Start()
|
||||
if *verifyKey != "" && *serverAddr != "" {
|
||||
client.NewRPClient(*serverAddr, *verifyKey, *connType).Start()
|
||||
} else {
|
||||
client.StartFromFile(*configPath)
|
||||
}
|
||||
<-stop
|
||||
}
|
||||
|
Reference in New Issue
Block a user