Browse Source

added buildV5 interface to dns and router

Shelikhoo 4 years ago
parent
commit
62f71c80ca
2 changed files with 22 additions and 9 deletions
  1. 4 0
      infra/conf/synthetic/dns/dns.go
  2. 18 9
      infra/conf/synthetic/router/router.go

+ 4 - 0
infra/conf/synthetic/dns/dns.go

@@ -70,6 +70,10 @@ func toDomainMatchingType(t router.Domain_Type) dns.DomainMatchingType {
 		panic("unknown domain type")
 		panic("unknown domain type")
 	}
 	}
 }
 }
+func (c *NameServerConfig) BuildV5(ctx context.Context) (*dns.NameServer, error) {
+	c.cfgctx = ctx
+	return c.Build()
+}
 
 
 func (c *NameServerConfig) Build() (*dns.NameServer, error) {
 func (c *NameServerConfig) Build() (*dns.NameServer, error) {
 	cfgctx := c.cfgctx
 	cfgctx := c.cfgctx

+ 18 - 9
infra/conf/synthetic/router/router.go

@@ -89,6 +89,8 @@ type RouterConfig struct {
 	Balancers      []*BalancingRule   `json:"balancers"`
 	Balancers      []*BalancingRule   `json:"balancers"`
 
 
 	DomainMatcher string `json:"domainMatcher"`
 	DomainMatcher string `json:"domainMatcher"`
+
+	cfgctx context.Context
 }
 }
 
 
 func (c *RouterConfig) getDomainStrategy() router.Config_DomainStrategy {
 func (c *RouterConfig) getDomainStrategy() router.Config_DomainStrategy {
@@ -111,20 +113,27 @@ func (c *RouterConfig) getDomainStrategy() router.Config_DomainStrategy {
 	}
 	}
 }
 }
 
 
+func (c *RouterConfig) BuildV5(ctx context.Context) (*router.Config, error) {
+	c.cfgctx = ctx
+	return c.Build()
+}
+
 func (c *RouterConfig) Build() (*router.Config, error) {
 func (c *RouterConfig) Build() (*router.Config, error) {
 	config := new(router.Config)
 	config := new(router.Config)
 	config.DomainStrategy = c.getDomainStrategy()
 	config.DomainStrategy = c.getDomainStrategy()
 
 
-	cfgctx := cfgcommon.NewConfigureLoadingContext(context.Background())
+	if c.cfgctx == nil {
+		c.cfgctx = cfgcommon.NewConfigureLoadingContext(context.Background())
 
 
-	geoloadername := platform.NewEnvFlag("v2ray.conf.geoloader").GetValue(func() string {
-		return "standard"
-	})
+		geoloadername := platform.NewEnvFlag("v2ray.conf.geoloader").GetValue(func() string {
+			return "standard"
+		})
 
 
-	if loader, err := geodata.GetGeoDataLoader(geoloadername); err == nil {
-		cfgcommon.SetGeoDataLoader(cfgctx, loader)
-	} else {
-		return nil, newError("unable to create geo data loader ").Base(err)
+		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)
+		}
 	}
 	}
 
 
 	var rawRuleList []json.RawMessage
 	var rawRuleList []json.RawMessage
@@ -137,7 +146,7 @@ func (c *RouterConfig) Build() (*router.Config, error) {
 	}
 	}
 
 
 	for _, rawRule := range rawRuleList {
 	for _, rawRule := range rawRuleList {
-		rule, err := rule2.ParseRule(cfgctx, rawRule)
+		rule, err := rule2.ParseRule(c.cfgctx, rawRule)
 		if err != nil {
 		if err != nil {
 			return nil, err
 			return nil, err
 		}
 		}