whatsmyip
is a Go package that provides functionality to determine the external IP address of a machine. It's designed to be reliable, fast, and efficient by using multiple online services concurrently.
- Fetches the external IP address using multiple online services
- Employs concurrent requests to improve speed and reliability
- Automatically distributes load across different IP lookup services
- Configurable logging based on environment
- Easy to use with a single function call
To install the whatsmyip
package, use the following command:
go get github.com/jipaix/whatsmyip
Here's a simple example of how to use the whatsmyip
package:
package main
import (
"fmt"
"github.com/jipaix/whatsmyip"
)
func main() {
ip, url, err := whatsmyip.Get()
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("Your IP address is: %s\n", ip)
fmt.Printf("Retrieved from: %s\n", url)
}
See more examples in the /examples folder
The package uses the APP_ENV environment variable to determine the log level:
APP_ENV | Logging |
---|---|
local , dev , development |
Debug level |
test , staging |
Info level |
prod , production |
Disabled |
Not set | Info level |
Any other value | Disabled |
- The
Get()
function shuffles a list of IP lookup service URLs. - It then sends concurrent HTTP GET requests to all URLs.
- The first successfully retrieved IP address is returned.
- All ongoing requests are cancelled once a successful response is received.
- If all requests fail, an error is returned.
This package depends on the following external library:
- github.com/charmbracelet/log for logging
Contributions are welcome! Please feel free to submit a Pull Request.
see LICENSE
This package relies on external services to determine the IP address. The availability and accuracy of these services are not guaranteed.