From 45dae48d218eb66d4fc4b1c77bcf98dc2da47ecf Mon Sep 17 00:00:00 2001 From: Shelikhoo Date: Sat, 4 Sep 2021 21:54:21 +0100 Subject: [PATCH] added build context to dns --- infra/conf/synthetic/dns/dns.go | 32 ++++++++++++++++++++++---------- infra/conf/v5cfg/root.go | 4 ++-- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/infra/conf/synthetic/dns/dns.go b/infra/conf/synthetic/dns/dns.go index dc5e004a6..0f8208cfc 100644 --- a/infra/conf/synthetic/dns/dns.go +++ b/infra/conf/synthetic/dns/dns.go @@ -70,6 +70,7 @@ func toDomainMatchingType(t router.Domain_Type) dns.DomainMatchingType { panic("unknown domain type") } } + func (c *NameServerConfig) BuildV5(ctx context.Context) (*dns.NameServer, error) { c.cfgctx = ctx return c.Build() @@ -146,6 +147,8 @@ type DNSConfig struct { QueryStrategy string `json:"queryStrategy"` DisableCache bool `json:"disableCache"` DisableFallback bool `json:"disableFallback"` + + cfgctx context.Context } type HostAddress struct { @@ -194,21 +197,30 @@ func getHostMapping(ha *HostAddress) *dns.Config_HostMapping { } } +func (c *DNSConfig) BuildV5(ctx context.Context) (*dns.Config, error) { + c.cfgctx = ctx + return c.Build() +} + // Build implements Buildable func (c *DNSConfig) Build() (*dns.Config, error) { - cfgctx := cfgcommon.NewConfigureLoadingContext(context.Background()) - geoloadername := platform.NewEnvFlag("v2ray.conf.geoloader").GetValue(func() string { - return "standard" - }) + if c.cfgctx == nil { + c.cfgctx = cfgcommon.NewConfigureLoadingContext(context.Background()) + + geoloadername := platform.NewEnvFlag("v2ray.conf.geoloader").GetValue(func() string { + return "standard" + }) + + if loader, err := geodata.GetGeoDataLoader(geoloadername); err == nil { + cfgcommon.SetGeoDataLoader(c.cfgctx, loader) + } else { + return nil, newError("unable to create geo data loader ").Base(err) + } - if loader, err := geodata.GetGeoDataLoader(geoloadername); err == nil { - cfgcommon.SetGeoDataLoader(cfgctx, loader) - } else { - return nil, newError("unable to create geo data loader ").Base(err) } - cfgEnv := cfgcommon.GetConfigureLoadingEnvironment(cfgctx) + cfgEnv := cfgcommon.GetConfigureLoadingEnvironment(c.cfgctx) geoLoader := cfgEnv.GetGeoLoader() config := &dns.Config{ @@ -235,7 +247,7 @@ func (c *DNSConfig) Build() (*dns.Config, error) { } for _, server := range c.Servers { - server.cfgctx = cfgctx + server.cfgctx = c.cfgctx ns, err := server.Build() if err != nil { return nil, newError("failed to build nameserver").Base(err) diff --git a/infra/conf/v5cfg/root.go b/infra/conf/v5cfg/root.go index f3ab4b516..b46896e29 100644 --- a/infra/conf/v5cfg/root.go +++ b/infra/conf/v5cfg/root.go @@ -35,7 +35,7 @@ func (c RootConfig) BuildV5(ctx context.Context) (proto.Message, error) { config.App = append([]*anypb.Any{logConfMsg}, config.App...) if c.RouterConfig != nil { - routerConfig, err := c.RouterConfig.Build() + routerConfig, err := c.RouterConfig.BuildV5(ctx) if err != nil { return nil, err } @@ -43,7 +43,7 @@ func (c RootConfig) BuildV5(ctx context.Context) (proto.Message, error) { } if c.DNSConfig != nil { - dnsApp, err := c.DNSConfig.Build() + dnsApp, err := c.DNSConfig.BuildV5(ctx) if err != nil { return nil, newError("failed to parse DNS config").Base(err) }