Documentation ¶
Index ¶
- func HashSKey(key []byte) [20]byte
- type Conn
- type CryptoMethod
- func Accept(conn net.Conn, handshakeTimeout time.Duration, ...) (encConn net.Conn, cipher CryptoMethod, peerExtensions [8]byte, peerID [20]byte, ...)
- func Dial(addr net.Addr, deadline time.Time, ourExtensions [8]byte, ih [20]byte, ...) (conn net.Conn, cipher CryptoMethod, peerExtensions [8]byte, peerID [20]byte, ...)
- type Stream
- func (s *Stream) HandshakeIncoming(getSKey func(sKeyHash [20]byte) (sKey []byte), ...) (err error)
- func (s *Stream) HandshakeOutgoing(sKey []byte, cryptoProvide CryptoMethod, initialPayload []byte) (selected CryptoMethod, err error)
- func (s *Stream) Read(p []byte) (n int, err error)
- func (s *Stream) Write(p []byte) (n int, err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Conn ¶
Conn is a wrapper around net.Conn that does encryption/decryption on Read/Write methods.
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.