netutil

package
v0.2.12 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	XForwardedFor = "X-Forwarded-For"
	XRealIP       = "X-Real-IP"
)

Variables

View Source
var ErrIFNotFound = errors.New("network interface not found")

ErrIFNotFound is returned if we can't find an available network interface.

Functions

func AuthorityAddr added in v0.2.11

func AuthorityAddr(scheme, authority string) (addr string)

AuthorityAddr returns a given authority (a host/IP, or host:port / ip:port) and returns a host:port. The port 443 is added if needed.

func AuthorityHostPort added in v0.2.11

func AuthorityHostPort(scheme, authority string) (host, port string)

func AuthorityKey added in v0.2.11

func AuthorityKey(u *url.URL) string

func ClientIP added in v0.2.11

func ClientIP(r *http.Request) string

ClientIP 尽最大努力实现获取客户端 IP 的算法。 解析 X-Real-IP 和 X-Forwarded-For 以便于反向代理(nginx 或 haproxy)可以正常工作。

func ClientPublicIP added in v0.2.11

func ClientPublicIP(r *http.Request) string

ClientPublicIP 尽最大努力实现获取客户端公网 IP 的算法。 解析 X-Real-IP 和 X-Forwarded-For 以便于反向代理(nginx 或 haproxy)可以正常工作。

func ConnCheck

func ConnCheck(conn net.Conn) error

func DiscoverDNS

func DiscoverDNS(service, proto, address string) ([]*net.SRV, error)

DiscoverDNS ...

func GetIP added in v0.2.11

func GetIP(r *http.Request) (string, error)

GetIP returns request real ip.

func GetLocalIP

func GetLocalIP() string

func GetPort

func GetPort(addrOrNet interface{}) (int, error)

GetPort returns the port of an endpoint address.

func HasLocalIP added in v0.2.11

func HasLocalIP(ip net.IP) bool

HasLocalIP 检测 IP 地址是否是内网地址 通过直接对比ip段范围效率更高,详见:https://github.com/thinkeridea/go-extend/issues/2

func HasLocalIPAddr added in v0.2.11

func HasLocalIPAddr(ip string) bool

HasLocalIPAddr 检测 IP 地址字符串是否是内网地址

func HasLocalIPddr added in v0.2.11

func HasLocalIPddr(ip string) bool

HasLocalIPddr 检测 IP 地址字符串是否是内网地址 Deprecated: 此为一个错误名称错误拼写的函数,计划在将来移除,请使用 HasLocalIPAddr 函数

func HostIP added in v0.2.11

func HostIP() (string, error)

HostIP tries to return the IPv4 string from the primary network interface.

func IP2Long added in v0.2.11

func IP2Long(ip net.IP) (uint, error)

IP2Long 把net.IP转为数值

func IPString2Long added in v0.2.11

func IPString2Long(ip string) (uint, error)

IPString2Long 把ip字符串转为数值

func IsPortUsed added in v0.2.11

func IsPortUsed(protocol, addr string) bool

IsPortUsed 检查端口是否被占用

func Listen

func Listen(address string, opts ...SockOpt) (_ net.Listener, err error)

func ListenPacket

func ListenPacket(address string, opts ...SockOpt) (_ net.PacketConn, err error)

func Long2IP added in v0.2.11

func Long2IP(i uint) (net.IP, error)

Long2IP 把数值转为net.IP

func Long2IPString added in v0.2.11

func Long2IPString(i uint) (string, error)

Long2IPString 把数值转为ip字符串

func MustGetPort

func MustGetPort(addrOrNet interface{}) int

func NewTCPSocket

func NewTCPSocket(addr string, tlsConfig *tls.Config) (net.Listener, error)

NewTCPSocket creates a TCP socket listener with the specified address and the specified tls configuration. If tlsConfig is set, will encapsulate the TCP listener inside a TLS one.

func RealIP added in v0.2.11

func RealIP(r *http.Request) string

RealIP resolves the real client IP address from the request.

func RemoteIP added in v0.2.11

func RemoteIP(r *http.Request) string

RemoteIP 通过 RemoteAddr 获取 IP 地址, 只是一个快速解析方法。

func RemoteIp added in v0.2.11

func RemoteIp(req *http.Request) string

RemoteIp 返回远程客户端的 IP,如 192.168.1.1

Types

type SockOpt

type SockOpt func(opts *sockOpts) error

SockOpt sets up socket file's creating option

func WithChmod

func WithChmod(mask os.FileMode) SockOpt

WithChmod modifies socket file's access mode

func WithChown

func WithChown(uid, gid int) SockOpt

WithChown modifies the socket file's uid and gid

func WithNetCfg

func WithNetCfg(fn func(lc *net.ListenConfig)) SockOpt

Jump to

Keyboard shortcuts

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