net

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2023 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ShaCommand   = 1
	FastAuthFail = 4
)
View Source
const (
	// MaxPayloadLen is the max packet payload length.
	MaxPayloadLen = 1<<24 - 1
)

Variables

View Source
var (
	ErrExpectSSLRequest = errors.New("expect a SSLRequest packet")
	ErrReadConn         = errors.New("failed to read the connection")
	ErrWriteConn        = errors.New("failed to write the connection")
	ErrFlushConn        = errors.New("failed to flush the connection")
	ErrCloseConn        = errors.New("failed to close the connection")
	ErrHandshakeTLS     = errors.New("failed to complete tls handshake")
)
View Source
var (
	ServerVersion = mysql.ServerVersion
	Collation     = uint8(mysql.DefaultCollationID)
	ConnID        = 100
	Status        = mysql.ServerStatusAutocommit
)
View Source
var (
	ErrSaltNotLongEnough = errors.New("salt is not long enough")
)

Functions

func DumpLengthEncodedInt

func DumpLengthEncodedInt(buffer []byte, n uint64) []byte

func DumpLengthEncodedString

func DumpLengthEncodedString(buffer []byte, bytes []byte) []byte

DumpLengthEncodedString dumps string<int>.

func DumpUint16

func DumpUint16(buffer []byte, n uint16) []byte

func DumpUint32

func DumpUint32(buffer []byte, n uint32) []byte

func IsDisconnectError

func IsDisconnectError(err error) bool

IsDisconnectError returns whether the error is caused by peer disconnection.

func IsEOFPacket

func IsEOFPacket(data []byte) bool

IsEOFPacket returns true if it's an EOF packet.

func IsErrorPacket

func IsErrorPacket(data []byte) bool

IsErrorPacket returns true if it's an error packet.

func IsOKPacket

func IsOKPacket(data []byte) bool

IsOKPacket returns true if it's an OK packet (but not ResultSet OK).

func IsResultSetOKPacket

func IsResultSetOKPacket(data []byte) bool

IsResultSetOKPacket returns true if it's an OK packet after the result set when CLIENT_DEPRECATE_EOF is enabled. A row packet may also begin with 0xfe, so we need to judge it with the packet length. See https://mariadb.com/kb/en/result-set-packets/

func MakeChangeUser

func MakeChangeUser(username, db, authPlugin string, authData []byte) []byte

MakeChangeUser creates the data of COM_CHANGE_USER. It's only used for testing.

func MakeHandshakeResponse

func MakeHandshakeResponse(resp *HandshakeResp) []byte

func ParseChangeUser

func ParseChangeUser(data []byte) (username, db string)

ParseChangeUser parses the data of COM_CHANGE_USER.

func ParseErrorPacket

func ParseErrorPacket(data []byte) error

ParseErrorPacket transforms an error packet into a MyError object.

func ParseLengthEncodedBytes

func ParseLengthEncodedBytes(b []byte) ([]byte, bool, int, error)

func ParseLengthEncodedInt

func ParseLengthEncodedInt(b []byte) (num uint64, isNull bool, n int)

func ParseNullTermString

func ParseNullTermString(b []byte) (str []byte, remain []byte)

func ParseOKPacket

func ParseOKPacket(data []byte) *gomysql.Result

ParseOKPacket transforms an OK packet into a Result object.

func ReadServerVersion

func ReadServerVersion(conn net.Conn) (string, error)

ReadServerVersion only reads server version.

func WithProxy

func WithProxy(pi *PacketIO)

func WithRemoteAddr

func WithRemoteAddr(readdr string, addr net.Addr) func(pi *PacketIO)

func WithWrapError

func WithWrapError(err error) func(pi *PacketIO)

func WriteServerVersion

func WriteServerVersion(conn net.Conn, serverVersion string) error

WriteServerVersion only writes server version. It's only used for testing.

Types

type Capability

