proxy

package
v0.0.0-...-6ea4ef9 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2021 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CopyRawBytesFlag byte = 1           // 0000_0001
	HeartBeatFlag    byte = 1 << 5      // 0010_0000
	RpcRequestFlag   byte = 1 << 6      // 0100_0000
	RpcOnewayFlag    byte = 1<<6 | 1<<7 // 1100_0000
)
View Source
const (
	ResponseStatusSuccess                    uint16 = 0  // 0x00 response status
	ResponseStatusError                      uint16 = 1  // 0x01
	ResponseStatusServerException            uint16 = 2  // 0x02
	ResponseStatusUnknown                    uint16 = 3  // 0x03
	ResponseStatusServerThreadPoolBusy       uint16 = 4  // 0x04
	ResponseStatusErrorComm                  uint16 = 5  // 0x05
	ResponseStatusNoProcessor                uint16 = 6  // 0x06
	ResponseStatusTimeout                    uint16 = 7  // 0x07
	ResponseStatusClientSendError            uint16 = 8  // 0x08
	ResponseStatusCodecException             uint16 = 9  // 0x09
	ResponseStatusConnectionClosed           uint16 = 16 // 0x10
	ResponseStatusServerSerialException      uint16 = 17 // 0x11
	ResponseStatusServerDeserializeException uint16 = 18 // 0x12

	CodecExceptionCode       = 0
	UnknownCode              = 2
	DeserializeExceptionCode = 3
	SuccessCode              = 200
	PermissionDeniedCode     = 403
	RouterUnavailableCode    = 404
	InternalErrorCode        = 500
	NoHealthUpstreamCode     = 502
	UpstreamOverFlowCode     = 503
	TimeoutExceptionCode     = 504
	LimitExceededCode        = 509
)
View Source
const (
	RootContextID uint32 = 1 // TODO: support multiple rootContext
)

Variables

View Source
var BufferTooLarge = errors.New("wasm plugin Buffer: too large")
View Source
var IndexOutOfBound = errors.New("wasm plugin Buffer: index out of bound")

Functions

func DecodeHeader

func DecodeHeader(bytes []byte, h Header) (err error)

func DecodeMap

func DecodeMap(buf []byte) map[string]string

DecodeMap decode map from byte slice see EncodeMap for more detail.

func EncodeHeader

func EncodeHeader(buf Buffer, h Header)

func EncodeMap

func EncodeMap(pairs map[string]string) []byte

EncodeMap encode map to bytes encoded bytes format: pairs number + all key/value length + all key/value bytes

eg: {"key1": "value1", "hello": "world"} 2(pairs number) + 4(key1 length) + 6(value1 length) => { "key1": "value1" } length

  • 5(hello length) + 5(world length) => { "hello": "world" } length
  • key1 bytes + nil byte + value1 bytes + nil byte => { "key1": "value1" } bytes
  • hello bytes + nil byte + world bytes + nil byte => { "hello": "world" } bytes

func EncodePropertyPath

func EncodePropertyPath(path []string) []byte

func GetEncodeHeaderLength

func GetEncodeHeaderLength(h Header) int

func Mapping

func Mapping(httpStatusCode int32) uint32

func SetNewFilterContext

func SetNewFilterContext(f func(rootContextID, contextID uint32) FilterContext)

func SetNewProtocolContext

func SetNewProtocolContext(f func(rootContextID, contextID uint32) ProtocolContext)

func SetNewRootContext

func SetNewRootContext(f func(contextID uint32) RootContext)

func SetNewStreamContext

func SetNewStreamContext(f func(rootContextID, contextID uint32) StreamContext)

func SetTickPeriodMilliSeconds

func SetTickPeriodMilliSeconds(millSec uint32) error

func VMStateGetActiveContextID

func VMStateGetActiveContextID() uint32

func VMStateReset

func VMStateReset()

Types

type Buffer

