systemd-resolved: let a network's own servers answer while the mesh's resolvers are silent (issue 1981)
Behind a guest network's sign-in page the private network is out of reach, and the guard sent every name to the mesh's resolvers, so the sign-in page itself could not be found. The guard now asks the mesh's resolvers each round and gives the links it turned off their default route back while none answers; once one answers they lose it again.
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/netip"
|
||||
"os"
|
||||
"os/exec"
|
||||
@@ -85,11 +86,35 @@ type Resolver struct {
|
||||
NetDir string
|
||||
// SuffixFile holds the mesh's own domain, which is never routed elsewhere.
|
||||
SuffixFile string
|
||||
// Answers says whether one of the mesh's resolvers answers a name now.
|
||||
Answers func(ctx context.Context, server string) bool
|
||||
}
|
||||
|
||||
// ThisResolver is the machine's.
|
||||
func ThisResolver() *Resolver {
|
||||
return &Resolver{Change: execRunner, Read: readRunner, NetDir: "/sys/class/net", SuffixFile: SuffixPath}
|
||||
r := &Resolver{Change: execRunner, Read: readRunner, NetDir: "/sys/class/net", SuffixFile: SuffixPath}
|
||||
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
|
||||
|
||||
// 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)
|
||||
defer cancel()
|
||||
ask := &net.Resolver{PreferGo: true, Dial: func(ctx context.Context, network, _ string) (net.Conn, error) {
|
||||
var d net.Dialer
|
||||
if _, _, err := net.SplitHostPort(server); err == nil {
|
||||
return d.DialContext(ctx, network, server)
|
||||
}
|
||||
return d.DialContext(ctx, network, net.JoinHostPort(server, "53"))
|
||||
}}
|
||||
_, err := ask.LookupHost(ctx, "node-resolver-asks."+r.suffix())
|
||||
var dnsErr *net.DNSError
|
||||
return err == nil || (errors.As(err, &dnsErr) && dnsErr.IsNotFound)
|
||||
}
|
||||
|
||||
// SuffixPath is the file the mesh renders the mesh's own domain into (the manifest's fact `suffix`).
|
||||
@@ -376,3 +401,35 @@ func (r *Resolver) OnlyTheMeshIsADefaultRoute(ctx context.Context) ([]string, er
|
||||
}
|
||||
return changed, nil
|
||||
}
|
||||
|
||||
// TheMeshAnswers says whether one of the mesh's resolvers answers now, asking them all at once. Behind a
|
||||
// network's sign-in page none does: the private network is out of reach until the person signs in.
|
||||
func (r *Resolver) TheMeshAnswers(ctx context.Context, servers []string) bool {
|
||||
answered := make(chan bool, len(servers))
|
||||
for _, s := range servers {
|
||||
go func() { answered <- r.Answers(ctx, s) }()
|
||||
}
|
||||
for range servers {
|
||||
if <-answered {
|
||||
return true
|
||||
}
|
||||
}
|
||||
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) {
|
||||
var changed []string
|
||||
for _, l := range routes.Links {
|
||||
if !links[l.Link] || (l.DefaultRoute != nil && *l.DefaultRoute) {
|
||||
continue
|
||||
}
|
||||
if _, err := r.Change(ctx, "resolvectl", "default-route", l.Link, "true"); err != nil {
|
||||
return changed, err
|
||||
}
|
||||
changed = append(changed, l.Link)
|
||||
}
|
||||
return changed, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user