noise

package module
v0.45.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2022 License: AGPL-3.0 Imports: 17 Imported by: 0

README

P2P Noise

Go Go Reference Go Report Card codecov

P2P Noise library aims to serve as a tool to create secure P2P networks based on the Noise Framework.

  • Quick creation of custom P2P networks.
  • Simplistic and lightweight.
  • Small and secure.

Features

Blake2 Hashing: BLAKE2 is a cryptographic hash function faster than MD5, SHA-1, SHA-2, and SHA-3, yet is at least as secure as the latest standard SHA-3. BLAKE2 has been adopted by many projects due to its high speed, security, and simplicity.

ED255519 Signature: A digital signature is a mathematical scheme for verifying the authenticity of digital messages or documents. A valid digital signature, where the prerequisites are satisfied, gives a recipient very high confidence that the message was created by a known sender (authenticity), and that the message was not altered in transit (integrity).

Noise Secure Handshake: Noise is a framework for building crypto protocols. Noise protocols support mutual and optional authentication, identity hiding, forward secrecy, zero round-trip encryption, and other advanced features.

Adaptive Lookup for Unstructured Peer-to-Peer Overlays: Most of the unstructured peer-to-peer overlays do not provide any performance guarantee. "Adaptive Lookup" propose a novel Quality of Service enabled lookup for unstructured peer-to-peer overlays that will allow the user’s query to traverse only those overlay links which satisfy the given constraints.

Install

go get github.com/geolffreym/p2p-noise

Basic usage


import (
	noise "github.com/geolffreym/p2p-noise"
	"github.com/geolffreym/p2p-noise/config"
)

func main() {

	// Create configuration from params and write in configuration reference
	configuration := config.New()
	configuration.Write(
		config.SetMaxPeersConnected(10),
		config.SetPeerDeadline(1800),
	)

	// Node factory
	node := noise.New(configuration)
	// Network events channel
	signals, cancel := node.Signals()

	go func() {
		for signal := range signals {
			// Here could be handled events
			if signal.Type() == noise.NewPeerDetected {
				cancel()
			}
		}
	}()

	// ... some code here
	// node.Dial("192.168.1.1:4008")
	// node.Close()

	// ... more code here
	node.Listen()

}

Development

Some available capabilities for dev support:

  • Run Tests: make test
  • Build: make build
  • Test Coverage: make coverage
  • Benchmark: make benchmark
  • Profiling: make profiling
  • Code check: make code-check
  • Code format: make code-fmt
  • Flush cache: make clean
  • Build: make build

Note: Please check Makefile for more capabilities.

More info

Documentation

Overview

P2P Noise Library. Please read more about Noise Protocol.

Index

Constants

This section is empty.

Variables

View Source
var HandshakePattern = noise.HandshakeXX

Default Handshake "XX" noise pattern. Our approach its use a balanced "time/security" pattern. Please see NoisePatternExplorer for more details.

Functions

This section is empty.

Types

type BytePool added in v0.33.0

type BytePool = *bpool.BytePool

BytePool implements a leaky pool of []byte in the form of a bounded channel.

type CipherState added in v0.33.0

type CipherState = *noise.CipherState

CipherState provides symmetric encryption and decryption after a successful handshake. Please see docs for more information.

type Config added in v0.11.0

type Config interface {
	// Default "tcp"
	Protocol() string
	// Default 0.0.0.0:8010
	SelfListeningAddress() string
	// Default 100
	MaxPeersConnected() uint8
	// Default 10 << 20 = 10MB
	MaxPayloadSize() uint32
	// Default 3600 seconds = 60 minutes
	PeerDeadline() time.Duration
	// Default 5 seconds
	DialTimeout() time.Duration
}

type DHKey added in v0.34.0

type DHKey = noise.DHKey

DHKey is a keypair used for Diffie-Hellman key agreement. Please see docs for more details.

type EDKeyPair added in v0.39.0

type EDKeyPair struct {
	Private PrivateKey
	Public  PublicKey
}

EDKeyPair hold public/private using entropy from rand. Every new handshake generate a new key pair.

type Event

type Event uint8

Event aliases for int type.

const (
	// Event to notify when a new peer get connected
	NewPeerDetected Event = iota
	// On new message received event
	MessageReceived
	// Closed peer connection
	PeerDisconnected
)

type HandshakeState added in v0.33.0

