Skip to content

Commit

Permalink
refactor(config): move dohUserAgent to upstreams.userAgent
Browse files Browse the repository at this point in the history
That way it can be accessed without using `GetConfig`
  • Loading branch information
ThinkChaos committed Nov 23, 2023
1 parent 4e89b44 commit 9760735
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 9 deletions.
3 changes: 2 additions & 1 deletion config/config.go
Expand Up @@ -201,7 +201,6 @@ type Config struct {
Redis RedisConfig `yaml:"redis"`
Log log.Config `yaml:"log"`
Ports PortsConfig `yaml:"ports"`
DoHUserAgent string `yaml:"dohUserAgent"`
MinTLSServeVer string `yaml:"minTlsServeVersion" default:"1.2"`
CertFile string `yaml:"certFile"`
KeyFile string `yaml:"keyFile"`
Expand All @@ -227,6 +226,7 @@ type Config struct {
HTTPSPorts *ListenConfig `yaml:"httpsPort"`
TLSPorts *ListenConfig `yaml:"tlsPort"`
StartVerifyUpstream *bool `yaml:"startVerifyUpstream"`
DoHUserAgent *string `yaml:"dohUserAgent"`
} `yaml:",inline"`
}

Expand Down Expand Up @@ -523,6 +523,7 @@ func (cfg *Config) migrate(logger *logrus.Entry) bool {
"logPrivacy": Move(To("log.privacy", &cfg.Log)),
"logTimestamp": Move(To("log.timestamp", &cfg.Log)),
"startVerifyUpstream": Move(To("upstreams.startVerify", &cfg.Upstreams)),
"dohUserAgent": Move(To("upstreams.userAgent", &cfg.Upstreams)),
})

