Skip to content

Commit

Permalink
add errExpectedIPNonMatch and return nil if empty response
Browse files Browse the repository at this point in the history
  • Loading branch information
weaving118 committed Nov 20, 2019
1 parent e916a3b commit 1345185
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
21 changes: 12 additions & 9 deletions app/dns/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"v2ray.com/core"
"v2ray.com/core/app/router"
"v2ray.com/core/common"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/net"
"v2ray.com/core/common/session"
"v2ray.com/core/common/strmatcher"
Expand All @@ -38,6 +39,8 @@ type MultiGeoIPMatcher struct {
matchers []*router.GeoIPMatcher
}

var errExpectedIPNonMatch = errors.New("expected ip not match")

// Match check ip match
func (c *MultiGeoIPMatcher) Match(ip net.IP) bool {
for _, matcher := range c.matchers {
Expand Down Expand Up @@ -178,19 +181,14 @@ func (s *Server) IsOwnLink(ctx context.Context) bool {

// Match check dns ip match geoip
func (s *Server) Match(idx uint32, client Client, domain string, ips []net.IP) ([]net.IP, error) {
if len(ips) == 0 {
newError("domain ", domain, " has empty response at server ", client.Name(), " idx:", idx).AtDebug().WriteToLog()
return nil, context.Canceled
}

matcher, exist := s.ipIndexMap[idx]
if exist == false {
newError("domain ", domain, " server not in ipIndexMap: ", client.Name(), " idx:", idx, " just return").AtDebug().WriteToLog()
return ips, nil
}

if matcher.HasMatcher() == false {
newError("domain ", domain, "server has not valid matcher: ", client.Name(), " idx:", idx, " just return").AtDebug().WriteToLog()
newError("domain ", domain, " server has not valid matcher: ", client.Name(), " idx:", idx, " just return").AtDebug().WriteToLog()
return ips, nil
}

Expand All @@ -204,7 +202,7 @@ func (s *Server) Match(idx uint32, client Client, domain string, ips []net.IP) (
}
}
if len(newIps) == 0 {
return nil, context.Canceled
return nil, errExpectedIPNonMatch
}
return newIps, nil
}
Expand All @@ -217,8 +215,13 @@ func (s *Server) queryIPTimeout(idx uint32, client Client, domain string, option
})
}
ips, err := client.QueryIP(ctx, domain, option)
ips, err = s.Match(idx, client, domain, ips)
cancel()

if err != nil {
return ips, err
}

ips, err = s.Match(idx, client, domain, ips)
return ips, err
}

Expand Down Expand Up @@ -324,7 +327,7 @@ func (s *Server) lookupIPInternal(domain string, option IPOption) ([]net.IP, err
newError("failed to lookup ip for domain ", domain, " at server ", client.Name()).Base(err).WriteToLog()
lastErr = err
}
if err != context.Canceled && err != context.DeadlineExceeded {
if err != context.Canceled && err != context.DeadlineExceeded && err != errExpectedIPNonMatch {
return nil, err
}
}
Expand Down
22 changes: 11 additions & 11 deletions infra/conf/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
)

type NameServerConfig struct {
Address *Address
Port uint16
Domains []string
IP StringList
Address *Address
Port uint16
Domains []string
ExpectIPs StringList
}

func (c *NameServerConfig) UnmarshalJSON(data []byte) error {
Expand All @@ -26,16 +26,16 @@ func (c *NameServerConfig) UnmarshalJSON(data []byte) error {
}

var advanced struct {
Address *Address `json:"address"`
Port uint16 `json:"port"`
Domains []string `json:"domains"`
IP StringList `json:"ip"`
Address *Address `json:"address"`
Port uint16 `json:"port"`
Domains []string `json:"domains"`
ExpectIPs StringList `json:"expectIps"`
}
if err := json.Unmarshal(data, &advanced); err == nil {
c.Address = advanced.Address
c.Port = advanced.Port
c.Domains = advanced.Domains
c.IP = advanced.IP
c.ExpectIPs = advanced.ExpectIPs
return nil
}

Expand Down Expand Up @@ -78,9 +78,9 @@ func (c *NameServerConfig) Build() (*dns.NameServer, error) {
}
}

geoipList, err := toCidrList(c.IP)
geoipList, err := toCidrList(c.ExpectIPs)
if err != nil {
return nil, newError("invalid ip rule: ", c.IP).Base(err)
return nil, newError("invalid ip rule: ", c.ExpectIPs).Base(err)
}

return &dns.NameServer{
Expand Down

0 comments on commit 1345185

Please sign in to comment.