WIP
This commit is contained in:
@@ -53,7 +53,7 @@ func (a *App) devAddDirect(p *Peer, endpoint netip.AddrPort) {
|
||||
func (a *App) devSetRelay(p *Peer, endpoint netip.AddrPort) {
|
||||
log.Printf("RELAY: %s - %s @ %s", p.Name, p.VPNIP.String(), endpoint.String())
|
||||
devRetry(p.VPNIP, "SetRelay", func() error { return a.dev.SetRelay(p.PubKey(), endpoint, a.vpnNet) })
|
||||
p.State = StateDirect
|
||||
p.State = StateDirect // Dirrect connection. The app marks peer as relay.
|
||||
}
|
||||
|
||||
func (a *App) devPromote(p *Peer) {
|
||||
|
||||
@@ -14,10 +14,18 @@ import (
|
||||
|
||||
const (
|
||||
hostsFile = "/etc/hosts"
|
||||
hostsBegin = "# BEGIN vppn "
|
||||
hostsEnd = "# END vppn "
|
||||
hostsBegin = "# BEGIN vppn"
|
||||
hostsEnd = "# END vppn"
|
||||
)
|
||||
|
||||
// hostMarkers returns the begin/end marker lines that delimit the managed
|
||||
// section for localDomain. The domain is wrapped in parentheses so one domain's
|
||||
// marker can never be a prefix of another's (e.g. "net" vs "net2") when
|
||||
// multiple vppn instances share /etc/hosts.
|
||||
func hostMarkers(localDomain string) (begin, end string) {
|
||||
return hostsBegin + "(" + localDomain + ")", hostsEnd + "(" + localDomain + ")"
|
||||
}
|
||||
|
||||
// updateHosts rewrites the managed vppn section in /etc/hosts using the
|
||||
// current peersByIP map. Peers without a Name are skipped.
|
||||
func (a *App) updateHosts() {
|
||||
@@ -36,8 +44,7 @@ func updateHosts(hostsPath, localDomain string, peers map[netip.Addr]*Peer) erro
|
||||
}
|
||||
defer lockFile.Close()
|
||||
|
||||
begin := hostsBegin + localDomain
|
||||
end := hostsEnd + localDomain
|
||||
begin, end := hostMarkers(localDomain)
|
||||
|
||||
info, err := os.Stat(hostsPath)
|
||||
if err != nil {
|
||||
|
||||
@@ -28,8 +28,7 @@ func readManagedSection(t *testing.T, path, localDomain string) (inside, outside
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
begin := hostsBegin + localDomain
|
||||
end := hostsEnd + localDomain
|
||||
begin, end := hostMarkers(localDomain)
|
||||
|
||||
inSection := false
|
||||
for _, line := range strings.Split(string(raw), "\n") {
|
||||
@@ -157,6 +156,45 @@ func TestUpdateHosts_Idempotent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestUpdateHosts_PrefixDomainsCoexist guards finding 4.4: two domains where
|
||||
// one label is a prefix of the other ("net" vs "net2") must each manage their
|
||||
// own section without clobbering the other's, even sharing one hosts file.
|
||||
func TestUpdateHosts_PrefixDomainsCoexist(t *testing.T) {
|
||||
path := writeTempHosts(t, "127.0.0.1 localhost\n")
|
||||
|
||||
if err := updateHosts(path, "net2.local", map[netip.Addr]*Peer{
|
||||
netip.MustParseAddr("10.0.2.1"): peer("a"),
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := updateHosts(path, "net.local", map[netip.Addr]*Peer{
|
||||
netip.MustParseAddr("10.0.1.1"): peer("b"),
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Both sections coexist after writing the prefix domain.
|
||||
if in, _ := readManagedSection(t, path, "net2.local"); len(in) != 1 || in[0] != "10.0.2.1 a.net2.local" {
|
||||
t.Errorf("net2 section clobbered: %v", in)
|
||||
}
|
||||
if in, _ := readManagedSection(t, path, "net.local"); len(in) != 1 || in[0] != "10.0.1.1 b.net.local" {
|
||||
t.Errorf("net section wrong: %v", in)
|
||||
}
|
||||
|
||||
// Re-updating net2 must not disturb the net section.
|
||||
if err := updateHosts(path, "net2.local", map[netip.Addr]*Peer{
|
||||
netip.MustParseAddr("10.0.2.2"): peer("c"),
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if in, _ := readManagedSection(t, path, "net.local"); len(in) != 1 || in[0] != "10.0.1.1 b.net.local" {
|
||||
t.Errorf("net section disturbed by net2 update: %v", in)
|
||||
}
|
||||
if in, _ := readManagedSection(t, path, "net2.local"); len(in) != 1 || in[0] != "10.0.2.2 c.net2.local" {
|
||||
t.Errorf("net2 section not updated: %v", in)
|
||||
}
|
||||
}
|
||||
|
||||
func contains(ss []string, s string) bool {
|
||||
for _, x := range ss {
|
||||
if x == s {
|
||||
|
||||
Reference in New Issue
Block a user