type Capability uint32
const (
	ClientLongPassword Capability = 1 << iota
	ClientFoundRows
	ClientLongFlag
	ClientConnectWithDB
	ClientNoSchema
	ClientCompress
	ClientODBC
	ClientLocalFiles
	ClientIgnoreSpace
	ClientProtocol41
	ClientInteractive
	ClientSSL
	ClientIgnoreSigpipe
	ClientTransactions
	ClientReserved
	ClientSecureConnection
	ClientMultiStatements
	ClientMultiResults
	ClientPSMultiResults
	ClientPluginAuth
	ClientConnectAttrs
	ClientPluginAuthLenencClientData
	ClientCanHandleExpiredPasswords
	ClientSessionTrack
	ClientDeprecateEOF
	ClientOptionalResultsetMetadata
	ClientZstdCompressionAlgorithm
	ClientQueryAttributes
	MultiFactorAuthentication
	ClientCapabilityExtension
	ClientSSLVerifyServerCert
	ClientRememberOptions
)

Capability flags. Ref https://dev.mysql.com/doc/dev/mysql-server/latest/group__group__cs__capabilities__flags.html.

func ParseInitialHandshake

func ParseInitialHandshake(data []byte) (Capability, string)

ParseInitialHandshake parses the initial handshake received from the server.

func (*Capability) MarshalText

func (f *Capability) MarshalText() ([]byte, error)

func (Capability) String

func (f Capability) String() string

func (Capability) Uint32

func (f Capability) Uint32() uint32

func (*Capability) UnmarshalText

func (f *Capability) UnmarshalText(o []byte) error

type Command

type Command byte
const (
	ComSleep Command = iota
	ComQuit
	ComInitDB
	ComQuery
	ComFieldList
	ComCreateDB
	ComDropDB
	ComRefresh
	ComDeprecated1
	ComStatistics
	ComProcessInfo
	ComConnect
	ComProcessKill
	ComDebug
	ComPing
	ComTime
	ComDelayedInsert
	ComChangeUser
	ComBinlogDump
	ComTableDump
	ComConnectOut
	ComRegisterSlave
	ComStmtPrepare
	ComStmtExecute
	ComStmtSendLongData
	ComStmtClose
	ComStmtReset
	ComSetOption
	ComStmtFetch
	ComDaemon
	ComBinlogDumpGtid
	ComResetConnection
	ComEnd // Not a real command
)

Command information. Ref https://dev.mysql.com/doc/dev/mysql-server/latest/my__command_8h.html#ae2ff1badf13d2b8099af8b47831281e1.

func (Command) Byte

func (f Command) Byte() byte

func (*Command) MarshalText

func (f *Command) MarshalText() ([]byte, error)

func (Command) String

func (f Command) String() string

func (*Command) UnmarshalText

func (f *Command) UnmarshalText(o []byte) error

type HandshakeResp

type HandshakeResp struct {
	Attrs      map[string]string
	User       string
	DB         string
	AuthPlugin string
	AuthData   []byte
	Capability uint32
	Collation  uint8
}

HandshakeResp indicates the response read from the client.

func ParseHandshakeResponse

func ParseHandshakeResponse(data []byte) (*HandshakeResp, error)

type PacketIO

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

PacketIO is a helper to read and write sql and proxy protocol.

func NewPacketIO

func NewPacketIO(conn net.Conn, lg *zap.Logger, opts ...PacketIOption) *PacketIO

func (*PacketIO) ApplyOpts

func (p *PacketIO) ApplyOpts(opts ...PacketIOption)

func (*PacketIO) ClientTLSHandshake

func (p *PacketIO) ClientTLSHandshake(tlsConfig *tls.Config) error

func (*PacketIO) Close

func (p *PacketIO) Close() error

func (*PacketIO) Flush

func (p *PacketIO) Flush() error

func (*PacketIO) GetSequence

func (p *PacketIO) GetSequence() uint8

GetSequence is used in tests to assert that the sequences on the client and server are equal.

func (*PacketIO) GracefulClose

func (p *PacketIO) GracefulClose() error

func (*PacketIO) InBytes

