mirror of
https://github.com/e1732a364fed/v2ray_simple.git
synced 2025-12-24 13:27:56 +08:00
整理代码
This commit is contained in:
@@ -504,7 +504,7 @@ func interactively_hotLoadConfigFile(m *machine.M) {
|
||||
|
||||
var confMode int
|
||||
var simpleConf proxy.SimpleConf
|
||||
confMode, simpleConf, _, err = LoadConfig(fpath, "", "")
|
||||
confMode, simpleConf, _, err = defaultMachine.LoadConfig(fpath, "", "")
|
||||
if err != nil {
|
||||
|
||||
log.Printf("can not load standard config file: %s\n", err)
|
||||
@@ -523,15 +523,7 @@ func interactively_hotLoadConfigFile(m *machine.M) {
|
||||
|
||||
switch confMode {
|
||||
case proxy.StandardMode:
|
||||
if len(standardConf.Dial) > 0 {
|
||||
m.LoadDialConf(standardConf.Dial)
|
||||
|
||||
}
|
||||
|
||||
if len(standardConf.Listen) > 0 {
|
||||
m.LoadListenConf(standardConf.Listen, true)
|
||||
|
||||
}
|
||||
m.LoadStandardConf()
|
||||
case proxy.SimpleMode:
|
||||
result := m.HotLoadSimpleConf(simpleConf)
|
||||
if result < 0 {
|
||||
|
||||
@@ -1,72 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/e1732a364fed/v2ray_simple/httpLayer"
|
||||
"github.com/e1732a364fed/v2ray_simple/machine"
|
||||
"github.com/e1732a364fed/v2ray_simple/proxy"
|
||||
"github.com/e1732a364fed/v2ray_simple/utils"
|
||||
)
|
||||
|
||||
var (
|
||||
standardConf proxy.StandardConf
|
||||
appConf *machine.AppConf
|
||||
)
|
||||
|
||||
// 先检查configFileName是否存在,存在就尝试加载文件到 standardConf or simpleConf,否则尝试 listenURL, dialURL 参数.
|
||||
// 若 返回的是 simpleConf, 则还可能返回 mainFallback.
|
||||
func LoadConfig(configFileName, listenURL, dialURL string) (confMode int, simpleConf proxy.SimpleConf, mainFallback *httpLayer.ClassicFallback, err error) {
|
||||
|
||||
fpath := utils.GetFilePath(configFileName)
|
||||
if fpath != "" {
|
||||
|
||||
ext := filepath.Ext(fpath)
|
||||
if ext == ".toml" {
|
||||
|
||||
if cf, err := os.Open(fpath); err == nil {
|
||||
defer cf.Close()
|
||||
bs, _ := io.ReadAll(cf)
|
||||
|
||||
standardConf, appConf, err = machine.LoadVSConfFromBs(bs)
|
||||
if err != nil {
|
||||
|
||||
log.Printf("can not load standard config file: %v, \n", err)
|
||||
goto url
|
||||
|
||||
}
|
||||
|
||||
confMode = proxy.StandardMode
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
confMode = proxy.SimpleMode
|
||||
simpleConf, mainFallback, err = proxy.LoadSimpleConf_byFile(fpath)
|
||||
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
}
|
||||
url:
|
||||
if listenURL != "" {
|
||||
log.Printf("trying listenURL and dialURL \n")
|
||||
|
||||
confMode = proxy.SimpleMode
|
||||
simpleConf, err = proxy.LoadSimpleConf_byUrl(listenURL, dialURL)
|
||||
} else {
|
||||
|
||||
log.Println(proxy.ErrStrNoListenUrl)
|
||||
err = errors.New(proxy.ErrStrNoListenUrl)
|
||||
confMode = -1
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
// var (
|
||||
// standardConf proxy.StandardConf
|
||||
// appConf *machine.AppConf
|
||||
// )
|
||||
|
||||
@@ -197,11 +197,11 @@ func mainFunc() (result int) {
|
||||
|
||||
var simpleConf proxy.SimpleConf
|
||||
|
||||
configMode, simpleConf, fallback, loadConfigErr = LoadConfig(configFileName, listenURL, dialURL)
|
||||
configMode, simpleConf, fallback, loadConfigErr = defaultMachine.LoadConfig(configFileName, listenURL, dialURL)
|
||||
|
||||
if loadConfigErr == nil {
|
||||
|
||||
machine.SetupByAppConf(appConf)
|
||||
defaultMachine.SetupAppConf()
|
||||
|
||||
}
|
||||
|
||||
@@ -271,30 +271,7 @@ func mainFunc() (result int) {
|
||||
|
||||
case proxy.StandardMode:
|
||||
|
||||
if appConf != nil {
|
||||
defaultMachine.DefaultUUID = appConf.DefaultUUID
|
||||
}
|
||||
|
||||
//虽然标准模式支持多个Server,目前先只考虑一个
|
||||
//多个Server存在的话,则必须要用 tag指定路由; 然后,我们需在预先阶段就判断好tag指定的路由
|
||||
|
||||
if len(standardConf.Listen) < 1 {
|
||||
utils.Warn("no listen in config settings")
|
||||
break
|
||||
}
|
||||
|
||||
defaultMachine.LoadListenConf(standardConf.Listen, false)
|
||||
|
||||
if len(standardConf.Fallbacks) > 0 {
|
||||
defaultMachine.ParseFallbacksAtSymbol(standardConf.Fallbacks)
|
||||
}
|
||||
var myCountryISO_3166 string
|
||||
if appConf != nil {
|
||||
myCountryISO_3166 = appConf.MyCountryISO_3166
|
||||
}
|
||||
|
||||
defaultMachine.RoutingEnv = proxy.LoadEnvFromStandardConf(&standardConf, myCountryISO_3166)
|
||||
|
||||
defaultMachine.SetupListen()
|
||||
}
|
||||
|
||||
// load outClients
|
||||
@@ -306,15 +283,7 @@ func mainFunc() (result int) {
|
||||
}
|
||||
case proxy.StandardMode:
|
||||
|
||||
if len(standardConf.Dial) < 1 {
|
||||
utils.Warn("no dial in config settings, will add 'direct'")
|
||||
|
||||
defaultMachine.SetDefaultDirectClient()
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
defaultMachine.LoadDialConf(standardConf.Dial)
|
||||
defaultMachine.SetupDial()
|
||||
|
||||
}
|
||||
|
||||
@@ -348,16 +317,6 @@ func mainFunc() (result int) {
|
||||
|
||||
defaultMachine.ApiServerConf = defaultApiServerConf
|
||||
|
||||
var thepass string
|
||||
|
||||
if defaultMachine.AdminPass == "" && appConf != nil {
|
||||
if ap := appConf.AdminPass; ap != "" {
|
||||
thepass = ap
|
||||
}
|
||||
defaultMachine.AdminPass = thepass
|
||||
|
||||
}
|
||||
|
||||
defaultMachine.TryRunApiServer()
|
||||
|
||||
}
|
||||
|
||||
@@ -33,6 +33,16 @@ type ApiServerConf struct {
|
||||
// 非阻塞,如果运行成功则 apiServerRunning 会被设为 true
|
||||
func (m *M) TryRunApiServer() {
|
||||
|
||||
var thepass string
|
||||
|
||||
if m.AdminPass == "" && m.appConf != nil {
|
||||
if ap := m.appConf.AdminPass; ap != "" {
|
||||
thepass = ap
|
||||
}
|
||||
m.AdminPass = thepass
|
||||
|
||||
}
|
||||
|
||||
m.ApiServerRunning = true
|
||||
|
||||
go m.runApiServer()
|
||||
|
||||
112
machine/conf.go
112
machine/conf.go
@@ -1,9 +1,15 @@
|
||||
package machine
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/e1732a364fed/v2ray_simple/httpLayer"
|
||||
"github.com/e1732a364fed/v2ray_simple/netLayer"
|
||||
"github.com/e1732a364fed/v2ray_simple/proxy"
|
||||
"github.com/e1732a364fed/v2ray_simple/utils"
|
||||
@@ -94,3 +100,109 @@ func SetupByAppConf(ac *AppConf) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 先检查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) {
|
||||
|
||||
fpath := utils.GetFilePath(configFileName)
|
||||
if fpath != "" {
|
||||
|
||||
ext := filepath.Ext(fpath)
|
||||
if ext == ".toml" {
|
||||
|
||||
if cf, err := os.Open(fpath); err == nil {
|
||||
defer cf.Close()
|
||||
bs, _ := io.ReadAll(cf)
|
||||
|
||||
m.standardConf, m.appConf, err = LoadVSConfFromBs(bs)
|
||||
if err != nil {
|
||||
|
||||
log.Printf("can not load standard config file: %v, \n", err)
|
||||
goto url
|
||||
|
||||
}
|
||||
|
||||
confMode = proxy.StandardMode
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
confMode = proxy.SimpleMode
|
||||
simpleConf, mainFallback, err = proxy.LoadSimpleConf_byFile(fpath)
|
||||
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
}
|
||||
url:
|
||||
if listenURL != "" {
|
||||
log.Printf("trying listenURL and dialURL \n")
|
||||
|
||||
confMode = proxy.SimpleMode
|
||||
simpleConf, err = proxy.LoadSimpleConf_byUrl(listenURL, dialURL)
|
||||
} else {
|
||||
|
||||
log.Println(proxy.ErrStrNoListenUrl)
|
||||
err = errors.New(proxy.ErrStrNoListenUrl)
|
||||
confMode = -1
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (m *M) SetupAppConf() {
|
||||
SetupByAppConf(m.appConf)
|
||||
}
|
||||
|
||||
func (defaultMachine *M) SetupListen() {
|
||||
if defaultMachine.appConf != nil {
|
||||
defaultMachine.DefaultUUID = defaultMachine.appConf.DefaultUUID
|
||||
}
|
||||
|
||||
//虽然标准模式支持多个Server,目前先只考虑一个
|
||||
//多个Server存在的话,则必须要用 tag指定路由; 然后,我们需在预先阶段就判断好tag指定的路由
|
||||
|
||||
if len(defaultMachine.standardConf.Listen) < 1 {
|
||||
utils.Warn("no listen in config settings")
|
||||
return
|
||||
}
|
||||
|
||||
defaultMachine.LoadListenConf(defaultMachine.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
|
||||
}
|
||||
|
||||
defaultMachine.RoutingEnv = proxy.LoadEnvFromStandardConf(&defaultMachine.standardConf, myCountryISO_3166)
|
||||
|
||||
}
|
||||
func (defaultMachine *M) SetupDial() {
|
||||
if len(defaultMachine.standardConf.Dial) < 1 {
|
||||
utils.Warn("no dial in config settings, will add 'direct'")
|
||||
|
||||
defaultMachine.SetDefaultDirectClient()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
defaultMachine.LoadDialConf(defaultMachine.standardConf.Dial)
|
||||
}
|
||||
func (m *M) LoadStandardConf() {
|
||||
if len(m.standardConf.Dial) > 0 {
|
||||
m.LoadDialConf(m.standardConf.Dial)
|
||||
|
||||
}
|
||||
|
||||
if len(m.standardConf.Listen) > 0 {
|
||||
m.LoadListenConf(m.standardConf.Listen, true)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,9 @@ type M struct {
|
||||
ApiServerConf
|
||||
DefaultUUID string
|
||||
|
||||
standardConf proxy.StandardConf
|
||||
appConf *AppConf
|
||||
|
||||
v2ray_simple.GlobalInfo
|
||||
sync.RWMutex
|
||||
|
||||
|
||||
Reference in New Issue
Block a user