exchange

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2026 License: Apache-2.0 Imports: 16 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	V1          int8 = 1
	WebsocketV1 int8 = 2
	WebsocketV2 int8 = 3
)
View Source
var (
	CloseError = errors.New("close error")
)
View Source
var ErrNeedMoreData = errors.New("need more data")

Functions

func Encoder

func Encoder(data *Protocol) []byte

Encoder

@Description: encoder.
@param data
@return []byte

func GetByteLen

func GetByteLen(lenByte []byte) int

func Parse

func Parse[T any](data []byte) (*T, error)

Parse

@Description: parse
@receiver receiver
@param value
@return error

Types

type BucketRead

type BucketRead func(p *Protocol, rw io.ReadWriteCloser, ctx context.Context) error

BucketRead is a function type that takes a pointer to a Protocol struct and an io.ReadWriteCloser as parameters

type BytesBucket

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

func NewBytesBucket

func NewBytesBucket(rw io.ReadWriteCloser,
	headerLength int,
	ctx context.Context) *BytesBucket

func (*BytesBucket) AddHandler

func (m *BytesBucket) AddHandler(cmd any, bucket BytesBucketRead)

AddHandler This function adds a handler to the BytesBucket struct

func (*BytesBucket) Close

func (m *BytesBucket) Close()

Close the bucket

func (*BytesBucket) Done

func (m *BytesBucket) Done() <-chan struct{}

func (*BytesBucket) Push

func (m *BytesBucket) Push(bytes []byte) error

func (*BytesBucket) SetReadFunction

func (m *BytesBucket) SetReadFunction(fun ReadFunction)

SetReadFunction This function sets the read function for the BytesBucket struct

type BytesBucketRead

type BytesBucketRead func(heads, bodies []byte, rw io.ReadWriteCloser, ctx context.Context) error

type ClientWorkConnReq

type ClientWorkConnReq struct {
	ProxyId    string `json:"proxyId"`
	HttpId     string `json:"httpId"`
	TunnelPort int    `json:"tunnelPort"`
	ServerId   string `json:"serverId"`
}

func (ClientWorkConnReq) Cmd

func (r ClientWorkConnReq) Cmd() Cmd

type Cmd

type Cmd int8
const (
	// Heart Client request ping.
	Heart Cmd = 1

	// Register : Register tunnel port.
	Register Cmd = 2

	// Communication communication connection.
	Communication Cmd = 3

	//LoginTunnel Query tunnel configs.
	LoginTunnel Cmd = 4

	// OpenTunnel Open Tunnel
	OpenTunnel Cmd = 5

	// WorkerConnReq ConnWork
	WorkerConnReq Cmd = 6

	UdpRegister Cmd = 7

	ClientWorkerConnReq Cmd = 8

	RegisterVisitor = 9
)

InBound cmd

type Heartbeat

type Heartbeat struct {
	Value      string   `json:"value"`
	StartTime  int64    `json:"start_time"`
	ServerTime int64    `json:"server_time"`
	ProxyId    []string `json:"proxy_id"`
}

Heartbeat @Description: Ping InBound info. This is empty request,server use Cmd discern.

func (*Heartbeat) Cmd

func (p *Heartbeat) Cmd() Cmd

Cmd

@Description: getCmd.
@receiver p
@return Cmd

type InBound

type InBound interface {
	//
	// Cmd
	//  @Description: get cmd.
	//  @return Cmd
	//
	Cmd() Cmd
}

InBound @Description: request.

type LoginReq

type LoginReq struct {
	Token string `json:"token"`
}

func (*LoginReq) Cmd

func (r *LoginReq) Cmd() Cmd

Cmd

@Description: getCmd
@receiver r
@return Cmd

func (*LoginReq) QueryTunnelResp

func (r *LoginReq) QueryTunnelResp() Cmd

type LoginResp

type LoginResp struct {
	TunnelHost string `json:"tunnel_host"`

	TunnelPort int `json:"tunnel_port"`

	UnId string `json:"un_id"`

	Tunnels []*configs.ClientTunnelConfig `json:"tunnels"`
}

LoginResp @Description: Resp.

type MessageBucket

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

MessageBucket is a struct that contains a BytesBucket, a channel for pushing Protocol structs, a map of Cmd to BucketRead functions, and a default BucketRead function

func NewMessageBucket

func NewMessageBucket(rw io.ReadWriteCloser, ctx context.Context) *MessageBucket

NewMessageBucket creates a new MessageBucket struct and returns a pointer to it

func (*MessageBucket) AddHandler

func (m *MessageBucket) AddHandler(cmd Cmd, bucket BucketRead)

AddHandler is a function that takes a Cmd and a BucketRead function and adds them to the bucketHandler map

func (*MessageBucket) Close

func (m *MessageBucket) Close()

Close the bucket

func (*MessageBucket) Done

func (m *MessageBucket) Done() <-chan struct{}

