netx

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: Apache-2.0 Imports: 33 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	HdrASN    = http.CanonicalHeaderKey("P-Asn")
	HdrIP     = http.CanonicalHeaderKey("P-Ip")
	HdrIPGeo  = http.CanonicalHeaderKey("P-Ip-Geo")
	HdrVPN    = http.CanonicalHeaderKey("P-Vpn")
	HdrCF     = http.CanonicalHeaderKey("P-Cf")
	HdrMarked = http.CanonicalHeaderKey("P-Marked")
	HdrRay    = http.CanonicalHeaderKey("X-Ray")
)
View Source
var (
	IPv4zero             = IPv4(0, 0, 0, 0) // all zeros
	IPv4StandardLoopback = IPv4(127, 0, 0, 1)
	IPv6loopback         = FromIP(net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1})
)

Well-known addresses

View Source
var DebugSocketCount = false
View Source
var LocalH2Transport = &http2.Transport{
	DisableCompression: true,
	DialTLSContext: func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) {
		cfg.InsecureSkipVerify = true
		tdial := tls.Dialer{
			Config:    cfg,
			NetDialer: MakeLocalDialer(nil),
		}
		conn, err := tdial.DialContext(ctx, network, addr)
		if err != nil {
			return nil, err
		}
		return NewTracedConn(conn), nil
	},
}
View Source
var LocalIPInfoHeaders = http.Header{
	HdrIP:    []string{"127.0.0.1"},
	HdrASN:   []string{"AS0 Local"},
	HdrIPGeo: []string{"XX"},
}
View Source
var LocalTransport = MakeLocalTransport(16384, 0, &http.Transport{
	TLSClientConfig:       &tls.Config{InsecureSkipVerify: true},
	ResponseHeaderTimeout: 1 * time.Minute,
})

Functions

func GetLocalDialerSocketCount

func GetLocalDialerSocketCount() int64

func IsCloudflareIP

func IsCloudflareIP(ip IP) (bool, error)

func MakeLocalDialer

func MakeLocalDialer(d *net.Dialer) *net.Dialer

func MakeLocalTransport

func MakeLocalTransport(idle int, max int, opts *http.Transport) *http.Transport

func NewTracedConn

func NewTracedConn(conn net.Conn) net.Conn

func NormalizeOrg

func NormalizeOrg(k string) string

func ResetConn

func ResetConn(conn net.Conn)

func ResetRequestConn

func ResetRequestConn(w http.ResponseWriter)

func SetIPInfoHeaders

func SetIPInfoHeaders(headers http.Header, ip string, info IPInfo)

Types

type CombinedIPInfo

type CombinedIPInfo struct {
	OrgPrimary, GeoPrimary IPInfo
}

func (CombinedIPInfo) ASN

func (c CombinedIPInfo) ASN() uint32

func (CombinedIPInfo) Country

func (c CombinedIPInfo) Country() CountryISO

func (CombinedIPInfo) Desc

func (c CombinedIPInfo) Desc() string

func (CombinedIPInfo) Flags

func (c CombinedIPInfo) Flags() Flags

func (CombinedIPInfo) Unwrap

func (c CombinedIPInfo) Unwrap(v any) bool

type CombinedProvider

type CombinedProvider struct {
	OrgPrimary, GeoPrimary IPInfoProvider
}

func (CombinedProvider) LookupContext

func (c CombinedProvider) LookupContext(ctx context.Context, ip IP) IPInfo

type CountryISO

type CountryISO [2]byte

func (CountryISO) IsValid

func (c CountryISO) IsValid() bool

func (CountryISO) String

func (c CountryISO) String() string

func (*CountryISO) UnmarshalText

func (c *CountryISO) UnmarshalText(text []byte) error

type Flags

type Flags uint32
const (
	FlagVPN Flags = 1 << iota
	FlagCF
	FlagMarked
)

type GeoLite2ASN

type GeoLite2ASN struct {
	Set IPMap[GeoLite2ASNBlock]
}

func (*GeoLite2ASN) UnmarshalBinary

func (g2 *GeoLite2ASN) UnmarshalBinary(data []byte) (e error)

type GeoLite2ASNBlock

type GeoLite2ASNBlock struct {
	Network                IPNet  `xsv:"network"`
	AutonomousSystemNumber int    `xsv:"autonomous_system_number"`
	OrganizationName       string `xsv:"autonomous_system_organization"`
}

type GeoLite2Country

type GeoLite2Country struct {
	Locations map[uint32]*GeoLite2Location
	Set       IPMap[GeoLite2CountryBlock]
}

func (*GeoLite2Country) UnmarshalBinary

