getLocalIP returns the local machine's IP address (excluding localhost) It tries to connect to a remote address to determine the local interface
()
| 78 | // getLocalIP returns the local machine's IP address (excluding localhost) |
| 79 | // It tries to connect to a remote address to determine the local interface |
| 80 | func getLocalIP() string { |
| 81 | // Try to connect to a public DNS server to determine local interface |
| 82 | // This doesn't actually send any data, just determines which interface would be used |
| 83 | conn, err := net.Dial("udp", "8.8.8.8:80") |
| 84 | if err != nil { |
| 85 | return "" |
| 86 | } |
| 87 | defer conn.Close() |
| 88 | |
| 89 | localAddr := conn.LocalAddr().(*net.UDPAddr) |
| 90 | return localAddr.IP.String() |
| 91 | } |
| 92 | |
| 93 | func teardown() { |
| 94 | TeardownApacheServer() |