btconn

package
v1.62.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2024 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HashSKey

func HashSKey(key []byte) [20]byte

HashSKey returns the hash of key.

Types

type Conn

type Conn struct {
	net.Conn
	*Stream
}

Conn is a wrapper around net.Conn that does encryption/decryption on Read/Write methods.

func WrapConn

func WrapConn(conn net.Conn) *Conn

WrapConn returns a new wrapper around conn. You must call HandshakeIncoming or HandshakeOutgoing methods before using Read/Write methods.

func (*Conn) Read

func (c *Conn) Read(p []byte) (n int, err error)

func (*Conn) Write

func (c *Conn) Write(p []byte) (n int, err error)

type CryptoMethod

type CryptoMethod uint32

CryptoMethod is 32-bit bitfield each bit representing a single crypto method.

const (
	PlainText CryptoMethod = 1 << iota
	RC4
)

Crypto methods

func Accept

func Accept(
	conn net.Conn,
	handshakeTimeout time.Duration,
	getSKey func(sKeyHash [20]byte) (sKey []byte),
	hasInfoHash func([20]byte) bool,
	ourExtensions [8]byte, ourID [20]byte) (
	encConn net.Conn, cipher CryptoMethod, peerExtensions [8]byte, peerID [20]byte, infoHash [20]byte, err error)

Accept BitTorrent handshake from the connection. Handles encryption. Returns a new connection that is ready for sending/receiving BitTorrent protocol messages.

func Dial

func Dial(
	addr net.Addr,
	deadline time.Time,
	ourExtensions [8]byte,
	ih [20]byte,
	ourID [20]byte) (
	conn net.Conn, cipher CryptoMethod, peerExtensions [8]byte, peerID [20]byte, err error)

Dial new connection to the address. Does the BitTorrent protocol handshake. Handles encryption. May try to connect again if encryption does not match with given setting. Returns a net.Conn that is ready for sending/receiving BitTorrent peer protocol messages.

func (CryptoMethod) String

func (c CryptoMethod) String() string

type Stream

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

Stream wraps a io.ReadWriter that automatically does encrypt/decrypt on read/write.

func NewStream

func NewStream(rw io.ReadWriter) *Stream

NewStream returns a new Stream. You must call HandshakeIncoming or HandshakeOutgoing methods before using Read/Write methods. If any error happens during the handshake underlying io.ReadWriter will be closed if it implements io.Closer.

func (*Stream) HandshakeIncoming

func (s *Stream) HandshakeIncoming(
	getSKey func(sKeyHash [20]byte) (sKey []byte),
	cryptoSelect func(provided CryptoMethod) (selected CryptoMethod)) (err error)

HandshakeIncoming initiates MSE handshake for incoming stream.

getSKey must return the correct stream identifier for given sKeyHash. sKeyHash can be calculated with mse.HashSKey function. If there is no matching sKeyHash in your application, you must return nil.

cryptoSelect is a function that takes provided methods as a bitfield and returns the selected crypto method. Function may return zero value that means none of the provided methods are selected and handshake fails.

payloadIn is a buffer for writing initial payload that is coming along with the handshake from the initiator of the handshake. If initial payload does not fit into payloadIn, handshake returns io.ErrShortBuffer.

lenPayloadIn is length of the data read into payloadIn.

processPayloadIn is an optional function that processes incoming initial payload and generate outgoing initial payload. If this function returns an error, handshake fails.

func (*Stream) HandshakeOutgoing

func (s *Stream) HandshakeOutgoing(sKey []byte, cryptoProvide CryptoMethod, initialPayload []byte) (selected CryptoMethod, err error)

HandshakeOutgoing initiates MSE handshake for outgoing stream.

sKey is stream identifier key. Same key must be used at the other side of the stream, otherwise handshake fails.

cryptoProvide is a bitfield for specifying supported encryption methods.

initialPayload is going to be sent along with handshake. It may be nil if you want to wait for the encryption negotiation.

func (*Stream) Read

func (s *Stream) Read(p []byte) (n int, err error)

Read from underlying io.ReadWriter, decrypt bytes and put into p.

func (*Stream) Write

func (s *Stream) Write(p []byte) (n int, err error)

Encrypt bytes in p and write into underlying io.ReadWriter.

Jump to

Keyboard shortcuts

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