@@ -11,7 +11,6 @@ import (
11
11
"strings"
12
12
"time"
13
13
14
- "github.com/AdguardTeam/AdGuardHome/internal/aghalg"
15
14
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
16
15
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
17
16
"github.com/AdguardTeam/AdGuardHome/internal/aghtls"
@@ -168,43 +167,34 @@ type EDNSClientSubnet struct {
168
167
UseCustom bool `yaml:"use_custom"`
169
168
}
170
169
171
- // TLSConfig is the TLS configuration for HTTPS, DNS-over-HTTPS, and DNS-over-TLS
170
+ // TLSConfig contains the TLS configuration settings for DNS-over-HTTPS (DoH),
171
+ // DNS-over-TLS (DoT), DNS-over-QUIC (DoQ), and Discovery of Designated
172
+ // Resolvers (DDR).
172
173
type TLSConfig struct {
173
- cert tls.Certificate
174
+ // Cert is the TLS certificate used for TLS connections. It is nil if
175
+ // encryption is disabled.
176
+ Cert * tls.Certificate
174
177
175
- TLSListenAddrs [] * net. TCPAddr `yaml:"-" json:"-"`
176
- QUICListenAddrs [] * net. UDPAddr `yaml:"-" json:"-"`
177
- HTTPSListenAddrs []* net.TCPAddr `yaml:"-" json:"-"`
178
+ // TLSListenAddrs are the addresses to listen on for DoT connections. Each
179
+ // item in the list must be non-nil if Cert is not nil.
180
+ TLSListenAddrs []* net.TCPAddr
178
181
179
- // PEM-encoded certificates chain
180
- CertificateChain string `yaml:"certificate_chain" json:"certificate_chain"`
181
- // PEM-encoded private key
182
- PrivateKey string `yaml:"private_key" json:"private_key"`
182
+ // QUICListenAddrs are the addresses to listen on for DoQ connections. Each
183
+ // item in the list must be non-nil if Cert is not nil.
184
+ QUICListenAddrs []* net.UDPAddr
183
185
184
- CertificatePath string `yaml:"certificate_path" json:"certificate_path"`
185
- PrivateKeyPath string `yaml:"private_key_path" json:"private_key_path"`
186
-
187
- CertificateChainData []byte `yaml:"-" json:"-"`
188
- PrivateKeyData []byte `yaml:"-" json:"-"`
186
+ // HTTPSListenAddrs should be the addresses AdGuard Home is listening on for
187
+ // DoH connections. These addresses are announced with DDR. Each item in
188
+ // the list must be non-nil.
189
+ HTTPSListenAddrs []* net.TCPAddr
189
190
190
191
// ServerName is the hostname of the server. Currently, it is only being
191
192
// used for ClientID checking and Discovery of Designated Resolvers (DDR).
192
- ServerName string `yaml:"-" json:"-"`
193
-
194
- // DNS names from certificate (SAN) or CN value from Subject
195
- dnsNames []string
196
-
197
- // OverrideTLSCiphers, when set, contains the names of the cipher suites to
198
- // use. If the slice is empty, the default safe suites are used.
199
- OverrideTLSCiphers []string `yaml:"override_tls_ciphers,omitempty" json:"-"`
193
+ ServerName string
200
194
201
195
// StrictSNICheck controls if the connections with SNI mismatching the
202
196
// certificate's ones should be rejected.
203
- StrictSNICheck bool `yaml:"strict_sni_check" json:"-"`
204
-
205
- // hasIPAddrs is set during the certificate parsing and is true if the
206
- // configured certificate contains at least a single IP address.
207
- hasIPAddrs bool
197
+ StrictSNICheck bool
208
198
}
209
199
210
200
// DNSCryptConfig is the DNSCrypt server configuration struct.
@@ -239,8 +229,11 @@ type ServerConfig struct {
239
229
// Remove that.
240
230
AddrProcConf * client.DefaultAddrProcConfig
241
231
232
+ // TLSConf is the TLS configuration for DNS-over-TLS, DNS-over-QUIC, and
233
+ // HTTPS. It must not be nil.
234
+ TLSConf * TLSConfig
235
+
242
236
Config
243
- TLSConfig
244
237
DNSCryptConfig
245
238
TLSAllowUnencryptedDoH bool
246
239
@@ -608,45 +601,33 @@ func (conf *ServerConfig) ourAddrsSet() (m addrPortSet, err error) {
608
601
}
609
602
}
610
603
611
- // prepareTLS - prepares TLS configuration for the DNS proxy
604
+ // prepareTLS sets up the TLS configuration for the DNS proxy.
612
605
func (s * Server ) prepareTLS (proxyConfig * proxy.Config ) (err error ) {
613
- if len ( s .conf .CertificateChainData ) == 0 || len ( s . conf . PrivateKeyData ) == 0 {
614
- return nil
606
+ if s .conf .TLSConf . Cert == nil {
607
+ return
615
608
}
616
609
617
- if s .conf .TLSListenAddrs == nil && s .conf .QUICListenAddrs == nil {
610
+ if s .conf .TLSConf . TLSListenAddrs == nil && s .conf . TLSConf .QUICListenAddrs == nil {
618
611
return nil
619
612
}
620
613
621
- proxyConfig .TLSListenAddr = aghalg .CoalesceSlice (
622
- s .conf .TLSListenAddrs ,
623
- proxyConfig .TLSListenAddr ,
624
- )
625
-
626
- proxyConfig .QUICListenAddr = aghalg .CoalesceSlice (
627
- s .conf .QUICListenAddrs ,
628
- proxyConfig .QUICListenAddr ,
629
- )
630
-
631
- s .conf .cert , err = tls .X509KeyPair (s .conf .CertificateChainData , s .conf .PrivateKeyData )
632
- if err != nil {
633
- return fmt .Errorf ("failed to parse TLS keypair: %w" , err )
634
- }
614
+ proxyConfig .TLSListenAddr = s .conf .TLSConf .TLSListenAddrs
615
+ proxyConfig .QUICListenAddr = s .conf .TLSConf .QUICListenAddrs
635
616
636
- cert , err := x509 .ParseCertificate (s .conf .cert .Certificate [0 ])
617
+ cert , err := x509 .ParseCertificate (s .conf .TLSConf . Cert .Certificate [0 ])
637
618
if err != nil {
638
619
return fmt .Errorf ("x509.ParseCertificate(): %w" , err )
639
620
}
640
621
641
- s .conf . hasIPAddrs = aghtls .CertificateHasIP (cert )
622
+ s .hasIPAddrs = aghtls .CertificateHasIP (cert )
642
623
643
- if s .conf .StrictSNICheck {
624
+ if s .conf .TLSConf . StrictSNICheck {
644
625
if len (cert .DNSNames ) != 0 {
645
- s .conf . dnsNames = cert .DNSNames
626
+ s .dnsNames = cert .DNSNames
646
627
log .Debug ("dns: using certificate's SAN as DNS names: %v" , cert .DNSNames )
647
- slices .Sort (s .conf . dnsNames )
628
+ slices .Sort (s .dnsNames )
648
629
} else {
649
- s .conf . dnsNames = append ( s . conf . dnsNames , cert .Subject .CommonName )
630
+ s .dnsNames = [] string { cert .Subject .CommonName }
650
631
log .Debug ("dns: using certificate's CN as DNS name: %s" , cert .Subject .CommonName )
651
632
}
652
633
}
@@ -695,11 +676,11 @@ func anyNameMatches(dnsNames []string, sni string) (ok bool) {
695
676
// Called by 'tls' package when Client Hello is received
696
677
// If the server name (from SNI) supplied by client is incorrect - we terminate the ongoing TLS handshake.
697
678
func (s * Server ) onGetCertificate (ch * tls.ClientHelloInfo ) (* tls.Certificate , error ) {
698
- if s .conf .StrictSNICheck && ! anyNameMatches (s . conf .dnsNames , ch .ServerName ) {
679
+ if s .conf .TLSConf . StrictSNICheck && ! anyNameMatches (s .dnsNames , ch .ServerName ) {
699
680
log .Info ("dns: tls: unknown SNI in Client Hello: %s" , ch .ServerName )
700
681
return nil , fmt .Errorf ("invalid SNI" )
701
682
}
702
- return & s .conf .cert , nil
683
+ return s .conf .TLSConf . Cert , nil
703
684
}
704
685
705
686
// preparePlain prepares the plain-DNS configuration for the DNS proxy.
0 commit comments