Package whatsmyip

import "funnyhaha.technology/whatsmyip"
View the source code

Overview

Package whatsmyip is a service that tells a client its IP address, as the server sees it.

$ curl https://whatsmyip.example
203.0.113.7
$ curl -H 'Accept: application/json' https://whatsmyip.example
{"ip":"203.0.113.7","version":4,"hostnames":["host.example.net"]}

Behind a reverse proxy, the server sees the proxy's address, so the client's comes from the X-Forwarded-For header. A client can write that header too, so it's read only through proxies listed with APIBuilder.WithTrustedProxies, walking the chain from the right.

The binary is in cmd/whatsmyip, and its image is built by the Dockerfile.

Subpackages

PackageDescription
funnyhaha.technology/whatsmyip/cmd/whatsmyipCommand whatsmyip serves the whatsmyip API.

Index

Variables

var ErrUnknownAddr = errors.New("can't tell your address: the proxy chain in X-Forwarded-For didn't name it")
source

ErrUnknownAddr is returned when the client's address can't be found: its X-Forwarded-For chain has an entry that isn't an address, or every entry is a trusted proxy.

Types

APIBuilder source

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

APIBuilder configures the API's handler. Its With methods return a modified copy.

NewAPI source

func NewAPI(lookup LookupIPOpFunc) APIBuilder

NewAPI returns a builder for the API, which serves:

It panics if lookup is nil.

(APIBuilder) Handler source

func (b APIBuilder) Handler() http.Handler

Handler returns the API's handler.

(APIBuilder) WithLogger source

func (b APIBuilder) WithLogger(logger *slog.Logger) APIBuilder

WithLogger sets the logger for request logs.

(APIBuilder) WithTrustedProxies source

func (b APIBuilder) WithTrustedProxies(prefixes ...netip.Prefix) APIBuilder

WithTrustedProxies trusts the X-Forwarded-For entries written by proxies in these prefixes. Without any, the client's address is the connection's remote address, and X-Forwarded-For is ignored.

LookupIPOpFunc source

type LookupIPOpFunc func(context.Context, LookupIPParams) (LookupIPResult, error)

LookupIPOpFunc looks up what's known about a client's address.

DNSLookupIP source

func DNSLookupIP(r *net.Resolver, timeout time.Duration) LookupIPOpFunc

DNSLookupIP returns a LookupIPOpFunc that finds the address's hostnames with a reverse DNS lookup through r, giving up after timeout. A failed or timed-out lookup isn't an error: the address just has no hostnames.

LookupIPParams source

type LookupIPParams struct {
	// Addr is the client's address.
	Addr netip.Addr
}

LookupIPParams are the params of a LookupIPOpFunc.

LookupIPResult source

type LookupIPResult struct {
	// Addr is the client's address.
	Addr netip.Addr
	// Hostnames are the names its reverse DNS lookup returned, if any.
	Hostnames []string
}

LookupIPResult is the result of a LookupIPOpFunc.