commands

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2020 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Command 0123456789abcdef are reserved for user IDs
	CmdLogin = Command{
		Code: 'l',
	}
	CmdTestMultiQuery = Command{
		Code: 'm',
	}
)
View Source
var (
	BadVersion    = errors.New("BADVER")
	BadLen        = errors.New("BADLEN")
	BadIp         = errors.New("BADIP")
	BadCommand    = errors.New("BADCOMMAND")
	BadCodec      = errors.New("BADCODEC")
	BadFrag       = errors.New("BADFRAG")
	BadUser       = errors.New("BADUSER")
	BadConn       = errors.New("BADCONN")
	BadServerFull = errors.New("VFUL")
	NoData        = errors.New("VOK")
	VersionOk     = errors.New("VACK")
	VersionNotOk  = errors.New("VNAK")
	LazyModeOk    = errors.New("LACK")
	ErrTimeout    = errors.New("TIMEOUT")
)

Declare a few of standard error responses

View Source
var CmdError = Command{
	Code: 'e',
	NewResponse: func() Response {
		return &ErrorResponse{}
	},
}
View Source
var CmdPacket = Command{
	Code:        'c',
	NeedsUserId: true,
	NewRequest: func() Request {
		return &PacketRequest{}
	},
	NewResponse: func() Response {
		return &PacketResponse{}
	},
}
View Source
var CmdSetOptions = Command{
	Code:        'o',
	NeedsUserId: true,
	NewRequest: func() Request {
		return &SetOptionsRequest{}
	},
	NewResponse: func() Response {
		return &SetOptionsResponse{}
	},
}
View Source
var CmdTestDownstreamEncoder = Command{
	Code: 'y',
	NewRequest: func() Request {
		return &TestDownstreamEncoderRequest{}
	},
	NewResponse: func() Response {
		return &TestDownstreamEncoderResponse{}
	},
}
View Source
var CmdTestDownstreamFragmentSize = Command{
	Code:        'r',
	NeedsUserId: true,
	NewRequest: func() Request {
		return &TestDownstreamFragmentSizeRequest{}
	},
	NewResponse: func() Response {
		return &TestDownstreamFragmentSizeResponse{}
	},
}
View Source
var CmdTestUpstreamEncoder = Command{
	Code:        'z',
	NeedsUserId: true,
	NewRequest: func() Request {
		return &TestUpstreamEncoderRequest{}
	},
	NewResponse: func() Response {
		return &TestUpstreamEncoderResponse{}
	},
}
View Source
var CmdVersion = Command{
	Code:        'v',
	NeedsUserId: false,
	NewRequest: func() Request {
		return &VersionRequest{}
	},
	NewResponse: func() Response {
		return &VersionResponse{}
	},
}
View Source
var Digits = regexp.MustCompile("^[0-9]{3}")

Functions

func ComposeRequest

func ComposeRequest(msg *dns.Msg, domain string) (data []byte)

ComposeRequest will take a DNS message and recompose it back to a complete request, if multiQuery was used

func DecodeRequestHeader

func DecodeRequestHeader(c Command, req []byte) (remaining []byte, userId uint16, err error)

func EncodeRequestHeader

func EncodeRequestHeader(c Command, userId uint16) []byte

EncodeRequestHeader will prepare a common request header used by all commands

func EncodeUserId

func EncodeUserId(userId uint16) string

func StripDomain

func StripDomain(data []byte, domain string) (res []byte)

StripDomain will remove the domain from the end of data string and return the string without this domain. If the string does not end with the domain, it does nothing.

Types

type Command

type Command struct {
	Code        byte
	NeedsUserId bool // Defines if the command needs user ID in the query sring or not
	NewRequest  func() Request
	NewResponse func() Response
}

func (Command) Byte

func (c Command) Byte() byte

String will retun the command code as string, e.g. 'z', 's', 'v'...

func (Command) IsOfType

func (c Command) IsOfType(data []byte) bool

IsOfType will check if the supplied string starts with the given command type

func (Command) String

func (c Command) String() string

String will retun the command code as string, e.g. 'z', 's', 'v'...

func (Command) ValidateType

func (c Command) ValidateType(data []byte) error

ValidateType will check if the supplied string starts with the given command type and return an error if its not.

type ErrorResponse

type ErrorResponse struct {
	Err error
}

func (*ErrorResponse) Command

func (vr *ErrorResponse) Command() Command

func (*ErrorResponse) Decode

func (vr *ErrorResponse) Decode(e enc.Encoder, response []byte) error

func (*ErrorResponse) Encode

func (vr *ErrorResponse) Encode(e enc.Encoder) ([]byte, error)

type LazyMode

