check

package
v0.19.1 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2022 License: MIT Imports: 17 Imported by: 2

Documentation

Overview

Package check defines how to check an IP address. It provides types and functions that are useful for writing checks.

Index

Constants

This section is empty.

Variables

View Source
var DefaultHttpClient = NewHttpClient(&http.Client{Timeout: 5 * time.Second})

DefaultHttpClient is reused by checks that make HTTP requests.

View Source
var GetConfigValue = func(key string) (string, error) {
	var v string

	if v = os.Getenv(key); v != "" {
		return v, nil
	}

	usr, err := user.Current()
	if err != nil {
		return "", err
	}
	confFile := filepath.Join(usr.HomeDir, ".checkip.yaml")
	buf, err := ioutil.ReadFile(confFile)
	if err != nil {
		return "", err
	}

	var data map[string]string
	err = yaml.Unmarshal(buf, &data)
	if err != nil {
		return "", err
	}

	if v, ok := data[key]; ok {
		return v, nil
	}
	return "", fmt.Errorf("%s not found in %s", key, confFile)
}

GetConfigValue tries to get value for key first from an environment variable then from a configuration file at $HOME/.checkip.yaml.

Functions

func Na

func Na(s string) string

Na returns "n/a" if s is empty.

func NonEmpty

func NonEmpty(strings ...string) []string

NonEmpty returns strings that are not empty.

func UpdateFile

func UpdateFile(file, url string, compressFmt string) error

UpdateFile updates file from url if the file is older than a week. If file does not exist it downloads and creates it. compressFmt is the compression format of the file to download; gz or tgz. Empty string means no compression.

Types

type Check

type Check func(ipaddr net.IP) (Result, error)

Check checks an IP address providing generic and/or security information.

type EmptyInfo added in v0.10.1

type EmptyInfo struct {
}

EmptyInfo is returned by checks that don't provide generic information about an IP address.

func (EmptyInfo) JsonString added in v0.10.1

func (EmptyInfo) JsonString() (string, error)

func (EmptyInfo) Summary added in v0.12.0

func (EmptyInfo) Summary() string

type Error added in v0.10.1

type Error struct {
	ErrString string `json:"error"` // secrets redacted
	// contains filtered or unexported fields
}

Error is an error returned by a check.

func NewError added in v0.10.1

func NewError(err error) *Error

func (*Error) Error added in v0.10.1

func (e *Error) Error() string

type HttpClient

type HttpClient struct {
	// contains filtered or unexported fields
}

func NewHttpClient added in v0.15.2

func NewHttpClient(client *http.Client) HttpClient

func (HttpClient) Get

func (c HttpClient) Get(apiUrl string, headers map[string]string, queryParams map[string]string) ([]byte, error)

func (HttpClient) GetJson

func (c HttpClient) GetJson(apiUrl string, headers map[string]string, queryParams map[string]string, response interface{}) error

type Info added in v0.10.1

type Info interface {
	Summary() string
	JsonString() (string, error) // all data in JSON format
}

Info is some generic information provided by an Info check.

type Result

type Result struct {
	Name      string `json:"name"`      // check name
	Type      Type   `json:"type"`      // check type
	Info      Info   `json:"info"`      // provided by TypeInfo check
	Malicious bool   `json:"malicious"` // provided by TypeSec check
}

Result is the results of a check.

type Type

type Type int

Type is the type of a check.

const (
	TypeInfo    Type = iota // generic information about the IP address
	TypeSec                 // whether the IP address is considered malicious
	TypeInfoSec             // both of the above
)

func (Type) String added in v0.15.0

func (t Type) String() string

String returns the name of the check type: Info, Sec or InfoSec.

Jump to

Keyboard shortcuts

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