at_common

package
v0.0.0-...-8296c84 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2019 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TUNNEL_PROTOCOL_TCP
	TUNNEL_PROTOCOL_UDP
	CSTYPE_SERVER
	CSTYPE_CLIENT
	MSG_CLIENT_OPEN_CONNECTION
	MSG_SERVER_OPEN_PORT
	MSG_SERVER_CLOSE_PORT
	MSG_SERVER_STATUS_PORT
	MSG_RESPONSE
	STATUS_SUCCESS
	STATUS_FAIL
	MSG_TYPE_LOGIN
	MSG_TYPE_PING
	MSG_TYPE_PONG
	CS_CLIENT uint8 = 1
	CS_SERVER uint8 = 2
)
View Source
const (
	BASE_64_TABLE = "1234567890poiuytreqwasdfghjklmnbvcxzQWERTYUIOPLKJHGFDSAZXCVBNM-_"
)
View Source
const (
	DES_KEY_HOSTPORT = "3j49d723"
)

Variables

View Source
var SHARD_COUNT = 32

Functions

func Connect

func Connect(host string, port, timeout int) (conn net.Conn, err error)

func ConnectHost

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

func Decode

func Decode(reader *bufio.Reader) (msg message, err error)

func Encode

func Encode(data interface{}) (msg []byte, err error)

func FileGetContents

func FileGetContents(file string) (content string, err error)

fileGetContents

func GetAllInterfaceAddr

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

func GetCSTypeString

func GetCSTypeString(cstype int) string

func GetClientCert

func GetClientCert() []byte

func GetClientKey

func GetClientKey() []byte

func GetClusterHost

func GetClusterHost(url, token, typ string) (addr string, err error)

func GetProtocolString

func GetProtocolString(protocol int) string

func GetRootCert

func GetRootCert() []byte

func GetServerCert

func GetServerCert() []byte

func GetServerKey

func GetServerKey() []byte

func HttpGet

func HttpGet(URL string) (body []byte, code int, err error)

func HttpGetNotInternal

func HttpGetNotInternal(URL string) (body []byte, code int, err error)

func HttpPost

func HttpPost(URL string, data map[string]string, header map[string]string) (body []byte, code int, err error)

func HttpPostNotInternal

func HttpPostNotInternal(URL string, data map[string]string, header map[string]string) (body []byte, code int, err error)

func HttpPostRaw

func HttpPostRaw(URL, postParamsString string, header map[string]string, isInternal bool) (body []byte, code int, err error)

func InArray

func InArray(val interface{}, array interface{}) (exists bool, index int)

func IoBind

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

func IsChina

func IsChina(country string) bool

func ListenTls

func ListenTls(ip string, port int) (ln *net.Listener, err error)

func Md5

func Md5(str string) string

md5加密

func PathExists

func PathExists(_path string) bool

func ReadUDPPacket

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

func Response

func Response(channel *MessageChannel, success bool, msg string) (err error)

func TlsConnect

func TlsConnect(host string, port, timeout int) (conn tls.Conn, err error)

func TlsConnectHost

func TlsConnectHost(host string, timeout int) (conn tls.Conn, err error)

func UDPPacket

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

func UrlArgs

func UrlArgs(url, args string) string

Types

type Author

type Author struct {
	Conn    *tls.Conn
	Channel MessageChannel
	// contains filtered or unexported fields
}

func NewAuthor

func NewAuthor(token, clusterHost string, clusterPort, cstype int) (author Author, err error)

func (*Author) DoControlAuth

func (a *Author) DoControlAuth() (err error)

func (*Author) IsClient

func (a *Author) IsClient() bool

func (*Author) IsServer

func (a *Author) IsServer() bool

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

Returns the number of elements within the map.

func (ConcurrentMap) Get

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

Retrieves an element from map under given key.

func (ConcurrentMap) GetShard

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

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

Checks if map is empty.

func (ConcurrentMap) Items

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

Returns all items as map[string]interface{}

func (ConcurrentMap) Iter deprecated

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

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

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

Return 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)

Removes an element from the map and returns it

func (ConcurrentMap) Remove

func (m ConcurrentMap) Remove(key string)

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 Encrypt

type Encrypt struct{}

func NewEncrypt

func NewEncrypt() *Encrypt

func (*Encrypt) Base64Decode

func (encrypt *Encrypt) Base64Decode(str string) (string, error)

base64 解密

func (*Encrypt) Base64DecodeBytes

