packet

package
v0.0.0-...-a9387a1 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2022 License: MIT Imports: 9 Imported by: 0

README

Packets

HeartBeat

Req: Payload: sendTime

Resp: Payload: sendTime

Documentation

Overview

Package packet handling packet communicate between server and client.

Packet Layout

--------------------------------------------------------------
iv +
crc32(type+payload) + aes(crc32(type+payload), token, iv) +
length + aes(payload + type, token, iv)
--------------------------------------------------------------
iv => userId(uint16) + portNum(uint16) + reqId(int32) + rand(8) = 16byte
length => int16
aes => aes-256-cfb
type => int8
payload => []byte
token => auth request

Index

Constants

View Source
const PacketL2HeaderSize = 24

Variables

View Source
var (
	IsHasLoopbackPrefix = runtime.GOOS == "darwin"

	MaxPayloadLength = math.MaxUint16 - 27 // header(26) + type(1)
)
View Source
var (
	ErrPacketTooShort  = logex.Define("packet too short: %v")
	ErrInvalidType     = logex.Define("invalid type: %v")
	ErrInvalidToken    = logex.Define("invalid token")
	ErrInvalidLength   = logex.Define("invalid length, want:%v, got: %v")
	ErrPayloadTooLarge = logex.Define("payload is too large: %v")
)
View Source
var (
	ErrUserNotMatch = logex.Define("user %v is not matched")
)

Functions

This section is empty.

Types

type AuthDelegate

type AuthDelegate interface {
	GetUserToken(userId int) ([]byte, error)
}

type Chan

type Chan chan []*Packet

func NewChan

func NewChan(n int) Chan

func (Chan) Recv

func (c Chan) Recv() RecvChan

func (Chan) Send

func (c Chan) Send() SendChan

func (Chan) SendSafe

func (ch Chan) SendSafe(f *flow.Flow, p []*Packet) bool

type DataPacket

type DataPacket struct {
	*Packet
}

func NewDataPacket

func NewDataPacket(payload []byte) *DataPacket

func (*DataPacket) DestIP

func (d *DataPacket) DestIP() ip.IP

func (*DataPacket) SrcIP

func (d *DataPacket) SrcIP() ip.IP

type Packet

type Packet struct {
	ReqId uint32
	Type  Type
	// contains filtered or unexported fields
}

ReqId + Type + Payload

func New

func New(payload []byte, t Type) *Packet

func Unmarshal

func Unmarshal(b []byte) (*Packet, error)

func (*Packet) Marshal

func (p *Packet) Marshal(ret []byte) int

func (*Packet) Payload

func (p *Packet) Payload() []byte

func (*Packet) Reply

func (p *Packet) Reply(payload []byte) *Packet

func (*Packet) SetReqId

func (p *Packet) SetReqId(r Reqider)

func (*Packet) Size

func (p *Packet) Size() int

func (*Packet) TotalSize

func (p *Packet) TotalSize() int

type PacketL2

type PacketL2 struct {
	IV       []byte
	UserId   uint16
	Payload  []byte
	Checksum uint32
	// contains filtered or unexported fields
}

to verify auth iv + userid + // header (18) crc32(payload) // checksum (4) len(payload) + payload // payload (2+n)

func NewPacketL2

func NewPacketL2(iv []byte, userId uint16, payload []byte, checksum uint32) *PacketL2

func WrapL2

func WrapL2(s *Session, p []*Packet) *PacketL2

func (*PacketL2) Unmarshal

func (p *PacketL2) Unmarshal() ([]*Packet, error)

func (*PacketL2) Verify

func (p *PacketL2) Verify(s *Session) error

type RecvChan

type RecvChan <-chan []*Packet

func (RecvChan) RecvAll

func (c RecvChan) RecvAll(f *flow.Flow) []*Packet

type Reqider

type Reqider interface {
	GetReqId() uint32
}

type SendChan

type SendChan chan<- []*Packet

func (SendChan) SendOneSafe

func (s SendChan) SendOneSafe(f *flow.Flow, p *Packet) bool

func (SendChan) SendSafe

func (s SendChan) SendSafe(f *flow.Flow, p []*Packet) bool

type Session

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

func NewSessionCli

func NewSessionCli(userId int, token []byte) *Session

func NewSessionSvr

func NewSessionSvr(delegate AuthDelegate) *Session

func (*Session) Clone

func (s *Session) Clone() *Session

func (*Session) Decode

func (s *Session) Decode(iv []byte, dst, src []byte)

func (*Session) Encode

func (s *Session) Encode(iv, dst, src []byte)

func (*Session) UserId

func (s *Session) UserId() int

func (*Session) Verify

func (s *Session) Verify(userId int, crc32 uint32, iv, payload []byte) error

func (*Session) VerifyUserId

func (s *Session) VerifyUserId(userId int) error

svr method

type Type

type Type int
const (
	AUTH   Type // 1: payload: token
	AUTH_R      // 2: payload: token
	DATA        // 3: payload: ip packet
	DATA_R      // 4: payload: nil

	HEARTBEAT   // 5: payload: nil
	HEARTBEAT_R // 6: payload: nil

	NEWDC   // 7: payload: nil
	NEWDC_R // 8: payload: json([port])

	// send bytes to remote
	SPEED   // 9: payload: [4096]bytes in random
	SPEED_R // 10: payload: nil

	// let remote send N bytes to local
	SPEED_REQ   // 11: payload: byte size(uint64)
	SPEED_REQ_R // 12:

	InvalidType
)

packet type

func (Type) Bytes

func (t Type) Bytes() []byte

func (Type) IsInvalid

func (t Type) IsInvalid() bool

func (Type) IsReq

func (t Type) IsReq() bool

func (Type) IsResp

func (t Type) IsResp() bool

func (*Type) Marshal

func (t *Type) Marshal(b []byte) error

func (Type) String

func (t Type) String() string

Jump to

Keyboard shortcuts

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