Package whatsmyip
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
| Package | Description |
|---|---|
funnyhaha.technology/whatsmyip/cmd/whatsmyip | Command 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")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) APIBuilderNewAPI returns a builder for the API, which serves:
- GET /, the client's address, as text or JSON;
- GET /meta, the service's build information.
It panics if lookup is nil.
(APIBuilder) Handler source
func (b APIBuilder) Handler() http.HandlerHandler returns the API's handler.
(APIBuilder) WithLogger source
func (b APIBuilder) WithLogger(logger *slog.Logger) APIBuilderWithLogger sets the logger for request logs.
(APIBuilder) WithTrustedProxies source
func (b APIBuilder) WithTrustedProxies(prefixes ...netip.Prefix) APIBuilderWithTrustedProxies 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) LookupIPOpFuncDNSLookupIP 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.