publicipgrabber

package
v0.0.51 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2024 License: GPL-3.0 Imports: 10 Imported by: 0

README

Public IP Grabber

Overview

This package is designed to contact WhatIsMyIP and pull down information. This has the ability to grab the public IP of the machine querying the site, or information related to a public IP specified by the user. This contacts the site's API (api.whatismyip.com) to pull down the desired information.

The package has one main struct that all the functionality is linked to. The main struct is called PublicIPGrabber. To initialize this struct, the NewPublicIPGrabber function should be called. This function returns a pointer to a PublicIPGrabber object. The user can set the configuration options for the new object by passing in PublicIPGrabberOptFuncs.

Examples

The following snippet will initialize a PublicIPGrabber object which uses the default http client:

import "github.com/thomas-osgood/OGOR/networking/publicipgrabber"

var err error
var grabber *publicipgrabber.PublicIPGrabber

grabber, err = NewPublicIPGrabber()
if err != nil {
    // process error here
}

The following snippet will pull down and display the public IP info for the current machine:

err = grabber.GetMyIPInformation()
if err != nil {
    // process error here
}

log.Printf("IP: %s\n", grabber.PublicIP.Ip)
log.Printf("Location: %s\n", grabber.PublicIP.Location)
log.Printf("Provider: %s\n", grabber.PublicIP.Provider)

Documentation

Overview

package designed to query api.whatismyip.com for public IP address information. this package contains the structs defining the JSON responses expected and the objects that will utilize them to pull down information.

Index

Constants

View Source
const BASE_URL string = "https://api.whatismyip.com"

base url address for all whatismyip API queries.

Variables

This section is empty.

Functions

func QueryIPIfy added in v0.0.22

func QueryIPIfy() (publicip string, err error)

function designed to contact the ipify api to pull down the current machine's public IPv4 address. this returns only the IP address and no additional information. this can be used to quickly query the public IPv4 without the need to create a PublicIPGrabber object.

for further info: https://www.ipify.org

func QueryIPIfy6 added in v0.0.22

func QueryIPIfy6() (publicip string, err error)

function designed to contact the ipify api to pull down the current machine's public IPv6 address. this returns only the IP address and no additional information. this can be used to quickly query the public IPv6 without the need to create a PublicIPGrabber object.

note: if no IPv6 address is found, the IPv4 address will be returned.

for further info: https://www.ipify.org

Types

type AppResponse added in v0.0.22

type AppResponse struct {
	// data returned from ip2location.com query made
	// by api.whatismyip.com/app.php.
	Ip2location LocationResponse `json:"ip2location.com"`

	// data returned from ipdata.co query made
	// by api.whatismyip.com/app.php.
	Ipdata LocationResponse `json:"ipdata.co"`
}

structure representing the response from api.whatismyip.com/app.php when querying information related to an IP address.

type DnsResponse added in v0.0.22

type DnsResponse struct {
	Arecords []string `json:"a-records"`
}

structure representing the response from api.whatismyip.com/app.php when querying DNS information related to a URL.

type ErrorResponse added in v0.0.22

type ErrorResponse struct {
	Error string `json:"error"`
}

structure representing an error response from api.whatismyip.com.

type LocationResponse added in v0.0.22

type LocationResponse struct {
	Asn        string `json:"asn"`
	City       string `json:"city"`
	Region     string `json:"region"`
	Country    string `json:"country"`
	PostalCode string `json:"postal_code"`
	Isp        string `json:"isp"`
	TimeZone   string `json:"time_zone"`
}

structure defining the IP2Location and IPData JSON returns from app.php.

type PublicIPGrabber

type PublicIPGrabber struct {

	// information pulled down from api.whatismyip.com.
	PublicIP PublicIPInfo
	// contains filtered or unexported fields
}

structure defining a PublicIPGrabber object. this will have associated functions to query the site api.whatismyip.com and grab the public IP info.

func NewPublicIPGrabber

func NewPublicIPGrabber(optfuncs ...PublicIPGrabberOptFunc) (grabber *PublicIPGrabber, err error)

function designed to create and initialize a new PubliIPGrabber object. the user can pass in option functions to change the configuration.

func (*PublicIPGrabber) GetIPInformation added in v0.0.22

func (ipg *PublicIPGrabber) GetIPInformation(targetip string) (ipinformation *AppResponse, err error)

function designed to query api.whatismyip.com and get information related to a given IP address.

func (*PublicIPGrabber) GetMyIPInformation

func (ipg *PublicIPGrabber) GetMyIPInformation() (err error)

function designed to query api.whatismyip.com and pull down the public IP address information for the machine executing the program.

func (*PublicIPGrabber) GetUrlIP added in v0.0.22

func (ipg *PublicIPGrabber) GetUrlIP(target string) (arecords *DnsResponse, err error)

function designed to contact api.whatismyip.com and pull down the IP address attached to a given URL.

type PublicIPGrabberOptFunc

type PublicIPGrabberOptFunc func(*PublicIPGrabberOptions) error

type alias defining the function structure that will be used to set the configuration options for a PublicIPGrabberOptions object.

func WithClient

func WithClient(client *http.Client) PublicIPGrabberOptFunc

function designed to set the PublicIPGrabberOptions client.

type PublicIPGrabberOptions

type PublicIPGrabberOptions struct {
	// http client that will be used to carry
	// out queries to api.whatismyip.com.
	Client *http.Client
}

structure defining the object that will be used to initialize a public ip grabber object.

type PublicIPInfo

type PublicIPInfo struct {
	// public ip address
	Ip string `json:"ip"`

	// geolocation of the server hosting the ip
	Location string `json:"geo"`

	// provider hosting the IP address
	Provider string `json:"isp"`
}

structure holding public ip information. this will be used in the PublicIPGrabber object and associated request to api.whatismyip.com.

Jump to

Keyboard shortcuts

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