|
| 1 | +package docker |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +const socketPath = "/var/run/docker.sock" |
| 14 | + |
| 15 | +// Neighbor is a container visible on a Docker bridge network. |
| 16 | +type Neighbor struct { |
| 17 | + IP string |
| 18 | + MAC string |
| 19 | +} |
| 20 | + |
| 21 | +func client() *http.Client { |
| 22 | + return &http.Client{ |
| 23 | + Timeout: 2 * time.Second, |
| 24 | + Transport: &http.Transport{ |
| 25 | + DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { |
| 26 | + return (&net.Dialer{}).DialContext(ctx, "unix", socketPath) |
| 27 | + }, |
| 28 | + }, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// Networks queries the Docker daemon and returns a map of kernel bridge interface |
| 33 | +// name (e.g. "br-09ff0748a767") to human-readable Docker network name |
| 34 | +// (e.g. "myproject_default"). Returns nil if the socket is unavailable. |
| 35 | +func Networks() map[string]string { |
| 36 | + resp, err := client().Get("http://localhost/networks") |
| 37 | + if err != nil { |
| 38 | + return nil |
| 39 | + } |
| 40 | + defer resp.Body.Close() |
| 41 | + |
| 42 | + var networks []struct { |
| 43 | + Name string `json:"Name"` |
| 44 | + ID string `json:"Id"` |
| 45 | + } |
| 46 | + if err := json.NewDecoder(resp.Body).Decode(&networks); err != nil { |
| 47 | + return nil |
| 48 | + } |
| 49 | + |
| 50 | + result := make(map[string]string, len(networks)) |
| 51 | + for _, n := range networks { |
| 52 | + if len(n.ID) >= 12 { |
| 53 | + result["br-"+n.ID[:12]] = n.Name |
| 54 | + } |
| 55 | + } |
| 56 | + return result |
| 57 | +} |
| 58 | + |
| 59 | +// IsBridgeIface returns true if the kernel interface name belongs to a Docker |
| 60 | +// network, using the map returned by Networks(). Falls back to name-prefix |
| 61 | +// heuristic (docker0, br-*) when the socket was unavailable and networks is nil. |
| 62 | +func IsBridgeIface(kernelName string, networks map[string]string) bool { |
| 63 | + if networks != nil { |
| 64 | + _, ok := networks[kernelName] |
| 65 | + return ok |
| 66 | + } |
| 67 | + return kernelName == "docker0" || strings.HasPrefix(kernelName, "br-") |
| 68 | +} |
| 69 | + |
| 70 | +// BridgeHasPeers returns true if the bridge has at least one veth peer attached, |
| 71 | +// meaning at least one container is connected. Uses sysfs rather than ARP cache |
| 72 | +// so idle-but-running containers are not incorrectly filtered out. |
| 73 | +func BridgeHasPeers(name string) bool { |
| 74 | + entries, err := os.ReadDir("/sys/class/net/" + name + "/brif") |
| 75 | + return err == nil && len(entries) > 0 |
| 76 | +} |
| 77 | + |
| 78 | +// ApplyNetworkNames renames any Docker bridge entries in ifaces and addrsByIface |
| 79 | +// to their human-readable Docker network names. Returns the set of display names |
| 80 | +// that correspond to Docker networks, for use in scan-time lookups. |
| 81 | +func ApplyNetworkNames(ifaces []net.Interface, addrsByIface map[string][]net.Addr, networks map[string]string) map[string]struct{} { |
| 82 | + dockerDisplayNames := make(map[string]struct{}, len(networks)) |
| 83 | + for i, iface := range ifaces { |
| 84 | + dockerName, ok := networks[iface.Name] |
| 85 | + if !ok { |
| 86 | + // No rename — track under the kernel name if it's a bridge. |
| 87 | + if strings.HasPrefix(iface.Name, "br-") || iface.Name == "docker0" { |
| 88 | + dockerDisplayNames[iface.Name] = struct{}{} |
| 89 | + } |
| 90 | + continue |
| 91 | + } |
| 92 | + addrs := addrsByIface[iface.Name] |
| 93 | + delete(addrsByIface, iface.Name) |
| 94 | + addrsByIface[dockerName] = addrs |
| 95 | + ifaces[i].Name = dockerName |
| 96 | + dockerDisplayNames[dockerName] = struct{}{} |
| 97 | + } |
| 98 | + return dockerDisplayNames |
| 99 | +} |
| 100 | + |
| 101 | +// ContainerNeighbors queries the Docker daemon and returns the IP and MAC of |
| 102 | +// every running container attached to networkName that falls within subnet. |
| 103 | +// Returns nil if the socket is unavailable. |
| 104 | +func ContainerNeighbors(networkName string, subnet *net.IPNet) []Neighbor { |
| 105 | + resp, err := client().Get("http://localhost/containers/json") |
| 106 | + if err != nil { |
| 107 | + return nil |
| 108 | + } |
| 109 | + defer resp.Body.Close() |
| 110 | + |
| 111 | + var containers []struct { |
| 112 | + NetworkSettings struct { |
| 113 | + Networks map[string]struct { |
| 114 | + IPAddress string `json:"IPAddress"` |
| 115 | + MacAddress string `json:"MacAddress"` |
| 116 | + } `json:"Networks"` |
| 117 | + } `json:"NetworkSettings"` |
| 118 | + } |
| 119 | + if err := json.NewDecoder(resp.Body).Decode(&containers); err != nil { |
| 120 | + return nil |
| 121 | + } |
| 122 | + |
| 123 | + var out []Neighbor |
| 124 | + for _, c := range containers { |
| 125 | + for name, ns := range c.NetworkSettings.Networks { |
| 126 | + if name != networkName { |
| 127 | + continue |
| 128 | + } |
| 129 | + ip := net.ParseIP(ns.IPAddress) |
| 130 | + if ip == nil || !subnet.Contains(ip) { |
| 131 | + continue |
| 132 | + } |
| 133 | + out = append(out, Neighbor{IP: ns.IPAddress, MAC: ns.MacAddress}) |
| 134 | + } |
| 135 | + } |
| 136 | + return out |
| 137 | +} |
| 138 | + |
| 139 | +// ClampToCIDR returns addrs with any IPv4 prefix longer than bits clamped to bits. |
| 140 | +func ClampToCIDR(addrs []net.Addr, bits int) []net.Addr { |
| 141 | + out := make([]net.Addr, 0, len(addrs)) |
| 142 | + for _, addr := range addrs { |
| 143 | + ipnet, ok := addr.(*net.IPNet) |
| 144 | + if !ok { |
| 145 | + out = append(out, addr) |
| 146 | + continue |
| 147 | + } |
| 148 | + ones, total := ipnet.Mask.Size() |
| 149 | + if total == 32 && ones > bits { |
| 150 | + out = append(out, &net.IPNet{ |
| 151 | + IP: ipnet.IP, |
| 152 | + Mask: net.CIDRMask(bits, 32), |
| 153 | + }) |
| 154 | + } else { |
| 155 | + out = append(out, addr) |
| 156 | + } |
| 157 | + } |
| 158 | + return out |
| 159 | +} |
0 commit comments