From 2f46d512c69245f61353366657143aedde4a6bef Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Thu, 22 Jun 2023 22:33:51 +0700 Subject: [PATCH] Not send client info with non-Control D upstream by default --- config.go | 7 ++---- config_internal_test.go | 55 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/config.go b/config.go index ac6904b..cb51e97 100644 --- a/config.go +++ b/config.go @@ -248,11 +248,8 @@ func (uc *UpstreamConfig) VerifyDomain() string { // - Lan IP // - Hostname func (uc *UpstreamConfig) UpstreamSendClientInfo() bool { - if uc.SendClientInfo != nil && !(*uc.SendClientInfo) { - return false - } - if uc.SendClientInfo == nil { - return true + if uc.SendClientInfo != nil { + return *uc.SendClientInfo } switch uc.Type { case ResolverTypeDOH, ResolverTypeDOH3: diff --git a/config_internal_test.go b/config_internal_test.go index fb3692e..6fc1844 100644 --- a/config_internal_test.go +++ b/config_internal_test.go @@ -223,6 +223,61 @@ func TestUpstreamConfig_VerifyDomain(t *testing.T) { }) } } + +func TestUpstreamConfig_UpstreamSendClientInfo(t *testing.T) { + tests := []struct { + name string + uc *UpstreamConfig + sendClientInfo bool + }{ + { + "default with controld upstream DoH", + &UpstreamConfig{Endpoint: "https://freedns.controld.com/p2", Type: ResolverTypeDOH}, + true, + }, + { + "default with controld upstream DoH3", + &UpstreamConfig{Endpoint: "https://freedns.controld.com/p2", Type: ResolverTypeDOH3}, + true, + }, + { + "default with non-ControlD upstream", + &UpstreamConfig{Endpoint: "https://dns.google/dns-query", Type: ResolverTypeDOH}, + false, + }, + { + "set false with controld upstream", + &UpstreamConfig{Endpoint: "https://freedns.controld.com/p2", Type: ResolverTypeDOH, SendClientInfo: ptrBool(false)}, + false, + }, + { + "set true with controld upstream", + &UpstreamConfig{Endpoint: "https://freedns.controld.com/p2", SendClientInfo: ptrBool(true)}, + true, + }, + { + "set false with non-ControlD upstream", + &UpstreamConfig{Endpoint: "https://dns.google/dns-query", SendClientInfo: ptrBool(false)}, + false, + }, + { + "set true with non-ControlD upstream", + &UpstreamConfig{Endpoint: "https://dns.google/dns-query", Type: ResolverTypeDOH, SendClientInfo: ptrBool(true)}, + true, + }, + } + + for _, tc := range tests { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + if got := tc.uc.UpstreamSendClientInfo(); got != tc.sendClientInfo { + t.Errorf("unexpected result, want: %v, got: %v", tc.sendClientInfo, got) + } + }) + } +} + func ptrBool(b bool) *bool { return &b }