type LazyMode byte
const (
	LazyModeOn  LazyMode = 'l'
	LazyModeOff LazyMode = 'i'
)

type PacketRequest

type PacketRequest struct {
	UserId         uint16
	LastAckedSeqNo uint16
	Packet         *util.Packet
}

func (*PacketRequest) Command

func (vr *PacketRequest) Command() Command

func (*PacketRequest) Decode

func (vr *PacketRequest) Decode(e enc.Encoder, req []byte) error

func (*PacketRequest) Encode

func (vr *PacketRequest) Encode(e enc.Encoder) ([]byte, error)

type PacketResponse

type PacketResponse struct {
	Err            error
	LastAckedSeqNo uint16
	Packet         *util.Packet
}

func (*PacketResponse) Command

func (vr *PacketResponse) Command() Command

func (*PacketResponse) Decode

func (vr *PacketResponse) Decode(e enc.Encoder, req []byte) error

func (*PacketResponse) Encode

func (vr *PacketResponse) Encode(e enc.Encoder) ([]byte, error)

type Request

type Request interface {
	// Command is the command that this request reffers to
	Command() Command
	// Encode will encode this requires into a DNS-compatible query, potentially using the encoder specified. Note that
	// encoding allways happens in the "hostname/domain" format -- e.g. so you can execute a A, CNAME, or a MX query
	// whith this.
	Encode(e enc.Encoder) ([]byte, error)
	// Decode will decode the data from the query into this object
	Decode(e enc.Encoder, request []byte) error
}

Request represents a (serialized) request to a DNS server

type Response

type Response interface {
	// Command is the command that this request reffers to
	Command() Command
	// EncodeResponse will encode this response into a data stream which can be the sent as a DNS response.
	Encode(e enc.Encoder) ([]byte, error)
	// DecodeResponse will take a byte (data) stream and create a response object
	Decode(e enc.Encoder, response []byte) error
}

Response it the response from the DNS server

type Serializer

type Serializer struct {
	Upstream      util.UpstreamConfig
	Downstream    util.DownstreamConfig
	UseEdns0      bool
	UseMultiQuery bool
	UseLazyMode   bool
	Domain        string
}

func (Serializer) DecodeDnsRequest

func (cl Serializer) DecodeDnsRequest(request []byte) (Request, error)

DecodeDnsRequest will take a DNS message and decode it into one of the DNS requests objects

func (Serializer) DecodeDnsResponse

func (cl Serializer) DecodeDnsResponse(msg *dns.Msg) (Response, error)

DecodeDnsResponse will take a DNS message and decode it into one of the DNS response object

func (Serializer) DecodeDnsResponseWithParams

func (cl Serializer) DecodeDnsResponseWithParams(msg *dns.Msg, downstream enc.Encoder) (Response, error)

DecodeDnsResponse will take a DNS message and decode it into one of the DNS response object

func (Serializer) DetectCommandType

func (cl Serializer) DetectCommandType(data []byte) *Command

DetectCommandType will try to detect the type of command from the given data stream. If it cannot be detected, it returns `nil`.

func (Serializer) EncodeDnsRequest

func (cl Serializer) EncodeDnsRequest(req Request) (*dns.Msg, error)

EncodeDnsRequest will take a Request and encode it as a DNS message

func (Serializer) EncodeDnsRequestWithParams

func (cl Serializer) EncodeDnsRequestWithParams(req Request, qt dnsmessage.Type, upstream enc.Encoder) (*dns.Msg, error)

EncodeDnsRequestWithParams will take a Request and encode it as a DNS message using given (overriden) params

func (Serializer) EncodeDnsResponse

func (cl Serializer) EncodeDnsResponse(resp Response, request *dns.Msg) (*dns.Msg, error)

EncodeDnsResponse will take a DNS response and create a DNS message

func (Serializer) EncodeDnsResponseWithParams

func (cl Serializer) EncodeDnsResponseWithParams(resp Response, request *dns.Msg, qt dnsmessage.Type, downstream enc.Encoder) (*dns.Msg, error)

EncodeDnsResponse will take a DNS response and create a DNS message

type SetOptionsRequest

type SetOptionsRequest struct {
	UserId                 uint16
	LazyMode               *bool
	MultiQuery             *bool
	Closed                 *bool
	DownstreamEncoder      enc.Encoder
	UpstreamEncoder        enc.Encoder
	DownstreamFragmentSize *uint32
}

func (*SetOptionsRequest) Command

func (vr *SetOptionsRequest) Command() Command

func (*SetOptionsRequest) Decode

func (vr *SetOptionsRequest) Decode(e enc.Encoder, req []byte) error

func (*SetOptionsRequest) Encode

func (vr *SetOptionsRequest) Encode(e enc.Encoder) ([]byte, error)