func (p *PacketIO) InBytes() uint64

func (*PacketIO) IsPeerActive

func (p *PacketIO) IsPeerActive() bool

IsPeerActive checks if the peer connection is still active. This function cannot be called concurrently with other functions of PacketIO. This function normally costs 1ms, so don't call it too frequently. This function may incorrectly return true if the system is extremely slow.

func (*PacketIO) LastKeepAlive

func (p *PacketIO) LastKeepAlive() config.KeepAlive

LastKeepAlive is used for test.

func (*PacketIO) LocalAddr

func (p *PacketIO) LocalAddr() net.Addr

func (*PacketIO) OutBytes

func (p *PacketIO) OutBytes() uint64

func (*PacketIO) Proxy

func (p *PacketIO) Proxy() *proxyprotocol.Proxy

Proxy returned parsed proxy header from clients if any.

func (*PacketIO) ReadPacket

func (p *PacketIO) ReadPacket() (data []byte, err error)

ReadPacket reads data and removes the header

func (*PacketIO) ReadSSLRequestOrHandshakeResp

func (p *PacketIO) ReadSSLRequestOrHandshakeResp() (pkt []byte, isSSL bool, err error)

func (*PacketIO) RemoteAddr

func (p *PacketIO) RemoteAddr() net.Addr

func (*PacketIO) ResetSequence

func (p *PacketIO) ResetSequence()

func (*PacketIO) ServerTLSHandshake

func (p *PacketIO) ServerTLSHandshake(tlsConfig *tls.Config) (tls.ConnectionState, error)

func (*PacketIO) SetKeepalive

func (p *PacketIO) SetKeepalive(cfg config.KeepAlive) error

func (*PacketIO) TLSConnectionState

func (p *PacketIO) TLSConnectionState() tls.ConnectionState

func (*PacketIO) WriteEOFPacket

func (p *PacketIO) WriteEOFPacket(status uint16) error

WriteEOFPacket writes an EOF packet. It's only for testing.

func (*PacketIO) WriteErrPacket

func (p *PacketIO) WriteErrPacket(merr *mysql.SQLError) error

WriteErrPacket writes an Error packet.

func (*PacketIO) WriteInitialHandshake

func (p *PacketIO) WriteInitialHandshake(capability Capability, salt []byte, authPlugin string, serverVersion string) error

WriteInitialHandshake writes an initial handshake as a server. It's used for tenant-aware routing and testing.

func (*PacketIO) WriteOKPacket

func (p *PacketIO) WriteOKPacket(status uint16, header byte) error

WriteOKPacket writes an OK packet. It's only for testing.

func (*PacketIO) WritePacket

func (p *PacketIO) WritePacket(data []byte, flush bool) (err error)

WritePacket writes data without a header

func (*PacketIO) WriteProxyV2

func (p *PacketIO) WriteProxyV2(m *proxyprotocol.Proxy) error

WriteProxyV2 should only be called at the beginning of connection, before any write operations.

func (*PacketIO) WriteShaCommand

func (p *PacketIO) WriteShaCommand() error

func (*PacketIO) WriteSwitchRequest

func (p *PacketIO) WriteSwitchRequest(authPlugin string, salt []byte) error

WriteSwitchRequest writes a switch request to the client. It's only for testing.

func (*PacketIO) WriteUserError added in v0.1.1

func (p *PacketIO) WriteUserError(err error)

WriteUserError writes an unknown error to the client.

type PacketIOption

type PacketIOption = func(*PacketIO)

type UserError added in v0.1.1

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

UserError is returned to the client. err is used to log and userMsg is used to report to the user.

func WrapUserError added in v0.1.1

func WrapUserError(err error, userMsg string) *UserError

func (*UserError) Error added in v0.1.1

func (ue *UserError) Error() string

func (*UserError) Unwrap added in v0.1.1

func (ue *UserError) Unwrap() error

func (*UserError) UserMsg added in v0.1.1

func (ue *UserError) UserMsg() string

Jump to

Keyboard shortcuts

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