Done is a function that returns a channel that is closed when the bytesBucket is done

func (*MessageBucket) Push

func (m *MessageBucket) Push(message *Protocol) error

Push is a function that takes a pointer to a Protocol struct and pushes it to the bucketPush channel

func (*MessageBucket) PushWitchRequest

func (m *MessageBucket) PushWitchRequest(message InBound) error

func (*MessageBucket) Run

func (m *MessageBucket) Run()

Run is a function that adds a handler to the bytesBucket and starts the bytesBucket

func (*MessageBucket) SetDefaultHandler

func (m *MessageBucket) SetDefaultHandler(def BucketRead)

SetDefaultHandler This function sets the default handler for the MessageBucket struct

func (*MessageBucket) SetReaderFunction

func (m *MessageBucket) SetReaderFunction(fun ReadFunction)

func (*MessageBucket) SyncPushWitchProtocol

func (m *MessageBucket) SyncPushWitchProtocol(message *Protocol) (*Protocol, error)

SyncPushWitchProtocol This function takes a pointer to a MessageBucket and a pointer to a Protocol as parameters and returns a pointer to a Protocol and an error.

func (*MessageBucket) SyncPushWithRequest

func (m *MessageBucket) SyncPushWithRequest(message InBound) (*Protocol, error)

type OpenTunnelReq

type OpenTunnelReq struct {
	ProxyId string `json:"proxy_id"`
	UnId    string `json:"unId"`
}

func (OpenTunnelReq) Cmd

func (o OpenTunnelReq) Cmd() Cmd

type OpenTunnelResp

type OpenTunnelResp struct {
	RemotePort  int    `json:"remotePort"`
	Destination string `json:"destination"`
	UnId        string `json:"unId"`
}

func (OpenTunnelResp) Cmd

func (o OpenTunnelResp) Cmd() Cmd

type PType

type PType int8
const (
	// REQUEST InBound.
	REQUEST PType = 0

	// RESPONSE Receiver.
	RESPONSE PType = 1
)

type Protocol

type Protocol struct {
	Data []byte

	//reqId.
	ReqId int64

	//InBound cmd.
	Cmd Cmd

	// 0 request 1 response.
	PType PType

	//responseCode.
	//request never is zero.
	RspCode RspCode

	//message.
	RspMsg string
}

Protocol @Description: Internal request struct.

func Decoder

func Decoder(reader io.Reader) (*Protocol, error)

func Decoder2

func Decoder2(rb gnet.Conn) (int, *Protocol, error)

func GetBody

func GetBody(bodies []byte) (*Protocol, error)

func NewRequest

func NewRequest(data InBound) (*Protocol, error)

NewRequest This function creates a new Protocol object from an InBound object

func NewResponse

func NewResponse(cmd Cmd, reqId int64) (*Protocol, error)

NewResponse This function creates a new Protocol object with the given command and request ID

func SyncWriteByProtocol

func SyncWriteByProtocol(message *Protocol, timeout time.Duration, writer func(protocol *Protocol) error) (*Protocol, error)

func SyncWriteInBound

func SyncWriteInBound(message InBound, timeout time.Duration, writer func(protocol *Protocol) error) (*Protocol, error)

func (*Protocol) Bytes

func (receiver *Protocol) Bytes() []byte

Bytes

@Description: to bytes.
@receiver receiver
@return []byte

func (*Protocol) IsSuccess

func (receiver *Protocol) IsSuccess() bool

IsSuccess This function checks if the Protocol receiver is successful

type ReadFunction

type ReadFunction func(sch io.ReadWriteCloser) error

type RegisterReqAndRsp

type RegisterReqAndRsp struct {
	TunnelType lang.TunnelType `json:"tunnel_type"`

	// TunnelPort is port.
	TunnelPort int `json:"tunnel_port"`

	//request id.
	BindId string `json:"bind_id"`

	//proxy id. only httpx or http.
	HttpId string `json:"http_id"`

	//proxyId.
	ProxyId string `json:"proxyId"`

	//ServerId
	ServerId string `json:"serverId"`

	Open bool `json:"open"`
}

RegisterReqAndRsp @Description: Register Info.

func (*RegisterReqAndRsp) Cmd

func (r *RegisterReqAndRsp) Cmd() Cmd

func (*RegisterReqAndRsp) GetBindId

func (r *RegisterReqAndRsp) GetBindId() string

func (*RegisterReqAndRsp) GetHttpId

func (r *RegisterReqAndRsp) GetHttpId() string

func (*RegisterReqAndRsp) GetProxyId

func (r *RegisterReqAndRsp) GetProxyId() string

func (*RegisterReqAndRsp) GetTunnelPort

func (r *RegisterReqAndRsp) GetTunnelPort() int

func (*RegisterReqAndRsp) GetTunnelType

func (r *RegisterReqAndRsp) GetTunnelType() lang.TunnelType

func (*RegisterReqAndRsp) IsOpen

