diff --git a/cmd/verysimple/cli.go b/cmd/verysimple/cli.go index 3516150..15d38f0 100644 --- a/cmd/verysimple/cli.go +++ b/cmd/verysimple/cli.go @@ -503,8 +503,7 @@ func interactively_hotLoadConfigFile(m *machine.M) { fmt.Printf("你输入了 %s\n", fpath) var confMode int - var simpleConf proxy.SimpleConf - confMode, simpleConf, _, err = defaultMachine.LoadConfig(fpath, "", "") + confMode, err = defaultMachine.LoadConfig(fpath, "", "") if err != nil { log.Printf("can not load standard config file: %s\n", err) @@ -525,7 +524,7 @@ func interactively_hotLoadConfigFile(m *machine.M) { case proxy.StandardMode: m.LoadStandardConf() case proxy.SimpleMode: - result := m.HotLoadSimpleConf(simpleConf) + result := m.LoadSimpleConf(true) if result < 0 { utils.PrintStr("添加失败!当前状态:\n") utils.PrintStr(delimiter) diff --git a/cmd/verysimple/config.go b/cmd/verysimple/config.go deleted file mode 100644 index e1e567f..0000000 --- a/cmd/verysimple/config.go +++ /dev/null @@ -1,6 +0,0 @@ -package main - -// var ( -// standardConf proxy.StandardConf -// appConf *machine.AppConf -// ) diff --git a/cmd/verysimple/main.go b/cmd/verysimple/main.go index 1bc9962..408c051 100644 --- a/cmd/verysimple/main.go +++ b/cmd/verysimple/main.go @@ -15,7 +15,6 @@ import ( "github.com/pkg/profile" "go.uber.org/zap" - "github.com/e1732a364fed/v2ray_simple/httpLayer" "github.com/e1732a364fed/v2ray_simple/machine" "github.com/e1732a364fed/v2ray_simple/netLayer" "github.com/e1732a364fed/v2ray_simple/proxy" @@ -178,7 +177,6 @@ func mainFunc() (result int) { } var configMode int - var fallback *httpLayer.ClassicFallback var loadConfigErr error @@ -195,15 +193,7 @@ func mainFunc() (result int) { } - var simpleConf proxy.SimpleConf - - configMode, simpleConf, fallback, loadConfigErr = defaultMachine.LoadConfig(configFileName, listenURL, dialURL) - - if loadConfigErr == nil { - - defaultMachine.SetupAppConf() - - } + configMode, loadConfigErr = defaultMachine.LoadConfig(configFileName, listenURL, dialURL) if utils.LogOutFileName == defaultLogFile { @@ -238,7 +228,6 @@ func mainFunc() (result int) { ce.Write(zap.Error(loadConfigErr)) } else { log.Print(willExitStr) - } return -1 @@ -257,34 +246,16 @@ func mainFunc() (result int) { fmt.Printf("UseReadv:%t\n", netLayer.UseReadv) } - if fallback != nil { - defaultMachine.RoutingEnv.Fallback = fallback - } - - //load inServers and RoutingEnv switch configMode { case proxy.SimpleMode: - result, _ = defaultMachine.LoadSimpleServer(simpleConf) + result = defaultMachine.LoadSimpleConf(false) if result < 0 { return result } case proxy.StandardMode: - - defaultMachine.SetupListen() - } - - // load outClients - switch configMode { - case proxy.SimpleMode: - result, defaultMachine.DefaultOutClient = defaultMachine.LoadSimpleClient(simpleConf) - if result < 0 { - return result - } - case proxy.StandardMode: - + defaultMachine.SetupListenAndRoute() defaultMachine.SetupDial() - } runPreCommands() @@ -314,17 +285,13 @@ func mainFunc() (result int) { } if defaultMachine.EnableApiServer { - defaultMachine.ApiServerConf = defaultApiServerConf - defaultMachine.TryRunApiServer() - } if interactive_mode { if runCli != nil { runCli() - } interactive_mode = false diff --git a/machine/conf.go b/machine/conf.go index 8f7b2b8..fdc3935 100644 --- a/machine/conf.go +++ b/machine/conf.go @@ -57,53 +57,53 @@ func LoadVSConfFromBs(bs []byte) (sc proxy.StandardConf, ac *AppConf, err error) } func SetupByAppConf(ac *AppConf) { - if ac != nil { + if ac == nil { + return + } - if ac.LogFile != nil && utils.GivenFlags["lf"] == nil { - utils.LogOutFileName = *ac.LogFile + if ac.LogFile != nil && utils.GivenFlags["lf"] == nil { + utils.LogOutFileName = *ac.LogFile + + } + + if ac.LogLevel != nil && utils.GivenFlags["ll"] == nil { + utils.LogLevel = *ac.LogLevel + + } + if ac.NoReadV && utils.GivenFlags["readv"] == nil { + netLayer.UseReadv = false + } + + if ac.UDP_timeout != nil { + + if minutes := *ac.UDP_timeout; minutes > 0 { + netLayer.UDP_timeout = time.Minute * time.Duration(minutes) + } + } + + if ac.DialTimeoutSeconds != nil { + if s := *ac.DialTimeoutSeconds; s > 0 { + netLayer.DialTimeout = time.Duration(s) * time.Second } + } - if ac.LogLevel != nil && utils.GivenFlags["ll"] == nil { - utils.LogLevel = *ac.LogLevel - - } - if ac.NoReadV && utils.GivenFlags["readv"] == nil { - netLayer.UseReadv = false + if ac.ReadTimeoutSeconds != nil { + if s := *ac.ReadTimeoutSeconds; s > 0 { + proxy.CommonReadTimeout = time.Duration(s) * time.Second } + } - if ac.UDP_timeout != nil { - - if minutes := *ac.UDP_timeout; minutes > 0 { - netLayer.UDP_timeout = time.Minute * time.Duration(minutes) - } - } - - if ac.DialTimeoutSeconds != nil { - if s := *ac.DialTimeoutSeconds; s > 0 { - netLayer.DialTimeout = time.Duration(s) * time.Second - - } - } - - if ac.ReadTimeoutSeconds != nil { - if s := *ac.ReadTimeoutSeconds; s > 0 { - proxy.CommonReadTimeout = time.Duration(s) * time.Second - } - } - - if ac.GeoipFile != nil { - netLayer.GeoipFileName = *ac.GeoipFile - } - if ac.GeositeFolder != nil { - netLayer.GeositeFolder = *ac.GeositeFolder - } + if ac.GeoipFile != nil { + netLayer.GeoipFileName = *ac.GeoipFile + } + if ac.GeositeFolder != nil { + netLayer.GeositeFolder = *ac.GeositeFolder } } -// 先检查configFileName是否存在,存在就尝试加载文件到 standardConf or simpleConf,否则尝试 listenURL, dialURL 参数. -// 若 返回的是 simpleConf, 则还可能返回 mainFallback. -func (m *M) LoadConfig(configFileName, listenURL, dialURL string) (confMode int, simpleConf proxy.SimpleConf, mainFallback *httpLayer.ClassicFallback, err error) { +// 先检查configFileName是否存在,存在就尝试加载文件到 standardConf or simpleConf,否则尝试通过 listenURL, dialURL 参数 创建simpleConf +func (m *M) LoadConfig(configFileName, listenURL, dialURL string) (confMode int, err error) { fpath := utils.GetFilePath(configFileName) if fpath != "" { @@ -124,13 +124,19 @@ func (m *M) LoadConfig(configFileName, listenURL, dialURL string) (confMode int, } confMode = proxy.StandardMode + m.setupAppConf() } } else { + var mainFallback *httpLayer.ClassicFallback confMode = proxy.SimpleMode - simpleConf, mainFallback, err = proxy.LoadSimpleConf_byFile(fpath) + m.simpleConf, mainFallback, err = proxy.LoadSimpleConf_byFile(fpath) + + if mainFallback != nil { + m.RoutingEnv.Fallback = mainFallback + } } @@ -142,7 +148,7 @@ url: log.Printf("trying listenURL and dialURL \n") confMode = proxy.SimpleMode - simpleConf, err = proxy.LoadSimpleConf_byUrl(listenURL, dialURL) + m.simpleConf, err = proxy.LoadSimpleConf_byUrl(listenURL, dialURL) } else { log.Println(proxy.ErrStrNoListenUrl) @@ -154,46 +160,42 @@ url: return } -func (m *M) SetupAppConf() { +func (m *M) setupAppConf() { SetupByAppConf(m.appConf) } -func (defaultMachine *M) SetupListen() { - if defaultMachine.appConf != nil { - defaultMachine.DefaultUUID = defaultMachine.appConf.DefaultUUID +func (m *M) SetupListenAndRoute() { + var myCountryISO_3166 string + if m.appConf != nil { + myCountryISO_3166 = m.appConf.MyCountryISO_3166 + + m.DefaultUUID = m.appConf.DefaultUUID } - //虽然标准模式支持多个Server,目前先只考虑一个 - //多个Server存在的话,则必须要用 tag指定路由; 然后,我们需在预先阶段就判断好tag指定的路由 - - if len(defaultMachine.standardConf.Listen) < 1 { + if len(m.standardConf.Listen) < 1 { utils.Warn("no listen in config settings") return } - defaultMachine.LoadListenConf(defaultMachine.standardConf.Listen, false) + m.LoadListenConf(m.standardConf.Listen, false) - if len(defaultMachine.standardConf.Fallbacks) > 0 { - defaultMachine.ParseFallbacksAtSymbol(defaultMachine.standardConf.Fallbacks) - } - var myCountryISO_3166 string - if defaultMachine.appConf != nil { - myCountryISO_3166 = defaultMachine.appConf.MyCountryISO_3166 + if len(m.standardConf.Fallbacks) > 0 { + m.ParseFallbacksAtSymbol(m.standardConf.Fallbacks) } - defaultMachine.RoutingEnv = proxy.LoadEnvFromStandardConf(&defaultMachine.standardConf, myCountryISO_3166) + m.RoutingEnv = proxy.LoadEnvFromStandardConf(&m.standardConf, myCountryISO_3166) } -func (defaultMachine *M) SetupDial() { - if len(defaultMachine.standardConf.Dial) < 1 { +func (m *M) SetupDial() { + if len(m.standardConf.Dial) < 1 && m.DefaultOutClient == nil { utils.Warn("no dial in config settings, will add 'direct'") - defaultMachine.SetDefaultDirectClient() + m.SetDefaultDirectClient() return } - defaultMachine.LoadDialConf(defaultMachine.standardConf.Dial) + m.LoadDialConf(m.standardConf.Dial) } func (m *M) LoadStandardConf() { if len(m.standardConf.Dial) > 0 { diff --git a/machine/load.go b/machine/load.go index cece01c..56f7912 100644 --- a/machine/load.go +++ b/machine/load.go @@ -111,29 +111,34 @@ func (m *M) HotDeleteServer(index int) { m.listenCloserList = utils.TrimSlice(m.listenCloserList, index) } -func (m *M) HotLoadSimpleConf(simpleConf proxy.SimpleConf) (result int) { +func (m *M) LoadSimpleConf(hot bool) (result int) { var ser proxy.Server - result, ser = m.LoadSimpleServer(simpleConf) + result, ser = m.loadSimpleServer(m.simpleConf) if result < 0 { return } var cli proxy.Client - result, cli = m.LoadSimpleClient(simpleConf) + result, cli = m.loadSimpleClient(m.simpleConf) if result < 0 { return } - lis := v2ray_simple.ListenSer(ser, cli, &m.RoutingEnv, &m.GlobalInfo) - if lis != nil { - m.listenCloserList = append(m.listenCloserList, lis) + if hot { + lis := v2ray_simple.ListenSer(ser, cli, &m.RoutingEnv, &m.GlobalInfo) + if lis != nil { + m.listenCloserList = append(m.listenCloserList, lis) + } else { + result = -1 + } } else { - result = -1 + m.DefaultOutClient = cli } + return } // load failed if result <0, -func (m *M) LoadSimpleServer(simpleConf proxy.SimpleConf) (result int, server proxy.Server) { +func (m *M) loadSimpleServer(simpleConf proxy.SimpleConf) (result int, server proxy.Server) { var e error server, e = proxy.ServerFromURL(simpleConf.ListenUrl) if e != nil { @@ -160,7 +165,7 @@ func (m *M) LoadSimpleServer(simpleConf proxy.SimpleConf) (result int, server pr return } -func (m *M) LoadSimpleClient(simpleConf proxy.SimpleConf) (result int, client proxy.Client) { +func (m *M) loadSimpleClient(simpleConf proxy.SimpleConf) (result int, client proxy.Client) { var e error client, e = proxy.ClientFromURL(simpleConf.DialUrl) if e != nil { diff --git a/machine/machine.go b/machine/machine.go index fd09697..7d1aee1 100644 --- a/machine/machine.go +++ b/machine/machine.go @@ -25,6 +25,7 @@ type M struct { DefaultUUID string standardConf proxy.StandardConf + simpleConf proxy.SimpleConf appConf *AppConf v2ray_simple.GlobalInfo