utils

package
v0.0.0-...-1f4d2fe Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2023 License: GPL-3.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TE               = "TE"
	Trailers         = "Trailer"           // note: actual header name is Trailer
	TransferEncoding = "Transfer-Encoding" // note: this header is not normally removed when proxying, since the proxy does not re-chunk responses.
	Upgrade          = "Upgrade"
)

Variables

View Source
var (
	MaximumFailedCount = 3
)
View Source
var SHARD_COUNT = 32

Functions

func AvailableIPRResolvers

func AvailableIPRResolvers() (ret []string)

func CloseConn

func CloseConn(conn *net.Conn)

func ConnectHost

func ConnectHost(hostAndPort string, timeout int) (conn net.Conn, err error)

func ConnectHostWithLAddr

func ConnectHostWithLAddr(hostAndPort string, laddr string, timeout time.Duration) (conn net.Conn, err error)

func GetAllInterfaceAddr

func GetAllInterfaceAddr() ([]net.IP, error)

func GetAvailableIfaceAddr

func GetAvailableIfaceAddr() string

func GetTransport

func GetTransport(laddr string) http.RoundTripper

func HTTPGet

func HTTPGet(URL string, timeout int) (err error)

func IoBind

func IoBind(dst io.ReadWriter, src io.ReadWriter, fn func(isSrcErr bool, err error), cfn func(count int, isPositive bool), bytesPreSec float64)

func Keygen

func Keygen() (err error)

func ListenTls

func ListenTls(ip string, port int, certBytes, keyBytes []byte) (ln *net.Listener, err error)

func PathExists

func PathExists(_path string) bool

func ReadUDPPacket

func ReadUDPPacket(conn *net.Conn) (srcAddr string, packet []byte, err error)

func ResolveMapping

func ResolveMapping(ipResolver IPResolver, consume func(k, v string)) error

func StartMonitor

func StartMonitor(ipResolver IPResolver, mapping Mapping, checkInterval time.Duration)

func TlsConnect

func TlsConnect(host string, port, timeout int, certBytes, keyBytes []byte) (conn tls.Conn, err error)

func TlsConnectHost

func TlsConnectHost(host string, timeout int, certBytes, keyBytes []byte) (conn tls.Conn, err error)

func UDPPacket

func UDPPacket(srcAddr string, packet []byte) []byte

func UnmarshalMapping

func UnmarshalMapping(path string, consume func(k, v string)) (err error)

Types

type BasicAuth

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

func NewBasicAuth

func NewBasicAuth() BasicAuth

func (*BasicAuth) Add

func (ba *BasicAuth) Add(userpassArr []string) (n int)

func (*BasicAuth) AddFromFile

func (ba *BasicAuth) AddFromFile(file string) (n int, err error)

func (*BasicAuth) Check

func (ba *BasicAuth) Check(userpass string) (ok bool)

func (*BasicAuth) Total

func (ba *BasicAuth) Total() (n int)

type Checker

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

func NewChecker

func NewChecker(timeout int, interval int64, blockedFile, directFile string) Checker

NewChecker args: timeout : tcp timeout milliseconds ,connect to host interval: recheck domain interval seconds

func (*Checker) Add

func (c *Checker) Add(address string, isHTTPS bool, method, URL string, data []byte)

func (*Checker) IsBlocked

func (c *Checker) IsBlocked(address string) (blocked bool, failN, successN uint)

type CheckerItem

type CheckerItem struct {
	IsHTTPS      bool
	Method       string
	URL          string
	Domain       string
	Host         string
	Data         []byte
	SuccessCount uint
	FailCount    uint
}

type CommonIpResolver

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

CommonIpResolver do a request to an api to resolve iface address

func (CommonIpResolver) Get

func (r CommonIpResolver) Get(ifaceAddr string) (string, error)

func (CommonIpResolver) Name

func (r CommonIpResolver) Name() string

func (CommonIpResolver) String

func (r CommonIpResolver) String() string

type ConcurrentMap

type ConcurrentMap []*ConcurrentMapShared

A "thread" safe map of type string:Anything. To avoid lock bottlenecks this map is dived to several (SHARD_COUNT) map shards.

func NewConcurrentMap

func NewConcurrentMap() ConcurrentMap

Creates a new concurrent map.

func (ConcurrentMap) Count

func (m ConcurrentMap) Count() int

Count returns the number of elements within the map.

func (ConcurrentMap) Get

func (m ConcurrentMap) Get(key string) (interface{}, bool)

