p2p

package module
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2022 License: GPL-3.0 Imports: 20 Imported by: 4

README

Golang simple TCP client and server

Golang-p2p is a small client and server to make p2p communication over TCP with RSA encryption.

Main aim the package is to create an easy way of microservices communication.

Features

Feature Description
Gob, Json and Bytes support You can send you structure or data in binary presentation or binary serialized
RSA handshake Every communication between a client and a server starts with RSA public keys handshake.
All sending data are encrypted before sending.

Import

import "github.com/leprosus/golang-p2p"

Server example

package main

import (
	"context"
	"fmt"
	"log"

	p2p "github.com/leprosus/golang-p2p"
)

type Hello struct {
	Text string
}

type Buy struct {
	Text string
}

func main() {
	tcp := p2p.NewTCP("localhost", 8080)

	settings := p2p.NewServerSettings()

	server, err := p2p.NewServer(tcp, settings)
	if err != nil {
		log.Panicln(err)
	}

	server.SetHandle("dialog", func(ctx context.Context, req p2p.Request) (res p2p.Response, err error) {
		hello := Hello{}
		err = req.GetGob(&hello)
		if err != nil {
			return
		}

		fmt.Printf("> Hello: %s\n", hello.Text)

		buy := Buy{Text: hello.Text}
		err = res.SetGob(buy)

		return
	})

	err = server.Serve()
	if err != nil {
		log.Panicln(err)
	}
}

Client example

package main

import (
	"fmt"
	"log"

	p2p "github.com/leprosus/golang-p2p"
)

type Hello struct {
	Text string
}

type Buy struct {
	Text string
}

func main() {
	tcp := p2p.NewTCP("localhost", 8080)

	settings := p2p.NewClientSettings()

	client, err := p2p.NewClient(tcp, settings)
	if err != nil {
		log.Panicln(err)
	}

	for i := 0; i < 10; i++ {
		hello := Hello{Text: fmt.Sprintf("User #%d", i+1)}

		req := p2p.Request{}
		err = req.SetGob(hello)
		if err != nil {
			log.Panicln(err)
		}

		var res p2p.Response
		res, err = client.Send("dialog", req)
		if err != nil {
			log.Panicln(err)
		}

		var buy Buy
		err = res.GetGob(&buy)
		if err != nil {
			log.Panicln(err)
		}

		fmt.Printf("> Buy: %s\n", buy.Text)
	}
}

Running

If you run the server and the client separately then you see:

  • in the server stdout:
> Hello: User #1
dialog: addr (127.0.0.1:61620), handshake (511 µs), read (2 ms), handle (129 µs), write (62 µs), total (3 ms)
> Hello: User #2
dialog: addr (127.0.0.1:61621), handshake (310 µs), read (2 ms), handle (60 µs), write (30 µs), total (3 ms)
> Hello: User #3
dialog: addr (127.0.0.1:61623), handshake (220 µs), read (2 ms), handle (68 µs), write (26 µs), total (3 ms)
> Hello: User #4
dialog: addr (127.0.0.1:61624), handshake (252 µs), read (2 ms), handle (79 µs), write (30 µs), total (3 ms)
> Hello: User #5
dialog: addr (127.0.0.1:61625), handshake (340 µs), read (2 ms), handle (75 µs), write (41 µs), total (3 ms)
> Hello: User #6
dialog: addr (127.0.0.1:61626), handshake (276 µs), read (2 ms), handle (57 µs), write (34 µs), total (3 ms)
> Hello: User #7
dialog: addr (127.0.0.1:61627), handshake (251 µs), read (3 ms), handle (163 µs), write (65 µs), total (3 ms)
> Hello: User #8
dialog: addr (127.0.0.1:61628), handshake (268 µs), read (2 ms), handle (89 µs), write (50 µs), total (3 ms)
> Hello: User #9
dialog: addr (127.0.0.1:61629), handshake (413 µs), read (3 ms), handle (229 µs), write (85 µs), total (4 ms)
> Hello: User #10
dialog: addr (127.0.0.1:61630), handshake (663 µs), read (3 ms), handle (88 µs), write (73 µs), total (4 ms)
  • in the client stdout:
dialog: addr (127.0.0.1:8080), handshake (3 ms), write (90 µs), read (630 µs), total (3 ms)
> Buy: User #1
dialog: addr (127.0.0.1:8080), handshake (2 ms), write (34 µs), read (476 µs), total (3 ms)
> Buy: User #2
dialog: addr (127.0.0.1:8080), handshake (2 ms), write (33 µs), read (331 µs), total (3 ms)
> Buy: User #3
dialog: addr (127.0.0.1:8080), handshake (2 ms), write (43 µs), read (369 µs), total (3 ms)
> Buy: User #4
dialog: addr (127.0.0.1:8080), handshake (2 ms), write (60 µs), read (415 µs), total (3 ms)
> Buy: User #5
dialog: addr (127.0.0.1:8080), handshake (2 ms), write (48 µs), read (374 µs), total (3 ms)
> Buy: User #6
dialog: addr (127.0.0.1:8080), handshake (2 ms), write (115 µs), read (800 µs), total (3 ms)
> Buy: User #7
dialog: addr (127.0.0.1:8080), handshake (2 ms), write (54 µs), read (560 µs), total (3 ms)
> Buy: User #8
dialog: addr (127.0.0.1:8080), handshake (3 ms), write (198 µs), read (992 µs), total (4 ms)
> Buy: User #9
dialog: addr (127.0.0.1:8080), handshake (3 ms), write (53 µs), read (502 µs), total (4 ms)
> Buy: User #10
  • logging

