mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-08-10 13:20:33 +02:00
+12
-6
@@ -593,18 +593,24 @@ func processNoConfigFlags(noConfigStart bool) {
|
|||||||
}
|
}
|
||||||
processListenFlag()
|
processListenFlag()
|
||||||
|
|
||||||
|
endpointAndTyp := func(endpoint string) (string, string) {
|
||||||
|
typ := ctrld.ResolverTypeFromEndpoint(endpoint)
|
||||||
|
return strings.TrimPrefix(endpoint, "quic://"), typ
|
||||||
|
}
|
||||||
|
pEndpoint, pType := endpointAndTyp(primaryUpstream)
|
||||||
upstream := map[string]*ctrld.UpstreamConfig{
|
upstream := map[string]*ctrld.UpstreamConfig{
|
||||||
"0": {
|
"0": {
|
||||||
Name: primaryUpstream,
|
Name: pEndpoint,
|
||||||
Endpoint: primaryUpstream,
|
Endpoint: pEndpoint,
|
||||||
Type: ctrld.ResolverTypeDOH,
|
Type: pType,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if secondaryUpstream != "" {
|
if secondaryUpstream != "" {
|
||||||
|
sEndpoint, sType := endpointAndTyp(secondaryUpstream)
|
||||||
upstream["1"] = &ctrld.UpstreamConfig{
|
upstream["1"] = &ctrld.UpstreamConfig{
|
||||||
Name: secondaryUpstream,
|
Name: sEndpoint,
|
||||||
Endpoint: secondaryUpstream,
|
Endpoint: sEndpoint,
|
||||||
Type: ctrld.ResolverTypeLegacy,
|
Type: sType,
|
||||||
}
|
}
|
||||||
rules := make([]ctrld.Rule, 0, len(domains))
|
rules := make([]ctrld.Rule, 0, len(domains))
|
||||||
for _, domain := range domains {
|
for _, domain := range domains {
|
||||||
|
|||||||
@@ -380,3 +380,27 @@ func availableNameservers() []string {
|
|||||||
}
|
}
|
||||||
return nss[:n]
|
return nss[:n]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ResolverTypeFromEndpoint tries guessing the resolver type with a given endpoint
|
||||||
|
// using following rules:
|
||||||
|
//
|
||||||
|
// - If endpoint is an IP address -> ResolverTypeLegacy
|
||||||
|
// - If endpoint starts with "https://" -> ResolverTypeDOH
|
||||||
|
// - If endpoint starts with "quic://" -> ResolverTypeDOQ
|
||||||
|
// - For anything else -> ResolverTypeDOT
|
||||||
|
func ResolverTypeFromEndpoint(endpoint string) string {
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(endpoint, "https://"):
|
||||||
|
return ResolverTypeDOH
|
||||||
|
case strings.HasPrefix(endpoint, "quic://"):
|
||||||
|
return ResolverTypeDOQ
|
||||||
|
}
|
||||||
|
host := endpoint
|
||||||
|
if strings.Contains(endpoint, ":") {
|
||||||
|
host, _, _ = net.SplitHostPort(host)
|
||||||
|
}
|
||||||
|
if ip := net.ParseIP(host); ip != nil {
|
||||||
|
return ResolverTypeLegacy
|
||||||
|
}
|
||||||
|
return ResolverTypeDOT
|
||||||
|
}
|
||||||
|
|||||||
@@ -27,3 +27,27 @@ func Test_osResolver_Resolve(t *testing.T) {
|
|||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_upstreamTypeFromEndpoint(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
endpoint string
|
||||||
|
resolverType string
|
||||||
|
}{
|
||||||
|
{"doh", "https://freedns.controld.com/p2", ResolverTypeDOH},
|
||||||
|
{"doq", "quic://p2.freedns.controld.com", ResolverTypeDOQ},
|
||||||
|
{"dot", "p2.freedns.controld.com", ResolverTypeDOT},
|
||||||
|
{"legacy", "8.8.8.8:53", ResolverTypeLegacy},
|
||||||
|
{"legacy ipv6", "[2404:6800:4005:809::200e]:53", ResolverTypeLegacy},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
tc := tc
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
if rt := ResolverTypeFromEndpoint(tc.endpoint); rt != tc.resolverType {
|
||||||
|
t.Errorf("mismatch, want: %s, got: %s", tc.resolverType, rt)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user