gop2p

package module
v0.0.0-...-5e16a5b Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package gop2p provides a simple API for establishing encrypted peer-to-peer connections over UDP. Each node may have multiple connections with other nodes, each connection may have multiple streams. gop2p uses udp6 for all connections, with reliable transmission and encryption on each stream.

Example usage:

node, err := NewNode(localPrivateKeyED, localPublicKeyED, udpAddr)
if err != nil {
  log.Fatal(err)
}
defer node.Shutdown()
ctx, cancel := context.WithTimeout(context.Background(), 5 * time.Second)
defer cancel()
peerAddr, err := node.AcceptPeer(ctx)
if err != nil {
  log.Fatal(err)
}
defer node.ClosePeer(peerAddr)
streamID, err := node.AcceptStream(ctx, peerAddr)
if err != nil {
  log.Fatal(err)
}
defer node.CloseStream(peerAddr, streamID)
buf := make([]byte, 1024)
n, err := node.Recv(ctx, buf, peerAddr, streamID)
if err != nil {
  log.Fatal(err)
}
_, err = node.Send(ctx, []byte("Hello"), peerAddr, streamID)
if err != nil {
  log.Fatal(err)
}

Index

Constants

View Source
const (
	Version       = 0
	MaxPacketSize = 1184
)
View Source
const (
	PacketHello byte = iota
	PacketHelloRetry
	PacketIntroduction
	PacketData
	PacketConnectionClosed
)
View Source
const (
	DataAck       byte = 1
	DataClosed    byte = 2
	DataNewStream byte = 4
)
View Source
const (
	PublicKeyDHSize = 32
	PublicKeyEDSize = 32
	SignatureSize   = 64
	CookieSize      = 32
	IPV6Size        = 16
	PortSize        = 2
)
View Source
const (
	DataIDSize             = 1
	DataTypeSize           = 1
	DataSequenceNumberSize = 4
	DataAckNumberSize      = 4
	DataHeaderSize         = DataIDSize + DataTypeSize + DataSequenceNumberSize + DataAckNumberSize + DataLengthSize
	DataLengthSize         = 2
	MaxDataSize            = MaxPacketSize - DataHeaderSize - 16
)
View Source
const (
	VersionSize      = 1
	PacketTypeSize   = 1
	PaddingSize      = 1
	HeaderSize       = VersionSize + PacketTypeSize + PaddingSize
	HelloSize        = PublicKeyDHSize + PublicKeyEDSize + SignatureSize + CookieSize
	IntroductionSize = 1 + PublicKeyDHSize + PublicKeyEDSize + SignatureSize + IPV6Size + PortSize
)
View Source
const (
	IntroductionIsSourceAddress = 1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CancelledError

type CancelledError struct{}

func (CancelledError) Error

func (e CancelledError) Error() string

type ChannelClosedError

type ChannelClosedError struct{}

func (ChannelClosedError) Error

func (e ChannelClosedError) Error() string

type InvalidPacketError

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

func (InvalidPacketError) Error

func (e InvalidPacketError) Error() string

type Node

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

Node represents a peer-to-peer node. It is used to establish encrypted connections with other nodes.

func NewNode

func NewNode(localPrivateKeyED []byte, localPublicKeyED []byte, udpAddr *net.UDPAddr, maxConnectionQueue int, maxStreamQueue int) (*Node, error)

NewNode creates a new Node.

func (*Node) AcceptPeer

func (node *Node) AcceptPeer(ctx context.Context) (*net.UDPAddr, error)

AcceptPeer waits for a connection from a peer, returns the peer's address and error.

func (*Node) AcceptStream

func (node *Node) AcceptStream(ctx context.Context, addr *net.UDPAddr) (byte, error)

AcceptStream waits for a stream opened from a peer, returns the stream ID and an error.

func (*Node) Ack

func (node *Node) Ack(addr *net.UDPAddr, streamID byte) error

Ack sends an acknowledgment to a peer on a specific stream, which should promt the peer to send more data.

func (*Node) ClosePeer

func (node *Node) ClosePeer(addr *net.UDPAddr) error

ClosePeer gracefully closes a connection with a peer.

func (*Node) CloseStream

func (node *Node) CloseStream(addr *net.UDPAddr, streamID byte) error

CloseStream closes a stream with a peer.

func (*Node) ConnectPeer

func (node *Node) ConnectPeer(ctx context.Context, addr *net.UDPAddr) error

ConnectPeer establishes a connection with a peer.

func (*Node) ConnectPeerViaPeer

func (node *Node) ConnectPeerViaPeer(ctx context.Context, addr *net.UDPAddr, intermediate *net.UDPAddr) error

ConnectPeerViaPeer establishes a connection with a peer through an intermediate peer.

func (*Node) ConnectStream

func (node *Node) ConnectStream(addr *net.UDPAddr, streamID byte) error

OpenStream opens a stream with a peer.

func (*Node) GetPeerPublicKey

func (node *Node) GetPeerPublicKey(addr *net.UDPAddr) ([]byte, error)

GetPeerPublicKey returns the public key of a peer.

func (*Node) IsConnected

func (node *Node) IsConnected(addr *net.UDPAddr) bool

IsConnected returns true if a connection with a peer is established.

func (*Node) Recv

func (node *Node) Recv(ctx context.Context, buf []byte, addr *net.UDPAddr, streamID byte) (int, error)

Recv receives data from a specific peer, returns the data, stream ID and an error.

func (*Node) Send

func (node *Node) Send(ctx context.Context, data []byte, addr *net.UDPAddr, streamID byte) (int, error)

Send sends data to a specific peer on a specific channel, which must be opened or accepted before, returns number of bytes sent and an error.

func (*Node) Shutdown

func (node *Node) Shutdown()

Shutdown closes all connections and stops the node.

type PeerConnectionAlreadyEstablishedError

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

func (PeerConnectionAlreadyEstablishedError) Error

type PeerConnectionNotEstablishedError

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

func (PeerConnectionNotEstablishedError) Error

type ReadWriteCloserStream

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

ReadWriteStream is a stream on a specific address implements io.ReadWriteCloser. Before reading or writing, the stream must be opened by either accepting or connecting then open the stream.

func NewReadWriteStream

func NewReadWriteStream(node *Node, addr *net.UDPAddr, streamID byte, timeout time.Duration) *ReadWriteCloserStream

func (*ReadWriteCloserStream) Close

func (s *ReadWriteCloserStream) Close() error

func (*ReadWriteCloserStream) Read

func (s *ReadWriteCloserStream) Read(data []byte) (int, error)

func (*ReadWriteCloserStream) Write

func (s *ReadWriteCloserStream) Write(data []byte) (int, error)

type StreamAlreadyEstablishedError

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

func (StreamAlreadyEstablishedError) Error

type StreamNotEstablishedError

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

func (StreamNotEstablishedError) Error

Jump to

Keyboard shortcuts

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