10 "github.com/anacrolix/torrent/mse"
11 pp "github.com/anacrolix/torrent/peer_protocol"
14 // Wraps a raw connection and provides the interface we want for using the
15 // connection in the message loop.
16 type deadlineReader struct {
21 func (r deadlineReader) Read(b []byte) (int, error) {
22 // Keep-alives should be received every 2 mins. Give a bit of gracetime.
23 err := r.nc.SetReadDeadline(time.Now().Add(150 * time.Second))
25 return 0, fmt.Errorf("error setting read deadline: %s", err)
30 // Handles stream encryption for inbound connections.
31 func handleEncryption(
33 skeys mse.SecretKeyIter,
34 policy HeaderObfuscationPolicy,
35 selector mse.CryptoSelector,
39 cryptoMethod mse.CryptoMethod,
42 // Tries to start an unencrypted stream.
43 if !policy.RequirePreferred || !policy.Preferred {
44 var protocol [len(pp.Protocol)]byte
45 _, err = io.ReadFull(rw, protocol[:])
49 // Put the protocol back into the stream.
54 io.MultiReader(bytes.NewReader(protocol[:]), rw),
57 if string(protocol[:]) == pp.Protocol {
61 if policy.RequirePreferred {
62 // We are here because we require unencrypted connections.
63 err = fmt.Errorf("unexpected protocol string %q and header obfuscation disabled", protocol)
67 headerEncrypted = true
68 ret, cryptoMethod, err = mse.ReceiveHandshake(rw, skeys, selector)
72 type PeerExtensionBits = pp.PeerExtensionBits