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
- type CancelledError
- type ChannelClosedError
- type InvalidPacketError
- type Node
- func (node *Node) AcceptPeer(ctx context.Context) (*net.UDPAddr, error)
- func (node *Node) AcceptStream(ctx context.Context, addr *net.UDPAddr) (byte, error)
- func (node *Node) Ack(addr *net.UDPAddr, streamID byte) error
- func (node *Node) ClosePeer(addr *net.UDPAddr) error
- func (node *Node) CloseStream(addr *net.UDPAddr, streamID byte) error
- func (node *Node) ConnectPeer(ctx context.Context, addr *net.UDPAddr) error
- func (node *Node) ConnectPeerViaPeer(ctx context.Context, addr *net.UDPAddr, intermediate *net.UDPAddr) error
- func (node *Node) ConnectStream(addr *net.UDPAddr, streamID byte) error
- func (node *Node) GetPeerPublicKey(addr *net.UDPAddr) ([]byte, error)
- func (node *Node) IsConnected(addr *net.UDPAddr) bool
- func (node *Node) Recv(ctx context.Context, buf []byte, addr *net.UDPAddr, streamID byte) (int, error)
- func (node *Node) Send(ctx context.Context, data []byte, addr *net.UDPAddr, streamID byte) (int, error)
- func (node *Node) Shutdown()
- type PeerConnectionAlreadyEstablishedError
- type PeerConnectionNotEstablishedError
- type ReadWriteCloserStream
- type StreamAlreadyEstablishedError
- type StreamNotEstablishedError
Constants ¶
const ( Version = 0 MaxPacketSize = 1184 )
const ( PacketHello byte = iota PacketHelloRetry PacketIntroduction PacketData PacketConnectionClosed )
const ( DataAck byte = 1 DataClosed byte = 2 DataNewStream byte = 4 )
const ( PublicKeyDHSize = 32 PublicKeyEDSize = 32 SignatureSize = 64 CookieSize = 32 IPV6Size = 16 PortSize = 2 )
const ( DataIDSize = 1 DataTypeSize = 1 DataSequenceNumberSize = 4 DataAckNumberSize = 4 DataHeaderSize = DataIDSize + DataTypeSize + DataSequenceNumberSize + DataAckNumberSize + DataLengthSize DataLengthSize = 2 MaxDataSize = MaxPacketSize - DataHeaderSize - 16 )
const ( VersionSize = 1 PacketTypeSize = 1 PaddingSize = 1 HeaderSize = VersionSize + PacketTypeSize + PaddingSize HelloSize = PublicKeyDHSize + PublicKeyEDSize + SignatureSize + CookieSize IntroductionSize = 1 + PublicKeyDHSize + PublicKeyEDSize + SignatureSize + IPV6Size + PortSize )
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 ¶
AcceptPeer waits for a connection from a peer, returns the peer's address and error.
func (*Node) AcceptStream ¶
AcceptStream waits for a stream opened from a peer, returns the stream ID and an error.
func (*Node) Ack ¶
Ack sends an acknowledgment to a peer on a specific stream, which should promt the peer to send more data.
func (*Node) CloseStream ¶
CloseStream closes a stream with a peer.
func (*Node) ConnectPeer ¶
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 ¶
OpenStream opens a stream with a peer.
func (*Node) GetPeerPublicKey ¶
GetPeerPublicKey returns the public key of a peer.
func (*Node) IsConnected ¶
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.
type PeerConnectionAlreadyEstablishedError ¶
type PeerConnectionAlreadyEstablishedError struct {
// contains filtered or unexported fields
}
func (PeerConnectionAlreadyEstablishedError) Error ¶
func (e PeerConnectionAlreadyEstablishedError) Error() string
type PeerConnectionNotEstablishedError ¶
type PeerConnectionNotEstablishedError struct {
// contains filtered or unexported fields
}
func (PeerConnectionNotEstablishedError) Error ¶
func (e PeerConnectionNotEstablishedError) Error() string
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 (*ReadWriteCloserStream) Close ¶
func (s *ReadWriteCloserStream) Close() error
type StreamAlreadyEstablishedError ¶
type StreamAlreadyEstablishedError struct {
// contains filtered or unexported fields
}
func (StreamAlreadyEstablishedError) Error ¶
func (e StreamAlreadyEstablishedError) Error() string
type StreamNotEstablishedError ¶
type StreamNotEstablishedError struct {
// contains filtered or unexported fields
}
func (StreamNotEstablishedError) Error ¶
func (e StreamNotEstablishedError) Error() string