getOutboundIP retrieves the preferred outbound IP address of this machine. It uses a UDP connection to a public DNS server to determine the local IP address that would be used for outbound traffic. Returns: - string: The outbound IP address as a string - error: An error if the IP address cannot be
()
| 72 | // - string: The outbound IP address as a string |
| 73 | // - error: An error if the IP address cannot be determined, nil otherwise |
| 74 | func getOutboundIP() (string, error) { |
| 75 | conn, err := net.Dial("udp", "8.8.8.8:80") |
| 76 | if err != nil { |
| 77 | return "", err |
| 78 | } |
| 79 | defer func() { |
| 80 | if closeErr := conn.Close(); closeErr != nil { |
| 81 | log.Warnf("Failed to close UDP connection: %v", closeErr) |
| 82 | } |
| 83 | }() |
| 84 | |
| 85 | localAddr, ok := conn.LocalAddr().(*net.UDPAddr) |
| 86 | if !ok { |
| 87 | return "", fmt.Errorf("could not assert UDP address type") |
| 88 | } |
| 89 | |
| 90 | return localAddr.IP.String(), nil |
| 91 | } |
| 92 | |
| 93 | // GetIPAddress attempts to find the best-available IP address. |
| 94 | // It first tries to get the public IP address, and if that fails, |
no test coverage detected