quickproto

package module
v1.3.5 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2022 License: GPL-2.0 Imports: 11 Imported by: 0

README

Fast golang protocol parser

Supports:

  • Headers (Support listed values, IE: Key=[Value, Value, Value])
  • Files (Supports multiple files)
  • Body (Can be encoded/decoded to any type)
    • !WARNING! !Make sure your delimiter not in the encoding's alphabet!
    • !WARNING! !Make sure your delimiter is not in the message headers, body or filenames!
  • Delimiters
    • No alphabetic characters from [A-Z a-z 0-9 =]

Data is split apart by the delimiter.

Say the delimiter is a $:

The body and header will be split by the delimiter * 4

Each key value pair will be split by the delimiter * 2

Then the key values will be split by the delimiter.

Example:

key1$value1&value2$$key2$value2$$$$BODYBODYBODY

Usage:

Initialize a config like so:

conf := quickproto.NewConfig([]byte(DELIMITER), USE_ENCODING, USE_CRYPTO, 2048, quickproto.Base16Encoding, quickproto.Base16Decoding)
// RSA only used if USE_CRYPTO is true, and when sending the AES key from client to server.
// The RSA keys are however not required, but highly recommended to securely send the AES key from client to server!
conf.PrivateKey = privkey // Client does not need the private key! This is a security risk!
conf.PublicKey = pubkey // Server does not need the public key, but it would not pose a security risk.

Then you can simply run a server with the following lines of code:

s := server.New(IP, Port, conf)
s.Listen()
for {
	conn, client, err := s.Accept()
	msg, err := s.Read(client)
}

Or create a client like so:

c := client.New(IP, Port, conf, nil)
c.Connect()
msg := c.CONFIG.NewMessage() // Use config to generate message to omit providing arguments
msg.AddHeader("Test_key", "Test_value")
msg.AddHeader("Test2_key", "Test2_value")
msg.AddHeader("Test3_key", "Test3_value")
msg.AddRawFile("test.txt", []byte("Hello World"))
msg.AddRawFile("test2.txt", []byte("Hello World"))
msg.AddRawFile("test3.txt", []byte("Hello World"))
msg.AddContent("Hello World")
c.Write(msg)

It is also possible to broadcast to multiple clients at once, simply use s.Broadcast(msg).

To capture broadcasts on the client side and interact with them, run a goroutine like so:

func OnBroadcast(msg *quickproto.Message) {
  // Do something with the message
}

c := client.New(IP, Port, conf, nil)
c.Connect()
c.OnMessage = OnBroadcast
go c.Listen()

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BANNED_DELIMITERS = []string{
	"=", "_", "\x08",
	"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
	"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
}
View Source
var STANDARD_DELIM []byte = []byte("$")

Functions

func Base16Decoding added in v1.2.5

func Base16Decoding(data []byte) ([]byte, error)

Hex decoding

func Base16Encoding added in v1.2.5

func Base16Encoding(data []byte) []byte

Hex encoding

func Base32Decoding added in v1.3.5

func Base32Decoding(data []byte) ([]byte, error)

Base32 decoding

func Base32Encoding added in v1.3.5

func Base32Encoding(data []byte) []byte

Base32 encoding

func Base64Decoding added in v1.2.5

func Base64Decoding(data []byte) ([]byte, error)

Base64 decoding

func Base64Encoding added in v1.2.5

func Base64Encoding(data []byte) []byte

Base64 encoding

func WriteConn added in v1.1.5

func WriteConn(conn net.Conn, msg *Message, aes_key *[32]byte) error

Types

type Config added in v1.1.6

type Config struct {
	Delimiter   []byte
	UseEncoding bool
	UseCrypto   bool
	BufSize     int
	Encode_func func([]byte) []byte
	Decode_func func([]byte) ([]byte, error)
	PrivateKey  *rsa.PrivateKey
	PublicKey   *rsa.PublicKey
}

func NewConfig added in v1.2.1

func NewConfig(delimiter []byte, useencoding bool, usecrypto bool, bufsize int, encode_f func([]byte) []byte, decode_f func([]byte) ([]byte, error)) *Config

NewConfig creates a new Config.

func (*Config) NewMessage added in v1.2.5

func (c *Config) NewMessage() *Message

type Message

type Message struct {
	Data        []byte
	Delimiter   []byte
	Headers     map[string][]string
	Body        []byte
	Files       map[string]MessageFile
	UseEncoding bool
	Encode_func func([]byte) []byte
	Decode_func func([]byte) ([]byte, error)
}

A Message is a protocol message.

func NewMessage

func NewMessage(delimiter []byte, useencoding bool, encode_func func([]byte) []byte, decode_func func([]byte) ([]byte, error)) *Message

NewMessage creates a new Message.

func ReadConn added in v1.1.5

func ReadConn(conn net.Conn, conf *Config, aes_key *[32]byte) (*Message, error)

func (*Message) AddContent added in v1.1.7

func (m *Message) AddContent(content any) error

func (*Message) AddFile added in v1.1.7

func (m *Message) AddFile(file MessageFile) error

func (*Message) AddHeader added in v1.1.1

func (m *Message) AddHeader(key string, value string) error

func (*Message) AddRawFile added in v1.1.7

func (m *Message) AddRawFile(name string, data []byte) error

func (*Message) BodyDelimiter added in v1.1.4

func (m *Message) BodyDelimiter() []byte

func (*Message) ContentLength

func (m *Message) ContentLength() int

Get content length of the message.

func (*Message) EndingDelimiter added in v1.1.4

func (m *Message) EndingDelimiter() []byte

func (*Message) FileDelimiter added in v1.1.7

func (m *Message) FileDelimiter() []byte

func (*Message) Generate

func (m *Message) Generate() (*Message, error)

creates a protocol message. Header is a map of key/value pairs. Body is a base64 encoded byte slice.

func (*Message) HeaderDelimiter added in v1.1.4

func (m *Message) HeaderDelimiter() []byte

func (*Message) Parse

func (m *Message) Parse() (*Message, error)

parses protocol messages. Header is a map of key/value pairs. Body is a base64 encoded byte slice.

type MessageFile added in v1.1.7

type MessageFile struct {
	Name string
	Data []byte
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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