torckapi 0.0.11 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/torckapi/errors.rb +2 -0
- data/lib/torckapi/response/error.rb +1 -1
- data/lib/torckapi/tracker/udp.rb +13 -28
- data/lib/torckapi/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 521381cc5f7043c7b0895498adb494875ab07247
|
4
|
+
data.tar.gz: 0d7a63fd41723fc794232936489e56fac2fd7d87
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6854a5a7625fd0b28ec785702eccb9e8a789491056f2c2ea1767f92567d30c86a11b655f4a45f18235a249037a7e902f6f1d605cc0746e610f3c992f0437d2dc
|
7
|
+
data.tar.gz: 8a52f192ad8183ce3795824a3c86da5235daf89c0691efa7a2acc3c189aa97aa661891ad7352ea43d3a28b8ed067fd448fa1753b9dd7880ff19880246ff08f6e
|
data/lib/torckapi/errors.rb
CHANGED
@@ -9,6 +9,8 @@ module Torckapi
|
|
9
9
|
class CommunicationError < Error; end
|
10
10
|
class CommunicationFailedError < CommunicationError; end
|
11
11
|
class CommunicationTimeoutError < CommunicationError; end
|
12
|
+
class MalformedResponseError < CommunicationError; end
|
13
|
+
class TransactionIdMismatchError < CommunicationError; end
|
12
14
|
end
|
13
15
|
|
14
16
|
module Response
|
@@ -14,7 +14,7 @@ module Torckapi
|
|
14
14
|
# @param data [String] UDP response data (omit action and transaction_id)
|
15
15
|
# @return [Torckapi::Response::Error] response
|
16
16
|
def self.from_udp info_hashes, data
|
17
|
-
new
|
17
|
+
new(info_hashes, data || "")
|
18
18
|
end
|
19
19
|
|
20
20
|
def info_hash
|
data/lib/torckapi/tracker/udp.rb
CHANGED
@@ -9,26 +9,20 @@ module Torckapi
|
|
9
9
|
# Implementation of http://www.bittorrent.org/beps/bep_0015.html
|
10
10
|
class UDP < Base
|
11
11
|
CONNECTION_TIMEOUT = 60
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
RESPONSES = [
|
16
|
-
[nil, 16],
|
17
|
-
[Torckapi::Response::Announce, 20],
|
18
|
-
[Torckapi::Response::Scrape, 8],
|
19
|
-
[Torckapi::Response::Error, 8]
|
20
|
-
].freeze
|
12
|
+
REQUEST_ACTIONS = [Connect = 0, Announce = 1, Scrape = 2].freeze
|
13
|
+
RESPONSE_CLASSES = [nil, Torckapi::Response::Announce, Torckapi::Response::Scrape, Torckapi::Response::Error].freeze
|
14
|
+
RESPONSE_MIN_LENGTHS = [16, 20, 8, 8].freeze
|
21
15
|
|
22
16
|
# (see Base#announce)
|
23
|
-
def announce info_hash
|
17
|
+
def announce info_hash, peer_id=SecureRandom.random_bytes(20)
|
24
18
|
super info_hash
|
25
|
-
perform_request
|
19
|
+
perform_request Announce, announce_request_data(info_hash, peer_id), info_hash
|
26
20
|
end
|
27
21
|
|
28
22
|
# (see Base#scrape)
|
29
23
|
def scrape info_hashes=[]
|
30
24
|
super info_hashes
|
31
|
-
perform_request
|
25
|
+
perform_request Scrape, scrape_request_data(info_hashes), info_hashes
|
32
26
|
end
|
33
27
|
|
34
28
|
private
|
@@ -37,21 +31,11 @@ module Torckapi
|
|
37
31
|
connect
|
38
32
|
response = communicate action, data
|
39
33
|
|
40
|
-
|
41
|
-
action = response[0][0..3].unpack('L>')[0]
|
42
|
-
raise CommunicationFailedError if RESPONSES[action][1] > response[0].length
|
43
|
-
|
44
|
-
begin
|
45
|
-
RESPONSES[action][0].from_udp(*args, response[0][8..-1])
|
46
|
-
rescue Torckapi::Response::ArgumentError => e
|
47
|
-
$stderr.puts "Error: #{e.inspect}"
|
48
|
-
$stderr.puts "Response: #{response.inspect}"
|
49
|
-
raise CommunicationFailedError
|
50
|
-
end
|
34
|
+
RESPONSE_CLASSES[response[0][0..3].unpack('L>')[0]].from_udp(*args, response[0][8..-1])
|
51
35
|
end
|
52
36
|
|
53
|
-
def announce_request_data info_hash
|
54
|
-
[[info_hash].pack('H*'),
|
37
|
+
def announce_request_data info_hash, peer_id
|
38
|
+
[[info_hash].pack('H*'), peer_id, [0, 0, 0, 0, 0, 0, -1, 0].pack('Q>3L>4S>')].join
|
55
39
|
end
|
56
40
|
|
57
41
|
def scrape_request_data info_hashes
|
@@ -62,8 +46,7 @@ module Torckapi
|
|
62
46
|
return if @connection_id && @communicated_at.to_i >= Time.now.to_i - CONNECTION_TIMEOUT
|
63
47
|
|
64
48
|
@connection_id = [0x41727101980].pack('Q>')
|
65
|
-
response = communicate
|
66
|
-
raise ConnectionFailedError if response.nil? or 16 > response[0].length
|
49
|
+
response = communicate Connect
|
67
50
|
@connection_id = response[0][8..15]
|
68
51
|
end
|
69
52
|
|
@@ -81,13 +64,15 @@ module Torckapi
|
|
81
64
|
@socket.send(packet, 0, @url.host, @url.port)
|
82
65
|
response = @socket.recvfrom(65536)
|
83
66
|
raise TransactionIdMismatchError if transaction_id != response[0][4..7]
|
67
|
+
raise MalformedResponseError if RESPONSE_MIN_LENGTHS[response[0][0..3].unpack('L>')[0]] > response[0].length
|
84
68
|
@communicated_at = Time.now
|
85
69
|
end
|
86
70
|
rescue CommunicationTimeoutError
|
87
71
|
retry if (tries += 1) <= @options[:tries]
|
88
|
-
raise CommunicationFailedError
|
89
72
|
end
|
90
73
|
|
74
|
+
raise CommunicationFailedError if response.nil?
|
75
|
+
|
91
76
|
response
|
92
77
|
end
|
93
78
|
end
|
data/lib/torckapi/version.rb
CHANGED