usesDepredOpts = cfg.Blocking.migrate(logger) || usesDepredOpts
Expand Down
9 changes: 5 additions & 4 deletions config/config_test.go
Expand Up @@ -771,6 +771,7 @@ bootstrapDns:
func defaultTestFileConfig() {
Expect(config.Ports.DNS).Should(Equal(ListenConfig{"55553", ":55554", "[::1]:55555"}))
Expect(config.Upstreams.StartVerify).Should(BeFalse())
Expect(config.Upstreams.UserAgent).Should(Equal("testBlocky"))
Expect(config.Upstreams.Groups["default"]).Should(HaveLen(3))
Expect(config.Upstreams.Groups["default"][0].Host).Should(Equal("8.8.8.8"))
Expect(config.Upstreams.Groups["default"][1].Host).Should(Equal("8.8.4.4"))
Expand All @@ -797,7 +798,6 @@ func defaultTestFileConfig() {
Expect(config.Caching.MaxCachingTime).Should(BeZero())
Expect(config.Caching.MinCachingTime).Should(BeZero())

Expect(config.DoHUserAgent).Should(Equal("testBlocky"))
Expect(config.MinTLSServeVer).Should(Equal("1.3"))

Expect(GetConfig()).Should(Not(BeNil()))
Expand All @@ -807,6 +807,7 @@ func writeConfigYml(tmpDir *helpertest.TmpFolder) *helpertest.TmpFile {
return tmpDir.CreateStringFile("config.yml",
"upstreams:",
" startVerify: false",
" userAgent: testBlocky",
" groups:",
" default:",
" - tcp+udp:8.8.8.8",
Expand Down Expand Up @@ -859,7 +860,6 @@ func writeConfigYml(tmpDir *helpertest.TmpFolder) *helpertest.TmpFile {
" target: /opt/log",
"port: 55553,:55554,[::1]:55555",
"logLevel: debug",
"dohUserAgent: testBlocky",
"minTlsServeVersion: 1.3",
)
}
Expand All @@ -868,6 +868,7 @@ func writeConfigDir(tmpDir *helpertest.TmpFolder) error {
f1 := tmpDir.CreateStringFile("config1.yaml",
"upstreams:",
" startVerify: false",
" userAgent: testBlocky",
" groups:",
" default:",
" - tcp+udp:8.8.8.8",
Expand All @@ -884,7 +885,8 @@ func writeConfigDir(tmpDir *helpertest.TmpFolder) error {
"filtering:",
" queryTypes:",
" - AAAA",
" - A")
" - A",
)
if f1.Error != nil {
return f1.Error
}
Expand Down Expand Up @@ -925,7 +927,6 @@ func writeConfigDir(tmpDir *helpertest.TmpFolder) error {
" target: /opt/log",
"port: 55553,:55554,[::1]:55555",
"logLevel: debug",
"dohUserAgent: testBlocky",
"minTlsServeVersion: 1.3",
)

Expand Down
1 change: 1 addition & 0 deletions config/upstreams.go
Expand Up @@ -12,6 +12,7 @@ type Upstreams struct {
Groups UpstreamGroups `yaml:"groups"`
Strategy UpstreamStrategy `yaml:"strategy" default:"parallel_best"`
StartVerify bool `yaml:"startVerify" default:"false"`
UserAgent string `yaml:"userAgent"`
}

type UpstreamGroups map[string][]Upstream
Expand Down
2 changes: 2 additions & 0 deletions docs/config.yml
Expand Up @@ -26,6 +26,8 @@ upstreams:
timeout: 2s
# optional: If true, blocky will fail to start unless at least one upstream server per group is reachable. Default: false
startVerify: false
# optional: HTTP User Agent when connecting to upstreams. Default: none
userAgent: "custom UA"

# optional: Determines how blocky will create outgoing connections. This impacts both upstreams, and lists.
# accepted: dual, v4, v6
Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.md
Expand Up @@ -15,7 +15,6 @@ configuration properties as [JSON](config.yml).
| ------------------- | ------------------- | --------- | ------------- | ---------------------------------------------------------------------------------------------------------- |
| certFile | path | no | | Path to cert and key file for SSL encryption (DoH and DoT); if empty, self-signed certificate is generated |
| keyFile | path | no | | Path to cert and key file for SSL encryption (DoH and DoT); if empty, self-signed certificate is generated |
| dohUserAgent | string | no | | HTTP User Agent for DoH upstreams |
| minTlsServeVersion | string | no | 1.2 | Minimum TLS version that the DoT and DoH server use to serve those encrypted DNS requests |
| connectIPVersion | enum (dual, v4, v6) | no | dual | IP version to use for outgoing connections (dual, v4, v6) |

Expand Down Expand Up @@ -75,6 +74,7 @@ All logging options are optional.
| usptreams.startVerify | bool | no | false | If true, blocky will fail to start unless at least one upstream server per group is functional. |
| usptreams.strategy | enum (parallel_best, random, strict) | no | parallel_best | Upstream server usage strategy. |
| usptreams.timeout | duration | no | 2s | Upstream connection timeout. |
| usptreams.userAgent | string | no | | HTTP User Agent when connecting to upstreams. |


### Upstream Groups
Expand Down
8 changes: 5 additions & 3 deletions resolver/upstream_resolver.go
Expand Up @@ -73,8 +73,9 @@ type dnsUpstreamClient struct {
}

type httpUpstreamClient struct {
client *http.Client
host string
client *http.Client
host string
userAgent string
}

func createUpstreamClient(cfg upstreamConfig) upstreamClient {
Expand All @@ -90,6 +91,7 @@ func createUpstreamClient(cfg upstreamConfig) upstreamClient {
switch cfg.Net {
case config.NetProtocolHttps:
return &httpUpstreamClient{
userAgent: cfg.UserAgent,
client: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tlsConfig,
Expand Down Expand Up @@ -143,7 +145,7 @@ func (r *httpUpstreamClient) callExternal(
return nil, 0, fmt.Errorf("can't create the new request %w", err)
}

req.Header.Set("User-Agent", config.GetConfig().DoHUserAgent)
req.Header.Set("User-Agent", r.userAgent)
req.Header.Set("Content-Type", dnsContentType)
req.Host = r.host

Expand Down

0 comments on commit 9760735

Please sign in to comment.