v0.37.2
This commit is contained in:
1 parent
f48843e934
commit
e2c8099d9c
64 files changed
+5354
-673
No files matched your search
+134
-2
@@ -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
|
||||
|
||||
Reference in new issue
Block a user