func (g2 *GeoLite2Country) UnmarshalBinary(data []byte) (e error)

type GeoLite2CountryBlock

type GeoLite2CountryBlock struct {
	Network              IPNet             `xsv:"network"`
	LocationID           uint32            `xsv:"geoname_id"`
	RegisteredCountryID  uint32            `xsv:"registered_country_geoname_id"`
	RepresentedCountryID uint32            `xsv:"represented_country_geoname_id"`
	IsAnonymousProxy     bool              `xsv:"is_anonymous_proxy"`
	Location             *GeoLite2Location `xsv:"-"`
}

type GeoLite2Location

type GeoLite2Location struct {
	ID             uint32     `xsv:"geoname_id"`
	CountryISOCode CountryISO `xsv:"country_iso_code"`
}

type IP

type IP struct {
	Low  uint64
	High uint64
}

func FromIP

func FromIP(ip net.IP) IP

func GetOutboundIP

func GetOutboundIP() (res IP)

func IPFromAddr

func IPFromAddr(a netip.Addr) IP

func IPFromMask

func IPFromMask(ones uint8) (r IP)

func IPFromSlice

func IPFromSlice(a []byte) (i IP)

func IPv4

func IPv4(a, b, c, d byte) IP

func IPv4Uint32 added in v0.2.5

func IPv4Uint32(a uint32) IP

func IPv6

func IPv6(a, b, c, d, e, f, g, h uint8) IP

func ParseIP

func ParseIP(ip string) (s IP)

func (IP) BitOr

func (i IP) BitOr(o IP) IP

func (IP) Compare

func (s IP) Compare(o IP) int64

func (IP) Equal

func (s IP) Equal(o IP) bool

func (IP) IsLoopback

func (ip IP) IsLoopback() bool

IsLoopback reports whether ip is a loopback address.

func (IP) IsPrivate

func (ip IP) IsPrivate() bool

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

func (IP) IsPublic

func (ip IP) IsPublic() bool

func (IP) IsUnspecified

func (ip IP) IsUnspecified() bool

func (IP) IsV4

func (i IP) IsV4() bool

func (IP) IsZero

func (i IP) IsZero() bool

func (IP) MarshalText

func (i IP) MarshalText() ([]byte, error)

func (IP) Shr

func (i IP) Shr(n uint8) IP

func (IP) String

func (i IP) String() string

func (IP) ToAddr

func (i IP) ToAddr() netip.Addr

func (IP) ToIP

func (i IP) ToIP() net.IP

func (IP) ToSlice

func (i IP) ToSlice() []byte

func (*IP) UnmarshalText

func (i *IP) UnmarshalText(b []byte) error

type IP2ASN

type IP2ASN struct {
	Set IPMap[IP2ASNBlock]
}

func (*IP2ASN) UnmarshalBinary

func (i2 *IP2ASN) UnmarshalBinary(data []byte) error

type IP2ASNBlock

type IP2ASNBlock struct {
	IPStart                IP         `xsv:"ip_start"`
	IPEnd                  IP         `xsv:"ip_end"`
	AutonomousSystemNumber uint32     `xsv:"asn"`
	CountryCode            CountryISO `xsv:"country_code"`
	OrganizationName       string     `xsv:"asn_description"`
}

func (*IP2ASNBlock) ASN

func (i *IP2ASNBlock) ASN() uint32

func (*IP2ASNBlock) Country

func (i *IP2ASNBlock) Country() CountryISO

func (*IP2ASNBlock) Desc

func (i *IP2ASNBlock) Desc() string

func (*IP2ASNBlock) Flags

func (i *IP2ASNBlock) Flags() Flags

func (*IP2ASNBlock) Unwrap

func (i *IP2ASNBlock) Unwrap(v any) bool

type IPInfo

type IPInfo interface {
	ASN() uint32
	Desc() string
	Country() CountryISO
	Flags() Flags
	Unwrap(any) bool
}

func CombineIPInfo

func CombineIPInfo(org, geo IPInfo) IPInfo

type IPInfoProvider

type IPInfoProvider interface {
	LookupContext(context.Context, IP) IPInfo
}
var CloudflareProvider IPInfoProvider = &cloudflareProvider{
	newRemoteParsedFile[cfList](cf4URL, config.AsnDir.File("cf-ips-v4.txt")),
	newRemoteParsedFile[cfList](cf6URL, config.AsnDir.File("cf-ips-v6.txt")),
}
var IP2ASNProvider IPInfoProvider = &ip2AsnProvider{
	newRemoteParsedFile[IP2ASN](ip2asnURL, config.AsnDir.File("ip2asn-combined.tsv.gz")),
}
var NullIPInfoProvider IPInfoProvider = nullIPInfoProvider{}

