gate

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2023 License: MIT Imports: 22 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsClientClosed added in v1.5.0

func IsClientClosed(err error) bool

func IsClientNotExist added in v1.5.0

func IsClientNotExist(err error) bool

func IsIDAlreadyExist added in v1.5.0

func IsIDAlreadyExist(err error) bool

IsIDAlreadyExist returns true if the error is caused by the ID of the client already exist. Returns when SetClientID is called with the existing new ID.

func SetMessageReader added in v1.5.0

func SetMessageReader(s MessageReader)

Types

type AesCBCCrypto added in v1.5.0

type AesCBCCrypto struct {
	Key []byte
}

AesCBCCrypto cbc mode PKCS7 padding

func NewAesCBCCrypto added in v1.5.0

func NewAesCBCCrypto(key []byte) *AesCBCCrypto

func (*AesCBCCrypto) Decrypt added in v1.5.0

func (a *AesCBCCrypto) Decrypt(src, iv []byte) ([]byte, error)

func (*AesCBCCrypto) DecryptCredentials added in v1.5.0

func (a *AesCBCCrypto) DecryptCredentials(src []byte) (*ClientAuthCredentials, error)

func (*AesCBCCrypto) Encrypt added in v1.5.0

func (a *AesCBCCrypto) Encrypt(src, iv []byte) ([]byte, error)

func (*AesCBCCrypto) EncryptCredentials added in v1.5.0

func (a *AesCBCCrypto) EncryptCredentials(c *ClientAuthCredentials) ([]byte, error)

type Authenticator added in v1.5.0

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

Authenticator handle client authentication message

func NewAuthenticator added in v1.5.0

func NewAuthenticator(gateway DefaultGateway, key string) *Authenticator

func (*Authenticator) ClientAuthMessageInterceptor added in v1.5.0

func (a *Authenticator) ClientAuthMessageInterceptor(dc DefaultClient, msg *messages.GlideMessage) (intercept bool)

func (*Authenticator) MessageInterceptor added in v1.5.0

func (a *Authenticator) MessageInterceptor(dc DefaultClient, msg *messages.GlideMessage) bool

type Client

type Client interface {

	// SetID sets the ID of the client.
	SetID(id ID)

	// IsRunning returns true if the client is running/alive.
	IsRunning() bool

	// EnqueueMessage enqueues a message to be sent to the client.
	EnqueueMessage(message *messages.GlideMessage) error

	// Exit the client and close the connection.
	Exit()

	// Run starts the client message handling loop and blocks until the client.
	Run()

	// GetInfo returns the client's information.
	GetInfo() Info
}

Client is a client connection abstraction.

type ClientAuthCredentials added in v1.5.0

type ClientAuthCredentials struct {

	// Type is the type of the client.
	Type int `json:"type"`

	// UserID uniquely identifies the client.
	UserID string `json:"user_id"`

	// DeviceID is the id of the client device, it is unique for same client.
	DeviceID string `json:"device_id"`

	DeviceName string `json:"device_name"`

	Secrets *ClientSecrets `json:"secrets"`

	// ConnectionID is the temporary connection id of the client, generated by the client.
	ConnectionID string `json:"connection_id"`

	// Timestamp of credentials creation.
	Timestamp int64 `json:"timestamp"`
}

ClientAuthCredentials represents the client authentication credentials. Used to client authentication when connecting to the gateway, credentials are generated by business service, encrypted use the gateway's secret key, and sent to the client.

type ClientConfig added in v1.5.0

type ClientConfig struct {

	// ClientHeartbeatDuration is the duration of heartbeat.
	ClientHeartbeatDuration time.Duration

	// ServerHeartbeatDuration is the duration of server heartbeat.
	ServerHeartbeatDuration time.Duration

	// HeartbeatLostLimit is the max lost heartbeat count.
	HeartbeatLostLimit int

	// CloseImmediately true express when client exit, discard all message in queue, and close connection immediately,
	// otherwise client will close runRead, and mark as stateClosing, the client cannot receive and enqueue message,
	// after all message in queue is sent, client will close runWrite and connection.
	CloseImmediately bool
}

ClientConfig client config

type ClientSecrets added in v1.5.0