func (r *RegisterReqAndRsp) IsOpen() bool

func (*RegisterReqAndRsp) SetServerId

func (r *RegisterReqAndRsp) SetServerId(serverId string)

type RequestTracker

type RequestTracker struct {
	// contains filtered or unexported fields
}
var Tracker *RequestTracker

func (*RequestTracker) Complete

func (rt *RequestTracker) Complete(resp *Protocol) bool

Complete delivers a response and removes the tracker entry.

func (*RequestTracker) Register

func (rt *RequestTracker) Register(reqId int64) chan *Protocol

Register

@Description: Register
@receiver rt
@param reqId
@return chan

func (*RequestTracker) Remove

func (rt *RequestTracker) Remove(reqId int64)

Remove explicitly removes a request from tracker.

type RspCode

type RspCode int16
const (
	// RspSuccess success.
	RspSuccess RspCode = 0

	RspFail = 101
)

RspSuccess RspCode.

type TRegister

type TRegister interface {
	Cmd() Cmd

	GetTunnelType() lang.TunnelType

	GetProxyId() string

	GetHttpId() string

	GetTunnelPort() int

	GetBindId() string

	IsOpen() bool

	SetServerId(serverId string)
}

type TunnelBucket

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

func NewTunnelBucket

func NewTunnelBucket(rw io.ReadWriteCloser,
	ctx context.Context) *TunnelBucket

NewTunnelBucket creates a new TunnelBucket instance with a given ReadWriteCloser and context It initializes the TunnelBucket with a BytesBucket that uses the provided ReadWriteCloser, a buffer size of 4, and the given context

func (*TunnelBucket) DefaultRead

func (t *TunnelBucket) DefaultRead(defaultRead TunnelBucketRead)

func (*TunnelBucket) Done

func (t *TunnelBucket) Done() <-chan struct{}

func (*TunnelBucket) Push

func (t *TunnelBucket) Push(data []byte, read TunnelBucketRead) error

func (*TunnelBucket) Run

func (t *TunnelBucket) Run() *TunnelBucket

Run is a method of TunnelBucket that starts the tunnel's operation

type TunnelBucketRead

type TunnelBucketRead func(p *TunnelProtocol)

type TunnelProtocol

type TunnelProtocol struct {
	Len     int32
	ReqId   int64
	Ver     int8
	AttrLen int32
	Attr    []byte
	Data    []byte
}

func NewTunnelRead

func NewTunnelRead() *TunnelProtocol

func NewTunnelWebsocketWriterV1

func NewTunnelWebsocketWriterV1(data []byte, attr []byte, reqId int64) *TunnelProtocol

func NewTunnelWebsocketWriterV2

func NewTunnelWebsocketWriterV2(data []byte, attr []byte, reqId int64) *TunnelProtocol

func NewTunnelWriter

func NewTunnelWriter(data []byte, reqId int64) *TunnelProtocol

NewTunnelWriter creates a new instance of TunnelProtocol with the provided data It initializes the protocol version to v1 and increments the request ID counter

func (*TunnelProtocol) Decode

func (t *TunnelProtocol) Decode(data []byte)

func (*TunnelProtocol) Encode

func (t *TunnelProtocol) Encode() []byte

func (*TunnelProtocol) Read

func (t *TunnelProtocol) Read(r io.Reader) error

func (*TunnelProtocol) Writer

func (t *TunnelProtocol) Writer(w io.Writer) error

Writer is a method of TunnelProtocol that handles writing data to the given io.Writer It takes a writer as parameter and returns an error if any occurs during the write operation

type UdpPackage

type UdpPackage struct {
	Data []byte `json:"data"`

	LocalAddress *net.UDPAddr `json:"local_address"`

	RemoteAddress *net.UDPAddr `json:"remote_address"`
}

func NewUdpPackage

func NewUdpPackage(data []byte, localAddr, remoteAddr *net.UDPAddr) *UdpPackage

func (*UdpPackage) GetData

func (p *UdpPackage) GetData() []byte

func (*UdpPackage) GetLocalAddress

func (p *UdpPackage) GetLocalAddress() *net.UDPAddr

func (*UdpPackage) GetRemoteAddress

func (p *UdpPackage) GetRemoteAddress() *net.UDPAddr

type UdpRegisterReqAndRsp

type UdpRegisterReqAndRsp struct {
	*RegisterReqAndRsp
	RemoteAddress string `json:"remote_address"`
}

func (*UdpRegisterReqAndRsp) Cmd

func (r *UdpRegisterReqAndRsp) Cmd() Cmd

type VisitorInfo added in v0.4.0

type VisitorInfo struct {
	Id string

	Token string
}

type WorkConnReq

type WorkConnReq struct {
	ProxyId string `json:"proxy_id"`
}

func (*WorkConnReq) Cmd

func (r *WorkConnReq) Cmd() Cmd

Jump to

Keyboard shortcuts

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