type Buffer interface {
	Bytes() []byte
	Len() int
	Cap() int
	Pos() int
	Move(int)
	Grow(n int)
	Reset()
	Peek(n int) []byte
	Drain(len int)
	Mark()
	ResetMark()
	ByteOrder(order Order) Buffer

	WriteByte(value byte) error
	WriteUint16(value uint16) error
	WriteUint32(value uint32) error
	WriteUint(value uint) error
	WriteUint64(value uint64) error
	WriteInt16(value int16) error
	WriteInt32(value int32) error
	WriteInt(value int) error
	WriteInt64(value int64) error

	PutByte(index int, value byte) error
	PutUint16(index int, value uint16) error
	PutUint32(index int, value uint32) error
	PutUint(index int, value uint) error
	PutUint64(index int, value uint64) error
	PutInt16(index int, value int16) error
	PutInt32(index int, value int32) error
	PutInt(index int, value int) error
	PutInt64(index int, value int64) error

	Write(p []byte) (n int, err error)
	WriteString(s string) (n int, err error)

	ReadByte() (byte, error)
	ReadUint16() (uint16, error)
	ReadUint32() (uint32, error)
	ReadUint() (uint, error)
	ReadUint64() (uint64, error)
	ReadInt16() (int16, error)
	ReadInt32() (int32, error)
	ReadInt() (int, error)
	ReadInt64() (int64, error)

	GetByte(index int) (byte, error)
	GetUint16(index int) (uint16, error)
	GetUint32(index int) (uint32, error)
	GetUint(index int) (uint, error)
	GetUint64(index int) (uint64, error)
	GetInt16(index int) (int16, error)
	GetInt32(index int) (int32, error)
	GetInt(index int) (int, error)
	GetInt64(index int) (int64, error)
}

func AllocateBuffer

func AllocateBuffer() Buffer

func NewBuffer

func NewBuffer(size int) Buffer

func WrapBuffer

func WrapBuffer(buf []byte) Buffer

type Codec

type Codec interface {
	Decode(ctx context.Context, data Buffer) (Command, error)
	Encode(ctx context.Context, cmd Command) (Buffer, error)
}

type Command

type Command interface {
	// Header get the data exchange header, maybe return nil.
	GetHeader() Header
	// GetData return the full message buffer, the protocol header is not included
	GetData() Buffer
	// SetData update the full message buffer, the protocol header is not included
	SetData(data Buffer)
	// IsHeartbeat check if the request is a heartbeat request
	IsHeartbeat() bool
	// CommandId get command id
	CommandId() uint64
	// SetCommandId update command id
	// In upstream, because of connection multiplexing,
	// the id of downstream needs to be replaced with id of upstream
	// blog: https://mosn.io/blog/posts/multi-protocol-deep-dive/#%E5%8D%8F%E8%AE%AE%E6%89%A9%E5%B1%95%E6%A1%86%E6%9E%B6
	SetCommandId(id uint64)
}

Command base request or response command

type CommonHeader

type CommonHeader struct {
	Changed bool
	// contains filtered or unexported fields
}

func (*CommonHeader) Add

func (h *CommonHeader) Add(key string, value string)

Add value for given key. Multiple headers with the same key may be added with this function. Use Set for setting a single header for the given key.

func (*CommonHeader) Clone

func (h *CommonHeader) Clone() *CommonHeader

func (*CommonHeader) Del

func (h *CommonHeader) Del(key string)

Del delete pair of specified key

func (*CommonHeader) Get

func (h *CommonHeader) Get(key string) (value string, ok bool)

Get value of key

func (*CommonHeader) Range

func (h *CommonHeader) Range(f func(key, value string) bool)

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

func (*CommonHeader) Set

func (h *CommonHeader) Set(key string, value string)

Set key-value pair in header map, the previous pair will be replaced if exists

func (*CommonHeader) Size

func (h *CommonHeader) Size() int

func (*CommonHeader) String

func (h *CommonHeader) String() string

func (*CommonHeader) ToMap

func (h *CommonHeader) ToMap() map[string]string

type ConfigMap

type ConfigMap Header

func NewConfigMap

func NewConfigMap() ConfigMap

type DefaultFilterContext

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

func (*DefaultFilterContext) OnDownStreamReceived

func (c *DefaultFilterContext) OnDownStreamReceived(headers Header, data Buffer, trailers Header) types.Action

impl FilterContext

func (*DefaultFilterContext) OnFilterStreamDone

func (*DefaultFilterContext) OnFilterStreamDone()

func (*DefaultFilterContext) OnLog

func (*DefaultFilterContext) OnLog()

func (*DefaultFilterContext) OnUpstreamReceived

func (c *DefaultFilterContext) OnUpstreamReceived(headers Header, data Buffer, trailers Header) types.Action

type DefaultOptions

type DefaultOptions struct {
}

func (*DefaultOptions) EnableGenerateRequestID

func (o *DefaultOptions) EnableGenerateRequestID() bool

func (*DefaultOptions) EnableWorkerPool

func (o *DefaultOptions) EnableWorkerPool() bool

func (*DefaultOptions) GenerateRequestID

func (o *DefaultOptions) GenerateRequestID(v *uint64) uint64

func (*DefaultOptions) PoolMode

func (o *DefaultOptions) PoolMode() types.PoolMode

type DefaultProtocolContext

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

func (*DefaultProtocolContext) Codec

func (*DefaultProtocolContext) Codec() Codec

func (*DefaultProtocolContext) Hijacker

func (*DefaultProtocolContext) Hijacker() Hijacker

func (*DefaultProtocolContext) KeepAlive