Get retrieves an element from map under given key.

func (ConcurrentMap) GetShard

func (m ConcurrentMap) GetShard(key string) *ConcurrentMapShared

GetShard returns shard under given key

func (ConcurrentMap) Has

func (m ConcurrentMap) Has(key string) bool

Looks up an item under specified key

func (ConcurrentMap) IsEmpty

func (m ConcurrentMap) IsEmpty() bool

IsEmpty checks if map is empty.

func (ConcurrentMap) Items

func (m ConcurrentMap) Items() map[string]interface{}

Items returns all items as map[string]interface{}

func (ConcurrentMap) Iter deprecated

func (m ConcurrentMap) Iter() <-chan Tuple

Iter returns an iterator which could be used in a for range loop.

Deprecated: using IterBuffered() will get a better performence

func (ConcurrentMap) IterBuffered

func (m ConcurrentMap) IterBuffered() <-chan Tuple

IterBuffered returns a buffered iterator which could be used in a for range loop.

func (ConcurrentMap) IterCb

func (m ConcurrentMap) IterCb(fn IterCb)

Callback based iterator, cheapest way to read all elements in a map.

func (ConcurrentMap) Keys

func (m ConcurrentMap) Keys() []string

Keys returns all keys as []string

func (ConcurrentMap) MSet

func (m ConcurrentMap) MSet(data map[string]interface{})

func (ConcurrentMap) MarshalJSON

func (m ConcurrentMap) MarshalJSON() ([]byte, error)

Reviles ConcurrentMap "private" variables to json marshal.

func (ConcurrentMap) Pop

func (m ConcurrentMap) Pop(key string) (v interface{}, exists bool)

Pop removes an element from the map and returns it

func (ConcurrentMap) Remove

func (m ConcurrentMap) Remove(key string)

Remove removes an element from the map.

func (ConcurrentMap) Set

func (m ConcurrentMap) Set(key string, value interface{})

Sets the given value under the specified key.

func (ConcurrentMap) SetIfAbsent

func (m ConcurrentMap) SetIfAbsent(key string, value interface{}) bool

Sets the given value under the specified key if no value was associated with it.

func (ConcurrentMap) Upsert

func (m ConcurrentMap) Upsert(key string, value interface{}, cb UpsertCb) (res interface{})

Insert or Update - updates existing element or inserts a new one using UpsertCb

type ConcurrentMapShared

type ConcurrentMapShared struct {
	sync.RWMutex // Read Write mutex, guards access to internal map.
	// contains filtered or unexported fields
}

A "thread" safe string to anything map.

type ConnPool

type ConnPool interface {
	Get() (conn interface{}, err error)
	Put(conn interface{})
	ReleaseAll()
	Len() (length int)
}

ConnPool to use

func NewConnPool

func NewConnPool(poolConfig poolConfig) (pool ConnPool, err error)

type FallbackIPResolver

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

FallbackIPResolver that resolve iface address using multiple IPResolver(s) until it succeeds

func (FallbackIPResolver) Get

func (r FallbackIPResolver) Get(ifaceAddr string) (ret string, err error)

func (FallbackIPResolver) Name

func (r FallbackIPResolver) Name() string

type HTTPRequest

type HTTPRequest struct {
	HeadBuf []byte

	Host   string
	Method string
	URL    string
	// contains filtered or unexported fields
}

func NewHTTPRequest

func NewHTTPRequest(inConn *net.Conn, bufSize int, isBasicAuth bool, basicAuth *BasicAuth) (req HTTPRequest, err error)

func (*HTTPRequest) BasicAuth

func (req *HTTPRequest) BasicAuth() (err error)

func (*HTTPRequest) DelHeader

func (req *HTTPRequest) DelHeader(key string) bool

func (*HTTPRequest) GetHeader

func (req *HTTPRequest) GetHeader(key string) (val string, err error)

func (*HTTPRequest) HTTP

func (req *HTTPRequest) HTTP() (err error)

func (*HTTPRequest) HTTPS

func (req *HTTPRequest) HTTPS() (err error)

func (*HTTPRequest) HTTPSReply

func (req *HTTPRequest) HTTPSReply() (err error)

func (*HTTPRequest) IsHTTPS

func (req *HTTPRequest) IsHTTPS() bool

func (*HTTPRequest) RemoveHopByHopHeaders

func (req *HTTPRequest) RemoveHopByHopHeaders()

type IPResolver

type IPResolver interface {
	Get(ifaceAddr string) (string, error)
	Name() string
}