type ClientSecrets struct {
	// MessageDeliverSecret is the secret of the client, used to authenticate the client message.
	// The secret is generated by the business service, saved in business service, client should not know it.
	// When client send a message to someone else, it should get the sign of the message target, and send it
	// with the message. If business service want to control which one the client can send message to,
	// business service can generate different secret for client, and notify the gateway update the secret, to make
	// client old sign invalid.
	MessageDeliverSecret string `json:"message_deliver_secret"`
}

ClientSecrets used to control client permission.

type CredentialCrypto added in v1.5.0

type CredentialCrypto interface {
	EncryptCredentials(c *ClientAuthCredentials) ([]byte, error)

	DecryptCredentials(src []byte) (*ClientAuthCredentials, error)
}

type DefaultClient added in v1.5.0

type DefaultClient interface {
	Client

	SetCredentials(credentials *ClientAuthCredentials)

	GetCredentials() *ClientAuthCredentials

	AddMessageInterceptor(interceptor MessageInterceptor)
}

func NewClient added in v1.5.0

func NewClient(conn conn.Connection, mgr Gateway, handler MessageHandler) DefaultClient

func NewClientWithConfig added in v1.5.0

func NewClientWithConfig(conn conn.Connection, mgr Gateway, handler MessageHandler, config *ClientConfig) DefaultClient

type DefaultGateway added in v1.5.0

type DefaultGateway interface {
	Gateway

	GetClient(id ID) Client

	GetAll() map[ID]Info

	SetMessageHandler(h MessageHandler)

	AddClient(cs Client)
}

DefaultGateway is gateway default implements.

type EncryptedCredential added in v1.5.0

type EncryptedCredential struct {
	// Version is the version of the credential.
	Version int `json:"version"`

	// Credential is the encrypted credential string.
	Credential string `json:"credential"`
}

EncryptedCredential represents the encrypted credential.

type Gateway

type Gateway interface {

	// SetClientID sets the client id with the new id.
	SetClientID(old ID, new_ ID) error

	UpdateClient(id ID, info *ClientSecrets) error

	// ExitClient exits the client with the given id.
	ExitClient(id ID) error

	// EnqueueMessage enqueues the message to the client with the given id.
	EnqueueMessage(id ID, message *messages.GlideMessage) error
}

Gateway is the basic and common interface for all gate implementations. As the basic gate, it is used to provide a common gate interface for other modules to interact with the gate.

type ID

type ID string

ID is used to identify the client, the ID is consist of multiple parts, some of them are optional. The ID is constructed by concatenating the parts with a '_' separator, and the parts are:

  • The gateway id (optional): the string id of the gateway that the client is connected to.
  • The client id (required): the string id of the client, it is unique for user.
  • if the client is temporary, this id is a string generated by the gateway and start with `tmp`.
  • The client type (optional): the int type of the client, like 'web', 'mobile', 'desktop', etc.

func GenTempID added in v1.1.0

func GenTempID(gateID string) (ID, error)

GenTempID generates a temporary ID. ID implementation is a UUID v4, which is generated by random number generator.

func NewID

func NewID(gate string, uid string, device string) ID

NewID creates a new ID from the given user id, gateway id and client type.

func NewID2

func NewID2(uid string) ID

NewID2 creates a new ID from the given user id, use the empty gateway id and the empty client type.

func (*ID) Device

func (i *ID) Device() string

Device returns the device type of the client, if the client device type is not set, it returns "".

func (*ID) Equals added in v1.5.0

func (i *ID) Equals(other ID) bool

func (*ID) Gateway

func (i *ID) Gateway() string

Gateway returns the gateway id of the client, if not set, it returns an empty string.

func (*ID) IsTemp

func (i *ID) IsTemp() bool

IsTemp returns true if the ID is a temporary.

func (*ID) SetDevice added in v1.2.3

func (i *ID) SetDevice(device string) bool

SetDevice sets the device type of the client.

func (*ID) SetGateway added in v1.2.3

func (i *ID) SetGateway(gateway string) bool

SetGateway sets the gateway part of the ID.

func (*ID) UID

func (i *ID) UID() string

UID returns the user id of the client, if the client is temporary, it returns "".

type Impl added in v1.5.0

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

func NewServer added in v1.5.0

func NewServer(options *Options) (*Impl, error)

func (*Impl) AddClient added in v1.5.0

func (c *Impl) AddClient(cs Client)

func (*Impl) EnqueueMessage added in v1.5.0

func (c *Impl) EnqueueMessage(id ID, msg *messages.GlideMessage) error