func (*DefaultProtocolContext) KeepAlive() KeepAlive

func (*DefaultProtocolContext) Name

impl ProtocolContext

func (*DefaultProtocolContext) OnLog

func (*DefaultProtocolContext) OnLog()

func (*DefaultProtocolContext) OnProtocolDone

func (*DefaultProtocolContext) OnProtocolDone()

func (*DefaultProtocolContext) Options

func (*DefaultProtocolContext) Options() Options

type DefaultRootContext

type DefaultRootContext struct{}

func (*DefaultRootContext) OnPluginStart

func (*DefaultRootContext) OnPluginStart(conf ConfigMap) bool

func (*DefaultRootContext) OnTick

func (*DefaultRootContext) OnTick()

impl RootContext

func (*DefaultRootContext) OnVMDone

func (*DefaultRootContext) OnVMDone() bool

func (*DefaultRootContext) OnVMStart

func (*DefaultRootContext) OnVMStart(conf ConfigMap) bool

type DefaultStreamContext

type DefaultStreamContext struct{}

func (*DefaultStreamContext) OnDownstreamClose

func (*DefaultStreamContext) OnDownstreamClose(peerType types.PeerType)

func (*DefaultStreamContext) OnDownstreamData

func (*DefaultStreamContext) OnDownstreamData(buffer Buffer, endOfStream bool) types.Action

impl StreamContext

func (*DefaultStreamContext) OnLog

func (*DefaultStreamContext) OnLog()

func (*DefaultStreamContext) OnNewConnection

func (*DefaultStreamContext) OnNewConnection() types.Action

func (*DefaultStreamContext) OnStreamDone

func (*DefaultStreamContext) OnStreamDone()

func (*DefaultStreamContext) OnUpstreamClose

func (*DefaultStreamContext) OnUpstreamClose(peerType types.PeerType)

func (*DefaultStreamContext) OnUpstreamData

func (*DefaultStreamContext) OnUpstreamData(buffer Buffer, endOfStream bool) types.Action

type FilterContext

type FilterContext interface {
	// OnDownStreamReceived Called when the data requests,
	// The caller should check if the parameter value is nil
	OnDownStreamReceived(headers Header, data Buffer, trailers Header) types.Action
	// OnUpstreamReceived Called when the data responds,
	// The caller should check if the parameter value is nil
	OnUpstreamReceived(headers Header, data Buffer, trailers Header) types.Action
	//// Context Used to save and pass data during a session
	//Context() context.Context
	OnFilterStreamDone()
	OnLog()
}

L7 layer extension

type Header interface {
	// Get value of key
	// If multiple values associated with this key, first one will be returned.
	Get(key string) (string, bool)

	// Set key-value pair in header map, the previous pair will be replaced if exists
	Set(key, value string)

	// Add value for given key.
	// Multiple headers with the same key may be added with this function.
	// Use Set for setting a single header for the given key.
	Add(key, value string)

	// Del delete pair of specified key
	Del(key string)

	// Range calls f sequentially for each key and value present in the map.
	// If f returns false, range stops the iteration.
	Range(f func(key, value string) bool)

	// Size header key value pair count
	Size() int

	ToMap() map[string]string
}

func NewHeader

func NewHeader() Header

type Hijacker

type Hijacker interface {
	// Hijack allows sidecar to hijack requests
	Hijack(request Request, code uint32) Response
}

type HostEmulator

type HostEmulator interface {
	// release emulator resource and reset state
	Done()

	// Root
	StartVM()
	StartPlugin()
	FinishVM()

	GetLogs(level types.LogLevel) []string
	GetTickPeriod() uint32
	Tick()

	// protocol L7 level
	NewProtocolContext() (contextID uint32)
	Decode(contextID uint32, data Buffer) (Command, error)
	Encode(contextID uint32, cmd Command) (Buffer, error)
	// heartbeat
	KeepAlive(contextID uint32, requestId uint64) Request
	ReplyKeepAlive(contextID uint32, request Request) Response
	// hijacker
	Hijack(contextID uint32, request Request, code uint32) Response
	// easy for test only
	CurrentStreamId() uint64
	// finish protocol pipeline
	CompleteProtocolContext(contextID uint32)

	// filter L7 level
	NewFilterContext() (contextID uint32)
	PutRequestHeaders(contextID uint32, headers map[string]string, endOfStream bool)
	PutRequestBody(contextID uint32, body []byte, endOfStream bool)
	PutRequestTrailers(contextID uint32, headers map[string]string)

	GetRequestHeaders(contextID uint32) (headers map[string]string)
	GetRequestBody(contextID uint32) []byte

	PutResponseHeaders(contextID uint32, headers map[string]string, endOfStream bool)
	PutResponseBody(contextID uint32, body []byte, endOfStream bool)
	PutResponseTrailers(contextID uint32, headers map[string]string)

	GetResponseHeaders(contextID uint32) (headers map[string]string)
	GetResponseBody(contextID uint32) []byte

	GetCurrentStreamAction(contextID uint32) types.Action
	CompleteFilterContext(contextID uint32)
}