type HandshakeState interface {
	// WriteMessage appends a handshake message to out. The message will include the
	// optional payload if provided. If the handshake is completed by the call, two
	// CipherStates will be returned, one is used for encryption of messages to the
	// remote peer, the other is used for decryption of messages from the remote
	// peer. It is an error to call this method out of sync with the handshake
	// pattern.
	WriteMessage(out, payload []byte) ([]byte, CipherState, CipherState, error)
	// ReadMessage processes a received handshake message and appends the payload,
	// if any to out. If the handshake is completed by the call, two CipherStates
	// will be returned, one is used for encryption of messages to the remote peer,
	// the other is used for decryption of messages from the remote peer. It is an
	// error to call this method out of sync with the handshake pattern.
	ReadMessage(out, message []byte) ([]byte, CipherState, CipherState, error)
	// PeerStatic returns the static key provided by the remote peer during
	// a handshake. It is an error to call this method if a handshake message
	// containing a static key has not been read.
	PeerStatic() []byte
	// MessageIndex returns the current handshake message id
	MessageIndex() int
}

HandshakeState tracks the state of a Noise handshake. It may be discarded after the handshake is complete. Please see docs for more information.

type ID added in v0.33.0

type ID [32]byte

ID it's identity provider for peer.

func (ID) Bytes added in v0.33.0

func (i ID) Bytes() []byte

Bytes return a byte slice representation for id.

func (ID) String added in v0.33.0

func (i ID) String() string

String return a string representation for 32-bytes hash.

type KeyRing added in v0.39.0

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

KeyRing hold the set of local keys to use during handshake and session.

type NetError added in v0.11.0

type NetError struct {
	Context string
	Err     error
}

NetError represents errors related to network communication.

func (NetError) Error added in v0.11.0

func (e NetError) Error() string

Error give string representation of error based on error type.

type Node

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

func New

func New(config Config) *Node

New create a new node with defaults

func (*Node) Close

func (n *Node) Close()

Close all peers connections and stop listening.

func (*Node) Dial

func (n *Node) Dial(addr string) error

Dial attempt to connect to remote node and add connected peer to routing table. Return error if error occurred while dialing node.

func (*Node) Listen

func (n *Node) Listen() error

Listen start listening on the given address and wait for new connection. Return error if error occurred while listening.

func (*Node) Send added in v0.18.0

func (n *Node) Send(id ID, message []byte) (uint32, error)

Send emit a new message using peer id. If peer id doesn't exists or peer is not connected return error. Calling Send extends write deadline.

func (*Node) Signals added in v0.11.0

func (n *Node) Signals() (<-chan Signal, context.CancelFunc)

Signals proxy channels to subscriber. The listening routine should be stopped using context param.

type OperationalError added in v0.11.0

type OperationalError struct {
	Context string
	Err     error
}

OperationalError represents an error that occurred when an operation in node failed. eg. Send a new message to invalid or not connected peer. eg. Error during Handshake.

func (OperationalError) Error added in v0.11.0

func (e OperationalError) Error() string

Error give string representation of error based on error type.

type OverflowError added in v0.11.0

type OverflowError struct {
	Context string
	Err     error
}

OverflowError error represents a problem with the maximum setting of a parameter being exceeded. eg. MaxPeersConnected exceeded for incoming connections.

func (OverflowError) Error added in v0.11.0

func (e OverflowError) Error() string

Error give string representation of error based on error type.

type PrivateKey added in v0.39.0

type PrivateKey = ed25519.PrivateKey

type PublicKey added in v0.39.0

type PublicKey = ed25519.PublicKey

type SecError added in v0.34.0

type SecError struct {
	Context string
	Err     error
}

SecError represents errors related to network security.

func (SecError) Error added in v0.34.0

func (e SecError) Error() string

Error give string representation of error based on error type.

type Signal added in v0.30.0

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

Signal it is a message interface to transport network events. Each Signal keep a immutable state holding original header and body.

func (*Signal) Payload added in v0.30.0

func (s *Signal) Payload() string

Payload return an immutable payload.

func (*Signal) Reply added in v0.30.0

func (s *Signal) Reply(msg []byte) (uint32, error)

Reply send an answer to peer in context.

func (*Signal) Type added in v0.30.0

func (s *Signal) Type() Event

Type forward internal signal header event type.

Directories

Path Synopsis
Package conf provide a "functional option" design pattern to handle node settings.
Package conf provide a "functional option" design pattern to handle node settings.
examples

Jump to

Keyboard shortcuts

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