()
| 1638 | } |
| 1639 | |
| 1640 | func GetContainerIPv4() (string, error) { |
| 1641 | // Get all network interfaces |
| 1642 | interfaces, err := net.Interfaces() |
| 1643 | if err != nil { |
| 1644 | return "", err |
| 1645 | } |
| 1646 | |
| 1647 | // Iterate over the interfaces |
| 1648 | for _, i := range interfaces { |
| 1649 | // Skip down or loopback interfaces |
| 1650 | if i.Flags&net.FlagUp == 0 || i.Flags&net.FlagLoopback != 0 { |
| 1651 | continue |
| 1652 | } |
| 1653 | |
| 1654 | // Get the addresses for the current interface |
| 1655 | addrs, err := i.Addrs() |
| 1656 | if err != nil { |
| 1657 | continue |
| 1658 | } |
| 1659 | |
| 1660 | // Iterate over the addresses |
| 1661 | for _, addr := range addrs { |
| 1662 | var ip net.IP |
| 1663 | // The address can be of type *net.IPNet or *net.IPAddr |
| 1664 | if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { |
| 1665 | // Check if it's an IPv4 address |
| 1666 | if ipnet.IP.To4() != nil { |
| 1667 | ip = ipnet.IP |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | if ip != nil { |
| 1672 | // Found a valid IPv4 address, return it |
| 1673 | return ip.String(), nil |
| 1674 | } |
| 1675 | } |
| 1676 | } |
| 1677 | |
| 1678 | return "", fmt.Errorf("could not find a non-loopback IP for the container") |
| 1679 | } |
| 1680 | |
| 1681 | // GetFullCommandUsed returns the full command-line used to run the current process. |
| 1682 | // It reconstructs the command from os.Args, adding quoting for arguments with spaces or quotes. |
no test coverage detected