func NewHostEmulator

func NewHostEmulator(opt *Option) HostEmulator

type HttpCalloutAttribute

type HttpCalloutAttribute struct {
	CalloutID uint32
	Upstream  string
	Headers   map[string]string
	Body      []byte
	Trailers  map[string]string
}

type HttpCalloutCallBack

type HttpCalloutCallBack = func(headers Header, body Buffer)

type HttpCalloutResponse

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

type KeepAlive

type KeepAlive interface {
	KeepAlive(requestId uint64) Request
	ReplyKeepAlive(request Request) Response
}

type LocalHttpResponse

type LocalHttpResponse struct {
	StatusCode       uint32
	StatusCodeDetail string
	Data             []byte
	Headers          map[string]string
	GRPCStatus       int32
}

type Logger

type Logger interface {
	Debug(msg string)
	Debugf(format string, args ...interface{})
	Info(msg string)
	Infof(format string, args ...interface{})
	Warn(msg string)
	Warnf(format string, args ...interface{})
	Error(msg string)
	Errorf(format string, args ...interface{})
	Fatal(msg string)
	Fatalf(format string, args ...interface{})
}
var Log Logger

func NewLogger

func NewLogger() Logger

func SetLogger

func SetLogger(log Logger) Logger

type Node

type Node struct {
	Name    string      // extension name
	Context interface{} // extension context
	Order   int         // registered priority

	Next *Node // next node
	// contains filtered or unexported fields
}

Node pub or sub host metadata, Keep the structure as simple as possible.

type Option

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

func NewEmulatorOption

func NewEmulatorOption() *Option

func (*Option) WithNewHttpContext

func (o *Option) WithNewHttpContext(f func(uint32, uint32) FilterContext) *Option

func (*Option) WithNewProtocolContext

func (o *Option) WithNewProtocolContext(f func(uint32, uint32) ProtocolContext) *Option

func (*Option) WithNewRootContext

func (o *Option) WithNewRootContext(f func(uint32) RootContext) *Option

func (*Option) WithNewStreamContext

func (o *Option) WithNewStreamContext(f func(uint32, uint32) StreamContext) *Option

func (*Option) WithPluginConfiguration

func (o *Option) WithPluginConfiguration(data ConfigMap) *Option

func (*Option) WithVMConfiguration

func (o *Option) WithVMConfiguration(data ConfigMap) *Option

type Options

type Options interface {
	// default Multiplex
	PoolMode() types.PoolMode
	// EnableWorkerPool same meaning as EnableWorkerPool in types.StreamConnection
	EnableWorkerPool() bool
	// EnableGenerateRequestID check if the protocol requires custom streamId.
	// If need to generate, you should override the GenerateRequestID method implementation
	EnableGenerateRequestID() bool
	// GenerateRequestID generate a request id for stream to combine stream request && response
	// use connection param as base
	GenerateRequestID(*uint64) uint64
}

Options Features required for a particular host

type Order

type Order int
const (
	BigEndian    Order = 1 // big endian byteOrder
	LittleEndian Order = 2 // little endian byteOrder
)

type Protocol

type Protocol interface {
	Name() string
	Codec() Codec
	KeepAlive
	Hijacker
	Options
}

type ProtocolContext

type ProtocolContext interface {
	Name() string         // protocol name
	Codec() Codec         // frame encode & decode
	KeepAlive() KeepAlive // protocol keep alive
	Hijacker() Hijacker   // protocol hijacker
	Options() Options     // protocol options

}

L7 layer extension

type Request

type Request interface {
	Command
	// IsOneWay Check that the request does not care about the response
	IsOneWay() bool
	GetTimeout() uint32 // request timeout
}

type Response

type Response interface {
	Command
	GetStatus() uint32 // response status
}

type RootContext

type RootContext interface {
	OnVMStart(conf ConfigMap) bool
	OnPluginStart(conf ConfigMap) bool
	OnTick()
	OnVMDone() bool
}

type StreamContext

type StreamContext interface {
	OnDownstreamData(buffer Buffer, endOfStream bool) types.Action
	OnDownstreamClose(peerType types.PeerType)
	OnNewConnection() types.Action
	OnUpstreamData(buffer Buffer, endOfStream bool) types.Action
	OnUpstreamClose(peerType types.PeerType)
	OnStreamDone()
	OnLog()
}

L4 layer extension (host not support now.)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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