From 96ac3b0568d56ca539b2221ac3947d1c723d070d Mon Sep 17 00:00:00 2001 From: problematicconsumer Date: Sat, 3 Feb 2024 23:12:04 +0330 Subject: [PATCH] Change config options structure --- config/config.go | 101 ++++++++++++++----------------------- config/option.go | 126 ++++++++++++++++++++++++++++------------------- 2 files changed, 110 insertions(+), 117 deletions(-) diff --git a/config/config.go b/config/config.go index bce071b..f4804d4 100644 --- a/config/config.go +++ b/config/config.go @@ -52,28 +52,24 @@ func BuildConfigJson(configOpt ConfigOptions, input option.Options) (string, err } // TODO include selectors -func BuildConfig(configOpt ConfigOptions, input option.Options) (*option.Options, error) { - if configOpt.ExecuteAsIs { - return applyOverrides(configOpt, input), nil - } - - fmt.Printf("config options: %+v\n", configOpt) +func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, error) { + fmt.Printf("config options: %+v\n", opt) var options option.Options directDNSDomains := []string{} dnsRules := []option.DefaultDNSRule{} var bind string - if configOpt.AllowConnectionFromLAN { + if opt.AllowConnectionFromLAN { bind = "0.0.0.0" } else { bind = "127.0.0.1" } - if configOpt.EnableClashApi { + if opt.EnableClashApi { options.Experimental = &option.ExperimentalOptions{ ClashAPI: &option.ClashAPIOptions{ - ExternalController: fmt.Sprintf("%s:%d", "127.0.0.1", configOpt.ClashApiPort), + ExternalController: fmt.Sprintf("%s:%d", "127.0.0.1", opt.ClashApiPort), }, // CacheFile: &option.CacheFileOptions{ // Enabled: true, @@ -83,7 +79,7 @@ func BuildConfig(configOpt ConfigOptions, input option.Options) (*option.Options } options.Log = &option.LogOptions{ - Level: configOpt.LogLevel, + Level: opt.LogLevel, // Output: "box.log", Disabled: false, Timestamp: true, @@ -95,28 +91,28 @@ func BuildConfig(configOpt ConfigOptions, input option.Options) (*option.Options "sky.rethinkdns.com": getIPs([]string{"zula.ir", "www.speedtest.net", "sky.rethinkdns.com"}), }, DNSClientOptions: option.DNSClientOptions{ - IndependentCache: configOpt.IndependentDNSCache, + IndependentCache: opt.IndependentDNSCache, }, Final: DNSRemoteTag, Servers: []option.DNSServerOptions{ { Tag: DNSRemoteTag, - Address: configOpt.RemoteDnsAddress, + Address: opt.RemoteDnsAddress, AddressResolver: DNSDirectTag, - Strategy: configOpt.RemoteDnsDomainStrategy, + Strategy: opt.RemoteDnsDomainStrategy, }, { Tag: DNSTricksDirectTag, Address: "https://sky.rethinkdns.com/", // AddressResolver: "dns-local", - Strategy: configOpt.DirectDnsDomainStrategy, + Strategy: opt.DirectDnsDomainStrategy, Detour: OutboundDirectFragmentTag, }, { Tag: DNSDirectTag, - Address: configOpt.DirectDnsAddress, + Address: opt.DirectDnsAddress, AddressResolver: DNSLocalTag, - Strategy: configOpt.DirectDnsDomainStrategy, + Strategy: opt.DirectDnsDomainStrategy, Detour: OutboundDirectTag, }, { @@ -132,21 +128,21 @@ func BuildConfig(configOpt ConfigOptions, input option.Options) (*option.Options } var inboundDomainStrategy option.DomainStrategy - if !configOpt.ResolveDestination { + if !opt.ResolveDestination { inboundDomainStrategy = option.DomainStrategy(dns.DomainStrategyAsIS) } else { - inboundDomainStrategy = configOpt.IPv6Mode + inboundDomainStrategy = opt.IPv6Mode } - if configOpt.EnableTun { + if opt.EnableTun { tunInbound := option.Inbound{ Type: C.TypeTun, Tag: InboundTUNTag, TunOptions: option.TunInboundOptions{ - Stack: configOpt.TUNStack, - MTU: configOpt.MTU, + Stack: opt.TUNStack, + MTU: opt.MTU, AutoRoute: true, - StrictRoute: configOpt.StrictRoute, + StrictRoute: opt.StrictRoute, EndpointIndependentNat: true, InboundOptions: option.InboundOptions{ SniffEnabled: true, @@ -155,7 +151,7 @@ func BuildConfig(configOpt ConfigOptions, input option.Options) (*option.Options }, }, } - switch configOpt.IPv6Mode { + switch opt.IPv6Mode { case option.DomainStrategy(dns.DomainStrategyUseIPv4): tunInbound.TunOptions.Inet4Address = []netip.Prefix{ netip.MustParsePrefix("172.19.0.1/28"), @@ -183,14 +179,14 @@ func BuildConfig(configOpt ConfigOptions, input option.Options) (*option.Options MixedOptions: option.HTTPMixedInboundOptions{ ListenOptions: option.ListenOptions{ Listen: option.NewListenAddress(netip.MustParseAddr(bind)), - ListenPort: configOpt.MixedPort, + ListenPort: opt.MixedPort, InboundOptions: option.InboundOptions{ SniffEnabled: true, SniffOverrideDestination: true, DomainStrategy: inboundDomainStrategy, }, }, - SetSystemProxy: configOpt.SetSystemProxy, + SetSystemProxy: opt.SetSystemProxy, }, }, ) @@ -203,7 +199,7 @@ func BuildConfig(configOpt ConfigOptions, input option.Options) (*option.Options DirectOptions: option.DirectInboundOptions{ ListenOptions: option.ListenOptions{ Listen: option.NewListenAddress(netip.MustParseAddr(bind)), - ListenPort: configOpt.LocalDnsPort, + ListenPort: opt.LocalDnsPort, }, OverrideAddress: "1.1.1.1", OverridePort: 53, @@ -211,7 +207,7 @@ func BuildConfig(configOpt ConfigOptions, input option.Options) (*option.Options }, ) - remoteDNSAddress := configOpt.RemoteDnsAddress + remoteDNSAddress := opt.RemoteDnsAddress if strings.Contains(remoteDNSAddress, "://") { remoteDNSAddress = strings.SplitAfter(remoteDNSAddress, "://")[1] } @@ -252,7 +248,7 @@ func BuildConfig(configOpt ConfigOptions, input option.Options) (*option.Options }, } - if configOpt.BypassLAN { + if opt.BypassLAN { routeRules = append( routeRules, option.Rule{ @@ -265,7 +261,7 @@ func BuildConfig(configOpt ConfigOptions, input option.Options) (*option.Options ) } - if configOpt.EnableFakeDNS { + if opt.EnableFakeDNS { inet4Range := netip.MustParsePrefix("198.18.0.0/15") inet6Range := netip.MustParsePrefix("fc00::/18") options.DNS.FakeIP = &option.DNSFakeIPOptions{ @@ -295,7 +291,7 @@ func BuildConfig(configOpt ConfigOptions, input option.Options) (*option.Options } - for _, rule := range configOpt.Rules { + for _, rule := range opt.Rules { routeRule := rule.MakeRule() switch rule.Outbound { case "bypass": @@ -324,7 +320,7 @@ func BuildConfig(configOpt ConfigOptions, input option.Options) (*option.Options dnsRule.Server = DNSBlockTag dnsRule.DisableCache = true case "proxy": - if configOpt.EnableFakeDNS { + if opt.EnableFakeDNS { fakeDnsRule := dnsRule fakeDnsRule.Server = DNSFakeTag fakeDnsRule.Inbound = []string{InboundTUNTag} @@ -335,7 +331,7 @@ func BuildConfig(configOpt ConfigOptions, input option.Options) (*option.Options dnsRules = append(dnsRules, dnsRule) } - if configOpt.EnableDNSRouting { + if opt.EnableDNSRouting { for _, dnsRule := range dnsRules { if dnsRule.IsValid() { options.DNS.Rules = append( @@ -370,17 +366,17 @@ func BuildConfig(configOpt ConfigOptions, input option.Options) (*option.Options AutoDetectInterface: true, OverrideAndroidVPN: true, GeoIP: &option.GeoIPOptions{ - Path: configOpt.GeoIPPath, + Path: opt.GeoIPPath, }, Geosite: &option.GeositeOptions{ - Path: configOpt.GeoSitePath, + Path: opt.GeoSitePath, }, } var outbounds []option.Outbound var tags []string for _, out := range input.Outbounds { - outbound, serverDomain, err := patchOutbound(out, configOpt) + outbound, serverDomain, err := patchOutbound(out, opt) if err != nil { return nil, err } @@ -408,9 +404,9 @@ func BuildConfig(configOpt ConfigOptions, input option.Options) (*option.Options Tag: OutboundURLTestTag, URLTestOptions: option.URLTestOutboundOptions{ Outbounds: tags, - URL: configOpt.ConnectionTestUrl, - Interval: configOpt.URLTestInterval, - IdleTimeout: configOpt.URLTestIdleTimeout, + URL: opt.ConnectionTestUrl, + Interval: opt.URLTestInterval, + IdleTimeout: opt.URLTestIdleTimeout, }, } @@ -443,8 +439,8 @@ func BuildConfig(configOpt ConfigOptions, input option.Options) (*option.Options DialerOptions: option.DialerOptions{ TLSFragment: &option.TLSFragmentOptions{ Enabled: true, - Size: configOpt.TLSTricks.FragmentSize, - Sleep: configOpt.TLSTricks.FragmentSleep, + Size: opt.TLSTricks.FragmentSize, + Sleep: opt.TLSTricks.FragmentSleep, }, }, }, @@ -519,31 +515,6 @@ func isBlockedDomain(domain string) bool { return false } -func applyOverrides(overrides ConfigOptions, options option.Options) *option.Options { - if overrides.EnableClashApi { - options.Experimental.ClashAPI = &option.ClashAPIOptions{ - ExternalController: fmt.Sprintf("%s:%d", "127.0.0.1", overrides.ClashApiPort), - } - } - - options.Log = &option.LogOptions{ - Level: overrides.LogLevel, - Output: "box.log", - Disabled: false, - } - - var inbounds []option.Inbound - for _, inb := range options.Inbounds { - if inb.Type == C.TypeTun && !overrides.EnableTun { - continue - } - inbounds = append(inbounds, inb) - } - options.Inbounds = inbounds - - return &options -} - func removeDuplicateStr(strSlice []string) []string { allKeys := make(map[string]bool) list := []string{} diff --git a/config/option.go b/config/option.go index 414688f..63c59c4 100644 --- a/config/option.go +++ b/config/option.go @@ -8,36 +8,51 @@ import ( ) type ConfigOptions struct { - ExecuteAsIs bool `json:"execute-config-as-is"` - LogLevel string `json:"log-level"` - ResolveDestination bool `json:"resolve-destination"` - IPv6Mode option.DomainStrategy `json:"ipv6-mode"` + LogLevel string `json:"log-level"` + EnableClashApi bool `json:"enable-clash-api"` + ClashApiPort uint16 `json:"clash-api-port"` + GeoIPPath string `json:"geoip-path"` + GeoSitePath string `json:"geosite-path"` + Rules []Rule `json:"rules"` + DNSOptions + InboundOptions + URLTestOptions + RouteOptions + MuxOptions + TLSTricks +} + +type DNSOptions struct { RemoteDnsAddress string `json:"remote-dns-address"` RemoteDnsDomainStrategy option.DomainStrategy `json:"remote-dns-domain-strategy"` DirectDnsAddress string `json:"direct-dns-address"` DirectDnsDomainStrategy option.DomainStrategy `json:"direct-dns-domain-strategy"` - MixedPort uint16 `json:"mixed-port"` - LocalDnsPort uint16 `json:"local-dns-port"` - MTU uint32 `json:"mtu"` - StrictRoute bool `json:"strict-route"` - TUNStack string `json:"tun-stack"` - ConnectionTestUrl string `json:"connection-test-url"` - URLTestInterval option.Duration `json:"url-test-interval"` - URLTestIdleTimeout option.Duration `json:"url-test-idle-timeout"` - EnableClashApi bool `json:"enable-clash-api"` - ClashApiPort uint16 `json:"clash-api-port"` - EnableTun bool `json:"enable-tun"` - SetSystemProxy bool `json:"set-system-proxy"` - BypassLAN bool `json:"bypass-lan"` - AllowConnectionFromLAN bool `json:"allow-connection-from-lan"` + IndependentDNSCache bool `json:"independent-dns-cache"` EnableFakeDNS bool `json:"enable-fake-dns"` EnableDNSRouting bool `json:"enable-dns-routing"` - IndependentDNSCache bool `json:"independent-dns-cache"` - GeoIPPath string `json:"geoip-path"` - GeoSitePath string `json:"geosite-path"` - Rules []Rule `json:"rules"` - MuxOptions - TLSTricks +} + +type InboundOptions struct { + EnableTun bool `json:"enable-tun"` + SetSystemProxy bool `json:"set-system-proxy"` + MixedPort uint16 `json:"mixed-port"` + LocalDnsPort uint16 `json:"local-dns-port"` + MTU uint32 `json:"mtu"` + StrictRoute bool `json:"strict-route"` + TUNStack string `json:"tun-stack"` +} + +type URLTestOptions struct { + ConnectionTestUrl string `json:"connection-test-url"` + URLTestInterval option.Duration `json:"url-test-interval"` + URLTestIdleTimeout option.Duration `json:"url-test-idle-timeout"` +} + +type RouteOptions struct { + ResolveDestination bool `json:"resolve-destination"` + IPv6Mode option.DomainStrategy `json:"ipv6-mode"` + BypassLAN bool `json:"bypass-lan"` + AllowConnectionFromLAN bool `json:"allow-connection-from-lan"` } type TLSTricks struct { @@ -58,34 +73,41 @@ type MuxOptions struct { func DefaultConfigOptions() *ConfigOptions { return &ConfigOptions{ - ExecuteAsIs: false, - LogLevel: "info", - ResolveDestination: false, - IPv6Mode: option.DomainStrategy(dns.DomainStrategyAsIS), - RemoteDnsAddress: "1.1.1.1", - RemoteDnsDomainStrategy: option.DomainStrategy(dns.DomainStrategyAsIS), - DirectDnsAddress: "1.1.1.1", - DirectDnsDomainStrategy: option.DomainStrategy(dns.DomainStrategyAsIS), - MixedPort: 2334, - LocalDnsPort: 16450, - MTU: 9000, - StrictRoute: true, - TUNStack: "mixed", - ConnectionTestUrl: "http://cp.cloudflare.com/", - URLTestInterval: option.Duration(10 * time.Minute), - URLTestIdleTimeout: option.Duration(100 * time.Minute), - EnableClashApi: true, - ClashApiPort: 16756, - EnableTun: true, - SetSystemProxy: true, - BypassLAN: false, - AllowConnectionFromLAN: false, - EnableFakeDNS: false, - EnableDNSRouting: false, - IndependentDNSCache: false, - GeoIPPath: "geoip.db", - GeoSitePath: "geosite.db", - Rules: []Rule{}, + DNSOptions: DNSOptions{ + RemoteDnsAddress: "1.1.1.1", + RemoteDnsDomainStrategy: option.DomainStrategy(dns.DomainStrategyAsIS), + DirectDnsAddress: "1.1.1.1", + DirectDnsDomainStrategy: option.DomainStrategy(dns.DomainStrategyAsIS), + IndependentDNSCache: false, + EnableFakeDNS: false, + EnableDNSRouting: false, + }, + InboundOptions: InboundOptions{ + EnableTun: true, + SetSystemProxy: true, + MixedPort: 2334, + LocalDnsPort: 16450, + MTU: 9000, + StrictRoute: true, + TUNStack: "mixed", + }, + URLTestOptions: URLTestOptions{ + ConnectionTestUrl: "http://cp.cloudflare.com/", + URLTestInterval: option.Duration(10 * time.Minute), + URLTestIdleTimeout: option.Duration(100 * time.Minute), + }, + RouteOptions: RouteOptions{ + ResolveDestination: false, + IPv6Mode: option.DomainStrategy(dns.DomainStrategyAsIS), + BypassLAN: false, + AllowConnectionFromLAN: false, + }, + LogLevel: "info", + EnableClashApi: true, + ClashApiPort: 16756, + GeoIPPath: "geoip.db", + GeoSitePath: "geosite.db", + Rules: []Rule{}, MuxOptions: MuxOptions{ EnableMux: true, MuxPadding: true,