EnqueueMessage to the client with the specified id.

func (*Impl) ExitClient added in v1.5.0

func (c *Impl) ExitClient(id ID) error

ExitClient close the client with the specified id. If the client is not exist, return errClientNotExist.

func (*Impl) GetAll added in v1.5.0

func (c *Impl) GetAll() map[ID]Info

GetAll returns all clients in the gateway.

func (*Impl) GetClient added in v1.5.0

func (c *Impl) GetClient(id ID) Client

GetClient returns the client with specified id

func (*Impl) SetClientID added in v1.5.0

func (c *Impl) SetClientID(oldID, newID ID) error

SetClientID replace the oldID with newID of the client. If the oldID is not exist, return errClientNotExist. If the newID is existed, return errClientAlreadyExist.

func (*Impl) SetMessageHandler added in v1.5.0

func (c *Impl) SetMessageHandler(h MessageHandler)

func (*Impl) UpdateClient added in v1.5.0

func (c *Impl) UpdateClient(id ID, info *ClientSecrets) error

type Info

type Info struct {

	// ID is the unique identifier for the client.
	ID ID

	// ConnectionId generated by client, used to identify the client connection.
	ConnectionId string

	// Version is the version of the client.
	Version string

	// AliveAt is the time the client was last seen.
	AliveAt int64

	// ConnectionAt is the time the client was connected.
	ConnectionAt int64

	// Gateway is the name of the gateway the client is connected to.
	Gateway string

	// CliAddr is the address of the client.
	CliAddr string
}

Info represents a client's information.

type MessageHandler

type MessageHandler func(cliInfo *Info, message *messages.GlideMessage)

MessageHandler used to handle messages from the gate.

type MessageInterceptor added in v1.5.0

type MessageInterceptor = func(dc DefaultClient, msg *messages.GlideMessage) bool

type MessageReader added in v1.5.0

type MessageReader interface {

	// Read 阻塞读取, 会阻塞当前协程
	Read(conn conn.Connection) (*messages.GlideMessage, error)

	// ReadCh 返回两个管道, 第一个用于读取内容, 第二个用于发送停止读取, 停止读取时切记要发送停止信号
	ReadCh(conn conn.Connection) (<-chan *readerRes, chan<- interface{})
}

MessageReader 表示一个从连接中(Connection)读取消息的读取者, 可以用于定义如何从连接中读取并解析消息.

type Options added in v1.5.0

type Options struct {
	// ID is the gateway id.
	ID string
	// SecretKey is the secret key used to encrypt and decrypt authentication token.
	SecretKey string
	// MaxMessageConcurrency is the max message concurrency.
	MaxMessageConcurrency int
}

type Server

type Server interface {
	Gateway

	// SetMessageHandler sets the client message handler.
	SetMessageHandler(h MessageHandler)

	// HandleConnection handles the new client connection and returns the random and temporary id set for the connection.
	HandleConnection(c conn.Connection) ID

	Run() error
}

Server is the interface for the gateway server, which is used to handle and manager client connections.

type UserClient added in v1.5.0

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

UserClient represent a user conn client.

func (*UserClient) AddMessageInterceptor added in v1.5.0

func (c *UserClient) AddMessageInterceptor(interceptor MessageInterceptor)

func (*UserClient) EnqueueMessage added in v1.5.0

func (c *UserClient) EnqueueMessage(msg *messages.GlideMessage) error

EnqueueMessage enqueue message to client message queue.

func (*UserClient) Exit added in v1.5.0

func (c *UserClient) Exit()

Exit client, note: exit client will not close conn right now, but will close when message chan is empty. It's close read right now, and close write2Conn when all message in queue is sent.

func (*UserClient) GetCredentials added in v1.5.0

func (c *UserClient) GetCredentials() *ClientAuthCredentials

func (*UserClient) GetInfo added in v1.5.0

func (c *UserClient) GetInfo() Info

func (*UserClient) IsRunning added in v1.5.0

func (c *UserClient) IsRunning() bool

IsRunning return true if client is running

func (*UserClient) Run added in v1.5.0

func (c *UserClient) Run()

func (*UserClient) SetCredentials added in v1.5.0

func (c *UserClient) SetCredentials(credentials *ClientAuthCredentials)

func (*UserClient) SetID added in v1.5.0

func (c *UserClient) SetID(id ID)

SetID set client id.

Jump to

Keyboard shortcuts

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