type SetOptionsResponse

type SetOptionsResponse struct {
	Err error
}

func (*SetOptionsResponse) Command

func (vr *SetOptionsResponse) Command() Command

func (*SetOptionsResponse) Decode

func (vr *SetOptionsResponse) Decode(e enc.Encoder, response []byte) error

func (*SetOptionsResponse) Encode

func (vr *SetOptionsResponse) Encode(e enc.Encoder) ([]byte, error)

type TestDownstreamEncoderRequest

type TestDownstreamEncoderRequest struct {
	DownstreamEncoder enc.Encoder
}

func (*TestDownstreamEncoderRequest) Command

func (vr *TestDownstreamEncoderRequest) Command() Command

func (*TestDownstreamEncoderRequest) Decode

func (vr *TestDownstreamEncoderRequest) Decode(e enc.Encoder, req []byte) error

func (*TestDownstreamEncoderRequest) Encode

func (vr *TestDownstreamEncoderRequest) Encode(e enc.Encoder) ([]byte, error)

type TestDownstreamEncoderResponse

type TestDownstreamEncoderResponse struct {
	Data []byte // []byte(util.DownloadCodecCheck)
	Err  error
}

func (*TestDownstreamEncoderResponse) Command

func (vr *TestDownstreamEncoderResponse) Command() Command

func (*TestDownstreamEncoderResponse) Decode

func (vr *TestDownstreamEncoderResponse) Decode(e enc.Encoder, response []byte) error

func (*TestDownstreamEncoderResponse) Encode

func (vr *TestDownstreamEncoderResponse) Encode(e enc.Encoder) ([]byte, error)

type TestDownstreamFragmentSizeRequest

type TestDownstreamFragmentSizeRequest struct {
	UserId       uint16
	FragmentSize uint32
}

func (*TestDownstreamFragmentSizeRequest) Command

func (*TestDownstreamFragmentSizeRequest) Decode

func (*TestDownstreamFragmentSizeRequest) Encode

type TestDownstreamFragmentSizeResponse

type TestDownstreamFragmentSizeResponse struct {
	FragmentSize uint32
	Data         []byte
	Err          error
}

func (*TestDownstreamFragmentSizeResponse) Command

func (*TestDownstreamFragmentSizeResponse) Decode

func (vr *TestDownstreamFragmentSizeResponse) Decode(e enc.Encoder, response []byte) error

func (*TestDownstreamFragmentSizeResponse) Encode

type TestUpstreamEncoderRequest

type TestUpstreamEncoderRequest struct {
	UserId  uint16
	Pattern []byte
}

func (*TestUpstreamEncoderRequest) Command

func (vr *TestUpstreamEncoderRequest) Command() Command

func (*TestUpstreamEncoderRequest) Decode

func (vr *TestUpstreamEncoderRequest) Decode(e enc.Encoder, req []byte) error

Decode does not really decode, as the main point is to test if the charset goes through or not

func (*TestUpstreamEncoderRequest) Encode

func (vr *TestUpstreamEncoderRequest) Encode(e enc.Encoder) ([]byte, error)

Encode does not really encode, as the main point is to test if the charset goes through or not

type TestUpstreamEncoderResponse

type TestUpstreamEncoderResponse struct {
	Data []byte
	Err  error
}

func (*TestUpstreamEncoderResponse) Command

func (vr *TestUpstreamEncoderResponse) Command() Command

func (*TestUpstreamEncoderResponse) Decode

func (vr *TestUpstreamEncoderResponse) Decode(e enc.Encoder, response []byte) error

Decode happens before downstream encoder is selected, so encode with Base32 always

func (*TestUpstreamEncoderResponse) Encode

func (vr *TestUpstreamEncoderResponse) Encode(e enc.Encoder) ([]byte, error)

Encode happens before downstream encoder is selected, so encode with Base32 always

type VersionRequest

type VersionRequest struct {
	ClientVersion uint32
}

func (*VersionRequest) Command

func (vr *VersionRequest) Command() Command

func (*VersionRequest) Decode

func (vr *VersionRequest) Decode(e enc.Encoder, req []byte) error

func (*VersionRequest) Encode

func (vr *VersionRequest) Encode(e enc.Encoder) ([]byte, error)

type VersionResponse

type VersionResponse struct {
	ServerVersion uint32
	UserId        uint16
	Err           error
}

func (*VersionResponse) Command

func (vr *VersionResponse) Command() Command

func (*VersionResponse) Decode

func (vr *VersionResponse) Decode(e enc.Encoder, response []byte) error

func (*VersionResponse) Encode

func (vr *VersionResponse) Encode(e enc.Encoder) ([]byte, error)

Jump to

Keyboard shortcuts

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