Skip to content

Commit

Permalink
Merge pull request #10 from cego/check-same-4and6-ip
Browse files Browse the repository at this point in the history
remoteIP: check if ipv4 == ipv6
  • Loading branch information
FnuGk authored Sep 11, 2023
2 parents 40837c2 + d293deb commit e04d3fb
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,18 @@ func root(_ *cobra.Command, _ []string) {
log.Fatalf("Error: %s", err.Error())
}

if len(connections4) == len(connections6) && len(connections4) == 1 {
ip4 := connections4[0]
ip6 := connections6[0]

if !ip4.Equal(ip6) {
log.Fatalf("Unable to guess remote IP, found unmatching ipv4 and ipv6 addr %s != %s", ip4.String(), ip6.String())
}

// Just keep the ipv4 if we get the same ip in both forms
connections6 = nil
}

connections := append(connections4, connections6...)

if len(connections) != 1 {
Expand Down Expand Up @@ -299,27 +311,27 @@ func getOpenSockets(pid int) ([]int, error) {
return sockets, nil
}

func hexToIP(hexStr string) (*net.IPAddr, error) {
func hexToIP(hexStr string) (net.IP, error) {
data, err := hex.DecodeString(hexStr)
if err != nil {
return nil, err
}
ip := net.IP{data[3], data[2], data[1], data[0]}

return &net.IPAddr{IP: ip}, nil
return ip, nil
}

func hexToIP6(hexStr string) (*net.IPAddr, error) {
func hexToIP6(hexStr string) (net.IP, error) {
data, err := hex.DecodeString(hexStr)
if err != nil {
return nil, err
}
ip := net.IP{data[3], data[2], data[1], data[0], data[7], data[6], data[5], data[4], data[11], data[10], data[9], data[8], data[15], data[14], data[13], data[12]}

return &net.IPAddr{IP: ip}, nil
return ip, nil
}

func getTCP4Connections(sockets []int) ([]net.Addr, error) {
func getTCP4Connections(sockets []int) ([]net.IP, error) {
t := regexp.MustCompile(`\w[0-9]*\:\ [0-9A-F]{8}:[0-9A-F]{4} ([0-9A-F]{8}):[0-9A-F]{4} [0-9A-F]{2} [0-9A-F]{8}:[0-9A-F]{8} [0-9A-F]{2}:[0-9A-F]{8} [0-9A-F]{8} *[0-9]* *[0-9] ([0-9]*)`)

f, err := os.Open("/proc/net/tcp")
Expand All @@ -333,7 +345,7 @@ func getTCP4Connections(sockets []int) ([]net.Addr, error) {
// Throw away the first line containing headers.
scanner.Scan()

var connections []net.Addr
var connections []net.IP

for scanner.Scan() {
line := scanner.Bytes()
Expand Down Expand Up @@ -365,7 +377,7 @@ func getTCP4Connections(sockets []int) ([]net.Addr, error) {
return connections, nil
}

func getTCP6Connections(sockets []int) ([]net.Addr, error) {
func getTCP6Connections(sockets []int) ([]net.IP, error) {
t := regexp.MustCompile(`\w[0-9]*\:\ [0-9A-F]{32}:[0-9A-F]{4} ([0-9A-F]{32}):[0-9A-F]{4} [0-9A-F]{2} [0-9A-F]{8}:[0-9A-F]{8} [0-9A-F]{2}:[0-9A-F]{8} [0-9A-F]{8} *[0-9]* *[0-9] ([0-9]*)`)

f, err := os.Open("/proc/net/tcp6")
Expand All @@ -379,7 +391,7 @@ func getTCP6Connections(sockets []int) ([]net.Addr, error) {
// Throw away the first line containing headers.
scanner.Scan()

var connections []net.Addr
var connections []net.IP

for scanner.Scan() {
line := scanner.Bytes()
Expand Down

0 comments on commit e04d3fb

Please sign in to comment.