Skip to content

Commit 7b81109

Browse files
authored
Group by TCPPoolConfig config; Improve read config file lines; Fix typo (#209)
* Group by TCPPoolConfig config * Improve read config file lines; Fix typo
1 parent 14a8364 commit 7b81109

10 files changed

+100
-111
lines changed

README.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,11 @@ Configuration file is "config.json" by default:
113113
"OnlyPrimaryDNS": false,
114114
"IPv6UseAlternativeDNS": false,
115115
"AlternativeDNSConcurrent": false,
116-
"PoolIdleTimeout": 15,
117-
"PoolMaxCapacity": 15,
116+
"TCPPoolConfig": {
117+
"InitialCapacity": 0,
118+
"MaxCapacity": 15,
119+
"IdleTimeout": 30
120+
},
118121
"WhenPrimaryDNSAnswerNoneUse": "PrimaryDNS",
119122
"IPNetworkFile": {
120123
"Primary": "./ip_network_primary_sample",
@@ -208,8 +211,10 @@ IPv6). Overture will handle both TCP and UDP requests. Literal IPv6 addresses ar
208211
+ OnlyPrimaryDNS: Disable dispatcher feature, use primary DNS only.
209212
+ IPv6UseAlternativeDNS: Redirect IPv6 DNS queries to alternative DNS servers.
210213
+ AlternativeDNSConcurrent: Query the PrimaryDNS and AlternativeDNS at the same time
211-
+ PoolIdleTimeout: Specify idle timeout for connection in pool
212-
+ PoolMaxCapacity: Specify max capacity for connection pool
214+
+ TCPPoolConfig: Connection pool for TCP connections.
215+
+ IdleTimeout: Specify idle timeout for connection in pool
216+
+ InitialCapacity: Specify init capacity for connection pool
217+
+ MaxCapacity: Specify max capacity for connection pool
213218
+ WhenPrimaryDNSAnswerNoneUse: If the response of PrimaryDNS exists and there is no `ANSWER SECTION` in it, the final DNS should be defined. (There is no `AAAA` record for most domains right now)
214219
+ File: Absolute path like `/path/to/file` is allowed. For Windows users, please use properly escaped path like
215220
`C:\\path\\to\\file.txt` in the configuration.

config.sample.json

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
"OnlyPrimaryDNS": false,
3333
"IPv6UseAlternativeDNS": false,
3434
"AlternativeDNSConcurrent": false,
35+
"TCPPoolConfig": {
36+
"InitialCapacity": 0,
37+
"MaxCapacity": 15,
38+
"IdleTimeout": 30
39+
},
3540
"PoolIdleTimeout": 15,
3641
"PoolMaxCapacity": 15,
3742
"WhenPrimaryDNSAnswerNoneUse": "PrimaryDNS",

config.test.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@
3232
"OnlyPrimaryDNS": false,
3333
"IPv6UseAlternativeDNS": false,
3434
"AlternativeDNSConcurrent": false,
35-
"PoolIdleTimeout": 15,
36-
"PoolMaxCapacity": 15,
35+
"TCPPoolConfig": {
36+
"InitialCapacity": 0,
37+
"MaxCapacity": 15,
38+
"IdleTimeout": 30
39+
},
3740
"WhenPrimaryDNSAnswerNoneUse": "PrimaryDNS",
3841
"IPNetworkFile": {
3942
"Primary": "./ip_network_primary_sample",

core/config/config.go

+41-55
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ type Config struct {
3838
OnlyPrimaryDNS bool
3939
IPv6UseAlternativeDNS bool
4040
AlternativeDNSConcurrent bool
41-
PoolIdleTimeout int
42-
PoolMaxCapacity int
43-
IPNetworkFile struct {
41+
TCPPoolConfig struct {
42+
InitialCapacity int
43+
MaxCapacity int
44+
IdleTimeout int
45+
}
46+
IPNetworkFile struct {
4447
Primary string
4548
Alternative string
4649
}
@@ -141,31 +144,26 @@ func getDomainTTLMap(file string) map[string]uint32 {
141144

142145
dtl := map[string]uint32{}
143146

144-
reader := bufio.NewReader(f)
147+
scanner := bufio.NewScanner(f)
145148

146-
for {
147-
// The last line may not contains an '\n'
148-
line, err := reader.ReadString('\n')
149-
if err != nil && err != io.EOF {
150-
log.Errorf("Failed to read domain TTL file %s: %s", file, err)
151-
break
149+
for scanner.Scan() {
150+
line := scanner.Text()
151+
if len(line) == 0 {
152+
continue
152153
}
153-
154-
if line != "" {
155-
words := strings.Fields(line)
156-
if len(words) > 1 {
157-
tempInt64, err := strconv.ParseUint(words[1], 10, 32)
158-
dtl[words[0]] = uint32(tempInt64)
159-
if err != nil {
160-
log.WithFields(log.Fields{"domain": words[0], "ttl": words[1]}).Warnf("Invalid TTL for domain %s: %s", words[0], words[1])
161-
failures++
162-
failedLines = append(failedLines, line)
163-
}
164-
successes++
165-
} else {
166-
failedLines = append(failedLines, line)
154+
words := strings.Fields(line)
155+
if len(words) > 1 {
156+
tempInt64, err := strconv.ParseUint(words[1], 10, 32)
157+
dtl[words[0]] = uint32(tempInt64)
158+
if err != nil {
159+
log.WithFields(log.Fields{"domain": words[0], "ttl": words[1]}).Warnf("Invalid TTL for domain %s: %s", words[0], words[1])
167160
failures++
161+
failedLines = append(failedLines, line)
168162
}
163+
successes++
164+
} else {
165+
failedLines = append(failedLines, line)
166+
failures++
169167
}
170168
if line == "" && err == io.EOF {
171169
log.Debugf("Reading domain TTL file %s reached EOF", file)
@@ -221,7 +219,7 @@ func getFinder(name string) (f finder.Finder) {
221219
case "full-map":
222220
return &finderfull.Map{DataMap: make(map[string][]string, 100)}
223221
default:
224-
log.Warnf("Matcher %s does not exist, using full-map matcher as default", name)
222+
log.Warnf("Finder %s does not exist, using full-map finder as default", name)
225223
return &finderfull.Map{DataMap: make(map[string][]string, 100)}
226224
}
227225
}
@@ -246,15 +244,12 @@ func initDomainMatcher(file string, name string, defaultName string) (m matcher.
246244
defer f.Close()
247245

248246
lines := 0
249-
reader := bufio.NewReader(f)
250-
251-
for {
252-
line, err := reader.ReadString('\n')
253-
if err != nil && err != io.EOF {
254-
log.Errorf("Failed to read domain file %s: %s", file, err)
255-
break
247+
scanner := bufio.NewScanner(f)
248+
for scanner.Scan() {
249+
line := scanner.Text()
250+
if len(line) == 0 {
251+
continue
256252
}
257-
258253
line = strings.TrimSpace(line)
259254
if line != "" {
260255
_ = m.Insert(line)
@@ -289,31 +284,22 @@ func getIPNetworkList(file string) []*net.IPNet {
289284
failures := 0
290285
var failedLines []string
291286

292-
reader := bufio.NewReader(f)
293-
for {
294-
line, err := reader.ReadString('\n')
295-
if err != nil {
296-
if err != io.EOF {
297-
log.Errorf("Failed to read IP network file %s: %s", file, err)
298-
} else {
299-
log.Debugf("Reading IP network file %s has reached EOF", file)
300-
}
301-
break
287+
scanner := bufio.NewScanner(f)
288+
for scanner.Scan() {
289+
line := scanner.Text()
290+
if len(line) == 0 {
291+
continue
302292
}
303-
304-
if line != "" {
305-
_, ipNet, err := net.ParseCIDR(strings.TrimSuffix(line, "\n"))
306-
if err != nil {
307-
log.Errorf("Error parsing IP network CIDR %s: %s", line, err)
308-
failures++
309-
failedLines = append(failedLines, line)
310-
continue
311-
}
312-
ipNetList = append(ipNetList, ipNet)
313-
successes++
293+
_, ipNet, err := net.ParseCIDR(strings.TrimSuffix(line, "\n"))
294+
if err != nil {
295+
log.Errorf("Error parsing IP network CIDR %s: %s", line, err)
296+
failures++
297+
failedLines = append(failedLines, line)
298+
continue
314299
}
300+
ipNetList = append(ipNetList, ipNet)
301+
successes++
315302
}
316-
317303
if len(ipNetList) > 0 {
318304
log.Infof("IP network file %s has been loaded with %d records", file, successes)
319305
if failures > 0 {

core/hosts/hosts.go

+3-14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package hosts
77

88
import (
99
"bufio"
10-
"io"
1110
"net"
1211
"os"
1312
"strings"
@@ -64,19 +63,9 @@ func (h *Hosts) initHosts() error {
6463
defer f.Close()
6564
defer log.Debugf("%s took %s", "Load hosts", time.Since(time.Now()))
6665

67-
reader := bufio.NewReader(f)
68-
for {
69-
line, err := reader.ReadString('\n')
70-
if err != nil {
71-
if err != io.EOF {
72-
log.Errorf("NormalError reading hosts file: %s", err)
73-
} else {
74-
log.Debug("Reading hosts file reached EOF")
75-
}
76-
break
77-
}
78-
79-
if err := h.parseLine(line); err != nil {
66+
scanner := bufio.NewScanner(f)
67+
for scanner.Scan() {
68+
if err := h.parseLine(scanner.Text()); err != nil {
8069
log.Warnf("Bad formatted hosts file line: %s", err)
8170
}
8271
}

core/init.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ func InitServer(configFilePath string) {
2828

2929
RedirectIPv6Record: conf.IPv6UseAlternativeDNS,
3030
AlternativeDNSConcurrent: conf.AlternativeDNSConcurrent,
31-
PoolIdleTimeout: conf.PoolIdleTimeout,
32-
PoolMaxCapacity: conf.PoolMaxCapacity,
31+
TCPPoolConfig: conf.TCPPoolConfig,
3332
MinimumTTL: conf.MinimumTTL,
3433
DomainTTLMap: conf.DomainTTLMap,
3534

core/outbound/clients/resolver/base_resolver.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ func (c *BaseResolver) CreateBaseConn() (conn net.Conn, err error) {
8585
return conn, err
8686
}
8787

88-
var IdleTimeout time.Duration = 30 * time.Second
89-
var PoolMaxCapacity int = 15
88+
var InitialCapacity = 0
89+
var IdleTimeout = 30 * time.Second
90+
var MaxCapacity = 15
9091

9192
func (c *BaseResolver) setTimeout(conn net.Conn) {
9293
dnsTimeout := time.Duration(c.dnsUpstream.Timeout) * time.Second / 3
@@ -103,8 +104,8 @@ func (c *BaseResolver) setIdleTimeout(conn net.Conn) {
103104

104105
func (c *BaseResolver) createConnectionPool(connCreate func() (interface{}, error), connClose func(interface{}) error) pool.Pool {
105106
poolConfig := &pool.Config{
106-
InitialCap: 0,
107-
MaxCap: PoolMaxCapacity,
107+
InitialCap: InitialCapacity,
108+
MaxCap: MaxCapacity,
108109
Factory: connCreate,
109110
Close: connClose,
110111
//Ping: ping,

core/outbound/clients/resolver/resolver_test.go

+20-22
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010

1111
var questionDomain = "www.yahoo.com."
1212
var udpUpstream = &common.DNSUpstream{
13-
Name: "Test-UDP",
14-
Address: "8.8.8.8",
15-
Protocol: "udp",
16-
SOCKS5Address: "",
17-
Timeout: 6,
13+
Name: "Test-UDP",
14+
Address: "114.114.114.114",
15+
Protocol: "udp",
16+
SOCKS5Address: "",
17+
Timeout: 6,
1818
EDNSClientSubnet: &common.EDNSClientSubnetType{
1919
Policy: "disable",
2020
ExternalIP: "",
@@ -23,11 +23,11 @@ var udpUpstream = &common.DNSUpstream{
2323
}
2424

2525
var tcpUpstream = &common.DNSUpstream{
26-
Name: "Test-TCP",
27-
Address: "8.8.8.8",
28-
Protocol: "tcp",
29-
SOCKS5Address: "",
30-
Timeout: 6,
26+
Name: "Test-TCP",
27+
Address: "114.114.114.114",
28+
Protocol: "tcp",
29+
SOCKS5Address: "",
30+
Timeout: 6,
3131
EDNSClientSubnet: &common.EDNSClientSubnetType{
3232
Policy: "disable",
3333
ExternalIP: "",
@@ -36,11 +36,11 @@ var tcpUpstream = &common.DNSUpstream{
3636
}
3737

3838
var tcptlsUpstream = &common.DNSUpstream{
39-
Name: "Test-TCPTLS",
40-
Address: "dns.google:853@8.8.8.8",
41-
Protocol: "tcp-tls",
42-
SOCKS5Address: "",
43-
Timeout: 8,
39+
Name: "Test-TCPTLS",
40+
Address: "dns.google:853@8.8.8.8",
41+
Protocol: "tcp-tls",
42+
SOCKS5Address: "",
43+
Timeout: 8,
4444
EDNSClientSubnet: &common.EDNSClientSubnetType{
4545
Policy: "disable",
4646
ExternalIP: "",
@@ -49,11 +49,11 @@ var tcptlsUpstream = &common.DNSUpstream{
4949
}
5050

5151
var httpsUpstream = &common.DNSUpstream{
52-
Name: "Test-HTTPS",
53-
Address: "https://dns.google/dns-query",
54-
Protocol: "https",
55-
SOCKS5Address: "",
56-
Timeout: 8,
52+
Name: "Test-HTTPS",
53+
Address: "https://dns.google/dns-query",
54+
Protocol: "https",
55+
SOCKS5Address: "",
56+
Timeout: 8,
5757
EDNSClientSubnet: &common.EDNSClientSubnetType{
5858
Policy: "disable",
5959
ExternalIP: "",
@@ -94,7 +94,6 @@ func testTCP(t *testing.T) {
9494
}
9595
}
9696

97-
9897
func testTCPTLS(t *testing.T) {
9998
q := getQueryMsg(questionDomain, dns.TypeA)
10099
resolver := NewResolver(udpUpstream)
@@ -104,7 +103,6 @@ func testTCPTLS(t *testing.T) {
104103
}
105104
}
106105

107-
108106
func testHTTPS(t *testing.T) {
109107
q := getQueryMsg(questionDomain, dns.TypeA)
110108
resolver := NewResolver(udpUpstream)

core/outbound/dispatcher.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ type Dispatcher struct {
2727
DomainAlternativeList matcher.Matcher
2828
RedirectIPv6Record bool
2929
AlternativeDNSConcurrent bool
30-
PoolIdleTimeout int
31-
PoolMaxCapacity int
30+
TCPPoolConfig struct {
31+
InitialCapacity int
32+
MaxCapacity int
33+
IdleTimeout int
34+
}
3235

3336
MinimumTTL int
3437
DomainTTLMap map[string]uint32
@@ -49,9 +52,10 @@ func createResolver(ul []*common.DNSUpstream) (resolvers []resolver.Resolver) {
4952
}
5053

5154
func (d *Dispatcher) Init() {
52-
resolver.IdleTimeout = time.Duration(d.PoolIdleTimeout) * time.Second
53-
resolver.PoolMaxCapacity = d.PoolMaxCapacity
54-
log.Debugf("Set pool's IdleTimeout to %d, MaxCapacity to %d", d.PoolIdleTimeout, d.PoolMaxCapacity)
55+
resolver.IdleTimeout = time.Duration(d.TCPPoolConfig.IdleTimeout) * time.Second
56+
resolver.MaxCapacity = d.TCPPoolConfig.MaxCapacity
57+
resolver.InitialCapacity = d.TCPPoolConfig.InitialCapacity
58+
log.Debugf("Set pool's IdleTimeout to %d, InitialCapacity to %d, MaxCapacity to %d", d.TCPPoolConfig.IdleTimeout, d.TCPPoolConfig.InitialCapacity, d.TCPPoolConfig.MaxCapacity)
5559
d.primaryResolvers = createResolver(d.PrimaryDNS)
5660
d.alternativeResolvers = createResolver(d.AlternativeDNS)
5761
}

core/outbound/dispatcher_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ func init() {
3030

3131
RedirectIPv6Record: conf.IPv6UseAlternativeDNS,
3232
AlternativeDNSConcurrent: conf.AlternativeDNSConcurrent,
33-
PoolIdleTimeout: conf.PoolIdleTimeout,
34-
PoolMaxCapacity: conf.PoolMaxCapacity,
33+
TCPPoolConfig: conf.TCPPoolConfig,
3534
MinimumTTL: conf.MinimumTTL,
3635
DomainTTLMap: conf.DomainTTLMap,
3736

@@ -92,7 +91,7 @@ func testCache(t *testing.T) {
9291
exchange(questionDomain, dns.TypeA)
9392
now := time.Now()
9493
exchange(questionDomain, dns.TypeA)
95-
if time.Since(now) > 10 * time.Millisecond {
94+
if time.Since(now) > 10*time.Millisecond {
9695
t.Error(time.Since(now).String() + " " + "Cache response slower than 10ms")
9796
}
9897
}

0 commit comments

Comments
 (0)