mirror of
https://github.com/ehang-io/nps.git
synced 2025-07-03 04:53:50 +00:00
86 lines
2.3 KiB
Go
86 lines
2.3 KiB
Go
package main
|
||
|
||
import (
|
||
"flag"
|
||
"github.com/astaxie/beego"
|
||
"github.com/cnlh/nps/lib"
|
||
"github.com/cnlh/nps/server"
|
||
_ "github.com/cnlh/nps/web/routers"
|
||
"log"
|
||
"os"
|
||
"path/filepath"
|
||
)
|
||
|
||
const VERSION = "v0.0.13"
|
||
|
||
var (
|
||
TcpPort = flag.Int("tcpport", 0, "客户端与服务端通信端口")
|
||
httpPort = flag.Int("httpport", 8024, "对外监听的端口")
|
||
rpMode = flag.String("mode", "webServer", "启动模式")
|
||
tunnelTarget = flag.String("target", "127.0.0.1:80", "远程目标")
|
||
VerifyKey = flag.String("vkey", "", "验证密钥")
|
||
u = flag.String("u", "", "验证用户名(socks5和web)")
|
||
p = flag.String("p", "", "验证密码(socks5和web)")
|
||
compress = flag.String("compress", "", "数据压缩方式(snappy)")
|
||
crypt = flag.String("crypt", "false", "是否加密(true|false)")
|
||
logType = flag.String("log", "stdout", "日志输出方式(stdout|file)")
|
||
)
|
||
|
||
func main() {
|
||
flag.Parse()
|
||
if len(os.Args) > 1 && os.Args[1] == "test" {
|
||
server.TestServerConfig()
|
||
log.Println("test ok, no error")
|
||
return
|
||
}
|
||
lib.InitDaemon("nps")
|
||
if *logType == "stdout" {
|
||
lib.InitLogFile("nps", true)
|
||
} else {
|
||
lib.InitLogFile("nps", false)
|
||
}
|
||
task := &lib.Tunnel{
|
||
TcpPort: *httpPort,
|
||
Mode: *rpMode,
|
||
Target: *tunnelTarget,
|
||
Config: &lib.Config{
|
||
U: *u,
|
||
P: *p,
|
||
Compress: *compress,
|
||
Crypt: lib.GetBoolByStr(*crypt),
|
||
},
|
||
Flow: &lib.Flow{},
|
||
UseClientCnf: false,
|
||
}
|
||
if *VerifyKey != "" {
|
||
c := &lib.Client{
|
||
Id: 0,
|
||
VerifyKey: *VerifyKey,
|
||
Addr: "",
|
||
Remark: "",
|
||
Status: true,
|
||
IsConnect: false,
|
||
Cnf: &lib.Config{},
|
||
Flow: &lib.Flow{},
|
||
}
|
||
c.Cnf.CompressDecode, c.Cnf.CompressEncode = lib.GetCompressType(c.Cnf.Compress)
|
||
lib.GetCsvDb().Clients[0] = c
|
||
task.Client = c
|
||
}
|
||
if *TcpPort == 0 {
|
||
p, err := beego.AppConfig.Int("tcpport")
|
||
if err == nil && *rpMode == "webServer" {
|
||
*TcpPort = p
|
||
} else {
|
||
*TcpPort = 8284
|
||
}
|
||
}
|
||
lib.Println("服务端启动,监听tcp服务端端口:", *TcpPort)
|
||
task.Config.CompressDecode, task.Config.CompressEncode = lib.GetCompressType(task.Config.Compress)
|
||
if *rpMode != "webServer" {
|
||
lib.GetCsvDb().Tasks[0] = task
|
||
}
|
||
beego.LoadAppConfig("ini", filepath.Join(lib.GetRunPath(), "conf", "app.conf"))
|
||
server.StartNewServer(*TcpPort, task)
|
||
}
|