All lines that start from dialog is the topic for the communication.

All log lines write to StdOut.

If you want to reassign this logger you need to implement your own with the following interface:

type Logger interface {
Info(msg string)
Warn(msg string)
Error(msg string)
}

and set it up in your server or client implementation this way:

settings.SetLogger(yourLogger)

List all methods

TCP Initialization
  • p2p.NewTCP(host, port) (tcp, err) - creates TCP connection
Server settings initialization
  • p2p.NewServerSettings() (stg) - creates a new server's settings
  • stg.SetLogger(l) - reassigns server's logger
  • stg.SetConnTimeout(dur) - sets connection timout
  • stg.SetHandleTimeout(dur) - sets handle timout
  • stg.SetBodyLimit(limit) - sets max body size for reading
Server
  • p2p.NewServer(tcp, stg) (srv, err) - creates a new server
  • srv.SetContext(ctx) - sets context
  • srv.SetHandle(topic, handler) - sets a handler that processes all request with defined topic
  • srv.Serve() (err) - starts to serve
Client settings initialization
  • p2p.NewClientSettings() (stg) - creates a new server's settings
  • stg.SetLogger(l) - reassigns server's logger
  • stg.SetConnTimeout(dur) - sets connection timout
  • stg.SetBodyLimit(limit) - sets max body size for writing
  • stg.SetRetry(retries, delay) - sets retry parameters
Client
  • NewClient(tcp, stg) (clt, err) - creates a new client
  • clt.Send(topic, req) (res, err) - sends a request to a server by the topic
Request
  • req.SetBytes(bs) - sets bytes to the request
  • req.GetBytes() (bs) - gets bytes from the request
  • req.SetGob(obj) (err) - encodes to Gob and sets structure to the request
  • req.GetGob(obj) (err) - decode from Gob and gets structure from the request
  • req.SetJson(obj) (err) - encodes to Json and sets structure to the request
  • req.GetJson(obj) (err) - decode from Json and gets structure from the request
  • req.String() (str) - returns string from the request
Response
  • res.SetBytes(bs) - sets bytes to the response
  • res.GetBytes() (bs) - gets bytes from the response
  • res.SetGob(obj) (err) - encodes to Gob and sets structure to the response
  • res.GetGob(obj) (err) - decode from Gob and gets structure from the response
  • res.SetJson(obj) (err) - encodes to Json and sets structure to the response
  • res.GetJson(obj) (err) - decode from Json and gets structure from the response
  • res.String() (str) - returns string from the response

Documentation

Index

Constants

View Source
const (
	DefaultConnTimeout   = 250 * time.Millisecond
	DefaultHandleTimeout = 250 * time.Millisecond
)
View Source
const (
	DefaultRetries      = 3
	DefaultDelayTimeout = 50 * time.Millisecond
)
View Source
const DefaultBodyLimit = 1024

Variables

View Source
var (
	UnsupportedTopic      = errors.New("unsupported topic")
	ConnectionError       = errors.New("connection error")
	PresetConnectionError = errors.New("preset connection error")
)

Functions

func NewStdLogger added in v1.2.0

func NewStdLogger() (l *stdLogger)

Types

type CipherKey added in v1.3.2

type CipherKey []byte

func NewCipherKey added in v1.3.2

func NewCipherKey() (key CipherKey, err error)

func (CipherKey) Decode added in v1.3.2

func (key CipherKey) Decode(bs []byte) (rs []byte, err error)

func (CipherKey) Encode added in v1.3.2

func (key CipherKey) Encode(bs []byte) (rs []byte, err error)

type Client added in v1.2.0

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

func NewClient added in v1.2.0

func NewClient(tcp *TCP, stg *ClientSettings) (c *Client, err error)

func (*Client) Send added in v1.2.0

func (c *Client) Send(topic string, req Request) (res Response, err error)

type ClientSettings added in v1.2.0

type ClientSettings struct {
	Limiter
	Retry
	Logger
}

func NewClientSettings added in v1.2.0

func NewClientSettings() (stg *ClientSettings)

func (*ClientSettings) SetBodyLimit added in v1.2.2

func (stg *ClientSettings) SetBodyLimit(limit uint)

func (*ClientSettings) SetConnTimeout added in v1.2.0

