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 func handleEncryption(
32 skeys mse.SecretKeyIter,
33 policy EncryptionPolicy,
37 cryptoMethod mse.CryptoMethod,
40 if !policy.ForceEncryption {
41 var protocol [len(pp.Protocol)]byte
42 _, err = io.ReadFull(rw, protocol[:])
50 io.MultiReader(bytes.NewReader(protocol[:]), rw),
53 if string(protocol[:]) == pp.Protocol {
58 headerEncrypted = true
59 ret, cryptoMethod, err = mse.ReceiveHandshake(rw, skeys, func(provides mse.CryptoMethod) mse.CryptoMethod {
61 case policy.ForceEncryption:
62 return mse.CryptoMethodRC4
63 case policy.DisableEncryption:
64 return mse.CryptoMethodPlaintext
65 case policy.PreferNoEncryption && provides&mse.CryptoMethodPlaintext != 0:
66 return mse.CryptoMethodPlaintext
68 return mse.DefaultCryptoSelector(provides)
74 type PeerExtensionBits = pp.PeerExtensionBits