Skip to content

Commit

Permalink
Change config options structure
Browse files Browse the repository at this point in the history
  • Loading branch information
0xConsumer committed Feb 3, 2024
1 parent 2a7263b commit 96ac3b0
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 117 deletions.
101 changes: 36 additions & 65 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
},
{
Expand All @@ -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,
Expand All @@ -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"),
Expand Down Expand Up @@ -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,
},
},
)
Expand All @@ -203,15 +199,15 @@ 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,
},
},
)

remoteDNSAddress := configOpt.RemoteDnsAddress
remoteDNSAddress := opt.RemoteDnsAddress
if strings.Contains(remoteDNSAddress, "://") {
remoteDNSAddress = strings.SplitAfter(remoteDNSAddress, "://")[1]
}
Expand Down Expand Up @@ -252,7 +248,7 @@ func BuildConfig(configOpt ConfigOptions, input option.Options) (*option.Options
},
}

if configOpt.BypassLAN {
if opt.BypassLAN {
routeRules = append(
routeRules,
option.Rule{
Expand All @@ -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{
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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}
Expand All @@ -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(
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
},
}

Expand Down Expand Up @@ -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,
},
},
},
Expand Down Expand Up @@ -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{}
Expand Down
Loading

0 comments on commit 96ac3b0

Please # to comment.