Skip to content

Commit 7f6ceb3

Browse files
authored
DomainMatcher: Prevent illegal domain rules from causing core startup failures (#5430)
Closes #5429
1 parent fa64775 commit 7f6ceb3

File tree

5 files changed

+19
-13
lines changed

5 files changed

+19
-13
lines changed

app/dns/dns.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,12 @@ func New(ctx context.Context, config *Config) (*DNS, error) {
106106

107107
for _, ns := range config.NameServer {
108108
clientIdx := len(clients)
109-
updateDomain := func(domainRule strmatcher.Matcher, originalRuleIdx int, matcherInfos []*DomainMatcherInfo) error {
109+
updateDomain := func(domainRule strmatcher.Matcher, originalRuleIdx int, matcherInfos []*DomainMatcherInfo) {
110110
midx := domainMatcher.Add(domainRule)
111111
matcherInfos[midx] = &DomainMatcherInfo{
112112
clientIdx: uint16(clientIdx),
113113
domainRuleIdx: uint16(originalRuleIdx),
114114
}
115-
return nil
116115
}
117116

118117
myClientIP := clientIP

app/dns/hosts.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ func NewStaticHosts(hosts []*Config_HostMapping) (*StaticHosts, error) {
2727
for _, mapping := range hosts {
2828
matcher, err := toStrMatcher(mapping.Type, mapping.Domain)
2929
if err != nil {
30-
return nil, errors.New("failed to create domain matcher").Base(err)
30+
errors.LogErrorInner(context.Background(), err, "failed to create domain matcher, ignore domain rule [type: ", mapping.Type, ", domain: ", mapping.Domain, "]")
31+
continue
3132
}
3233
id := g.Add(matcher)
3334
ips := make([]net.Address, 0, len(mapping.Ip)+1)
@@ -46,10 +47,14 @@ func NewStaticHosts(hosts []*Config_HostMapping) (*StaticHosts, error) {
4647
for _, ip := range mapping.Ip {
4748
addr := net.IPAddress(ip)
4849
if addr == nil {
49-
return nil, errors.New("invalid IP address in static hosts: ", ip).AtWarning()
50+
errors.LogError(context.Background(), "invalid IP address in static hosts: ", ip, ", ignore this ip for rule [type: ", mapping.Type, ", domain: ", mapping.Domain, "]")
51+
continue
5052
}
5153
ips = append(ips, addr)
5254
}
55+
if len(ips) == 0 {
56+
continue
57+
}
5358
}
5459

5560
sh.ips[id] = ips

app/dns/nameserver.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func NewClient(
9797
tag string,
9898
ipOption dns.IPOption,
9999
matcherInfos *[]*DomainMatcherInfo,
100-
updateDomainRule func(strmatcher.Matcher, int, []*DomainMatcherInfo) error,
100+
updateDomainRule func(strmatcher.Matcher, int, []*DomainMatcherInfo),
101101
) (*Client, error) {
102102
client := &Client{}
103103

@@ -134,7 +134,8 @@ func NewClient(
134134
for _, domain := range ns.PrioritizedDomain {
135135
domainRule, err := toStrMatcher(domain.Type, domain.Domain)
136136
if err != nil {
137-
return errors.New("failed to create prioritized domain").Base(err).AtWarning()
137+
errors.LogErrorInner(ctx, err, "failed to create domain matcher, ignore domain rule [type: ", domain.Type, ", domain: ", domain.Domain, "]")
138+
domainRule, _ = toStrMatcher(DomainMatchingType_Full, "hack.fix.index.for.illegal.domain.rule")
138139
}
139140
originalRuleIdx := ruleCurr
140141
if ruleCurr < len(ns.OriginalRules) {
@@ -151,10 +152,7 @@ func NewClient(
151152
rules = append(rules, domainRule.String())
152153
ruleCurr++
153154
}
154-
err = updateDomainRule(domainRule, originalRuleIdx, *matcherInfos)
155-
if err != nil {
156-
return errors.New("failed to create prioritized domain").Base(err).AtWarning()
157-
}
155+
updateDomainRule(domainRule, originalRuleIdx, *matcherInfos)
158156
}
159157

160158
// Establish expected IPs

app/router/condition.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package router
22

33
import (
4+
"context"
45
"regexp"
56
"strings"
67

@@ -56,11 +57,13 @@ func NewMphMatcherGroup(domains []*Domain) (*DomainMatcher, error) {
5657
for _, d := range domains {
5758
matcherType, f := matcherTypeMap[d.Type]
5859
if !f {
59-
return nil, errors.New("unsupported domain type", d.Type)
60+
errors.LogError(context.Background(), "ignore unsupported domain type ", d.Type, " of rule ", d.Value)
61+
continue
6062
}
6163
_, err := g.AddPattern(d.Value, matcherType)
6264
if err != nil {
63-
return nil, err
65+
errors.LogErrorInner(context.Background(), err, "ignore domain rule ", d.Type, " ", d.Value)
66+
continue
6467
}
6568
}
6669
g.Build()

common/strmatcher/strmatcher.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package strmatcher
22

33
import (
4+
"errors"
45
"regexp"
56
)
67

@@ -44,7 +45,7 @@ func (t Type) New(pattern string) (Matcher, error) {
4445
pattern: r,
4546
}, nil
4647
default:
47-
panic("Unknown type")
48+
return nil, errors.New("unk type")
4849
}
4950
}
5051

0 commit comments

Comments
 (0)