type IPMap

type IPMap[Info any] struct {
	// contains filtered or unexported fields
}

func (*IPMap[Info]) Add

func (s *IPMap[Info]) Add(info *Info, min IP, max IP)

func (*IPMap[Info]) Find

func (i2 *IPMap[Info]) Find(ip IP) (v *Info)

func (*IPMap[Info]) Sort

func (s *IPMap[Info]) Sort()

type IPNet

type IPNet struct {
	IP    IP
	Shift uint8
}

func FromIPNet

func FromIPNet(a *net.IPNet) IPNet

func ParseIPNet

func ParseIPNet(ip string) (s IPNet)

func (IPNet) Contains

func (i IPNet) Contains(o IP) bool

func (IPNet) Limit

func (i IPNet) Limit() IP

func (IPNet) MarshalText

func (i IPNet) MarshalText() ([]byte, error)

func (IPNet) Size

func (i IPNet) Size() (ones, bits int)

func (IPNet) String

func (i IPNet) String() string

func (IPNet) ToIPNet

func (i IPNet) ToIPNet() *net.IPNet

func (*IPNet) UnmarshalText

func (i *IPNet) UnmarshalText(b []byte) (e error)

type IPPort

type IPPort struct {
	IP   IP
	Port uint16
}

func FromAddrPort

func FromAddrPort(a netip.AddrPort) IPPort

func FromNetAddr

func FromNetAddr(adr net.Addr) IPPort

func ParseIPPort

func ParseIPPort(ip string) (s IPPort)

func (IPPort) MarshalText

func (i IPPort) MarshalText() ([]byte, error)

func (IPPort) String

func (i IPPort) String() string

func (IPPort) ToAddrPort

func (i IPPort) ToAddrPort() netip.AddrPort

func (*IPPort) UnmarshalText

func (i *IPPort) UnmarshalText(b []byte) error

type ListenerInfo

type ListenerInfo struct {
	LocalAddr    IPPort
	OutboundAddr IPPort
}

func QueryListener

func QueryListener(lst net.Listener) (info ListenerInfo)

type LocalDialer

type LocalDialer struct {
	net.Dialer
}

func (LocalDialer) DialContext

func (d LocalDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error)

type MarkerProvider

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

func NewMarkerProvider

func NewMarkerProvider(inner IPInfoProvider, list []string) *MarkerProvider

func (*MarkerProvider) LookupContext

func (s *MarkerProvider) LookupContext(ctx context.Context, ip IP) (info IPInfo)

type MaxmindProvider

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

func NewMaxmindProvider

func NewMaxmindProvider(key string) MaxmindProvider

func (MaxmindProvider) LookupContext

func (m MaxmindProvider) LookupContext(c context.Context, ip IP) IPInfo

type NullIPInfo

type NullIPInfo struct{}

func (NullIPInfo) ASN

func (NullIPInfo) ASN() uint32

func (NullIPInfo) Country

func (NullIPInfo) Country() CountryISO

func (NullIPInfo) Desc

func (NullIPInfo) Desc() string

func (NullIPInfo) Flags

func (NullIPInfo) Flags() Flags

func (NullIPInfo) Unwrap

func (NullIPInfo) Unwrap(any) bool

type Proxier

type Proxier string
const (
	ProxierNone       Proxier = ""
	ProxierCloudflare Proxier = "cloudflare"
	ProxierGeneric    Proxier = "generic"
)

type ProxyTraits

type ProxyTraits struct {
	Proxier     Proxier // Proxier type
	Edge        IP      // Parsed value from http.Request.RemoteAddr
	Origin      IP      // Original client
	CountryHint CountryISO
}

func ResolveProxyTraits

func ResolveProxyTraits(request *http.Request) (traits ProxyTraits)

type PublicIPInfo

type PublicIPInfo struct {
	IP      IP      `json:"ip"`
	Country string  `json:"country"`
	Lat     float64 `json:"lat"`
	Lon     float64 `json:"lon"`
	Org     string  `json:"org"`
}

func GetPublicIPInfo

func GetPublicIPInfo(c context.Context) (r PublicIPInfo)

type RemoteFile

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

func NewRemoteFile

func NewRemoteFile(uri string, filePath string) *RemoteFile

func (*RemoteFile) Invalidate

func (r *RemoteFile) Invalidate()

func (*RemoteFile) Load

func (r *RemoteFile) Load(ifchanged bool) ([]byte, error)

type TracedConn

type TracedConn struct {
	net.Conn
}

func (*TracedConn) Close

func (c *TracedConn) Close() error

Jump to

Keyboard shortcuts

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