systemd-resolved: find the links to give back from resolved, never a routed one; answer-within is a config value (issue 1981 review)
The guard kept the links it had turned off in memory, so a VPN link turned off before it was routed got its default route back, and a guard restarted behind a sign-in page gave nothing back. It now reads them from resolved each round and skips a link whose domains are all routing domains. How long a mesh resolver is given to answer is the config value answer-within.
This commit is contained in:
@@ -23,6 +23,7 @@ import (
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -88,22 +89,45 @@ type Resolver struct {
|
||||
SuffixFile string
|
||||
// Answers says whether one of the mesh's resolvers answers a name now.
|
||||
Answers func(ctx context.Context, server string) bool
|
||||
// AnswerWithinFile holds how long one of them is given to answer (the config value answer-within).
|
||||
AnswerWithinFile string
|
||||
}
|
||||
|
||||
// ThisResolver is the machine's.
|
||||
func ThisResolver() *Resolver {
|
||||
r := &Resolver{Change: execRunner, Read: readRunner, NetDir: "/sys/class/net", SuffixFile: SuffixPath}
|
||||
r := &Resolver{Change: execRunner, Read: readRunner, NetDir: "/sys/class/net", SuffixFile: SuffixPath,
|
||||
AnswerWithinFile: AnswerWithinPath}
|
||||
r.Answers = r.asks
|
||||
return r
|
||||
}
|
||||
|
||||
// AnswerWithin is how long one of the mesh's resolvers is given to answer the guard's question.
|
||||
var AnswerWithin = time.Second
|
||||
// AnswerWithinPath is the file the mesh renders the config value answer-within into, as `seconds=<n>`.
|
||||
const AnswerWithinPath = "/etc/node-resolver/answer-within"
|
||||
|
||||
// DefaultAnswerWithin is the config value's default, used until the mesh has rendered the file.
|
||||
const DefaultAnswerWithin = time.Second
|
||||
|
||||
// answerWithin is how long one of the mesh's resolvers is given to answer the guard's question, read each
|
||||
// round, so a changed config value holds from the next round on.
|
||||
func (r *Resolver) answerWithin() time.Duration {
|
||||
raw, err := os.ReadFile(r.AnswerWithinFile)
|
||||
if err != nil {
|
||||
return DefaultAnswerWithin
|
||||
}
|
||||
for _, line := range strings.Split(string(raw), "\n") {
|
||||
if v, ok := strings.CutPrefix(strings.TrimSpace(line), "seconds="); ok {
|
||||
if n, err := strconv.ParseFloat(v, 64); err == nil && n > 0 {
|
||||
return time.Duration(n * float64(time.Second))
|
||||
}
|
||||
}
|
||||
}
|
||||
return DefaultAnswerWithin
|
||||
}
|
||||
|
||||
// asks one of the mesh's resolvers a name under the mesh's own domain, straight over the network and not
|
||||
// through resolved: any answer, a name not found included, is an answer; only silence is not.
|
||||
func (r *Resolver) asks(ctx context.Context, server string) bool {
|
||||
ctx, cancel := context.WithTimeout(ctx, AnswerWithin)
|
||||
ctx, cancel := context.WithTimeout(ctx, r.answerWithin())
|
||||
defer cancel()
|
||||
ask := &net.Resolver{PreferGo: true, Dial: func(ctx context.Context, network, _ string) (net.Conn, error) {
|
||||
var d net.Dialer
|
||||
@@ -128,6 +152,9 @@ type Scope struct {
|
||||
Domains []string `json:"domains"`
|
||||
// DefaultRoute is whether names no domain routes may also go here. Only the mesh's resolvers are.
|
||||
DefaultRoute *bool `json:"default_route,omitempty"`
|
||||
|
||||
// routed is a link given its domains by `route`: every one a routing domain, as Route writes them.
|
||||
routed bool
|
||||
}
|
||||
|
||||
// Routes is what resolved sends where.
|
||||
@@ -157,6 +184,17 @@ func perScope(out string) (global []string, links map[string][]string) {
|
||||
return global, links
|
||||
}
|
||||
|
||||
// onlyRouting is whether a link's domains are all routing domains, as `route` gives them; a network manager
|
||||
// gives a network's search domains, without the `~`.
|
||||
func onlyRouting(domains []string) bool {
|
||||
for _, d := range domains {
|
||||
if !strings.HasPrefix(d, "~") {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return len(domains) > 0
|
||||
}
|
||||
|
||||
func unrouted(domains []string) []string {
|
||||
out := make([]string, 0, len(domains))
|
||||
for _, d := range domains {
|
||||
@@ -188,7 +226,7 @@ func (r *Resolver) Routes(ctx context.Context) (*Routes, error) {
|
||||
}
|
||||
sort.Strings(names)
|
||||
for _, name := range names {
|
||||
s := Scope{Link: name, Servers: lServers[name], Domains: orEmpty(unrouted(lDomains[name]))}
|
||||
s := Scope{Link: name, Servers: lServers[name], Domains: orEmpty(unrouted(lDomains[name])), routed: onlyRouting(lDomains[name])}
|
||||
if d, ok := lDefault[name]; ok && len(d) > 0 {
|
||||
yes := d[0] == "yes"
|
||||
s.DefaultRoute = &yes
|
||||
@@ -417,13 +455,15 @@ func (r *Resolver) TheMeshAnswers(ctx context.Context, servers []string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// ADefaultRouteAgain lets the links named answer every name again, while the mesh's resolvers are silent:
|
||||
// a network's own servers are then the only ones that can find its sign-in page. A link that went, or
|
||||
// already answers every name, is left alone. Answers the links it changed.
|
||||
func (r *Resolver) ADefaultRouteAgain(ctx context.Context, routes *Routes, links map[string]bool) ([]string, error) {
|
||||
// ADefaultRouteAgain lets every link a network manager gave servers answer every name again, while the
|
||||
// mesh's resolvers are silent: a network's own servers are then the only ones that can find its sign-in
|
||||
// page. Read from what resolved holds, not from what the guard did, so it holds after the guard restarts.
|
||||
// A link `route` routed (a VPN's) is never one, and one that already answers every name is left alone.
|
||||
// Answers the links it changed.
|
||||
func (r *Resolver) ADefaultRouteAgain(ctx context.Context, routes *Routes) ([]string, error) {
|
||||
var changed []string
|
||||
for _, l := range routes.Links {
|
||||
if !links[l.Link] || (l.DefaultRoute != nil && *l.DefaultRoute) {
|
||||
if l.routed || l.DefaultRoute == nil || *l.DefaultRoute {
|
||||
continue
|
||||
}
|
||||
if _, err := r.Change(ctx, "resolvectl", "default-route", l.Link, "true"); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user