mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-08-10 13:20:33 +02:00
all: allowing config defined discover ptr endpoints
The default gateway is usually the DNS server in normal home network setup for most users. However, there's case that it is not, causing discover ptr failed. This commit add discover_ptr_endpoints config parameter, so users can define what DNS nameservers will be used.
This commit is contained in:
@@ -180,6 +180,7 @@ type ServiceConfig struct {
|
|||||||
DiscoverARP *bool `mapstructure:"discover_arp" toml:"discover_dhcp,omitempty"`
|
DiscoverARP *bool `mapstructure:"discover_arp" toml:"discover_dhcp,omitempty"`
|
||||||
DiscoverDHCP *bool `mapstructure:"discover_dhcp" toml:"discover_dhcp,omitempty"`
|
DiscoverDHCP *bool `mapstructure:"discover_dhcp" toml:"discover_dhcp,omitempty"`
|
||||||
DiscoverPtr *bool `mapstructure:"discover_ptr" toml:"discover_ptr,omitempty"`
|
DiscoverPtr *bool `mapstructure:"discover_ptr" toml:"discover_ptr,omitempty"`
|
||||||
|
DiscoverPtrEndpoints []string `mapstructure:"discover_ptr_endpoints" toml:"discover_ptr_endpoints,omitempty"`
|
||||||
DiscoverHosts *bool `mapstructure:"discover_hosts" toml:"discover_hosts,omitempty"`
|
DiscoverHosts *bool `mapstructure:"discover_hosts" toml:"discover_hosts,omitempty"`
|
||||||
Daemon bool `mapstructure:"-" toml:"-"`
|
Daemon bool `mapstructure:"-" toml:"-"`
|
||||||
AllocateIP bool `mapstructure:"-" toml:"-"`
|
AllocateIP bool `mapstructure:"-" toml:"-"`
|
||||||
|
|||||||
@@ -193,6 +193,22 @@ Perform LAN client discovery using PTR queries.
|
|||||||
- Required: no
|
- Required: no
|
||||||
- Default: true
|
- Default: true
|
||||||
|
|
||||||
|
### discover_ptr_endpoints
|
||||||
|
List of DNS nameservers used for PTR discovery.
|
||||||
|
|
||||||
|
Each entry can be either "ip" (default port 53) or "ip:port" pair. Invalid entry will be ignored.
|
||||||
|
|
||||||
|
- Type: array of string
|
||||||
|
- Required: no
|
||||||
|
- Default: []
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[service]
|
||||||
|
discover_ptr_endpoints = ["192.168.1.1", "192.168.2.1:5354"]
|
||||||
|
```
|
||||||
|
|
||||||
### discover_hosts
|
### discover_hosts
|
||||||
Perform LAN client discovery using hosts file.
|
Perform LAN client discovery using hosts file.
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ package clientinfo
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -183,6 +185,25 @@ func (t *Table) init() {
|
|||||||
// PTR lookup.
|
// PTR lookup.
|
||||||
if t.discoverPTR() {
|
if t.discoverPTR() {
|
||||||
t.ptr = &ptrDiscover{resolver: ctrld.NewPrivateResolver()}
|
t.ptr = &ptrDiscover{resolver: ctrld.NewPrivateResolver()}
|
||||||
|
if len(t.svcCfg.DiscoverPtrEndpoints) > 0 {
|
||||||
|
nss := make([]string, 0, len(t.svcCfg.DiscoverPtrEndpoints))
|
||||||
|
for _, ns := range t.svcCfg.DiscoverPtrEndpoints {
|
||||||
|
host, port := ns, "53"
|
||||||
|
if h, p, err := net.SplitHostPort(ns); err == nil {
|
||||||
|
host, port = h, p
|
||||||
|
}
|
||||||
|
// Only use valid ip:port pair.
|
||||||
|
if _, portErr := strconv.Atoi(port); portErr == nil && port != "0" && net.ParseIP(host) != nil {
|
||||||
|
nss = append(nss, net.JoinHostPort(host, port))
|
||||||
|
} else {
|
||||||
|
ctrld.ProxyLogger.Load().Warn().Msgf("ignoring invalid nameserver for ptr discover: %q", ns)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(nss) > 0 {
|
||||||
|
t.ptr.resolver = ctrld.NewResolverWithNameserver(nss)
|
||||||
|
ctrld.ProxyLogger.Load().Debug().Msgf("using nameservers %v for ptr discovery", nss)
|
||||||
|
}
|
||||||
|
}
|
||||||
ctrld.ProxyLogger.Load().Debug().Msg("start ptr discovery")
|
ctrld.ProxyLogger.Load().Debug().Msg("start ptr discovery")
|
||||||
if err := t.ptr.refresh(); err != nil {
|
if err := t.ptr.refresh(); err != nil {
|
||||||
ctrld.ProxyLogger.Load().Error().Err(err).Msg("could not init PTR discover")
|
ctrld.ProxyLogger.Load().Error().Err(err).Msg("could not init PTR discover")
|
||||||
|
|||||||
+14
-5
@@ -78,8 +78,9 @@ type osResolverResult struct {
|
|||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve performs DNS resolvers using OS default nameservers. Nameserver is chosen from
|
// Resolve resolves DNS queries using pre-configured nameservers.
|
||||||
// available nameservers with a roundrobin algorithm.
|
// Query is sent to all nameservers concurrently, and the first
|
||||||
|
// success response will be returned.
|
||||||
func (o *osResolver) Resolve(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) {
|
func (o *osResolver) Resolve(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) {
|
||||||
numServers := len(o.nameservers)
|
numServers := len(o.nameservers)
|
||||||
if numServers == 0 {
|
if numServers == 0 {
|
||||||
@@ -269,11 +270,19 @@ func NewPrivateResolver() Resolver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
nss = nss[:n]
|
nss = nss[:n]
|
||||||
if len(nss) == 0 {
|
return NewResolverWithNameserver(nss)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewResolverWithNameserver returns an OS resolver which uses the given nameservers
|
||||||
|
// for resolving DNS queries. If nameservers is empty, a dummy resolver will be returned.
|
||||||
|
//
|
||||||
|
// Each nameserver must be form "host:port". It's the caller responsibility to ensure all
|
||||||
|
// nameservers are well formatted by using net.JoinHostPort function.
|
||||||
|
func NewResolverWithNameserver(nameservers []string) Resolver {
|
||||||
|
if len(nameservers) == 0 {
|
||||||
return &dummyResolver{}
|
return &dummyResolver{}
|
||||||
}
|
}
|
||||||
resolver := &osResolver{nameservers: nss}
|
return &osResolver{nameservers: nameservers}
|
||||||
return resolver
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDialer(dnsAddress string) *net.Dialer {
|
func newDialer(dnsAddress string) *net.Dialer {
|
||||||
|
|||||||
Reference in New Issue
Block a user