func NewFallBackIPResolver

func NewFallBackIPResolver(names ...string) (IPResolver, error)

func NewRetryableIPResolver

func NewRetryableIPResolver(name string, times int) (IPResolver, error)

func NewRoundRobinIPResolver

func NewRoundRobinIPResolver(names ...string) (IPResolver, error)

func PickIPResolver

func PickIPResolver(name string) IPResolver

func PickIPResolvers

func PickIPResolvers(names ...string) []IPResolver

type IterCb

type IterCb func(key string, v interface{})

Iterator callback,called for every key,value found in maps. RLock is held for all calls for a given shard therefore callback sess consistent view of a shard, but not across the shards

type Mapping

type Mapping interface {
	Get(string) string
	Put(string, string)
	Remove(string)
	Iter(func(string, string) bool)
}

func NewInMemoryMapping

func NewInMemoryMapping() Mapping

type OutPool

type OutPool struct {
	Pool ConnPool
	// contains filtered or unexported fields
}

func NewOutPool

func NewOutPool(dur int, isTLS bool, certBytes, keyBytes []byte, address string, timeout int, InitialCap int, MaxCap int) (op OutPool)

type PBMapping

type PBMapping struct {
	Mapping
}

PBMapping supports public ip address mapping even if mapping is empty

func (*PBMapping) Get

func (m *PBMapping) Get(key string) string

type Reader

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

func NewReader

func NewReader(r io.Reader) *Reader

NewReader returns a reader that implements io.Reader with rate limiting.

func NewReaderWithContext

func NewReaderWithContext(r io.Reader, ctx context.Context) *Reader

NewReaderWithContext returns a reader that implements io.Reader with rate limiting.

func (*Reader) Read

func (s *Reader) Read(p []byte) (int, error)

Read reads bytes into p.

func (*Reader) SetRateLimit

func (s *Reader) SetRateLimit(bytesPerSec float64)

SetRateLimit sets rate limit (bytes/sec) to the reader.

type RetryableIPResolver

type RetryableIPResolver struct {
	IPResolver
	// contains filtered or unexported fields
}

RetryableIPResolver try resolve iface address multiple times until it succeeds

func (RetryableIPResolver) Get

func (r RetryableIPResolver) Get(ifaceAddr string) (ret string, err error)

type RoundRobinIPResolver

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

RoundRobinIPResolver do resolve in round-robin

func (*RoundRobinIPResolver) Get

func (r *RoundRobinIPResolver) Get(ifaceAddr string) (ret string, err error)

func (*RoundRobinIPResolver) Name

func (r *RoundRobinIPResolver) Name() string

type ServerChannel

type ServerChannel struct {
	Listener    *net.Listener
	UDPListener *net.UDPConn
	// contains filtered or unexported fields
}

func NewServerChannel

func NewServerChannel(ip string, port int) ServerChannel

func (*ServerChannel) ListenTCP

func (sc *ServerChannel) ListenTCP(fn func(conn net.Conn)) (err error)

func (*ServerChannel) ListenTls

func (sc *ServerChannel) ListenTls(certBytes, keyBytes []byte, fn func(conn net.Conn)) (err error)

func (*ServerChannel) ListenUDP

func (sc *ServerChannel) ListenUDP(fn func(packet []byte, localAddr, srcAddr *net.UDPAddr)) (err error)

func (*ServerChannel) SetErrAcceptHandler

func (sc *ServerChannel) SetErrAcceptHandler(fn func(err error))

type Tuple

type Tuple struct {
	Key string
	Val interface{}
}

Used by the Iter & IterBuffered functions to wrap two variables together over a channel,

type UpsertCb

type UpsertCb func(exist bool, valueInMap interface{}, newValue interface{}) interface{}

Callback to return new element to be inserted into the map It is called while lock is held, therefore it MUST NOT try to access other keys in same map, as it can lead to deadlock since Go sync.RWLock is not reentrant

type Writer

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

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter returns a writer that implements io.Writer with rate limiting.

func NewWriterWithContext

func NewWriterWithContext(w io.Writer, ctx context.Context) *Writer

NewWriterWithContext returns a writer that implements io.Writer with rate limiting.

func (*Writer) SetRateLimit

func (s *Writer) SetRateLimit(bytesPerSec float64)

SetRateLimit sets rate limit (bytes/sec) to the writer.

func (*Writer) Write

func (s *Writer) Write(p []byte) (int, error)

Write writes bytes from p.

Jump to

Keyboard shortcuts

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