This commit is contained in:
root 2025-06-25 14:22:34 -04:00
parent 853c0e6357
commit 86cbb31a2f
2 changed files with 39 additions and 45 deletions

BIN
main

Binary file not shown.

34
main.go
View File

@ -4,9 +4,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net" "net"
//"os"
"path/filepath" "path/filepath"
//"strings"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
@ -22,14 +20,15 @@ func main() {
addrStyle := lipgloss.NewStyle().Faint(true) addrStyle := lipgloss.NewStyle().Faint(true)
infoStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("33")) infoStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("33"))
// Save the current netns // Save the current namespace
origNS, err := netns.Get() origNS, err := netns.Get()
if err != nil { if err != nil {
fmt.Println("Error getting current netns:", err) fmt.Println("Error getting current namespace:", err)
return return
} }
defer origNS.Close() defer origNS.Close()
// Get all links in the current namespace
links, err := netlink.LinkList() links, err := netlink.LinkList()
if err != nil { if err != nil {
fmt.Println("Error listing links:", err) fmt.Println("Error listing links:", err)
@ -52,7 +51,7 @@ func main() {
fmt.Printf(addrStyle.Render(" %d"), attrs.MTU) fmt.Printf(addrStyle.Render(" %d"), attrs.MTU)
fmt.Printf(" %s\n", state) fmt.Printf(" %s\n", state)
// IP addresses (IPv4 only) // IPv4 only
addrs, err := netlink.AddrList(link, unix.AF_INET) addrs, err := netlink.AddrList(link, unix.AF_INET)
if err == nil { if err == nil {
for _, addr := range addrs { for _, addr := range addrs {
@ -67,21 +66,7 @@ func main() {
fmt.Println(" ", infoStyle.Render("Veth interface")) fmt.Println(" ", infoStyle.Render("Veth interface"))
// Try to find peer locally
peerFound := false peerFound := false
for _, other := range links {
if other.Attrs().Index == attrs.Index {
continue
}
if other.Type() == "veth" && other.Attrs().ParentIndex == attrs.Index {
fmt.Println(" ", infoStyle.Render("Likely peer:"), other.Attrs().Name)
peerFound = true
break
}
}
// If not found, scan /var/run/netns/
if !peerFound {
netnsDir := "/var/run/netns/" netnsDir := "/var/run/netns/"
entries, err := ioutil.ReadDir(netnsDir) entries, err := ioutil.ReadDir(netnsDir)
if err != nil { if err != nil {
@ -105,11 +90,21 @@ func main() {
for _, rl := range remoteLinks { for _, rl := range remoteLinks {
if rl.Type() == "veth" && rl.Attrs().ParentIndex == attrs.Index { if rl.Type() == "veth" && rl.Attrs().ParentIndex == attrs.Index {
fmt.Println(" ", infoStyle.Render("Peer in namespace:"), entry.Name(), "as", rl.Attrs().Name) fmt.Println(" ", infoStyle.Render("Peer in namespace:"), entry.Name(), "as", rl.Attrs().Name)
// Get peer IPs inside this namespace
peerAddrs, err := netlink.AddrList(rl, unix.AF_INET)
if err == nil {
for _, pa := range peerAddrs {
fmt.Println(" ", ipStyle.Render(pa.IPNet.String()))
}
}
peerFound = true peerFound = true
break break
} }
} }
} }
nsHandle.Close() nsHandle.Close()
netns.Set(origNS) netns.Set(origNS)
@ -118,7 +113,6 @@ func main() {
} }
} }
} }
}
if !peerFound { if !peerFound {
fmt.Println(" ", infoStyle.Render("Peer not found — may be deleted or hidden")) fmt.Println(" ", infoStyle.Render("Peer not found — may be deleted or hidden"))