fix message encode issue

This commit is contained in:
liwei committed 2019-07-09 10:34:33 +08:00
1 parent d6c88d0c4b
commit 075852b847
2 files changed
+16 -8

No files matched your search

+2 -2
View File
@@ -4,7 +4,7 @@ import (
"encoding/binary" "encoding/binary"
) )
type MessageID byte type MessageID int
const ( const (
Keepalive MessageID = -1 Keepalive MessageID = -1
@@ -42,7 +42,7 @@ func (msg *Message) Encode() []byte {
buf := make([]byte, 5+len(msg.Payload)) buf := make([]byte, 5+len(msg.Payload))
binary.BigEndian.PutUint32(buf, msg.Length) binary.BigEndian.PutUint32(buf, msg.Length)
buf[4] = byte(msg.ID) buf[4] = byte(msg.ID)
buf = append(buf, msg.Payload[:]...) copy(buf[5:], msg.Payload[:])
return buf return buf
} }
} }
+14 -6
View File
@@ -2,6 +2,7 @@ package torrent
import ( import (
"bufio" "bufio"
"encoding/binary"
"fmt" "fmt"
"github.com/cenkalti/mse" "github.com/cenkalti/mse"
"gopeed/down/bt/peer" "gopeed/down/bt/peer"
@@ -26,7 +27,7 @@ type peerState struct {
} }
// 使用MSE加密来避免运营商对bt流量的封锁,基本上现在市面上BT客户端都默认开启了,不用MSE的话很多Peer拒绝连接 // 使用MSE加密来避免运营商对bt流量的封锁,基本上现在市面上BT客户端都默认开启了,不用MSE的话很多Peer拒绝连接
// http://wiki.vuze.com/w/Message_Stream_Encryption // see http://wiki.vuze.com/w/Message_Stream_Encryption
func (ps *peerState) dialMse() error { func (ps *peerState) dialMse() error {
conn, err := net.Dial("tcp", ps.Address()) conn, err := net.Dial("tcp", ps.Address())
if err != nil { if err != nil {
@@ -43,7 +44,7 @@ func (ps *peerState) dialMse() error {
} }
// Handshake of Peer wire protocol // Handshake of Peer wire protocol
// https://wiki.theory.org/index.php/BitTorrentSpecification#Handshake // see https://wiki.theory.org/index.php/BitTorrentSpecification#Handshake
func (ps *peerState) handshake() (*peer.Handshake, error) { func (ps *peerState) handshake() (*peer.Handshake, error) {
handshakeRes, err := func() (*peer.Handshake, error) { handshakeRes, err := func() (*peer.Handshake, error) {
handshakeReq := peer.NewHandshake([8]byte{}, ps.torrent.MetaInfo.InfoHash, ps.torrent.PeerID) handshakeReq := peer.NewHandshake([8]byte{}, ps.torrent.MetaInfo.InfoHash, ps.torrent.PeerID)
@@ -79,7 +80,6 @@ func (ps *peerState) handshake() (*peer.Handshake, error) {
return handshakeRes, nil return handshakeRes, nil
} }
//
func (ps *peerState) download() error { func (ps *peerState) download() error {
scanner := bufio.NewScanner(ps.conn) scanner := bufio.NewScanner(ps.conn)
scanner.Split(peer.SplitMessage) scanner.Split(peer.SplitMessage)
@@ -124,9 +124,17 @@ func (ps *peerState) handleUnchoke() {
if ps.amInterested { if ps.amInterested {
have := ps.getHavePieces(ps.bitfield) have := ps.getHavePieces(ps.bitfield)
if len(have) > 0 { if len(have) > 0 {
/*for _, index := range have { for i := range have {
ps.conn.Write((&peer.Message{13, peer.Request,})) buf := make([]byte, 12)
}*/ binary.BigEndian.PutUint32(buf[:4], uint32(have[i]))
binary.BigEndian.PutUint32(buf[4:8], 0)
binary.BigEndian.PutUint32(buf[8:], 2<<13)
_, err := ps.conn.Write(peer.NewMessage(peer.Request, buf).Encode())
if err != nil {
ps.conn.Close()
return
}
}
return return
} }
} }