Skip to content

Commit

Permalink
main: upstream mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Jul 2, 2024
1 parent afd8043 commit d8bd87d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ it can work as a `DNS-over-HTTPS`, `DNS-over-TLS` or `DNS-over-QUIC` server.
- [Encrypted DNS server](#encrypted-dns-server)
- [Additional features](#additional-features)
- [DNS64 server](#dns64-server)
- [Fastest addr + cache-min-ttl](#fastest-addr--cache-min-ttl)
- [Fastest addr + cache-min-ttl](#fastest-addr-upstream-mode--cache-min-ttl)
- [Specifying upstreams for domains](#specifying-upstreams-for-domains)
- [EDNS Client Subnet](#edns-client-subnet)
- [Bogus NXDomain](#bogus-nxdomain)
Expand Down Expand Up @@ -86,8 +86,7 @@ Application Options:
--insecure Disable secure TLS certificate validation
--ipv6-disabled If specified, all AAAA requests will be replied with NoError RCode and empty answer
--http3 Enable HTTP/3 support
--all-servers If specified, parallel queries to all configured upstream servers are enabled
--fastest-addr Respond to A or AAAA requests only with the fastest IP address
--upstream-mode If specified, determines the upstream usage logic.
--cache-optimistic If specified, optimistic DNS cache is enabled
--cache If specified, DNS cache is enabled
--refuse-any If specified, refuse ANY requests
Expand Down Expand Up @@ -264,20 +263,22 @@ specified and enabled if DNS64 is enabled.
[wkp]: https://datatracker.ietf.org/doc/html/rfc6052#section-2.1
### Fastest addr + cache-min-ttl
### Fastest addr upstream mode + cache-min-ttl
This option would be useful to the users with problematic network connection.
In this mode, `dnsproxy` would detect the fastest IP address among all that were returned,
and it will return only it.
In this mode, `dnsproxy` would detect the fastest IP address among all that were
returned, and it will return only it.
Additionally, for those with problematic network connection, it makes sense to override `cache-min-ttl`.
In this case, `dnsproxy` will make sure that DNS responses are cached for at least the specified amount of time.
Additionally, for those with problematic network connection, it makes sense to
override `cache-min-ttl`. In this case, `dnsproxy` will make sure that DNS
responses are cached for at least the specified amount of time.
It makes sense to run it with multiple upstream servers only.
Run a DNS proxy with two upstreams, min-TTL set to 10 minutes, fastest address detection is enabled:
Run a DNS proxy with two upstreams, min-TTL set to 10 minutes, fastest address
detection is enabled:
```
./dnsproxy -u 8.8.8.8 -u 1.1.1.1 --cache --cache-min-ttl=600 --fastest-addr
./dnsproxy -u 8.8.8.8 -u 1.1.1.1 --cache --cache-min-ttl=600 --upstream-mode=fastest-addr
```
who run `dnsproxy` with multiple upstreams
Expand Down
22 changes: 7 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ type Options struct {
// EDNSAddr is the custom EDNS Client Address to send.
EDNSAddr string `yaml:"edns-addr" long:"edns-addr" description:"Send EDNS Client Address"`

// UpstreamMode determines the logic through which upstreams will be used.
UpstreamMode string `yaml:"upstream-mode" long:"upstream-mode" description:"" optional:"yes" optional-value:"load_balance"`

// ListenAddrs is the list of server's listen addresses.
ListenAddrs []string `yaml:"listen-addrs" short:"l" long:"listen" description:"Listening addresses"`

Expand Down Expand Up @@ -174,15 +177,6 @@ type Options struct {
// It enables HTTP/3 support for both the DoH upstreams and the DoH server.
HTTP3 bool `yaml:"http3" long:"http3" description:"Enable HTTP/3 support" optional:"yes" optional-value:"false"`

// AllServers makes server to query all configured upstream servers in
// parallel.
AllServers bool `yaml:"all-servers" long:"all-servers" description:"If specified, parallel queries to all configured upstream servers are enabled" optional:"yes" optional-value:"true"`

// FastestAddress controls whether the server should respond to A or AAAA
// requests only with the fastest IP address detected by ICMP response time
// or TCP connection time.
FastestAddress bool `yaml:"fastest-addr" long:"fastest-addr" description:"Respond to A or AAAA requests only with the fastest IP address" optional:"yes" optional-value:"true"`

// CacheOptimistic, if set to true, enables the optimistic DNS cache. That
// means that cached results will be served even if their cache TTL has
// already expired.
Expand Down Expand Up @@ -555,14 +549,12 @@ func (opts *Options) initUpstreams(
config.Fallbacks = fallbacks
}

if opts.AllServers {
config.UpstreamMode = proxy.UpstreamModeParallel
} else if opts.FastestAddress {
config.UpstreamMode = proxy.UpstreamModeFastestAddr
} else {
config.UpstreamMode = proxy.UpstreamModeLoadBalance
if opts.UpstreamMode != "" {
return config.UpstreamMode.UnmarshalText([]byte(opts.UpstreamMode))
}

config.UpstreamMode = proxy.UpstreamModeLoadBalance

return nil
}

Expand Down
11 changes: 10 additions & 1 deletion proxy/upstreammode.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@ import (
type UpstreamMode string

const (
// UpstreamModeLoadBalance is the default upstream mode. It balances the
// upstreams load.
UpstreamModeLoadBalance UpstreamMode = "load_balance"
UpstreamModeParallel UpstreamMode = "parallel"

// UpstreamModeParallel makes server to query all configured upstream
// servers in parallel.
UpstreamModeParallel UpstreamMode = "parallel"

// UpstreamModeFastestAddr controls whether the server should respond to A
// or AAAA requests only with the fastest IP address detected by ICMP
// response time or TCP connection time.
UpstreamModeFastestAddr UpstreamMode = "fastest_addr"
)

Expand Down

0 comments on commit d8bd87d

Please sign in to comment.