This commit is contained in:
Mateusz Gruszczyński committed 2026-11-14 21:52:48 +01:00
1 parent f48843e934
commit e2c8099d9c
64 files changed
+5354 -673

No files matched your search

+1 -1
View File
@@ -102,7 +102,7 @@ Both installation types use the **same** config and certificate paths, but diffe
### Removing or upgrading the package
Upgrades do not reset credentials or private state. The environment file is installed with `config|noreplace`; local edits remain in place according to package-manager conffile behavior. Removal does not delete `/etc/rustproxy/docker-agent.env` or `/var/lib/rustproxy-docker-agent` (back these up separately).
Upgrades do not reset credentials or private state. Since 0.37.2 the systemd unit allows `AF_NETLINK` in `RestrictAddressFamilies` so the agent can list all host interfaces (before, only the routed, public and override addresses appeared). A unit copied by hand to `/etc/systemd/system/` is not replaced by an upgrade: copy the new `rustproxy-docker-agent.service`, run `systemctl daemon-reload` and restart the agent. Without netlink the agent reads the addresses from `/proc/net` instead. The environment file is installed with `config|noreplace`; local edits remain in place according to package-manager conffile behavior. Removal does not delete `/etc/rustproxy/docker-agent.env` or `/var/lib/rustproxy-docker-agent` (back these up separately).
## Container labels and state
+1 -1
View File
@@ -1,6 +1,6 @@
package main
var version = "0.37.1" // overridden by the package builder using -ldflags -X main.version
var version = "0.37.2" // overridden by the package builder using -ldflags -X main.version
type config struct {
Listen string
+134 -2
View File
@@ -1,10 +1,14 @@
package main
import (
"bufio"
"context"
"encoding/binary"
"encoding/hex"
"io"
"net"
"net/http"
"os"
"sort"
"strings"
"sync"
@@ -87,15 +91,122 @@ func bridgeHostAddresses() []hostAddress {
return interfaceAddresses(true)
}
func interfaceAddresses(bridges bool) []hostAddress {
interfaces, err := net.Interfaces()
type procAddress struct {
iface string
ip string
}
func procIPv6Addresses(path string) []procAddress {
file, err := os.Open(path)
if err != nil {
return nil
}
defer file.Close()
out := make([]procAddress, 0)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) < 6 || len(fields[0]) != 32 {
continue
}
raw, err := hex.DecodeString(fields[0])
if err != nil {
continue
}
out = append(out, procAddress{iface: fields[5], ip: net.IP(raw).String()})
}
return out
}
type procRoute struct {
iface string
network *net.IPNet
}
func procIPv4Routes(path string) []procRoute {
file, err := os.Open(path)
if err != nil {
return nil
}
defer file.Close()
out := make([]procRoute, 0)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) < 8 || fields[0] == "Iface" {
continue
}
destination, errDestination := hex.DecodeString(fields[1])
mask, errMask := hex.DecodeString(fields[7])
if errDestination != nil || errMask != nil || len(destination) != 4 || len(mask) != 4 {
continue
}
ip := make(net.IP, 4)
binary.BigEndian.PutUint32(ip, binary.LittleEndian.Uint32(destination))
ipMask := make(net.IPMask, 4)
binary.BigEndian.PutUint32(ipMask, binary.LittleEndian.Uint32(mask))
if ones, _ := ipMask.Size(); ones == 0 {
continue
}
out = append(out, procRoute{iface: fields[0], network: &net.IPNet{IP: ip, Mask: ipMask}})
}
return out
}
func procIPv4Local(path string) []string {
file, err := os.Open(path)
if err != nil {
return nil
}
defer file.Close()
out := make([]string, 0)
seen := make(map[string]bool)
last := ""
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "|--") {
last = strings.TrimSpace(strings.TrimPrefix(line, "|--"))
continue
}
if strings.Contains(line, "host LOCAL") && last != "" && !seen[last] {
seen[last] = true
out = append(out, last)
}
}
return out
}
func procAddresses(root string) []procAddress {
out := procIPv6Addresses(root + "/if_inet6")
routes := procIPv4Routes(root + "/route")
for _, raw := range procIPv4Local(root + "/fib_trie") {
ip := net.ParseIP(raw).To4()
if ip == nil || ip.IsLoopback() {
continue
}
best, bestOnes := "", -1
for _, route := range routes {
if ones, _ := route.network.Mask.Size(); route.network.Contains(ip) && ones > bestOnes {
best, bestOnes = route.iface, ones
}
}
if best != "" {
out = append(out, procAddress{iface: best, ip: ip.String()})
}
}
return out
}
func interfaceAddresses(bridges bool) []hostAddress {
source := "interface"
if bridges {
source = "bridge"
}
interfaces, err := net.Interfaces()
if err != nil {
return procInterfaceAddresses(procAddresses("/proc/net"), bridges, source)
}
out := make([]hostAddress, 0)
for _, iface := range interfaces {
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
@@ -132,6 +243,27 @@ func interfaceAddresses(bridges bool) []hostAddress {
return out
}
func procInterfaceAddresses(items []procAddress, bridges bool, source string) []hostAddress {
out := make([]hostAddress, 0)
for _, item := range items {
if item.iface == "lo" || bridges != bridgeInterface(item.iface) || (!bridges && skipInterface(item.iface)) {
continue
}
clean := validIP(item.ip)
if clean == "" {
continue
}
out = append(out, hostAddress{IP: clean, Source: source, Interface: item.iface, Scope: ipScope(net.ParseIP(clean))})
}
sort.Slice(out, func(i, j int) bool {
if out[i].Interface != out[j].Interface {
return out[i].Interface < out[j].Interface
}
return out[i].IP < out[j].IP
})
return out
}
type ipDetector struct {
mu sync.Mutex
publicIP string
+39
View File
@@ -0,0 +1,39 @@
package main
import (
"os"
"path/filepath"
"testing"
)
func TestProcAddressesWithoutNetlink(t *testing.T) {
dir := t.TempDir()
write := func(name, body string) {
if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0o600); err != nil {
t.Fatal(err)
}
}
write("route", "Iface\tDestination\tGateway \tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU\tWindow\tIRTT\n"+
"eth0\t00000000\t0102570A\t0003\t0\t0\t0\t00000000\t0\t0\t0\n"+
"eth0\t0002570A\t00000000\t0001\t0\t0\t0\t00FFFFFF\t0\t0\t0\n"+
"eth1\t0010A8C0\t00000000\t0001\t0\t0\t0\t00FFFFFF\t0\t0\t0\n"+
"docker0\t000011AC\t00000000\t0001\t0\t0\t0\t0000FFFF\t0\t0\t0\n")
write("fib_trie", "Main:\n +-- 0.0.0.0/0 3 0 5\n |-- 10.87.2.6\n /32 host LOCAL\n |-- 192.168.16.4\n /32 host LOCAL\n |-- 172.17.0.1\n /32 host LOCAL\n |-- 127.0.0.1\n /32 host LOCAL\nLocal:\n |-- 10.87.2.6\n /32 host LOCAL\n")
write("if_inet6", "20010db8000000000000000000000001 02 40 00 80 eth1\nfe800000000000000000000000000001 02 40 20 80 eth0\n00000000000000000000000000000001 01 80 10 80 lo\n")
items := procAddresses(dir)
host := procInterfaceAddresses(items, false, "interface")
got := map[string]string{}
for _, item := range host {
got[item.IP] = item.Interface
}
if got["10.87.2.6"] != "eth0" || got["192.168.16.4"] != "eth1" || got["2001:db8::1"] != "eth1" {
t.Fatalf("unexpected host addresses: %#v", host)
}
if _, ok := got["172.17.0.1"]; ok {
t.Fatalf("bridge address listed as host interface: %#v", host)
}
bridges := procInterfaceAddresses(items, true, "bridge")
if len(bridges) != 1 || bridges[0].IP != "172.17.0.1" || bridges[0].Interface != "docker0" {
t.Fatalf("unexpected bridge addresses: %#v", bridges)
}
}
+1 -1
View File
@@ -27,7 +27,7 @@ RestrictSUIDSGID=true
RestrictNamespaces=true
LockPersonality=true
MemoryDenyWriteExecute=true
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK
SystemCallArchitectures=native
[Install]