-
Notifications
You must be signed in to change notification settings - Fork 2
/
request.go
180 lines (152 loc) · 3.17 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package metadata
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net"
"runtime"
"time"
)
// Request is the metadata request.
type Request struct {
IP string
Port int
HashInfo string
PeerID string
}
// RemoteAddr returns net address of the querying peer.
func (req *Request) RemoteAddr() string {
return fmt.Sprintf("%s:%d", req.IP, req.Port)
}
type metadataQuery struct {
*Request
result *Torrent
err error
done chan struct{}
then Then
reject Reject
dialTimeout time.Duration
}
func newMetadataQuery(req *Request, then Then, reject Reject) *metadataQuery {
return &metadataQuery{
Request: req,
then: then,
reject: reject,
dialTimeout: 10 * time.Second,
done: make(chan struct{}),
}
}
func (query *metadataQuery) wait() {
<-query.done
}
func (query *metadataQuery) start(collect *Collector) {
query.do()
select {
case <-collect.closeQ:
case collect.reply <- struct{}{}:
}
if query.err != nil {
query.reject(*query.Request, query.err)
} else {
query.then(*query.Request, *query.result)
}
close(query.done)
}
func (query *metadataQuery) do() {
defer func() {
v := recover()
if v != nil {
buf := make([]byte, 1024)
n := runtime.Stack(buf, false)
log.Println("[panic]", v, string(buf[:n]))
query.err = errors.New("query panic")
}
}()
conn, err := net.DialTimeout("tcp", query.RemoteAddr(), query.dialTimeout)
if err != nil {
query.err = err
return
}
tcpConn := conn.(*net.TCPConn)
tcpConn.SetLinger(0)
defer tcpConn.Close()
if err := sendHandshake(conn, query); err != nil {
query.err = err
return
}
var buf = new(bytes.Buffer)
if err := recvHandshake(conn, buf); err != nil {
query.err = err
return
}
buf.Reset()
if err := sendHandshakeExt(conn, query); err != nil {
query.err = err
return
}
utMetadata, metadataSize, err := recvHandshakeExt(conn, buf)
if err != nil {
query.err = err
return
}
var BLOCKSIZE int64 = 16384
var pieceNum = metadataSize / BLOCKSIZE
if metadataSize%BLOCKSIZE > 0 {
pieceNum++
}
if pieceNum > 10000 {
query.err = ErrTooMuchPieces
return
}
for i := 0; i < int(pieceNum); i++ {
err = sendPieceRequst(conn, int(utMetadata), i)
if err != nil {
query.err = err
return
}
}
var getPiecesTimeout = time.After(20 * time.Second)
pieces := make([][]byte, int(pieceNum))
getPieces:
for {
select {
case <-getPiecesTimeout:
query.err = ErrGetPiecesTimeout
return
default:
buf.Reset()
bid, _, err := readMessage(conn, buf)
if err != nil {
query.err = err
return
}
if bid != 20 {
buf.Reset()
continue
}
msgType, pieceID, _, offset, err := parsePiece(buf.Bytes())
if err != nil {
query.err = err
return
}
if msgType == REJECT {
query.err = ErrRejectByPeer
return
} else if msgType == DATA {
piece, _ := ioutil.ReadAll(buf)
pieces[pieceID] = piece[offset:]
// log.Println(pieceID, int64(len(piece[offset:])))
if int64(len(piece[offset:])) < BLOCKSIZE {
break getPieces
}
}
}
}
// handle metadata
p := bytes.Join(pieces, nil)
torrent := new(Torrent)
query.err = NewTorrentFromMetadata(p, torrent)
query.result = torrent
return
}