chg: HiddifyOptions
This commit is contained in:
46
v2/custom.go
46
v2/custom.go
@@ -17,7 +17,7 @@ import (
|
||||
|
||||
var (
|
||||
Box *libbox.BoxService
|
||||
configOptions *config.ConfigOptions
|
||||
HiddifyOptions *config.HiddifyOptions
|
||||
activeConfigPath string
|
||||
coreLogFactory log.Factory
|
||||
useFlutterBridge bool = true
|
||||
@@ -67,9 +67,11 @@ func Start(in *pb.StartRequest) (*pb.CoreInfoResponse, error) {
|
||||
resp, err := StartService(in)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (s *CoreService) StartService(ctx context.Context, in *pb.StartRequest) (*pb.CoreInfoResponse, error) {
|
||||
return StartService(in)
|
||||
}
|
||||
|
||||
func StartService(in *pb.StartRequest) (*pb.CoreInfoResponse, error) {
|
||||
Log(pb.LogLevel_DEBUG, pb.LogType_CORE, "Starting Core Service")
|
||||
content := in.ConfigContent
|
||||
@@ -98,7 +100,7 @@ func StartService(in *pb.StartRequest) (*pb.CoreInfoResponse, error) {
|
||||
}
|
||||
if !in.EnableRawConfig {
|
||||
Log(pb.LogLevel_DEBUG, pb.LogType_CORE, "Building config")
|
||||
parsedContent_tmp, err := config.BuildConfig(*configOptions, parsedContent)
|
||||
parsedContent_tmp, err := config.BuildConfig(*HiddifyOptions, parsedContent)
|
||||
if err != nil {
|
||||
Log(pb.LogLevel_FATAL, pb.LogType_CORE, err.Error())
|
||||
resp := SetCoreStatus(pb.CoreState_STOPPED, pb.MessageType_ERROR_BUILDING_CONFIG, err.Error())
|
||||
@@ -126,7 +128,6 @@ func StartService(in *pb.StartRequest) (*pb.CoreInfoResponse, error) {
|
||||
|
||||
Log(pb.LogLevel_DEBUG, pb.LogType_CORE, "Stating Service ")
|
||||
instance, err := NewService(parsedContent)
|
||||
|
||||
if err != nil {
|
||||
Log(pb.LogLevel_FATAL, pb.LogType_CORE, err.Error())
|
||||
resp := SetCoreStatus(pb.CoreState_STOPPED, pb.MessageType_CREATE_SERVICE, err.Error())
|
||||
@@ -152,12 +153,12 @@ func StartService(in *pb.StartRequest) (*pb.CoreInfoResponse, error) {
|
||||
|
||||
resp := SetCoreStatus(pb.CoreState_STARTED, pb.MessageType_EMPTY, "")
|
||||
return &resp, nil
|
||||
|
||||
}
|
||||
|
||||
func (s *CoreService) Parse(ctx context.Context, in *pb.ParseRequest) (*pb.ParseResponse, error) {
|
||||
return Parse(in)
|
||||
}
|
||||
|
||||
func Parse(in *pb.ParseRequest) (*pb.ParseResponse, error) {
|
||||
defer config.DeferPanicToError("parse", func(err error) {
|
||||
Log(pb.LogLevel_FATAL, pb.LogType_CONFIG, err.Error())
|
||||
@@ -175,7 +176,7 @@ func Parse(in *pb.ParseRequest) (*pb.ParseResponse, error) {
|
||||
|
||||
}
|
||||
|
||||
config, err := config.ParseConfigContent(content, true, configOptions, false)
|
||||
config, err := config.ParseConfigContent(content, true, HiddifyOptions, false)
|
||||
if err != nil {
|
||||
return &pb.ParseResponse{
|
||||
ResponseCode: pb.ResponseCode_FAILED,
|
||||
@@ -183,7 +184,7 @@ func Parse(in *pb.ParseRequest) (*pb.ParseResponse, error) {
|
||||
}, err
|
||||
}
|
||||
if in.ConfigPath != "" {
|
||||
err = os.WriteFile(in.ConfigPath, config, 0644)
|
||||
err = os.WriteFile(in.ConfigPath, config, 0o644)
|
||||
if err != nil {
|
||||
return &pb.ParseResponse{
|
||||
ResponseCode: pb.ResponseCode_FAILED,
|
||||
@@ -198,42 +199,44 @@ func Parse(in *pb.ParseRequest) (*pb.ParseResponse, error) {
|
||||
}, err
|
||||
}
|
||||
|
||||
func (s *CoreService) ChangeConfigOptions(ctx context.Context, in *pb.ChangeConfigOptionsRequest) (*pb.CoreInfoResponse, error) {
|
||||
return ChangeConfigOptions(in)
|
||||
func (s *CoreService) ChangeHiddifyOptions(ctx context.Context, in *pb.ChangeHiddifyOptionsRequest) (*pb.CoreInfoResponse, error) {
|
||||
return ChangeHiddifyOptions(in)
|
||||
}
|
||||
|
||||
func ChangeConfigOptions(in *pb.ChangeConfigOptionsRequest) (*pb.CoreInfoResponse, error) {
|
||||
configOptions = &config.ConfigOptions{}
|
||||
err := json.Unmarshal([]byte(in.ConfigOptionsJson), configOptions)
|
||||
func ChangeHiddifyOptions(in *pb.ChangeHiddifyOptionsRequest) (*pb.CoreInfoResponse, error) {
|
||||
HiddifyOptions = &config.HiddifyOptions{}
|
||||
err := json.Unmarshal([]byte(in.HiddifyOptionsJson), HiddifyOptions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if configOptions.Warp.WireguardConfigStr != "" {
|
||||
err := json.Unmarshal([]byte(configOptions.Warp.WireguardConfigStr), &configOptions.Warp.WireguardConfig)
|
||||
if HiddifyOptions.Warp.WireguardConfigStr != "" {
|
||||
err := json.Unmarshal([]byte(HiddifyOptions.Warp.WireguardConfigStr), &HiddifyOptions.Warp.WireguardConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if configOptions.Warp2.WireguardConfigStr != "" {
|
||||
err := json.Unmarshal([]byte(configOptions.Warp2.WireguardConfigStr), &configOptions.Warp2.WireguardConfig)
|
||||
if HiddifyOptions.Warp2.WireguardConfigStr != "" {
|
||||
err := json.Unmarshal([]byte(HiddifyOptions.Warp2.WireguardConfigStr), &HiddifyOptions.Warp2.WireguardConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return &pb.CoreInfoResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *CoreService) GenerateConfig(ctx context.Context, in *pb.GenerateConfigRequest) (*pb.GenerateConfigResponse, error) {
|
||||
return GenerateConfig(in)
|
||||
}
|
||||
|
||||
func GenerateConfig(in *pb.GenerateConfigRequest) (*pb.GenerateConfigResponse, error) {
|
||||
defer config.DeferPanicToError("generateConfig", func(err error) {
|
||||
Log(pb.LogLevel_FATAL, pb.LogType_CONFIG, err.Error())
|
||||
StopAndAlert(pb.MessageType_UNEXPECTED_ERROR, err.Error())
|
||||
})
|
||||
if configOptions == nil {
|
||||
configOptions = config.DefaultConfigOptions()
|
||||
if HiddifyOptions == nil {
|
||||
HiddifyOptions = config.DefaultHiddifyOptions()
|
||||
}
|
||||
config, err := generateConfigFromFile(in.Path, *configOptions)
|
||||
config, err := generateConfigFromFile(in.Path, *HiddifyOptions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -241,7 +244,8 @@ func GenerateConfig(in *pb.GenerateConfigRequest) (*pb.GenerateConfigResponse, e
|
||||
ConfigContent: config,
|
||||
}, nil
|
||||
}
|
||||
func generateConfigFromFile(path string, configOpt config.ConfigOptions) (string, error) {
|
||||
|
||||
func generateConfigFromFile(path string, configOpt config.HiddifyOptions) (string, error) {
|
||||
os.Chdir(filepath.Dir(path))
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
@@ -261,6 +265,7 @@ func generateConfigFromFile(path string, configOpt config.ConfigOptions) (string
|
||||
func (s *CoreService) Stop(ctx context.Context, empty *pb.Empty) (*pb.CoreInfoResponse, error) {
|
||||
return Stop()
|
||||
}
|
||||
|
||||
func Stop() (*pb.CoreInfoResponse, error) {
|
||||
defer config.DeferPanicToError("stop", func(err error) {
|
||||
Log(pb.LogLevel_FATAL, pb.LogType_CORE, err.Error())
|
||||
@@ -310,11 +315,12 @@ func Stop() (*pb.CoreInfoResponse, error) {
|
||||
}
|
||||
resp := SetCoreStatus(pb.CoreState_STOPPED, pb.MessageType_EMPTY, "")
|
||||
return &resp, nil
|
||||
|
||||
}
|
||||
|
||||
func (s *CoreService) Restart(ctx context.Context, in *pb.StartRequest) (*pb.CoreInfoResponse, error) {
|
||||
return Restart(in)
|
||||
}
|
||||
|
||||
func Restart(in *pb.StartRequest) (*pb.CoreInfoResponse, error) {
|
||||
defer config.DeferPanicToError("restart", func(err error) {
|
||||
Log(pb.LogLevel_FATAL, pb.LogType_CORE, err.Error())
|
||||
|
||||
@@ -19,7 +19,7 @@ import (
|
||||
"github.com/sagernet/sing-box/option"
|
||||
)
|
||||
|
||||
func RunStandalone(hiddifySettingPath string, configPath string, defaultConfig config.ConfigOptions) error {
|
||||
func RunStandalone(hiddifySettingPath string, configPath string, defaultConfig config.HiddifyOptions) error {
|
||||
fmt.Println("Running in standalone mode")
|
||||
useFlutterBridge = false
|
||||
current, err := readAndBuildConfig(hiddifySettingPath, configPath, &defaultConfig)
|
||||
@@ -48,12 +48,12 @@ func RunStandalone(hiddifySettingPath string, configPath string, defaultConfig c
|
||||
}
|
||||
|
||||
type ConfigResult struct {
|
||||
Config string
|
||||
RefreshInterval int
|
||||
HiddifyConfigOptions *config.ConfigOptions
|
||||
Config string
|
||||
RefreshInterval int
|
||||
HiddifyHiddifyOptions *config.HiddifyOptions
|
||||
}
|
||||
|
||||
func readAndBuildConfig(hiddifySettingPath string, configPath string, defaultConfig *config.ConfigOptions) (ConfigResult, error) {
|
||||
func readAndBuildConfig(hiddifySettingPath string, configPath string, defaultConfig *config.HiddifyOptions) (ConfigResult, error) {
|
||||
var result ConfigResult
|
||||
|
||||
result, err := readConfigContent(configPath)
|
||||
@@ -61,21 +61,21 @@ func readAndBuildConfig(hiddifySettingPath string, configPath string, defaultCon
|
||||
return result, err
|
||||
}
|
||||
|
||||
hiddifyconfig := config.DefaultConfigOptions()
|
||||
hiddifyconfig := config.DefaultHiddifyOptions()
|
||||
|
||||
if defaultConfig != nil {
|
||||
hiddifyconfig = defaultConfig
|
||||
}
|
||||
|
||||
if hiddifySettingPath != "" {
|
||||
hiddifyconfig, err = readConfigOptionsAt(hiddifySettingPath)
|
||||
hiddifyconfig, err = readHiddifyOptionsAt(hiddifySettingPath)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
}
|
||||
|
||||
result.HiddifyConfigOptions = hiddifyconfig
|
||||
result.Config, err = buildConfig(result.Config, *result.HiddifyConfigOptions)
|
||||
result.HiddifyHiddifyOptions = hiddifyconfig
|
||||
result.Config, err = buildConfig(result.Config, *result.HiddifyHiddifyOptions)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
@@ -150,7 +150,8 @@ func extractRefreshInterval(header http.Header, bodyStr string) (int, error) {
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
func buildConfig(configContent string, options config.ConfigOptions) (string, error) {
|
||||
|
||||
func buildConfig(configContent string, options config.HiddifyOptions) (string, error) {
|
||||
parsedContent, err := config.ParseConfigContent(configContent, true, &options, false)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to parse config content: %w", err)
|
||||
@@ -194,7 +195,7 @@ func updateConfigInterval(current ConfigResult, hiddifySettingPath string, confi
|
||||
|
||||
for {
|
||||
<-time.After(time.Duration(current.RefreshInterval) * time.Hour)
|
||||
new, err := readAndBuildConfig(hiddifySettingPath, configPath, current.HiddifyConfigOptions)
|
||||
new, err := readAndBuildConfig(hiddifySettingPath, configPath, current.HiddifyHiddifyOptions)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
@@ -210,7 +211,6 @@ func updateConfigInterval(current ConfigResult, hiddifySettingPath string, confi
|
||||
}
|
||||
current = new
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func readConfigBytes(content []byte) (*option.Options, error) {
|
||||
@@ -222,14 +222,13 @@ func readConfigBytes(content []byte) (*option.Options, error) {
|
||||
return &options, nil
|
||||
}
|
||||
|
||||
func readConfigOptionsAt(path string) (*config.ConfigOptions, error) {
|
||||
func readHiddifyOptionsAt(path string) (*config.HiddifyOptions, error) {
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var options config.ConfigOptions
|
||||
var options config.HiddifyOptions
|
||||
err = json.Unmarshal(content, &options)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user