clouddetect

package module
v0.0.0-...-6f366eb Latest Latest
Warning

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

Go to latest
Published: May 8, 2018 License: MIT Imports: 13 Imported by: 0

README

clouddetect

Go Report Card

Go package to determine if an IP address resolves to one of the major cloud providers. Helpful for risk-scoring potential bot traffic in conjunction with other signals such as User Agent.

Currently the library consumes the published IP ranges from Amazon, Google, and Microsoft.

API usage

package main

import (
	"fmt"
	"os"

	"github.com/99designs/clouddetect"
)

func main() {
    ip := net.ParseIP("127.0.0.1")
    if cloud, err := clouddetect.Resolve(ip); err == nil {
        fmt.Println(cloud.ProviderName)
    }
}

The default Client has an internal cache with a TTL of 12 hours. The first request to resolve an IP will be slow as it fetches all published ranges. See the clouddetect godocs for more detail on the API.

CLI usage

go get github.com/99designs/clouddetect/cli/clouddetect

then

clouddetect -ip=127.0.0.1

Caching to Disk

package main

import (
    "fmt"
    "os"

    "github.com/99designs/clouddetect"
    "time"
)

var (
    client = clouddetect.NewClient(24 * time.Hour)
)

func init() {
    client.CacheFilePath = path.Join(os.TempDir(), "cloud-ip-cache.json")
    // Cache will be persisted and loaded from disk. A lock file is also generated during this process to allow multiple instances to share the same cache file path without having competing refreshes.
    client.RefreshCache()
}

func main() {
    ip := net.ParseIP("127.0.0.1")
    if cloud, err := client.Resolve(ip); err == nil {
        fmt.Println(cloud.ProviderName)
    }
}

Using a CacheFilePath speeds up the initial launch time, so your first request to resolve an IP will only be slow if no local cache exists. Optionally, manually calling the client.RefreshCache() function in your init() ensures your cache is ready to go even for the first request.

LICENSE

MIT 2018 99designs

Documentation

Index

Constants

View Source
const (
	// ProviderAmazon is AWS
	ProviderAmazon = "Amazon Web Services"
	// ProviderGoogle is Google Cloud
	ProviderGoogle = "Google Cloud"
	// ProviderMicrosoft is Microsoft Azure
	ProviderMicrosoft = "Microsoft Azure"
)

Variables

View Source
var (
	// ErrNotCloudIP is error returned when IP does not match any of the published list of ranges
	ErrNotCloudIP = errors.New("not resolved to any known cloud IP range")
	// ErrCacheRefreshInProgress is returned when RefreshCache is called while an existing refresh is occurring
	ErrCacheRefreshInProgress = errors.New("cache refresh is already in progress")
	// ErrDiskCacheExpired is returned when trying to refresh from disk with a file that has exceeded the TTL
	ErrDiskCacheExpired = errors.New("cache on disk is expired")
)
View Source
var (
	DefaultCacheRefreshTimeout time.Duration = 2 * time.Minute
)

Functions

This section is empty.

Types

type Client

type Client struct {

	// Time to keep IP ranges cached for (default 12 hours)
	TTL                 time.Duration
	CacheFilePath       string
	CacheRefreshTimeout time.Duration
	// contains filtered or unexported fields
}

Client will eventually hold cache of IP ranges

func DefaultClient

func DefaultClient() *Client

DefaultClient is the default Client for resolving requests

func NewClient

func NewClient(TTL time.Duration) *Client

NewClient generates a Client with specified cache TTL

func (*Client) Count

func (c *Client) Count() (subnetCount int)

Count retruns the number of cloud provider subnets loaded in the cache

func (*Client) RefreshCache

func (c *Client) RefreshCache() (err error)

RefreshCache loads the cloud provider subnet data from disk (if available) and then from the web

func (*Client) Resolve

func (c *Client) Resolve(ip net.IP) (response *Response, err error)

Resolve will take the given ip and determine if it exists within any of the major cloud providers' published IP ranges and any extra metadata that may be of use. It returns ErrNotCloudIP if the IP does not resolve against any lists

type Logger

type Logger struct {
	Enabled bool
}

func (*Logger) Printf

func (this *Logger) Printf(format string, v ...interface{})

type Response

type Response struct {
	ProviderName string     `json:"providerName"`
	Region       string     `json:"region"`
	Subnet       *net.IPNet `json:"subnet"`
}

Response provides details of the cloud environment the IP resolved to

func Resolve

func Resolve(ip net.IP) (*Response, error)

Resolve is a convenience function to resolve an IP against the DefaultClient

Directories

Path Synopsis
cli

Jump to

Keyboard shortcuts

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