Skip to content

Commit 4b78281

Browse files
committed
Add a few comments
1 parent 9bbd085 commit 4b78281

5 files changed

Lines changed: 36 additions & 10 deletions

File tree

dnscrypt-proxy/common.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ func TrimAndStripInlineComments(str string) string {
144144
return strings.TrimSpace(str)
145145
}
146146

147+
// ExtractHostAndPort parses a string containing a host and optional port.
148+
// If no port is present or cannot be parsed, the defaultPort is returned.
147149
func ExtractHostAndPort(str string, defaultPort int) (host string, port int) {
148150
host, port = str, defaultPort
149151
if idx := strings.LastIndex(str, ":"); idx >= 0 && idx < len(str)-1 {
@@ -154,11 +156,14 @@ func ExtractHostAndPort(str string, defaultPort int) (host string, port int) {
154156
return
155157
}
156158

159+
// ReadTextFile reads a file and returns its contents as a string.
160+
// It automatically removes UTF-8 BOM if present.
157161
func ReadTextFile(filename string) (string, error) {
158162
bin, err := os.ReadFile(filename)
159163
if err != nil {
160164
return "", err
161165
}
166+
// Remove UTF-8 BOM if present
162167
bin = bytes.TrimPrefix(bin, []byte{0xef, 0xbb, 0xbf})
163168
return string(bin), nil
164169
}

dnscrypt-proxy/dnsutils.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,22 @@ func TruncatedResponse(packet []byte) ([]byte, error) {
3737
}
3838

3939
func RefusedResponseFromMessage(srcMsg *dns.Msg, refusedCode bool, ipv4 net.IP, ipv6 net.IP, ttl uint32) *dns.Msg {
40+
// Create an empty response based on the source message
4041
dstMsg := EmptyResponseFromMessage(srcMsg)
42+
43+
// Add Extended DNS Error (EDE) field
4144
ede := new(dns.EDNS0_EDE)
4245
if edns0 := dstMsg.IsEdns0(); edns0 != nil {
4346
edns0.Option = append(edns0.Option, ede)
4447
}
4548
ede.InfoCode = dns.ExtendedErrorCodeFiltered
49+
50+
// Either return with refused code or a synthetic response
4651
if refusedCode {
52+
// Return a simple refused response
4753
dstMsg.Rcode = dns.RcodeRefused
4854
} else {
55+
// Return a synthetic response
4956
dstMsg.Rcode = dns.RcodeSuccess
5057
questions := srcMsg.Question
5158
if len(questions) == 0 {
@@ -54,6 +61,7 @@ func RefusedResponseFromMessage(srcMsg *dns.Msg, refusedCode bool, ipv4 net.IP,
5461
question := questions[0]
5562
sendHInfoResponse := true
5663

64+
// For A records, provide synthetic IPv4 if available
5765
if ipv4 != nil && question.Qtype == dns.TypeA {
5866
rr := new(dns.A)
5967
rr.Hdr = dns.RR_Header{Name: question.Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: ttl}
@@ -64,6 +72,7 @@ func RefusedResponseFromMessage(srcMsg *dns.Msg, refusedCode bool, ipv4 net.IP,
6472
ede.InfoCode = dns.ExtendedErrorCodeForgedAnswer
6573
}
6674
} else if ipv6 != nil && question.Qtype == dns.TypeAAAA {
75+
// For AAAA records, provide synthetic IPv6 if available
6776
rr := new(dns.AAAA)
6877
rr.Hdr = dns.RR_Header{Name: question.Name, Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: ttl}
6978
rr.AAAA = ipv6.To16()

dnscrypt-proxy/pattern_matcher.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,40 +52,47 @@ func isGlobCandidate(str string) bool {
5252
}
5353

5454
func (patternMatcher *PatternMatcher) Add(pattern string, val interface{}, position int) error {
55+
// Determine pattern type based on wildcards and special characters
5556
leadingStar := strings.HasPrefix(pattern, "*")
5657
trailingStar := strings.HasSuffix(pattern, "*")
5758
exact := strings.HasPrefix(pattern, "=")
5859
patternType := PatternTypeNone
60+
61+
// Check for glob pattern with wildcard characters
5962
if isGlobCandidate(pattern) {
6063
patternType = PatternTypePattern
61-
_, err := filepath.Match(pattern, "example.com")
64+
_, err := filepath.Match(pattern, "example.com") // Validate pattern syntax
6265
if len(pattern) < 2 || err != nil {
6366
return fmt.Errorf("Syntax error in block rules at pattern %d", position)
6467
}
6568
} else if leadingStar && trailingStar {
69+
// Substring match (*contains*)
6670
patternType = PatternTypeSubstring
6771
if len(pattern) < 3 {
6872
return fmt.Errorf("Syntax error in block rules at pattern %d", position)
6973
}
70-
pattern = pattern[1 : len(pattern)-1]
74+
pattern = pattern[1 : len(pattern)-1] // Remove stars
7175
} else if trailingStar {
76+
// Prefix match (starts*)
7277
patternType = PatternTypePrefix
7378
if len(pattern) < 2 {
7479
return fmt.Errorf("Syntax error in block rules at pattern %d", position)
7580
}
76-
pattern = pattern[:len(pattern)-1]
81+
pattern = pattern[:len(pattern)-1] // Remove trailing star
7782
} else if exact {
83+
// Exact match (=example.com)
7884
patternType = PatternTypeExact
7985
if len(pattern) < 2 {
8086
return fmt.Errorf("Syntax error in block rules at pattern %d", position)
8187
}
82-
pattern = pattern[1:]
88+
pattern = pattern[1:] // Remove = prefix
8389
} else {
90+
// Default: suffix match (*ends or .ends)
8491
patternType = PatternTypeSuffix
8592
if leadingStar {
86-
pattern = pattern[1:]
93+
pattern = pattern[1:] // Remove leading star
8794
}
88-
pattern = strings.TrimPrefix(pattern, ".")
95+
pattern = strings.TrimPrefix(pattern, ".") // Remove leading dot if present
8996
}
9097
if len(pattern) == 0 {
9198
dlog.Errorf("Syntax error in block rule at line %d", position)

dnscrypt-proxy/plugin_allow_name.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,12 @@ func (plugin *PluginAllowName) Eval(pluginsState *PluginsState, msg *dns.Msg) er
8787
if xweeklyRanges != nil {
8888
weeklyRanges = xweeklyRanges.(*WeeklyRanges)
8989
}
90-
if allowList {
91-
if weeklyRanges != nil && !weeklyRanges.Match() {
92-
allowList = false
93-
}
90+
91+
// If time-based restrictions exist and don't match current time, don't allow
92+
if allowList && weeklyRanges != nil && !weeklyRanges.Match() {
93+
allowList = false
9494
}
95+
9596
if allowList {
9697
pluginsState.sessionData["whitelisted"] = true
9798
if plugin.logger != nil {

dnscrypt-proxy/plugin_block_name.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
100100
if len(line) == 0 {
101101
continue
102102
}
103+
104+
// Handle time-based restrictions with @timerange format
103105
parts := strings.Split(line, "@")
104106
timeRangeName := ""
105107
if len(parts) == 2 {
@@ -109,6 +111,8 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
109111
dlog.Errorf("Syntax error in block rules at line %d -- Unexpected @ character", 1+lineNo)
110112
continue
111113
}
114+
115+
// Look up the time range if specified
112116
var weeklyRanges *WeeklyRanges
113117
if len(timeRangeName) > 0 {
114118
weeklyRangesX, ok := (*xBlockedNames.allWeeklyRanges)[timeRangeName]

0 commit comments

Comments
 (0)