Skip to content

Commit 64c922a

Browse files
authored
feat: support request headers (#586)
1 parent 905c783 commit 64c922a

File tree

9 files changed

+71
-19
lines changed

9 files changed

+71
-19
lines changed

.toolbox.mk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ TB_LOCALDIR ?= $(shell which cygpath > /dev/null 2>&1 && cygpath -m $$(pwd) || p
66
## Location to install dependencies to
77
TB_LOCALBIN ?= $(TB_LOCALDIR)/bin
88
$(TB_LOCALBIN):
9-
mkdir -p $(TB_LOCALBIN)
9+
if [ ! -e $(TB_LOCALBIN) ]; then mkdir -p $(TB_LOCALBIN); fi
1010

1111
## Tool Binaries
1212
TB_CONTROLLER_GEN ?= $(TB_LOCALBIN)/controller-gen
@@ -24,15 +24,15 @@ TB_SEMVER ?= $(TB_LOCALBIN)/semver
2424
TB_CONTROLLER_GEN_VERSION ?= v0.18.0
2525
# renovate: packageName=mvdan.cc/gofumpt
2626
TB_GOFUMPT_VERSION ?= v0.8.0
27-
# renovate: packageName=github.com/golangci/golangci-lint/v2/cmd/golangci-lint
27+
# renovate: packageName=github.com/golangci/golangci-lint/v2
2828
TB_GOLANGCI_LINT_VERSION ?= v2.1.6
2929
# renovate: packageName=github.com/segmentio/golines
3030
TB_GOLINES_VERSION ?= v0.12.2
3131
# renovate: packageName=github.com/goreleaser/goreleaser/v2
3232
TB_GORELEASER_VERSION ?= v2.9.0
3333
# renovate: packageName=go.uber.org/mock/mockgen
3434
TB_MOCKGEN_VERSION ?= v0.5.2
35-
# renovate: packageName=github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen
35+
# renovate: packageName=github.com/oapi-codegen/oapi-codegen/v2
3636
TB_OAPI_CODEGEN_VERSION ?= v2.4.1
3737
# renovate: packageName=github.com/bakito/semver
3838
TB_SEMVER_VERSION ?= v1.1.3

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ For Replicas replace `#` with the index number for the replica. E.g: `REPLICA#_U
193193
| ORIGIN_USERNAME (string) | string | Adguardhome username |
194194
| ORIGIN_PASSWORD (string) | string | Adguardhome password |
195195
| ORIGIN_COOKIE (string) | string | Adguardhome cookie |
196+
| ORIGIN_REQUEST_HEADERS (map) | map | Request Headers 'key1:value1,key2:value2' |
196197
| ORIGIN_INSECURE_SKIP_VERIFY (bool) | bool | Skip TLS verification |
197198
| ORIGIN_AUTO_SETUP (bool) | bool | Automatically setup the instance if it is not initialized |
198199
| ORIGIN_INTERFACE_NAME (string) | string | Network interface name |
@@ -203,6 +204,7 @@ For Replicas replace `#` with the index number for the replica. E.g: `REPLICA#_U
203204
| REPLICA#_USERNAME (string) | string | Adguardhome username |
204205
| REPLICA#_PASSWORD (string) | string | Adguardhome password |
205206
| REPLICA#_COOKIE (string) | string | Adguardhome cookie |
207+
| REPLICA#_REQUEST_HEADERS (map) | map | Request Headers 'key1:value1,key2:value2' |
206208
| REPLICA#_INSECURE_SKIP_VERIFY (bool) | bool | Skip TLS verification |
207209
| REPLICA#_AUTO_SETUP (bool) | bool | Automatically setup the instance if it is not initialized |
208210
| REPLICA#_INTERFACE_NAME (string) | string | Network interface name |
@@ -264,6 +266,8 @@ origin:
264266
username: username
265267
password: password
266268
# cookie: Origin-Cookie-Name=CCCOOOKKKIIIEEE
269+
# requestHeaders: # Additional request headers
270+
# AAA: bbb
267271
268272
# replicas instances
269273
replicas:
@@ -278,6 +282,8 @@ replicas:
278282
# cookie: Replica2-Cookie-Name=CCCOOOKKKIIIEEE
279283
# autoSetup: true # if true, AdGuardHome is automatically initialized.
280284
# webURL: "https://some-other.url" # used in the web interface (default: <replica-url>
285+
# requestHeaders: # Additional request headers
286+
# AAA: bbb
281287
282288
# Configure the sync API server, disabled if api port is 0
283289
api:
@@ -320,9 +326,12 @@ features:
320326
accessLists: true
321327
rewrites: true
322328
```
329+
323330
## Home Assistant AdGuard Home Add-on users
324331

325-
To enable syncing with a Home Assistant instance using the [AdGuard Home Add-on](https://github.com/hassio-addons/addon-adguard-home), you will need to enable the disabled ports, under the Network heading
332+
To enable syncing with a Home Assistant instance using
333+
the [AdGuard Home Add-on](https://github.com/hassio-addons/addon-adguard-home), you will need to enable the disabled
334+
ports, under the Network heading
326335

327336
![show-disabled-ports](https://github.com/user-attachments/assets/1df5f352-37a2-4508-82ec-7f270087d0b4)
328337

@@ -334,9 +343,12 @@ Don't forget to save and restart the add-on.
334343

335344
Depending on your setup, you may also need to disable SSL for the add-on.
336345

337-
The username:password required for the Home Assistant replica is the one you use to login to your instance, however it's recommended to setup a new local only user with minimal permissions.
346+
The username:password required for the Home Assistant replica is the one you use to login to your instance, however it's
347+
recommended to setup a new local only user with minimal permissions.
338348

339-
All credit for this method goes to [Brunty](https://github.com/brunty) who has a far more [detailed write up](https://brunty.me/post/replicate-adguard-home-settings-into-home-assistant-adguard-home-addon/) about this on his blog.
349+
All credit for this method goes to [Brunty](https://github.com/brunty) who has a far
350+
more [detailed write up](https://brunty.me/post/replicate-adguard-home-settings-into-home-assistant-adguard-home-addon/)
351+
about this on his blog.
340352

341353
## Log Level
342354

docs/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ func printEnvTags(t reflect.Type, prefix string) {
3030
}
3131

3232
envTag := field.Tag.Get("env")
33-
if envTag == "REPLICA" {
34-
envTag = "REPLICA#"
33+
if envTag == "" {
34+
switch field.Name {
35+
case "Origin":
36+
envTag = "ORIGIN"
37+
case "Replica":
38+
envTag = "REPLICA#"
39+
}
3540
}
3641
combinedTag := envTag
3742
if prefix != "" && envTag != "" {

pkg/client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func New(config types.AdGuardInstance) (Client, error) {
5252
return nil, err
5353
}
5454
u.Path = path.Clean(u.Path)
55-
cl := resty.New().SetBaseURL(u.String()).SetDisableWarn(true)
55+
cl := resty.New().SetBaseURL(u.String()).SetDisableWarn(true).SetHeaders(config.RequestHeaders)
5656

5757
// #nosec G402 has to be explicitly enabled
5858
cl.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: config.InsecureSkipVerify})

pkg/config/config-schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
"webURL": {
3737
"format": "uri",
3838
"type": "string"
39+
},
40+
"requestHeaders": {
41+
"type": "object"
3942
}
4043
},
4144
"type": "object"

pkg/config/config_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,27 @@ var _ = Describe("Config", func() {
244244
Ω(cfg.Get().Features.DNS.ServerConfig).Should(BeFalse())
245245
})
246246
})
247+
248+
Context("Headers", func() {
249+
It("have headers from the config file", func() {
250+
flags.EXPECT().Changed(gm.Any()).Return(false).AnyTimes()
251+
252+
cfg, err := config.Get("../../testdata/config_test_replicas.yaml", flags)
253+
Ω(err).ShouldNot(HaveOccurred())
254+
Ω(cfg.Get().Replicas[0].RequestHeaders).Should(HaveLen(2))
255+
Ω(cfg.Get().Replicas[0].RequestHeaders["FOO"]).Should(Equal("bar"))
256+
Ω(cfg.Get().Replicas[0].RequestHeaders["Client-ID"]).Should(Equal("xxxx"))
257+
})
258+
It("have headers from the config file will be replaced when defined as ENV", func() {
259+
setEnv("REPLICA1_REQUEST_HEADERS", "AAA:bbb")
260+
flags.EXPECT().Changed(gm.Any()).Return(false).AnyTimes()
261+
262+
cfg, err := config.Get("../../testdata/config_test_replicas.yaml", flags)
263+
Ω(err).ShouldNot(HaveOccurred())
264+
Ω(cfg.Get().Replicas[0].RequestHeaders).Should(HaveLen(1))
265+
Ω(cfg.Get().Replicas[0].RequestHeaders["AAA"]).Should(Equal("bbb"))
266+
})
267+
})
247268
})
248269
})
249270
})

pkg/types/types.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,17 @@ func (cfg *Config) Init() error {
145145
// AdGuardInstance AdguardHome config instance
146146
// +k8s:deepcopy-gen=true
147147
type AdGuardInstance struct {
148-
URL string `documentation:"URL of adguardhome instance" env:"URL" faker:"url" json:"url" yaml:"url"`
149-
WebURL string `documentation:"Web URL of adguardhome instance" env:"WEB_URL" faker:"url" json:"webURL" yaml:"webURL"`
150-
APIPath string `documentation:"API Path" env:"API_PATH" json:"apiPath,omitempty" yaml:"apiPath,omitempty"`
151-
Username string `documentation:"Adguardhome username" env:"USERNAME" json:"username,omitempty" yaml:"username,omitempty"`
152-
Password string `documentation:"Adguardhome password" env:"PASSWORD" json:"password,omitempty" yaml:"password,omitempty"`
153-
Cookie string `documentation:"Adguardhome cookie" env:"COOKIE" json:"cookie,omitempty" yaml:"cookie,omitempty"`
154-
InsecureSkipVerify bool `documentation:"Skip TLS verification" env:"INSECURE_SKIP_VERIFY" json:"insecureSkipVerify" yaml:"insecureSkipVerify"`
155-
AutoSetup bool `documentation:"Automatically setup the instance if it is not initialized" env:"AUTO_SETUP" json:"autoSetup" yaml:"autoSetup"`
156-
InterfaceName string `documentation:"Network interface name" env:"INTERFACE_NAME" json:"interfaceName,omitempty" yaml:"interfaceName,omitempty"`
157-
DHCPServerEnabled *bool `documentation:"Enable DHCP server" env:"DHCP_SERVER_ENABLED" json:"dhcpServerEnabled,omitempty" yaml:"dhcpServerEnabled,omitempty"`
148+
URL string `documentation:"URL of adguardhome instance" env:"URL" faker:"url" json:"url" yaml:"url"`
149+
WebURL string `documentation:"Web URL of adguardhome instance" env:"WEB_URL" faker:"url" json:"webURL" yaml:"webURL"`
150+
APIPath string `documentation:"API Path" env:"API_PATH" json:"apiPath,omitempty" yaml:"apiPath,omitempty"`
151+
Username string `documentation:"Adguardhome username" env:"USERNAME" json:"username,omitempty" yaml:"username,omitempty"`
152+
Password string `documentation:"Adguardhome password" env:"PASSWORD" json:"password,omitempty" yaml:"password,omitempty"`
153+
Cookie string `documentation:"Adguardhome cookie" env:"COOKIE" json:"cookie,omitempty" yaml:"cookie,omitempty"`
154+
RequestHeaders map[string]string `documentation:"Request Headers 'key1:value1,key2:value2'" env:"REQUEST_HEADERS" json:"requestHeaders,omitempty" yaml:"requestHeaders,omitempty"`
155+
InsecureSkipVerify bool `documentation:"Skip TLS verification" env:"INSECURE_SKIP_VERIFY" json:"insecureSkipVerify" yaml:"insecureSkipVerify"`
156+
AutoSetup bool `documentation:"Automatically setup the instance if it is not initialized" env:"AUTO_SETUP" json:"autoSetup" yaml:"autoSetup"`
157+
InterfaceName string `documentation:"Network interface name" env:"INTERFACE_NAME" json:"interfaceName,omitempty" yaml:"interfaceName,omitempty"`
158+
DHCPServerEnabled *bool `documentation:"Enable DHCP server" env:"DHCP_SERVER_ENABLED" json:"dhcpServerEnabled,omitempty" yaml:"dhcpServerEnabled,omitempty"`
158159

159160
Host string `json:"-" yaml:"-"`
160161
WebHost string `json:"-" yaml:"-"`

pkg/types/zz_generated.deepcopy.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/config_test_replicas.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ replicas:
1616
autoSetup: false
1717
interfaceName: eth3
1818
dhcpServerEnabled: false
19+
requestHeaders:
20+
FOO: bar
21+
Client-ID: xxxx
1922
cron: '*/15 * * * *'
2023
runOnStart: true
2124
printConfigOnly: true

0 commit comments

Comments
 (0)