aasdsadasdasasd

This commit is contained in:
elee 2022-03-06 23:56:48 -06:00
parent 8ddc1a9e3a
commit 022cf93ccd
9 changed files with 253 additions and 10 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

10
enum.go
View File

@ -1,10 +0,0 @@
package packetflag
type Flag = byte
var (
ChangeKey Flag = 1
HeaderEncrypted Flag = 2
DataEncrypted Flag = 4
HasDummyBytes Flag = 8
)

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module tpack
go 1.17

50
proto/codec/decoder.go Normal file
View File

@ -0,0 +1,50 @@
package codec
import (
"bufio"
"io"
"net"
"tpack/proto/packet"
)
type Decoder struct {
underlying io.Reader
}
func NewDecoder(c net.Conn) *Decoder {
out := &Decoder{
underlying: &fullReader{bufio.NewReader(c)},
}
return out
}
type fullReader struct{ io.Reader }
func (fr *fullReader) Read(p []byte) (int, error) { return io.ReadFull(fr.Reader, p) }
func (D *Decoder) ReadPacket() (*packet.Packet, error) {
pkt := &packet.Packet{}
pkt.Head = &packet.Header{}
err := pkt.Head.Decode(D.underlying)
if err != nil {
return pkt, err
}
if pkt.Head.Length < 9 {
return pkt, err
}
pkt.Data = make([]byte, pkt.Head.Length-9)
err = D.DecryptTo(pkt.Data)
if err != nil {
return nil, err
}
return pkt, err
}
func (D *Decoder) DecryptTo(b []byte) error {
_, err := D.underlying.Read(b)
if err != nil {
return err
}
// you would decrypt the buffer here, since b now holds encrypted data
return err
}

4
proto/codec/encoder.go Normal file
View File

@ -0,0 +1,4 @@
package codec
type Encoder struct {
}

62
proto/packet/header.go Normal file
View File

@ -0,0 +1,62 @@
package packet
import (
"io"
"tpack/proto/packet/packetflag"
"tpack/util"
)
type HeaderFlag = byte
type Header struct {
Length uint16
Opcode uint16
Sequence uint16
Secret byte
Flag packetflag.Flag
Check byte
}
func (H *Header) Decode(r io.Reader) (err error) {
if H.Length, err = util.ReadUint16(r); err != nil {
return err
}
if H.Opcode, err = util.ReadUint16(r); err != nil {
return err
}
if H.Sequence, err = util.ReadUint16(r); err != nil {
return err
}
if H.Secret, err = util.ReadByte(r); err != nil {
return err
}
if H.Flag, err = util.ReadByte(r); err != nil {
return err
}
if H.Check, err = util.ReadByte(r); err != nil {
return err
}
return nil
}
func (H *Header) Encode(w io.Writer) (err error) {
if err = util.WriteUint16(w, H.Opcode); err != nil {
return err
}
if err = util.WriteUint16(w, H.Opcode); err != nil {
return err
}
if err = util.WriteUint16(w, H.Sequence); err != nil {
return err
}
if err = util.WriteByte(w, H.Secret); err != nil {
return err
}
if err = util.WriteByte(w, H.Flag); err != nil {
return err
}
if err = util.WriteByte(w, H.Check); err != nil {
return err
}
return nil
}

6
proto/packet/packet.go Normal file
View File

@ -0,0 +1,6 @@
package packet
type Packet struct {
Head *Header
Data []byte
}

60
util/reader.go Normal file
View File

@ -0,0 +1,60 @@
package util
import (
"encoding/binary"
"io"
)
var EndianNess = binary.LittleEndian
func ReadUint8(reader io.Reader) (val uint8, err error) {
if br, ok := reader.(io.ByteReader); ok {
return br.ReadByte()
}
var protocol [1]byte
_, err = reader.Read(protocol[:1])
val = protocol[0]
return
}
func ReadByte(reader io.Reader) (val byte, err error) {
return ReadUint8(reader)
}
func ReadUint16(reader io.Reader) (val uint16, err error) {
var protocol [2]byte
_, err = reader.Read(protocol[:2])
val = EndianNess.Uint16(protocol[:2])
return
}
func ReadInt16(reader io.Reader) (val int16, err error) {
uval, err := ReadUint16(reader)
val = int16(uval)
return
}
func ReadUint32(reader io.Reader) (val uint32, err error) {
var protocol [4]byte
_, err = reader.Read(protocol[:4])
val = EndianNess.Uint32(protocol[:4])
return
}
func ReadInt32(reader io.Reader) (val int32, err error) {
uval, err := ReadUint32(reader)
val = int32(uval)
return
}
func ReadUint64(reader io.Reader) (val uint64, err error) {
var protocol [8]byte
_, err = reader.Read(protocol[:8])
val = EndianNess.Uint64(protocol[:8])
return
}
func ReadInt64(reader io.Reader) (val int64, err error) {
uval, err := ReadUint64(reader)
val = int64(uval)
return
}

60
util/writer.go Normal file
View File

@ -0,0 +1,60 @@
package util
import (
"encoding/binary"
"io"
)
func WriteUint8(writer io.Writer, val uint8) (err error) {
var protocol [1]byte
protocol[0] = val
_, err = writer.Write(protocol[:1])
return
}
func WriteInt8(writer io.Writer, val int8) (err error) {
return WriteUint8(writer, uint8(val))
}
func WriteByte(writer io.Writer, val byte) (err error) {
return WriteUint8(writer, val)
}
func WriteInt16(writer io.Writer, val int16) (err error) {
err = WriteUint16(writer, uint16(val))
return
}
func WriteUint16(writer io.Writer, val uint16) (err error) {
var protocol [2]byte
binary.BigEndian.PutUint16(protocol[:2], val)
_, err = writer.Write(protocol[:2])
return
}
func WriteInt32(writer io.Writer, val int32) (err error) {
err = WriteUint32(writer, uint32(val))
return
}
func WriteInt(writer io.Writer, val int) (err error) {
return WriteInt32(writer, int32(val))
}
func WriteUint32(writer io.Writer, val uint32) (err error) {
var protocol [4]byte
binary.BigEndian.PutUint32(protocol[:4], val)
_, err = writer.Write(protocol[:4])
return
}
func WriteInt64(writer io.Writer, val int64) (err error) {
err = WriteUint64(writer, uint64(val))
return
}
func WriteUint64(writer io.Writer, val uint64) (err error) {
var protocol [8]byte
binary.BigEndian.PutUint64(protocol[:8], val)
_, err = writer.Write(protocol[:8])
return
}