netutil

package
v0.0.0-...-749ffa6 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientIP

func ClientIP(r *http.Request) string

ClientIP 使用默认配置获取客户端真实IP RemoteIP 为服务端收到客户端连接时识别到的IP地址,但受限于复杂的网络环境,客户端可能使用代理服务,RemoteIP拿到的只是代理服务器IP 通常代理服务会在HTTP Header 中追加客户端IP地址,通过读取该Header获取客户端ip 同时,代理服务可以设置任意IP,所以对于不信任的代理服务器,可能获取到伪造IP,使用 NewIPValidator 可自定义解析策略

func FigureOutListenOn

func FigureOutListenOn(listenOn string) string

func GetMacAddress

func GetMacAddress() ([]string, error)

func InternalIp

func InternalIp() string

InternalIp returns an internal ip.

func IsPrivate

func IsPrivate(ip string) (bool, error)

IsPrivate reports whether ip is a private address, according to RFC 1918 (IPv4 addresses) and RFC 4193 (IPv6 addresses).

func RemoteIP

func RemoteIP(r *http.Request) string

RemoteIP parses the IP from Request.RemoteAddr, normalizes and returns the IP (without the port).

Types

type IPValidator

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

func NewIPValidator

func NewIPValidator(options ...IPValidatorOption) *IPValidator

func (*IPValidator) ClientIP

func (v *IPValidator) ClientIP(r *http.Request) string

ClientIP implements one best effort algorithm to return the real client IP. It calls c.RemoteIP() under the hood, to check if the remote IP is a trusted proxy or not. If it is it will then try to parse the headers defined in Engine.RemoteIPHeaders (defaulting to [X-Forwarded-For, X-Real-Ip]). If the headers are not syntactically valid OR the remote IP does not correspond to a trusted proxy, the remote IP (coming from Request.RemoteAddr) is returned.

type IPValidatorOption

type IPValidatorOption func(v *IPValidator)

func WithForwardedByClientIP

func WithForwardedByClientIP(forwardedByClientIP bool) IPValidatorOption

func WithRemoteIPHeaders

func WithRemoteIPHeaders(remoteIPHeaders []string) IPValidatorOption

func WithTrustedPlatform

func WithTrustedPlatform(trustedPlatform string) IPValidatorOption

func WithTrustedProxies

func WithTrustedProxies(proxies []string) IPValidatorOption

Jump to

Keyboard shortcuts

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