func (stg *ClientSettings) SetConnTimeout(dur time.Duration)

func (*ClientSettings) SetLogger added in v1.2.0

func (stg *ClientSettings) SetLogger(l Logger)

func (*ClientSettings) SetRetry added in v1.2.0

func (stg *ClientSettings) SetRetry(retries uint, delay time.Duration)

type CryptCipherKey added in v1.3.2

type CryptCipherKey []byte

type CryptMessage added in v1.3.1

type CryptMessage []byte

func (CryptMessage) Decode added in v1.3.1

func (cm CryptMessage) Decode(ck CipherKey) (msg Message, err error)

type Handler added in v1.2.0

type Handler func(ctx context.Context, req Request) (res Response, err error)

type HandlerType added in v1.2.5

type HandlerType uint

type Limiter added in v1.2.0

type Limiter struct {
	Timeout
	// contains filtered or unexported fields
}

type Logger added in v1.2.0

type Logger interface {
	Info(msg string)
	Warn(msg string)
	Error(msg string)
}

type Message added in v1.2.0

type Message struct {
	Topic   string
	Content []byte
	Error   error
}

func (Message) Encode added in v1.3.1

func (msg Message) Encode(ck CipherKey) (cm CryptMessage, err error)

type Metrics added in v1.2.0

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

type PrivateKey added in v1.3.1

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

func (PrivateKey) Decode added in v1.3.1

func (pk PrivateKey) Decode(cck CryptCipherKey) (ck CipherKey, err error)

type PublicKey added in v1.3.1

type PublicKey struct {
	Key rsa.PublicKey
}

func (PublicKey) Encode added in v1.3.1

func (pk PublicKey) Encode(ck CipherKey) (cck CryptCipherKey, err error)

type RSA added in v1.3.1

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

func NewRSA added in v1.3.1

func NewRSA() (r *RSA, err error)

func (*RSA) PrivateKey added in v1.3.1

func (r *RSA) PrivateKey() (pk PrivateKey)

func (*RSA) PublicKey added in v1.3.1

func (r *RSA) PublicKey() (pk PublicKey)

type Request

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

func (*Request) GetBytes added in v1.3.0

func (req *Request) GetBytes() (bs []byte)

func (*Request) GetGob added in v1.3.0

func (req *Request) GetGob(val interface{}) (err error)

func (*Request) GetJson added in v1.3.0

func (req *Request) GetJson(val interface{}) (err error)

func (*Request) SetBytes added in v1.3.0

func (req *Request) SetBytes(bs []byte)

func (*Request) SetGob added in v1.3.0

func (req *Request) SetGob(val interface{}) (err error)

func (*Request) SetJson added in v1.3.0

func (req *Request) SetJson(val interface{}) (err error)

func (*Request) String added in v1.3.0

func (req *Request) String() (str string)

type Response

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

func (*Response) GetBytes added in v1.3.0

func (res *Response) GetBytes() (bs []byte)

func (*Response) GetGob added in v1.3.0

func (res *Response) GetGob(val interface{}) (err error)

func (*Response) GetJson added in v1.3.0

func (res *Response) GetJson(val interface{}) (err error)

func (*Response) SetBytes added in v1.3.0

func (res *Response) SetBytes(bs []byte)

func (*Response) SetGob added in v1.3.0

func (res *Response) SetGob(val interface{}) (err error)

func (*Response) SetJson added in v1.3.0

func (res *Response) SetJson(val interface{}) (err error)

func (*Response) String added in v1.3.0

func (res *Response) String() (str string)

type Retry added in v1.2.0

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

type Server added in v1.2.0

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

func NewServer added in v1.2.0

func NewServer(tcp *TCP, stg *ServerSettings) (s *Server, err error)

func (*Server) Serve added in v1.2.0

func (s *Server) Serve() (err error)

func (*Server) SetContext added in v1.2.4

func (s *Server) SetContext(ctx context.Context)

func (*Server) SetHandle added in v1.3.0

func (s *Server) SetHandle(topic string, handler Handler)

type ServerSettings added in v1.2.0

type ServerSettings struct {
	Limiter
	Logger
}

func NewServerSettings added in v1.2.0

func NewServerSettings() (stg *ServerSettings)

func (*ServerSettings) SetBodyLimit added in v1.2.2

func (stg *ServerSettings) SetBodyLimit(limit uint)

func (*ServerSettings) SetConnTimeout added in v1.2.0

func (stg *ServerSettings) SetConnTimeout(dur time.Duration)

func (*ServerSettings) SetHandleTimeout added in v1.2.0

func (stg *ServerSettings) SetHandleTimeout(dur time.Duration)

func (*ServerSettings) SetLogger added in v1.2.0

func (stg *ServerSettings) SetLogger(l Logger)

type TCP

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

func NewTCP

func NewTCP(host string, port uint) (tcp *TCP)

type Timeout added in v1.2.0

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

Directories

Path Synopsis
_example

Jump to

Keyboard shortcuts

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