func (encrypt *Encrypt) Base64DecodeBytes(str string) ([]byte, error)

base64 解密

func (*Encrypt) Base64Encode

func (encrypt *Encrypt) Base64Encode(str string) string

base64 加密

func (*Encrypt) Base64EncodeBytes

func (encrypt *Encrypt) Base64EncodeBytes(bytes []byte) []byte

base64 加密

func (*Encrypt) Md5Encode

func (encrypt *Encrypt) Md5Encode(str string) string

md5加密

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 MessageChannel

type MessageChannel struct {
	ConnectionID uint64
	Conn         *net.Conn
	// contains filtered or unexported fields
}

func NewMessageChannel

func NewMessageChannel(conn *net.Conn) MessageChannel

func NewMessageChannelTls

func NewMessageChannelTls(conn *tls.Conn) MessageChannel

func (*MessageChannel) CloseConn

func (mc *MessageChannel) CloseConn() (err error)

func (*MessageChannel) DoServe

func (mc *MessageChannel) DoServe(errfn func(err error))

func (*MessageChannel) LocalAddr

func (mc *MessageChannel) LocalAddr() net.Addr

func (*MessageChannel) Ping

func (mc *MessageChannel) Ping() (id string, err error)

func (*MessageChannel) Pong

func (mc *MessageChannel) Pong(id string) (err error)

func (*MessageChannel) Read

func (mc *MessageChannel) Read(msg interface{}) (err error)

func (*MessageChannel) ReadTimeout

func (mc *MessageChannel) ReadTimeout(msg interface{}, timeout int) (err error)

func (*MessageChannel) RegMsg

func (mc *MessageChannel) RegMsg(msgType int, msg interface{}, fn msgCallback, fns ...msgCallback)

func (*MessageChannel) RemoteAddr

func (mc *MessageChannel) RemoteAddr() net.Addr

func (*MessageChannel) SetMsgErrorHandler

func (mc *MessageChannel) SetMsgErrorHandler(fn func(channel *MessageChannel, rawMsg interface{}, err error))

func (*MessageChannel) Write

func (mc *MessageChannel) Write(msg interface{}) (err error)

type Msg

type Msg struct {
	MsgType int
}

type MsgClientOpenConnection

type MsgClientOpenConnection struct {
	Msg
	TunnelID    uint64
	ConnectinID uint64
	LocalHost   string
	LocalPort   int
	Protocol    int
}

type MsgLogin

type MsgLogin struct {
	Msg
	Token  string
	CSType int
}

func (*MsgLogin) CSTypeString

func (m *MsgLogin) CSTypeString() string

func (*MsgLogin) IsClient

func (m *MsgLogin) IsClient() bool

func (*MsgLogin) IsServer

func (m *MsgLogin) IsServer() bool

type MsgPing

type MsgPing struct {
	Msg
	ID string
}

type MsgPong

type MsgPong struct {
	Msg
	ID string
}

type MsgResponse

type MsgResponse struct {
	Msg
	Status  int
	Message string
}

func (*MsgResponse) IsSuccess

func (m *MsgResponse) IsSuccess() bool

type MsgServerClosePort

type MsgServerClosePort struct {
	Msg
	TunnelID uint64
	Protocol int
}

type MsgServerOpenPort

type MsgServerOpenPort struct {
	Msg
	TunnelID uint64
	BindPort int
	BindIP   string
	Protocol int
}

func (*MsgServerOpenPort) ProtocolString

func (m *MsgServerOpenPort) ProtocolString() string

type MsgServerStatusPort

type MsgServerStatusPort struct {
	Msg
	TunnelID uint64
	Protocol int
}

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 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(channel MessageChannel, conn net.Conn)) (err error)

func (*ServerChannel) ListenTls

func (sc *ServerChannel) ListenTls(fn func(channel MessageChannel, 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 TrafficStatistics

type TrafficStatistics struct {
	Total   TrafficTotal                 `json="total"`
	Traffic map[string]map[string]uint64 `json="traffic"`
}

type TrafficTotal

type TrafficTotal struct {
	Tunnels       int    `json="tunnels"`
	Servers       int    `json="servers"`
	Clients       int    `json="clinets"`
	UploadBytes   uint64 `json="uploadBytes"`
	DownloadBytes uint64 `json="downloadBytes"`
	TotalBytes    uint64 `json="totalBytes"`
	Connections   int    `json="connections"`
}

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.

Directories

Path Synopsis
v1

Jump to

Keyboard shortcuts

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