clientip

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package clientip provides client IP helpers. Package clientip 提供客户端 IP 辅助函数。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromRemote

func FromRemote(remoteAddr string) (netip.Addr, bool)

FromRemote parses an IP from remoteAddr. FromRemote 从 remoteAddr 解析 IP。

func FromRequest

func FromRequest(r *http.Request, opts Options) (netip.Addr, bool)

FromRequest returns the client IP for r. Call FromRequest(r, Options{TrustedProxies: ...}) when forwarded headers are trusted. FromRequest 返回 r 的客户端 IP。 需要信任转发头时,调用 FromRequest(r, Options{TrustedProxies: ...})。

Example / 示例:

ip, ok := clientip.FromRequest(r, clientip.Options{})

Types

type Options

type Options struct {
	TrustedProxies []netip.Prefix
	// Headers lists accepted client IP headers in priority order.
	// Nil uses X-Forwarded-For, X-Real-IP, Forwarded, True-Client-IP, then CF-Connecting-IP.
	// An empty non-nil slice disables forwarded headers. Other names accept a single IP.
	// Headers 按优先级列出接受的客户端 IP 头。
	// Nil 依次使用 X-Forwarded-For、X-Real-IP、Forwarded、True-Client-IP、CF-Connecting-IP。
	// 非 nil 空切片禁用转发头。其他头名称按单个 IP 解析。
	Headers []string
}

Options configures forwarded-header trust. Options 配置转发头信任边界。

Jump to

Keyboard shortcuts

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