chg: HiddifyOptions
This commit is contained in:
@@ -19,7 +19,7 @@ import (
|
||||
var (
|
||||
hiddifySettingPath string
|
||||
configPath string
|
||||
defaultConfigs config.ConfigOptions = *config.DefaultConfigOptions()
|
||||
defaultConfigs config.HiddifyOptions = *config.DefaultHiddifyOptions()
|
||||
commandBuildOutputPath string
|
||||
)
|
||||
|
||||
@@ -27,13 +27,13 @@ var commandBuild = &cobra.Command{
|
||||
Use: "build",
|
||||
Short: "Build configuration",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
err := build(configPath, hiddifySettingPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
var generateConfig = &cobra.Command{
|
||||
Use: "gen",
|
||||
Short: "gen configuration",
|
||||
@@ -65,7 +65,6 @@ func init() {
|
||||
|
||||
mainCommand.AddCommand(commandBuild)
|
||||
mainCommand.AddCommand(generateConfig)
|
||||
|
||||
}
|
||||
|
||||
func build(path string, optionsPath string) error {
|
||||
@@ -81,20 +80,20 @@ func build(path string, optionsPath string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
configOptions := &defaultConfigs //config.DefaultConfigOptions()
|
||||
HiddifyOptions := &defaultConfigs // config.DefaultHiddifyOptions()
|
||||
if optionsPath != "" {
|
||||
configOptions, err = readConfigOptionsAt(optionsPath)
|
||||
HiddifyOptions, err = readHiddifyOptionsAt(optionsPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
config, err := config.BuildConfigJson(*configOptions, *options)
|
||||
config, err := config.BuildConfigJson(*HiddifyOptions, *options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if commandBuildOutputPath != "" {
|
||||
outputPath, _ := filepath.Abs(filepath.Join(workingDir, commandBuildOutputPath))
|
||||
err = os.WriteFile(outputPath, []byte(config), 0644)
|
||||
err = os.WriteFile(outputPath, []byte(config), 0o644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -137,14 +136,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
|
||||
}
|
||||
@@ -165,7 +163,6 @@ func readConfigOptionsAt(path string) (*config.ConfigOptions, error) {
|
||||
}
|
||||
|
||||
func addHConfigFlags(commandRun *cobra.Command) {
|
||||
|
||||
commandRun.Flags().StringVarP(&configPath, "config", "c", "", "proxy config path or url")
|
||||
commandRun.MarkFlagRequired("config")
|
||||
commandRun.Flags().StringVarP(&hiddifySettingPath, "hiddify", "d", "", "Hiddify Setting JSON Path")
|
||||
|
||||
@@ -19,7 +19,7 @@ var commandService = &cobra.Command{
|
||||
arg := args[0]
|
||||
switch arg {
|
||||
case "activate":
|
||||
config.ActivateTunnelService(config.ConfigOptions{
|
||||
config.ActivateTunnelService(config.HiddifyOptions{
|
||||
InboundOptions: config.InboundOptions{
|
||||
EnableTunService: true,
|
||||
MixedPort: 12334,
|
||||
@@ -36,6 +36,5 @@ var commandService = &cobra.Command{
|
||||
code, out := v2.StartTunnelService(arg)
|
||||
fmt.Printf("exitCode:%d msg=%s", code, out)
|
||||
}
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
@@ -27,7 +27,8 @@ var tunnelServiceRunning = false
|
||||
func isSupportedOS() bool {
|
||||
return runtime.GOOS == "windows" || runtime.GOOS == "linux"
|
||||
}
|
||||
func ActivateTunnelService(opt ConfigOptions) (bool, error) {
|
||||
|
||||
func ActivateTunnelService(opt HiddifyOptions) (bool, error) {
|
||||
tunnelServiceRunning = true
|
||||
// if !isSupportedOS() {
|
||||
// return false, E.New("Unsupported OS: " + runtime.GOOS)
|
||||
@@ -36,11 +37,12 @@ func ActivateTunnelService(opt ConfigOptions) (bool, error) {
|
||||
go startTunnelRequestWithFailover(opt, true)
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func DeactivateTunnelServiceForce() (bool, error) {
|
||||
return stopTunnelRequest()
|
||||
}
|
||||
func DeactivateTunnelService() (bool, error) {
|
||||
|
||||
func DeactivateTunnelService() (bool, error) {
|
||||
// if !isSupportedOS() {
|
||||
// return true, nil
|
||||
// }
|
||||
@@ -58,16 +60,15 @@ func DeactivateTunnelService() (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func startTunnelRequestWithFailover(opt ConfigOptions, installService bool) {
|
||||
func startTunnelRequestWithFailover(opt HiddifyOptions, installService bool) {
|
||||
res, err := startTunnelRequest(opt, installService)
|
||||
fmt.Printf("Start Tunnel Result: %v\n", res)
|
||||
if err != nil {
|
||||
|
||||
fmt.Printf("Start Tunnel Failed! Stopping core... err=%v\n", err)
|
||||
// StopAndAlert(pb.MessageType.MessageType_UNEXPECTED_ERROR, "Start Tunnel Failed! Stopping...")
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func isPortInUse(port string) bool {
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:"+port)
|
||||
if err != nil {
|
||||
@@ -76,7 +77,8 @@ func isPortInUse(port string) bool {
|
||||
defer listener.Close()
|
||||
return false // Port is available
|
||||
}
|
||||
func startTunnelRequest(opt ConfigOptions, installService bool) (bool, error) {
|
||||
|
||||
func startTunnelRequest(opt HiddifyOptions, installService bool) (bool, error) {
|
||||
if !isPortInUse("18020") {
|
||||
if installService {
|
||||
return runTunnelService(opt)
|
||||
@@ -153,7 +155,7 @@ func ExitTunnelService() (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func runTunnelService(opt ConfigOptions) (bool, error) {
|
||||
func runTunnelService(opt HiddifyOptions) (bool, error) {
|
||||
executablePath := getTunnelServicePath()
|
||||
fmt.Printf("Executable path is %s", executablePath)
|
||||
out, err := ExecuteCmd(executablePath, false, "tunnel", "install")
|
||||
@@ -163,7 +165,7 @@ func runTunnelService(opt ConfigOptions) (bool, error) {
|
||||
fmt.Println("Shell command executed without flag:", out, err)
|
||||
}
|
||||
if err == nil {
|
||||
<-time.After(1 * time.Second) //wait until service loaded completely
|
||||
<-time.After(1 * time.Second) // wait until service loaded completely
|
||||
}
|
||||
return startTunnelRequest(opt, false)
|
||||
}
|
||||
|
||||
@@ -41,8 +41,7 @@ const (
|
||||
|
||||
var OutboundMainProxyTag = OutboundSelectTag
|
||||
|
||||
func BuildConfigJson(configOpt ConfigOptions, input option.Options) (string, error) {
|
||||
|
||||
func BuildConfigJson(configOpt HiddifyOptions, input option.Options) (string, error) {
|
||||
options, err := BuildConfig(configOpt, input)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -59,7 +58,7 @@ func BuildConfigJson(configOpt ConfigOptions, input option.Options) (string, err
|
||||
}
|
||||
|
||||
// TODO include selectors
|
||||
func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, error) {
|
||||
func BuildConfig(opt HiddifyOptions, input option.Options) (*option.Options, error) {
|
||||
fmt.Printf("config options: %++v\n", opt)
|
||||
|
||||
var options option.Options
|
||||
@@ -82,7 +81,8 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro
|
||||
|
||||
return &options, nil
|
||||
}
|
||||
func addForceDirect(options *option.Options, opt *ConfigOptions, directDNSDomains map[string]bool) {
|
||||
|
||||
func addForceDirect(options *option.Options, opt *HiddifyOptions, directDNSDomains map[string]bool) {
|
||||
remoteDNSAddress := opt.RemoteDnsAddress
|
||||
if strings.Contains(remoteDNSAddress, "://") {
|
||||
remoteDNSAddress = strings.SplitAfter(remoteDNSAddress, "://")[1]
|
||||
@@ -124,16 +124,15 @@ func addForceDirect(options *option.Options, opt *ConfigOptions, directDNSDomain
|
||||
dnsRule.Server = DNSDirectTag
|
||||
options.DNS.Rules = append([]option.DNSRule{{Type: C.RuleTypeDefault, DefaultOptions: dnsRule}}, options.DNS.Rules...)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func setOutbounds(options *option.Options, input *option.Options, opt *ConfigOptions) error {
|
||||
func setOutbounds(options *option.Options, input *option.Options, opt *HiddifyOptions) error {
|
||||
directDNSDomains := make(map[string]bool)
|
||||
var outbounds []option.Outbound
|
||||
var tags []string
|
||||
OutboundMainProxyTag = OutboundSelectTag
|
||||
//inbound==warp over proxies
|
||||
//outbound==proxies over warp
|
||||
// inbound==warp over proxies
|
||||
// outbound==proxies over warp
|
||||
if opt.Warp.EnableWarp {
|
||||
for _, out := range input.Outbounds {
|
||||
if out.Type == C.TypeCustom {
|
||||
@@ -266,7 +265,7 @@ func setOutbounds(options *option.Options, input *option.Options, opt *ConfigOpt
|
||||
return nil
|
||||
}
|
||||
|
||||
func setClashAPI(options *option.Options, opt *ConfigOptions) {
|
||||
func setClashAPI(options *option.Options, opt *HiddifyOptions) {
|
||||
if opt.EnableClashApi {
|
||||
if opt.ClashApiSecret == "" {
|
||||
opt.ClashApiSecret = generateRandomString(16)
|
||||
@@ -285,7 +284,7 @@ func setClashAPI(options *option.Options, opt *ConfigOptions) {
|
||||
}
|
||||
}
|
||||
|
||||
func setLog(options *option.Options, opt *ConfigOptions) {
|
||||
func setLog(options *option.Options, opt *HiddifyOptions) {
|
||||
options.Log = &option.LogOptions{
|
||||
Level: opt.LogLevel,
|
||||
Output: "box.log",
|
||||
@@ -293,11 +292,9 @@ func setLog(options *option.Options, opt *ConfigOptions) {
|
||||
Timestamp: true,
|
||||
DisableColor: true,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func setInbound(options *option.Options, opt *ConfigOptions) {
|
||||
|
||||
func setInbound(options *option.Options, opt *HiddifyOptions) {
|
||||
var inboundDomainStrategy option.DomainStrategy
|
||||
if !opt.ResolveDestination {
|
||||
inboundDomainStrategy = option.DomainStrategy(dns.DomainStrategyAsIS)
|
||||
@@ -312,7 +309,6 @@ func setInbound(options *option.Options, opt *ConfigOptions) {
|
||||
Tag: InboundTUNTag,
|
||||
|
||||
TunOptions: option.TunInboundOptions{
|
||||
|
||||
Stack: opt.TUNStack,
|
||||
MTU: opt.MTU,
|
||||
AutoRoute: true,
|
||||
@@ -391,7 +387,7 @@ func setInbound(options *option.Options, opt *ConfigOptions) {
|
||||
)
|
||||
}
|
||||
|
||||
func setDns(options *option.Options, opt *ConfigOptions) {
|
||||
func setDns(options *option.Options, opt *HiddifyOptions) {
|
||||
options.DNS = &option.DNSOptions{
|
||||
StaticIPs: map[string][]string{},
|
||||
DNSClientOptions: option.DNSClientOptions{
|
||||
@@ -436,7 +432,7 @@ func setDns(options *option.Options, opt *ConfigOptions) {
|
||||
}
|
||||
}
|
||||
|
||||
func setFakeDns(options *option.Options, opt *ConfigOptions) {
|
||||
func setFakeDns(options *option.Options, opt *HiddifyOptions) {
|
||||
if opt.EnableFakeDNS {
|
||||
inet4Range := netip.MustParsePrefix("198.18.0.0/15")
|
||||
inet6Range := netip.MustParsePrefix("fc00::/18")
|
||||
@@ -468,7 +464,7 @@ func setFakeDns(options *option.Options, opt *ConfigOptions) {
|
||||
}
|
||||
}
|
||||
|
||||
func setRoutingOptions(options *option.Options, opt *ConfigOptions) {
|
||||
func setRoutingOptions(options *option.Options, opt *HiddifyOptions) {
|
||||
dnsRules := []option.DefaultDNSRule{}
|
||||
routeRules := []option.Rule{}
|
||||
rulesets := []option.RuleSet{}
|
||||
@@ -505,7 +501,6 @@ func setRoutingOptions(options *option.Options, opt *ConfigOptions) {
|
||||
},
|
||||
})
|
||||
routeRules = append(routeRules, option.Rule{
|
||||
|
||||
Type: C.RuleTypeDefault,
|
||||
DefaultOptions: option.DefaultRule{
|
||||
Port: []uint16{53},
|
||||
@@ -664,12 +659,14 @@ func setRoutingOptions(options *option.Options, opt *ConfigOptions) {
|
||||
},
|
||||
})
|
||||
dnsRules = append(dnsRules, option.DefaultDNSRule{
|
||||
RuleSet: []string{"geosite-ads",
|
||||
RuleSet: []string{
|
||||
"geosite-ads",
|
||||
"geosite-malware",
|
||||
"geosite-phishing",
|
||||
"geosite-cryptominers",
|
||||
"geoip-malware",
|
||||
"geoip-phishing"},
|
||||
"geoip-phishing",
|
||||
},
|
||||
Server: DNSBlockTag,
|
||||
// DisableCache: true,
|
||||
})
|
||||
@@ -752,10 +749,9 @@ func setRoutingOptions(options *option.Options, opt *ConfigOptions) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func patchHiddifyWarpFromConfig(out option.Outbound, opt ConfigOptions) option.Outbound {
|
||||
func patchHiddifyWarpFromConfig(out option.Outbound, opt HiddifyOptions) option.Outbound {
|
||||
if opt.Warp.EnableWarp && opt.Warp.Mode == "proxy_over_warp" {
|
||||
if out.DirectOptions.Detour == "" {
|
||||
out.DirectOptions.Detour = "Hiddify Warp ✅"
|
||||
|
||||
@@ -294,16 +294,16 @@ func file_core_proto_rawDescGZIP() []byte {
|
||||
|
||||
var file_core_proto_msgTypes = make([]protoimpl.MessageInfo, 4)
|
||||
var file_core_proto_goTypes = []interface{}{
|
||||
(*ParseConfigRequest)(nil), // 0: ConfigOptions.ParseConfigRequest
|
||||
(*ParseConfigResponse)(nil), // 1: ConfigOptions.ParseConfigResponse
|
||||
(*GenerateConfigRequest)(nil), // 2: ConfigOptions.GenerateConfigRequest
|
||||
(*GenerateConfigResponse)(nil), // 3: ConfigOptions.GenerateConfigResponse
|
||||
(*ParseConfigRequest)(nil), // 0: HiddifyOptions.ParseConfigRequest
|
||||
(*ParseConfigResponse)(nil), // 1: HiddifyOptions.ParseConfigResponse
|
||||
(*GenerateConfigRequest)(nil), // 2: HiddifyOptions.GenerateConfigRequest
|
||||
(*GenerateConfigResponse)(nil), // 3: HiddifyOptions.GenerateConfigResponse
|
||||
}
|
||||
var file_core_proto_depIdxs = []int32{
|
||||
0, // 0: ConfigOptions.CoreService.ParseConfig:input_type -> ConfigOptions.ParseConfigRequest
|
||||
2, // 1: ConfigOptions.CoreService.GenerateFullConfig:input_type -> ConfigOptions.GenerateConfigRequest
|
||||
1, // 2: ConfigOptions.CoreService.ParseConfig:output_type -> ConfigOptions.ParseConfigResponse
|
||||
3, // 3: ConfigOptions.CoreService.GenerateFullConfig:output_type -> ConfigOptions.GenerateConfigResponse
|
||||
0, // 0: HiddifyOptions.CoreService.ParseConfig:input_type -> HiddifyOptions.ParseConfigRequest
|
||||
2, // 1: HiddifyOptions.CoreService.GenerateFullConfig:input_type -> HiddifyOptions.GenerateConfigRequest
|
||||
1, // 2: HiddifyOptions.CoreService.ParseConfig:output_type -> HiddifyOptions.ParseConfigResponse
|
||||
3, // 3: HiddifyOptions.CoreService.GenerateFullConfig:output_type -> HiddifyOptions.GenerateConfigResponse
|
||||
2, // [2:4] is the sub-list for method output_type
|
||||
0, // [0:2] is the sub-list for method input_type
|
||||
0, // [0:0] is the sub-list for extension type_name
|
||||
|
||||
@@ -19,8 +19,8 @@ import (
|
||||
const _ = grpc.SupportPackageIsVersion7
|
||||
|
||||
const (
|
||||
CoreService_ParseConfig_FullMethodName = "/ConfigOptions.CoreService/ParseConfig"
|
||||
CoreService_GenerateFullConfig_FullMethodName = "/ConfigOptions.CoreService/GenerateFullConfig"
|
||||
CoreService_ParseConfig_FullMethodName = "/HiddifyOptions.CoreService/ParseConfig"
|
||||
CoreService_GenerateFullConfig_FullMethodName = "/HiddifyOptions.CoreService/GenerateFullConfig"
|
||||
)
|
||||
|
||||
// CoreServiceClient is the client API for CoreService service.
|
||||
@@ -129,7 +129,7 @@ func _CoreService_GenerateFullConfig_Handler(srv interface{}, ctx context.Contex
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
var CoreService_ServiceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "ConfigOptions.CoreService",
|
||||
ServiceName: "HiddifyOptions.CoreService",
|
||||
HandlerType: (*CoreServiceServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
dns "github.com/sagernet/sing-dns"
|
||||
)
|
||||
|
||||
type ConfigOptions struct {
|
||||
type HiddifyOptions struct {
|
||||
EnableFullConfig bool `json:"enable-full-config"`
|
||||
LogLevel string `json:"log-level"`
|
||||
EnableClashApi bool `json:"enable-clash-api"`
|
||||
@@ -92,8 +92,8 @@ type WarpOptions struct {
|
||||
Account WarpAccount
|
||||
}
|
||||
|
||||
func DefaultConfigOptions() *ConfigOptions {
|
||||
return &ConfigOptions{
|
||||
func DefaultHiddifyOptions() *HiddifyOptions {
|
||||
return &HiddifyOptions{
|
||||
DNSOptions: DNSOptions{
|
||||
RemoteDnsAddress: "1.1.1.1",
|
||||
RemoteDnsDomainStrategy: option.DomainStrategy(dns.DomainStrategyAsIS),
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
type outboundMap map[string]interface{}
|
||||
|
||||
func patchOutboundMux(base option.Outbound, configOpt ConfigOptions, obj outboundMap) outboundMap {
|
||||
func patchOutboundMux(base option.Outbound, configOpt HiddifyOptions, obj outboundMap) outboundMap {
|
||||
if configOpt.Mux.Enable {
|
||||
multiplex := option.OutboundMultiplexOptions{
|
||||
Enabled: true,
|
||||
@@ -26,8 +26,7 @@ func patchOutboundMux(base option.Outbound, configOpt ConfigOptions, obj outboun
|
||||
return obj
|
||||
}
|
||||
|
||||
func patchOutboundTLSTricks(base option.Outbound, configOpt ConfigOptions, obj outboundMap) outboundMap {
|
||||
|
||||
func patchOutboundTLSTricks(base option.Outbound, configOpt HiddifyOptions, obj outboundMap) outboundMap {
|
||||
if base.Type == C.TypeSelector || base.Type == C.TypeURLTest || base.Type == C.TypeBlock || base.Type == C.TypeDNS {
|
||||
return obj
|
||||
}
|
||||
@@ -56,9 +55,7 @@ func patchOutboundTLSTricks(base option.Outbound, configOpt ConfigOptions, obj o
|
||||
"interval": configOpt.TLSTricks.FragmentSleep,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
if base.Type == C.TypeDirect {
|
||||
return patchOutboundFragment(base, configOpt, obj)
|
||||
@@ -100,7 +97,7 @@ func patchOutboundTLSTricks(base option.Outbound, configOpt ConfigOptions, obj o
|
||||
return obj
|
||||
}
|
||||
|
||||
func patchOutboundFragment(base option.Outbound, configOpt ConfigOptions, obj outboundMap) outboundMap {
|
||||
func patchOutboundFragment(base option.Outbound, configOpt HiddifyOptions, obj outboundMap) outboundMap {
|
||||
if configOpt.TLSTricks.EnableFragment {
|
||||
obj["tcp_fast_open"] = false
|
||||
obj["tls_fragment"] = option.TLSFragmentOptions{
|
||||
@@ -127,11 +124,9 @@ func isOutboundReality(base option.Outbound) bool {
|
||||
return false
|
||||
}
|
||||
return base.VLESSOptions.OutboundTLSOptionsContainer.TLS.Reality.Enabled
|
||||
|
||||
}
|
||||
|
||||
func patchOutbound(base option.Outbound, configOpt ConfigOptions, staticIpsDns map[string][]string) (*option.Outbound, string, error) {
|
||||
|
||||
func patchOutbound(base option.Outbound, configOpt HiddifyOptions, staticIpsDns map[string][]string) (*option.Outbound, string, error) {
|
||||
formatErr := func(err error) error {
|
||||
return fmt.Errorf("error patching outbound[%s][%s]: %w", base.Tag, base.Type, err)
|
||||
}
|
||||
|
||||
@@ -6,9 +6,8 @@ import (
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/hiddify/ray2sing/ray2sing"
|
||||
"github.com/sagernet/sing-box/experimental/libbox"
|
||||
@@ -31,9 +30,10 @@ func ParseConfig(path string, debug bool) ([]byte, error) {
|
||||
}
|
||||
return ParseConfigContent(string(content), debug, nil, false)
|
||||
}
|
||||
func ParseConfigContent(contentstr string, debug bool, configOpt *ConfigOptions, fullConfig bool) ([]byte, error) {
|
||||
|
||||
func ParseConfigContent(contentstr string, debug bool, configOpt *HiddifyOptions, fullConfig bool) ([]byte, error) {
|
||||
if configOpt == nil {
|
||||
configOpt = DefaultConfigOptions()
|
||||
configOpt = DefaultHiddifyOptions()
|
||||
}
|
||||
content := []byte(contentstr)
|
||||
var jsonObj map[string]interface{} = make(map[string]interface{})
|
||||
@@ -51,7 +51,6 @@ func ParseConfigContent(contentstr string, debug bool, configOpt *ConfigOptions,
|
||||
} else {
|
||||
jsonObj["outbounds"] = tmpJsonObj["outbounds"]
|
||||
}
|
||||
|
||||
}
|
||||
} else if jsonArray, ok := tmpJsonResult.([]map[string]interface{}); ok {
|
||||
jsonObj["outbounds"] = jsonArray
|
||||
@@ -89,7 +88,7 @@ func ParseConfigContent(contentstr string, debug bool, configOpt *ConfigOptions,
|
||||
return nil, fmt.Errorf("unable to determine config format")
|
||||
}
|
||||
|
||||
func patchConfig(content []byte, name string, configOpt *ConfigOptions) ([]byte, error) {
|
||||
func patchConfig(content []byte, name string, configOpt *HiddifyOptions) ([]byte, error) {
|
||||
options := option.Options{}
|
||||
err := json.Unmarshal(content, &options)
|
||||
if err != nil {
|
||||
@@ -113,7 +112,6 @@ func patchConfig(content []byte, name string, configOpt *ConfigOptions) ([]byte,
|
||||
for i, base := range options.Outbounds {
|
||||
options.Outbounds[i] = *res[base.Tag].Value
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
content, _ = json.MarshalIndent(options, "", " ")
|
||||
@@ -123,7 +121,6 @@ func patchConfig(content []byte, name string, configOpt *ConfigOptions) ([]byte,
|
||||
}
|
||||
|
||||
func validateResult(content []byte, name string) ([]byte, error) {
|
||||
|
||||
err := libbox.CheckConfig(string(content))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("[%s] invalid sing-box config: %w", name, err)
|
||||
|
||||
@@ -22,11 +22,10 @@ func String(s string) *string {
|
||||
|
||||
func (s *server) ParseConfig(ctx context.Context, in *ParseConfigRequest) (*ParseConfigResponse, error) {
|
||||
config, err := ParseConfig(in.TempPath, in.Debug)
|
||||
|
||||
if err != nil {
|
||||
return &ParseConfigResponse{Error: String(err.Error())}, nil
|
||||
}
|
||||
err = os.WriteFile(in.Path, config, 0644)
|
||||
err = os.WriteFile(in.Path, config, 0o644)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -47,7 +46,7 @@ func (s *server) GenerateFullConfig(ctx context.Context, in *GenerateConfigReque
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config, err := BuildConfigJson(*DefaultConfigOptions(), options)
|
||||
config, err := BuildConfigJson(*DefaultHiddifyOptions(), options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package config
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/netip"
|
||||
"os"
|
||||
"strings"
|
||||
@@ -11,7 +12,6 @@ import (
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
|
||||
// "github.com/bepass-org/wireguard-go/warp"
|
||||
"log/slog"
|
||||
|
||||
"github.com/sagernet/sing-box/option"
|
||||
T "github.com/sagernet/sing-box/option"
|
||||
@@ -66,7 +66,6 @@ func wireGuardToSingbox(wgConfig WarpWireguardConfig, server string, port uint16
|
||||
}
|
||||
|
||||
func getRandomIP() string {
|
||||
|
||||
ipPort, err := warp.RandomWarpEndpoint(true, true)
|
||||
if err == nil {
|
||||
return ipPort.Addr().String()
|
||||
@@ -75,7 +74,6 @@ func getRandomIP() string {
|
||||
}
|
||||
|
||||
func generateWarp(license string, host string, port uint16, fakePackets string, fakePacketsSize string, fakePacketsDelay string, fakePacketsMode string) (*T.Outbound, error) {
|
||||
|
||||
_, _, wgConfig, err := GenerateWarpInfo(license, "", "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -142,10 +140,9 @@ func GenerateWarpInfo(license string, oldAccountId string, oldAccessToken string
|
||||
}
|
||||
|
||||
return &identity, res, &warpcfg, err
|
||||
|
||||
}
|
||||
|
||||
func patchWarp(base *option.Outbound, configOpt *ConfigOptions, final bool, staticIpsDns map[string][]string) error {
|
||||
func patchWarp(base *option.Outbound, configOpt *HiddifyOptions, final bool, staticIpsDns map[string][]string) error {
|
||||
if base.Type == C.TypeCustom {
|
||||
if warp, ok := base.CustomOptions["warp"].(map[string]interface{}); ok {
|
||||
key, _ := warp["key"].(string)
|
||||
@@ -180,7 +177,6 @@ func patchWarp(base *option.Outbound, configOpt *ConfigOptions, final bool, stat
|
||||
base.WireGuardOptions = warpConfig.WireGuardOptions
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if final && base.Type == C.TypeWireGuard {
|
||||
@@ -202,7 +198,6 @@ func patchWarp(base *option.Outbound, configOpt *ConfigOptions, final bool, stat
|
||||
}
|
||||
base.WireGuardOptions.Server = rndDomain
|
||||
}
|
||||
|
||||
}
|
||||
if base.WireGuardOptions.ServerPort == 0 {
|
||||
port := warp.RandomWarpPort()
|
||||
|
||||
@@ -4,6 +4,7 @@ package main
|
||||
#include "stdint.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
@@ -41,15 +42,14 @@ func parse(path *C.char, tempPath *C.char, debug bool) (CErr *C.char) {
|
||||
return C.CString(err.Error())
|
||||
}
|
||||
|
||||
err = os.WriteFile(C.GoString(path), []byte(res.Content), 0644)
|
||||
err = os.WriteFile(C.GoString(path), []byte(res.Content), 0o644)
|
||||
return emptyOrErrorC(err)
|
||||
}
|
||||
|
||||
//export changeConfigOptions
|
||||
func changeConfigOptions(configOptionsJson *C.char) (CErr *C.char) {
|
||||
|
||||
_, err := v2.ChangeConfigOptions(&pb.ChangeConfigOptionsRequest{
|
||||
ConfigOptionsJson: C.GoString(configOptionsJson),
|
||||
//export changeHiddifyOptions
|
||||
func changeHiddifyOptions(HiddifyOptionsJson *C.char) (CErr *C.char) {
|
||||
_, err := v2.ChangeHiddifyOptions(&pb.ChangeHiddifyOptionsRequest{
|
||||
HiddifyOptionsJson: C.GoString(HiddifyOptionsJson),
|
||||
})
|
||||
return emptyOrErrorC(err)
|
||||
}
|
||||
@@ -69,7 +69,6 @@ func generateConfig(path *C.char) (res *C.char) {
|
||||
|
||||
//export start
|
||||
func start(configPath *C.char, disableMemoryLimit bool) (CErr *C.char) {
|
||||
|
||||
_, err := v2.Start(&pb.StartRequest{
|
||||
ConfigPath: C.GoString(configPath),
|
||||
EnableOldCommandServer: true,
|
||||
@@ -80,14 +79,12 @@ func start(configPath *C.char, disableMemoryLimit bool) (CErr *C.char) {
|
||||
|
||||
//export stop
|
||||
func stop() (CErr *C.char) {
|
||||
|
||||
_, err := v2.Stop()
|
||||
return emptyOrErrorC(err)
|
||||
}
|
||||
|
||||
//export restart
|
||||
func restart(configPath *C.char, disableMemoryLimit bool) (CErr *C.char) {
|
||||
|
||||
_, err := v2.Restart(&pb.StartRequest{
|
||||
ConfigPath: C.GoString(configPath),
|
||||
EnableOldCommandServer: true,
|
||||
@@ -110,7 +107,6 @@ func stopCommandClient(command C.int) *C.char {
|
||||
|
||||
//export selectOutbound
|
||||
func selectOutbound(groupTag *C.char, outboundTag *C.char) (CErr *C.char) {
|
||||
|
||||
_, err := v2.SelectOutbound(&pb.SelectOutboundRequest{
|
||||
GroupTag: C.GoString(groupTag),
|
||||
OutboundTag: C.GoString(outboundTag),
|
||||
@@ -143,7 +139,6 @@ func generateWarpConfig(licenseKey *C.char, accountId *C.char, accessToken *C.ch
|
||||
AccountId: C.GoString(accountId),
|
||||
AccessToken: C.GoString(accessToken),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return C.CString(fmt.Sprint("error: ", err.Error()))
|
||||
}
|
||||
@@ -170,7 +165,6 @@ func generateWarpConfig(licenseKey *C.char, accountId *C.char, accessToken *C.ch
|
||||
return C.CString("")
|
||||
}
|
||||
return C.CString(string(responseJson))
|
||||
|
||||
}
|
||||
|
||||
func main() {}
|
||||
|
||||
@@ -1212,16 +1212,16 @@ func (x *ParseResponse) GetMessage() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
type ChangeConfigOptionsRequest struct {
|
||||
type ChangeHiddifyOptionsRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
ConfigOptionsJson string `protobuf:"bytes,1,opt,name=config_options_json,json=configOptionsJson,proto3" json:"config_options_json,omitempty"`
|
||||
HiddifyOptionsJson string `protobuf:"bytes,1,opt,name=config_options_json,json=HiddifyOptionsJson,proto3" json:"config_options_json,omitempty"`
|
||||
}
|
||||
|
||||
func (x *ChangeConfigOptionsRequest) Reset() {
|
||||
*x = ChangeConfigOptionsRequest{}
|
||||
func (x *ChangeHiddifyOptionsRequest) Reset() {
|
||||
*x = ChangeHiddifyOptionsRequest{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_hiddify_proto_msgTypes[14]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
@@ -1229,13 +1229,13 @@ func (x *ChangeConfigOptionsRequest) Reset() {
|
||||
}
|
||||
}
|
||||
|
||||
func (x *ChangeConfigOptionsRequest) String() string {
|
||||
func (x *ChangeHiddifyOptionsRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*ChangeConfigOptionsRequest) ProtoMessage() {}
|
||||
func (*ChangeHiddifyOptionsRequest) ProtoMessage() {}
|
||||
|
||||
func (x *ChangeConfigOptionsRequest) ProtoReflect() protoreflect.Message {
|
||||
func (x *ChangeHiddifyOptionsRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_hiddify_proto_msgTypes[14]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
@@ -1247,14 +1247,14 @@ func (x *ChangeConfigOptionsRequest) ProtoReflect() protoreflect.Message {
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use ChangeConfigOptionsRequest.ProtoReflect.Descriptor instead.
|
||||
func (*ChangeConfigOptionsRequest) Descriptor() ([]byte, []int) {
|
||||
// Deprecated: Use ChangeHiddifyOptionsRequest.ProtoReflect.Descriptor instead.
|
||||
func (*ChangeHiddifyOptionsRequest) Descriptor() ([]byte, []int) {
|
||||
return file_hiddify_proto_rawDescGZIP(), []int{14}
|
||||
}
|
||||
|
||||
func (x *ChangeConfigOptionsRequest) GetConfigOptionsJson() string {
|
||||
func (x *ChangeHiddifyOptionsRequest) GetHiddifyOptionsJson() string {
|
||||
if x != nil {
|
||||
return x.ConfigOptionsJson
|
||||
return x.HiddifyOptionsJson
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -2176,7 +2176,7 @@ var file_hiddify_proto_goTypes = []any{
|
||||
(*SystemProxyStatus)(nil), // 15: hiddifyrpc.SystemProxyStatus
|
||||
(*ParseRequest)(nil), // 16: hiddifyrpc.ParseRequest
|
||||
(*ParseResponse)(nil), // 17: hiddifyrpc.ParseResponse
|
||||
(*ChangeConfigOptionsRequest)(nil), // 18: hiddifyrpc.ChangeConfigOptionsRequest
|
||||
(*ChangeHiddifyOptionsRequest)(nil), // 18: hiddifyrpc.ChangeHiddifyOptionsRequest
|
||||
(*GenerateConfigRequest)(nil), // 19: hiddifyrpc.GenerateConfigRequest
|
||||
(*GenerateConfigResponse)(nil), // 20: hiddifyrpc.GenerateConfigResponse
|
||||
(*SelectOutboundRequest)(nil), // 21: hiddifyrpc.SelectOutboundRequest
|
||||
@@ -2212,7 +2212,7 @@ var file_hiddify_proto_depIdxs = []int32{
|
||||
26, // 16: hiddifyrpc.Core.GetSystemInfo:input_type -> hiddifyrpc.StopRequest
|
||||
6, // 17: hiddifyrpc.Core.Setup:input_type -> hiddifyrpc.SetupRequest
|
||||
16, // 18: hiddifyrpc.Core.Parse:input_type -> hiddifyrpc.ParseRequest
|
||||
18, // 19: hiddifyrpc.Core.ChangeConfigOptions:input_type -> hiddifyrpc.ChangeConfigOptionsRequest
|
||||
18, // 19: hiddifyrpc.Core.ChangeHiddifyOptions:input_type -> hiddifyrpc.ChangeHiddifyOptionsRequest
|
||||
5, // 20: hiddifyrpc.Core.StartService:input_type -> hiddifyrpc.StartRequest
|
||||
31, // 21: hiddifyrpc.Core.Stop:input_type -> hiddifyrpc.Empty
|
||||
5, // 22: hiddifyrpc.Core.Restart:input_type -> hiddifyrpc.StartRequest
|
||||
@@ -2235,7 +2235,7 @@ var file_hiddify_proto_depIdxs = []int32{
|
||||
8, // 39: hiddifyrpc.Core.GetSystemInfo:output_type -> hiddifyrpc.SystemInfo
|
||||
7, // 40: hiddifyrpc.Core.Setup:output_type -> hiddifyrpc.Response
|
||||
17, // 41: hiddifyrpc.Core.Parse:output_type -> hiddifyrpc.ParseResponse
|
||||
4, // 42: hiddifyrpc.Core.ChangeConfigOptions:output_type -> hiddifyrpc.CoreInfoResponse
|
||||
4, // 42: hiddifyrpc.Core.ChangeHiddifyOptions:output_type -> hiddifyrpc.CoreInfoResponse
|
||||
4, // 43: hiddifyrpc.Core.StartService:output_type -> hiddifyrpc.CoreInfoResponse
|
||||
4, // 44: hiddifyrpc.Core.Stop:output_type -> hiddifyrpc.CoreInfoResponse
|
||||
4, // 45: hiddifyrpc.Core.Restart:output_type -> hiddifyrpc.CoreInfoResponse
|
||||
@@ -2432,7 +2432,7 @@ func file_hiddify_proto_init() {
|
||||
}
|
||||
}
|
||||
file_hiddify_proto_msgTypes[14].Exporter = func(v any, i int) any {
|
||||
switch v := v.(*ChangeConfigOptionsRequest); i {
|
||||
switch v := v.(*ChangeHiddifyOptionsRequest); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
|
||||
@@ -161,7 +161,7 @@ const (
|
||||
Core_GetSystemInfo_FullMethodName = "/hiddifyrpc.Core/GetSystemInfo"
|
||||
Core_Setup_FullMethodName = "/hiddifyrpc.Core/Setup"
|
||||
Core_Parse_FullMethodName = "/hiddifyrpc.Core/Parse"
|
||||
Core_ChangeConfigOptions_FullMethodName = "/hiddifyrpc.Core/ChangeConfigOptions"
|
||||
Core_ChangeHiddifyOptions_FullMethodName = "/hiddifyrpc.Core/ChangeHiddifyOptions"
|
||||
Core_StartService_FullMethodName = "/hiddifyrpc.Core/StartService"
|
||||
Core_Stop_FullMethodName = "/hiddifyrpc.Core/Stop"
|
||||
Core_Restart_FullMethodName = "/hiddifyrpc.Core/Restart"
|
||||
@@ -184,7 +184,7 @@ type CoreClient interface {
|
||||
GetSystemInfo(ctx context.Context, opts ...grpc.CallOption) (grpc.BidiStreamingClient[StopRequest, SystemInfo], error)
|
||||
Setup(ctx context.Context, in *SetupRequest, opts ...grpc.CallOption) (*Response, error)
|
||||
Parse(ctx context.Context, in *ParseRequest, opts ...grpc.CallOption) (*ParseResponse, error)
|
||||
ChangeConfigOptions(ctx context.Context, in *ChangeConfigOptionsRequest, opts ...grpc.CallOption) (*CoreInfoResponse, error)
|
||||
ChangeHiddifyOptions(ctx context.Context, in *ChangeHiddifyOptionsRequest, opts ...grpc.CallOption) (*CoreInfoResponse, error)
|
||||
//rpc GenerateConfig (GenerateConfigRequest) returns (GenerateConfigResponse);
|
||||
StartService(ctx context.Context, in *StartRequest, opts ...grpc.CallOption) (*CoreInfoResponse, error)
|
||||
Stop(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*CoreInfoResponse, error)
|
||||
@@ -287,10 +287,10 @@ func (c *coreClient) Parse(ctx context.Context, in *ParseRequest, opts ...grpc.C
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *coreClient) ChangeConfigOptions(ctx context.Context, in *ChangeConfigOptionsRequest, opts ...grpc.CallOption) (*CoreInfoResponse, error) {
|
||||
func (c *coreClient) ChangeHiddifyOptions(ctx context.Context, in *ChangeHiddifyOptionsRequest, opts ...grpc.CallOption) (*CoreInfoResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(CoreInfoResponse)
|
||||
err := c.cc.Invoke(ctx, Core_ChangeConfigOptions_FullMethodName, in, out, cOpts...)
|
||||
err := c.cc.Invoke(ctx, Core_ChangeHiddifyOptions_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -401,7 +401,7 @@ type CoreServer interface {
|
||||
GetSystemInfo(grpc.BidiStreamingServer[StopRequest, SystemInfo]) error
|
||||
Setup(context.Context, *SetupRequest) (*Response, error)
|
||||
Parse(context.Context, *ParseRequest) (*ParseResponse, error)
|
||||
ChangeConfigOptions(context.Context, *ChangeConfigOptionsRequest) (*CoreInfoResponse, error)
|
||||
ChangeHiddifyOptions(context.Context, *ChangeHiddifyOptionsRequest) (*CoreInfoResponse, error)
|
||||
//rpc GenerateConfig (GenerateConfigRequest) returns (GenerateConfigResponse);
|
||||
StartService(context.Context, *StartRequest) (*CoreInfoResponse, error)
|
||||
Stop(context.Context, *Empty) (*CoreInfoResponse, error)
|
||||
@@ -443,8 +443,8 @@ func (UnimplementedCoreServer) Setup(context.Context, *SetupRequest) (*Response,
|
||||
func (UnimplementedCoreServer) Parse(context.Context, *ParseRequest) (*ParseResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method Parse not implemented")
|
||||
}
|
||||
func (UnimplementedCoreServer) ChangeConfigOptions(context.Context, *ChangeConfigOptionsRequest) (*CoreInfoResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method ChangeConfigOptions not implemented")
|
||||
func (UnimplementedCoreServer) ChangeHiddifyOptions(context.Context, *ChangeHiddifyOptionsRequest) (*CoreInfoResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method ChangeHiddifyOptions not implemented")
|
||||
}
|
||||
func (UnimplementedCoreServer) StartService(context.Context, *StartRequest) (*CoreInfoResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method StartService not implemented")
|
||||
@@ -576,20 +576,20 @@ func _Core_Parse_Handler(srv interface{}, ctx context.Context, dec func(interfac
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _Core_ChangeConfigOptions_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(ChangeConfigOptionsRequest)
|
||||
func _Core_ChangeHiddifyOptions_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(ChangeHiddifyOptionsRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(CoreServer).ChangeConfigOptions(ctx, in)
|
||||
return srv.(CoreServer).ChangeHiddifyOptions(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: Core_ChangeConfigOptions_FullMethodName,
|
||||
FullMethod: Core_ChangeHiddifyOptions_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(CoreServer).ChangeConfigOptions(ctx, req.(*ChangeConfigOptionsRequest))
|
||||
return srv.(CoreServer).ChangeHiddifyOptions(ctx, req.(*ChangeHiddifyOptionsRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
@@ -765,8 +765,8 @@ var Core_ServiceDesc = grpc.ServiceDesc{
|
||||
Handler: _Core_Parse_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "ChangeConfigOptions",
|
||||
Handler: _Core_ChangeConfigOptions_Handler,
|
||||
MethodName: "ChangeHiddifyOptions",
|
||||
Handler: _Core_ChangeHiddifyOptions_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "StartService",
|
||||
|
||||
@@ -21,10 +21,10 @@ func Parse(path string, tempPath string, debug bool) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(path, config, 0644)
|
||||
return os.WriteFile(path, config, 0o644)
|
||||
}
|
||||
|
||||
func BuildConfig(path string, configOptionsJson string) (string, error) {
|
||||
func BuildConfig(path string, HiddifyOptionsJson string) (string, error) {
|
||||
os.Chdir(filepath.Dir(path))
|
||||
fileContent, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
@@ -35,26 +35,26 @@ func BuildConfig(path string, configOptionsJson string) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
configOptions := &config.ConfigOptions{}
|
||||
err = json.Unmarshal([]byte(configOptionsJson), configOptions)
|
||||
HiddifyOptions := &config.HiddifyOptions{}
|
||||
err = json.Unmarshal([]byte(HiddifyOptionsJson), HiddifyOptions)
|
||||
if err != nil {
|
||||
return "", nil
|
||||
}
|
||||
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 "", 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 "", err
|
||||
}
|
||||
}
|
||||
|
||||
return config.BuildConfigJson(*configOptions, options)
|
||||
return config.BuildConfigJson(*HiddifyOptions, options)
|
||||
}
|
||||
|
||||
func GenerateWarpConfig(licenseKey string, accountId string, accessToken string) (string, error) {
|
||||
|
||||
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