修订cmd,gui,apiServer代码

This commit is contained in:
e1732a364fed
2000-01-01 00:00:00 +00:00
parent 7764155118
commit 354d1db459
9 changed files with 82 additions and 30 deletions

View File

@@ -44,6 +44,15 @@ type exitCmd struct {
}
func init() {
flag.BoolVar(&download, "d", false, " automatically download required mmdb file")
//apiServer stuff
//defaultApiServerConf.SetupFlags()
}
func initExitCmds() {
for i, ec := range exitCmds {
if ec.isStr {
flag.StringVar(&exitCmds[i].strValue, ec.name, ec.defaultStringValue, ec.desc)
@@ -54,11 +63,6 @@ func init() {
}
}
flag.BoolVar(&download, "d", false, " automatically download required mmdb file")
//apiServer stuff
//defaultApiServerConf.SetupFlags()
}
// 运行一些 执行后立即退出程序的 命令

View File

@@ -41,6 +41,7 @@ func init() {
// // utils.InitLog("")
// // dns := netLayer.GetSystemDNS()
// // log.Println(len(dns), dns)
// log.Println(machine.NewApiServerConf())
// }},
}

View File

@@ -260,10 +260,10 @@ func makeBasicControlsPage() ui.Control {
}
fgrid.Append(button,
0, 0, 1, 1,
1, 0, 1, 1,
false, ui.AlignFill, false, ui.AlignFill)
fgrid.Append(confE,
1, 0, 1, 1,
0, 0, 1, 1,
true, ui.AlignFill, false, ui.AlignFill)
button = ui.NewButton("保存配置文件")
@@ -280,10 +280,10 @@ func makeBasicControlsPage() ui.Control {
})
fgrid.Append(button,
0, 1, 1, 1,
1, 1, 1, 1,
false, ui.AlignFill, false, ui.AlignFill)
fgrid.Append(saveFE,
1, 1, 1, 1,
0, 1, 1, 1,
true, ui.AlignFill, false, ui.AlignFill)
}
@@ -299,7 +299,7 @@ func makeBasicControlsPage() ui.Control {
systemGroup.SetMargined(true)
systemHbox.SetPadded(true)
vbox.Append(systemHbox, true)
vbox.Append(systemHbox, false)
proxyForm := ui.NewForm()
proxyForm.SetPadded(true)

View File

@@ -100,7 +100,9 @@ func main() {
}
func mainFunc() (result int) {
mainM.ApiServerConf.SetupFlags()
initExitCmds()
mainM.CmdApiServerConf.SetupFlags(nil)
defer func() {
//注意这个recover代码并不是万能的有时捕捉不到panic。
@@ -222,6 +224,8 @@ func mainFunc() (result int) {
ce.Write(zap.Any("flags", utils.GivenFlagKVs()))
}
mainM.SetupApiConf()
if loadConfigErr != nil && !IsFlexible(mainM) {
if ce := utils.CanLogErr(willExitStr); ce != nil {

2
go.mod
View File

@@ -32,6 +32,7 @@ require (
golang.zx2c4.com/wireguard v0.0.0-20220920152132-bb719d3a6e2c
gonum.org/v1/gonum v0.11.0
gvisor.dev/gvisor v0.0.0-20221214043228-7501cb5e258d
rsc.io/qr v0.2.0
)
@@ -45,7 +46,6 @@ require (
github.com/onsi/ginkgo/v2 v2.2.0 // indirect
github.com/riobard/go-bloom v0.0.0-20200614022211-cdc8013cb5b3 // indirect
golang.zx2c4.com/wintun v0.0.0-20211104114900-415007cec224 // indirect
rsc.io/qr v0.2.0 // indirect
)
require (

View File

@@ -22,7 +22,7 @@ curl -k https://127.0.0.1:48345/api/allstate
*/
type ApiServerConf struct {
EnableApiServer bool `toml:"app"`
EnableApiServer bool `toml:"enable"`
PlainHttp bool `toml:"plain"`
KeyFile string `toml:"key"`
CertFile string `toml:"cert"`
@@ -31,17 +31,55 @@ type ApiServerConf struct {
Addr string `toml:"addr"`
}
func (asc *ApiServerConf) SetupFlags() {
flag.BoolVar(&asc.EnableApiServer, "ea", false, "enable api server")
// 内含默认值的 ApiServerConf
func NewApiServerConf() (ac ApiServerConf) {
ac.SetupFlags(flag.NewFlagSet("", 10))
return
}
flag.BoolVar(&asc.PlainHttp, "sunsafe", false, "if given, api Server will use http instead of https")
func (asc *ApiServerConf) SetupFlags(fs *flag.FlagSet) {
if fs == nil {
fs = flag.CommandLine
}
fs.BoolVar(&asc.EnableApiServer, "ea", false, "enable api server")
flag.StringVar(&asc.PathPrefix, "spp", "/api", "api Server Path Prefix, must start with '/' ")
flag.StringVar(&asc.AdminPass, "sap", "", "api Server admin password, but won't be used if it's empty")
flag.StringVar(&asc.Addr, "sa", "127.0.0.1:48345", "api Server listen address")
flag.StringVar(&asc.CertFile, "scert", "", "api Server tls cert file path")
flag.StringVar(&asc.KeyFile, "skey", "", "api Server tls cert key path")
fs.BoolVar(&asc.PlainHttp, "sunsafe", false, "if given, api Server will use http instead of https")
fs.StringVar(&asc.PathPrefix, "spp", "/api", "api Server Path Prefix, must start with '/' ")
fs.StringVar(&asc.AdminPass, "sap", "", "api Server admin password, but won't be used if it's empty")
fs.StringVar(&asc.Addr, "sa", "127.0.0.1:48345", "api Server listen address")
fs.StringVar(&asc.CertFile, "scert", "", "api Server tls cert file path")
fs.StringVar(&asc.KeyFile, "skey", "", "api Server tls cert key path")
}
// 若 acref 里有与默认值不同的项且字符串不为空, 将该项的值赋值给 asc
func (asc *ApiServerConf) SetUnDefault(acref *ApiServerConf) {
defaultAc := NewApiServerConf()
var emptyAc ApiServerConf
if acref.PlainHttp != defaultAc.PlainHttp {
asc.PlainHttp = acref.PlainHttp
}
if acref.EnableApiServer != defaultAc.EnableApiServer {
asc.EnableApiServer = acref.EnableApiServer
}
if acref.Addr != defaultAc.Addr && acref.Addr != emptyAc.Addr {
asc.Addr = acref.Addr
}
if acref.AdminPass != defaultAc.AdminPass {
asc.AdminPass = acref.AdminPass
}
if acref.PathPrefix != defaultAc.PathPrefix && acref.PathPrefix != emptyAc.PathPrefix {
asc.PathPrefix = acref.PathPrefix
}
if acref.CertFile != defaultAc.CertFile {
asc.CertFile = acref.CertFile
}
if acref.KeyFile != defaultAc.KeyFile {
asc.KeyFile = acref.KeyFile
}
}
// 非阻塞,如果运行成功则 apiServerRunning 会被设为 true

View File

@@ -17,7 +17,7 @@ import (
// VS 标准toml文件格式 由 proxy.StandardConf , ApiServerConf, AppConf 3部分组成
type VSConf struct {
AppConf *AppConf `toml:"app"`
ApiServerConf *ApiServerConf `toml:"api_server"`
ApiServerConf *ApiServerConf `toml:"apiServer"`
proxy.StandardConf
}
@@ -145,7 +145,7 @@ func (m *M) LoadConfigByTomlBytes(bs []byte) (err error) {
m.AppConf.Setup()
}
if vsConf.ApiServerConf != nil {
m.ApiServerConf = *vsConf.ApiServerConf
m.TomlApiServerConf = *vsConf.ApiServerConf
}
return nil

View File

@@ -23,6 +23,9 @@ import (
type M struct {
ApiServerConf
TomlApiServerConf ApiServerConf
CmdApiServerConf ApiServerConf
AppConf
standardConf proxy.StandardConf
@@ -100,12 +103,21 @@ func (m *M) Start() {
}
m.Unlock()
}
if !m.ApiServerRunning && m.EnableApiServer {
m.TryRunApiServer()
}
}
// 融合 CmdApiServerConf 和 TomlApiServerConf, CmdApiServerConf 的值会覆盖 TomlApiServerConf
func (m *M) SetupApiConf() {
m.ApiServerConf = NewApiServerConf()
m.ApiServerConf.SetUnDefault(&m.TomlApiServerConf)
m.ApiServerConf.SetUnDefault(&m.CmdApiServerConf)
}
func (m *M) Stop() {
utils.Info("Stopping...")

View File

@@ -18,13 +18,6 @@ type MuxMarker interface {
IsMux() bool
}
// 实现 MuxMarker
type MuxMarkerConn struct {
netLayer.ReadWrapper
}
func (mh *MuxMarkerConn) IsMux() bool { return true }
// 实现 utils.MuxMarker, utils.User
type UserReadWrapper struct {
utils.User