whatsmyip

package module
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2024 License: MIT Imports: 11 Imported by: 1

README

whatsmyip

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.

Features

  • 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

Installation

To install the whatsmyip package, use the following command:

go get github.com/jipaix/whatsmyip

Usage

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

Configuration

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

How it works

  1. The Get() function shuffles a list of IP lookup service URLs.
  2. It then sends concurrent HTTP GET requests to all URLs.
  3. The first successfully retrieved IP address is returned.
  4. All ongoing requests are cancelled once a successful response is received.
  5. If all requests fail, an error is returned.

Dependencies

This package depends on the following external library:

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

see LICENSE

Disclaimer

This package relies on external services to determine the IP address. The availability and accuracy of these services are not guaranteed.

Documentation

Overview

Package whatsmyip provides functionality to determine the external IP address of the machine.

This package uses multiple online services to fetch the IP address, improving reliability and reducing dependency on any single service. It employs concurrent requests and returns the first successful response, cancelling other ongoing requests.

The main function of this package is Get(), which returns the external IP address. The package also includes internal utilities for logging.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get() (ip string, url string, err error)

Get fetches the external IP address of the machine by concurrently querying multiple URLs.

The function performs the following steps: 1. Creates a cancellable context 2. Shuffles the list of URLs to randomize the order of requests 3. Concurrently sends HTTP GET requests to all URLs 4. Returns the first successfully retrieved IP address 5. Cancels all ongoing requests once a successful response is received

If all requests fail, it returns an error.

Return values:

  • ip: The retrieved external IP address (empty string if all requests fail)
  • url: The URL that successfully provided the IP address (empty string if all requests fail)
  • err: Error if all requests fail, nil otherwise

The function uses the APP_ENV environment variable to determine the log level. It logs debug information for successful fetches and an error if all requests fail.

This function is designed to be resilient, fast, and to reduce load on any single IP lookup service.

Example
ip, url, err := Get()
if err != nil {
	fmt.Printf("Error: %v\n", err)
	panic(err.Error())
}
fmt.Printf("Your IP address is: %s\n", ip)
fmt.Printf("Retrieved from: %s\n", url)
Output:

Types

This section is empty.

Directories

Path Synopsis
examples
webserver
Fetches the IP once every 24 Hours The machine current ip is returned at http://localhost:8080/ip
Fetches the IP once every 24 Hours The machine current ip is returned at http://localhost:8080/ip

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL