@@ -29,8 +29,8 @@ type Client struct {
2929 server Server
3030 skipFallback bool
3131 domains []string
32- expectedIPs [] * router.GeoIPMatcher
33- unexpectedIPs [] * router.GeoIPMatcher
32+ expectedIPs router.GeoIPMatcher
33+ unexpectedIPs router.GeoIPMatcher
3434 actPrior bool
3535 actUnprior bool
3636 tag string
@@ -154,23 +154,21 @@ func NewClient(
154154 }
155155
156156 // Establish expected IPs
157- var expectedMatchers [] * router.GeoIPMatcher
158- for _ , geoip := range ns .ExpectedGeoip {
159- matcher , err : = router .GlobalGeoIPContainer . Add ( geoip )
157+ var expectedMatcher router.GeoIPMatcher
158+ if len ( ns .ExpectedGeoip ) > 0 {
159+ expectedMatcher , err = router .BuildOptimizedGeoIPMatcher ( ns . ExpectedGeoip ... )
160160 if err != nil {
161161 return errors .New ("failed to create expected ip matcher" ).Base (err ).AtWarning ()
162162 }
163- expectedMatchers = append (expectedMatchers , matcher )
164163 }
165164
166165 // Establish unexpected IPs
167- var unexpectedMatchers [] * router.GeoIPMatcher
168- for _ , geoip := range ns .UnexpectedGeoip {
169- matcher , err : = router .GlobalGeoIPContainer . Add ( geoip )
166+ var unexpectedMatcher router.GeoIPMatcher
167+ if len ( ns .UnexpectedGeoip ) > 0 {
168+ unexpectedMatcher , err = router .BuildOptimizedGeoIPMatcher ( ns . UnexpectedGeoip ... )
170169 if err != nil {
171170 return errors .New ("failed to create unexpected ip matcher" ).Base (err ).AtWarning ()
172171 }
173- unexpectedMatchers = append (unexpectedMatchers , matcher )
174172 }
175173
176174 if len (clientIP ) > 0 {
@@ -192,8 +190,8 @@ func NewClient(
192190 client .server = server
193191 client .skipFallback = ns .SkipFallback
194192 client .domains = rules
195- client .expectedIPs = expectedMatchers
196- client .unexpectedIPs = unexpectedMatchers
193+ client .expectedIPs = expectedMatcher
194+ client .unexpectedIPs = unexpectedMatcher
197195 client .actPrior = ns .ActPrior
198196 client .actUnprior = ns .ActUnprior
199197 client .tag = tag
@@ -243,32 +241,32 @@ func (c *Client) QueryIP(ctx context.Context, domain string, option dns.IPOption
243241 return nil , 0 , dns .ErrEmptyResponse
244242 }
245243
246- if len ( c .expectedIPs ) > 0 && ! c .actPrior {
247- ips = router . MatchIPs ( c .expectedIPs , ips , false )
244+ if c .expectedIPs != nil && ! c .actPrior {
245+ ips , _ = c .expectedIPs . FilterIPs ( ips )
248246 errors .LogDebug (context .Background (), "domain " , domain , " expectedIPs " , ips , " matched at server " , c .Name ())
249247 if len (ips ) == 0 {
250248 return nil , 0 , dns .ErrEmptyResponse
251249 }
252250 }
253251
254- if len ( c .unexpectedIPs ) > 0 && ! c .actUnprior {
255- ips = router . MatchIPs ( c .unexpectedIPs , ips , true )
252+ if c .unexpectedIPs != nil && ! c .actUnprior {
253+ _ , ips = c .unexpectedIPs . FilterIPs ( ips )
256254 errors .LogDebug (context .Background (), "domain " , domain , " unexpectedIPs " , ips , " matched at server " , c .Name ())
257255 if len (ips ) == 0 {
258256 return nil , 0 , dns .ErrEmptyResponse
259257 }
260258 }
261259
262- if len ( c .expectedIPs ) > 0 && c .actPrior {
263- ipsNew := router . MatchIPs ( c .expectedIPs , ips , false )
260+ if c .expectedIPs != nil && c .actPrior {
261+ ipsNew , _ := c .expectedIPs . FilterIPs ( ips )
264262 if len (ipsNew ) > 0 {
265263 ips = ipsNew
266264 errors .LogDebug (context .Background (), "domain " , domain , " priorIPs " , ips , " matched at server " , c .Name ())
267265 }
268266 }
269267
270- if len ( c .unexpectedIPs ) > 0 && c .actUnprior {
271- ipsNew := router . MatchIPs ( c .unexpectedIPs , ips , true )
268+ if c .unexpectedIPs != nil && c .actUnprior {
269+ _ , ipsNew := c .unexpectedIPs . FilterIPs ( ips )
272270 if len (ipsNew ) > 0 {
273271 ips = ipsNew
274272 errors .LogDebug (context .Background (), "domain " , domain , " unpriorIPs " , ips , " matched at server " , c .Name ())
0 commit comments