torckapi 0.0.9 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2c9a7381eb1ab2a6327c590e2f536e6c37e57b08
4
- data.tar.gz: 8b3eb6f625736f2189f9c1a62f0185d04bcfeb9b
3
+ metadata.gz: 3a0baba0c9a96c4a5d53910a0aa79c8aae7b7700
4
+ data.tar.gz: ab9bbfc9b647d4d86051b53f2e4f34cc81ece987
5
5
  SHA512:
6
- metadata.gz: d2b7de86de6fdd7b2041616b9b781f5a2cf74505cf6a9a1690844d409b6bc377afe8dab0053948ded9cbe73e4caaedfb4f9ff323b6b9f1b7e2b66d1e75abbfe9
7
- data.tar.gz: 92faf338aa7741f54996764877c35da4f8f6910b5904cae16155e503c2a69e50b56fc8baf3365f6c520ef411f231fbf5843170aaf6d8ba0d732b27fd87eaf835
6
+ metadata.gz: 42281cee6c47ef4a9b9bba6dd237cb21610fd2f79049b85e1d2c9fc36a8a9c320f6ebd43b05f76c884b927eae2a32774e64992586aa2d18847854db7c91ce2b5
7
+ data.tar.gz: 529731b856d62142d9fdddcf5c50ec48a600c3a6bb8bc3ab37bfbc428eadbc336f669e30878de2fbc2594c8624feaef68f1ef9b70a254b12f6d8db8c5ac67984
@@ -5,9 +5,12 @@ module Torckapi
5
5
  module Tracker
6
6
  class Error < Torckapi::Error; end
7
7
  class InvalidSchemeError < Error; end
8
+ class ConnectionFailedError < Error; end
8
9
  class CommunicationError < Error; end
9
- class CommunicationFailedError < CommunicationError; end
10
10
  class CommunicationTimeoutError < CommunicationError; end
11
+ class CommunicationFailedError < CommunicationError; end
12
+ class AnnounceFailedError < CommunicationFailedError; end
13
+ class ScrapeFailedError < CommunicationFailedError; end
11
14
  end
12
15
 
13
16
  module Response
@@ -21,7 +21,7 @@ module Torckapi
21
21
  def self.from_udp info_hash, data
22
22
  raise ArgumentError, "info_hash cannot be nil" if info_hash.nil?
23
23
  raise ArgumentError, "data cannot be nil" if data.nil?
24
- new info_hash, *data[4..11].unpack('L>2'), peers_from_compact(data[12..-1])
24
+ new info_hash, *data[4..11].unpack('L>2'), peers_from_compact(data[12..-1] || '')
25
25
  end
26
26
 
27
27
  # Construct response object from http response data
@@ -30,6 +30,8 @@ module Torckapi
30
30
  # @param compact [true, false] is peer data in compact format?
31
31
  # @return [Torckapi::Response::Announce] response
32
32
  def self.from_http info_hash, data, compact=true
33
+ raise ArgumentError, "info_hash cannot be nil" if info_hash.nil?
34
+ raise ArgumentError, "data cannot be nil" if data.nil?
33
35
  bdecoded_data = BEncode.load(data)
34
36
  new info_hash, *bdecoded_data.values_at("incomplete", "complete"), peers_from_compact(bdecoded_data["peers"])
35
37
  end
@@ -44,6 +46,7 @@ module Torckapi
44
46
  end
45
47
 
46
48
  def self.peers_from_compact data
49
+ # ipv4 address + tcp/udp port = 6 bytes
47
50
  data.unpack('a6' * (data.length / 6)).map { |i| [IPAddr.ntop(i[0..3]), i[4..5].unpack('S>')[0]] }
48
51
  end
49
52
  end
@@ -13,7 +13,8 @@ module Torckapi
13
13
  def self.from_udp info_hashes, data
14
14
  raise ArgumentError, "info_hashes cannot be nil" if info_hashes.nil?
15
15
  raise ArgumentError, "data cannot be nil" if data.nil?
16
- new Hash[info_hashes.zip(data.unpack('a12' * (info_hashes.count)).map { |i| Hash[[:seeders, :completed, :leechers].zip i.unpack('L>3').map(&:to_i)] })]
16
+ raise ArgumentError, "data size invalid" if data.length != info_hashes.count * 12
17
+ new Hash[info_hashes.zip(data.unpack('a12' * info_hashes.count).map { |i| Hash[[:seeders, :completed, :leechers].zip i.unpack('L>3').map(&:to_i)] })]
17
18
  end
18
19
 
19
20
  private
@@ -28,18 +28,23 @@ module Torckapi
28
28
  connect
29
29
  response = communicate action, data
30
30
 
31
+ raise CommunicationFailedError if response.nil?
32
+
31
33
  begin
32
34
  case response[0][0..3].unpack('L>')[0] # action
33
35
  when 1
36
+ raise AnnounceFailedError if 20 > response[0].length
34
37
  Torckapi::Response::Announce.from_udp(*args, response[0][8..-1])
35
38
  when 2
39
+ raise ScrapeFailedError if 8 > response[0].length
36
40
  Torckapi::Response::Scrape.from_udp(*args, response[0][8..-1])
37
41
  when 3
42
+ raise CommunicationFailedError if 8 > response[0].length
38
43
  Torckapi::Response::Error.from_udp(*args, response[0][8..-1])
39
44
  end
40
45
  rescue Torckapi::Response::ArgumentError => e
41
- puts "Error: #{e.inspect}"
42
- puts "Response: #{response.inspect}"
46
+ $stderr.puts "Error: #{e.inspect}"
47
+ $stderr.puts "Response: #{response.inspect}"
43
48
  raise CommunicationFailedError
44
49
  end
45
50
  end
@@ -56,8 +61,9 @@ module Torckapi
56
61
  return if @connection_id && @communicated_at.to_i >= Time.now.to_i - CONNECTION_TIMEOUT
57
62
 
58
63
  @connection_id = [0x41727101980].pack('Q>')
59
- response_body = communicate(0)[0] # connect
60
- @connection_id = response_body[8..15]
64
+ response = communicate 0 # connect
65
+ raise ConnectionFailedError if response.nil? or 16 > response[0].length
66
+ @connection_id = response[0][8..15]
61
67
  end
62
68
 
63
69
  def communicate action, data=nil
@@ -68,6 +74,7 @@ module Torckapi
68
74
 
69
75
  tries = 0
70
76
  response = nil
77
+
71
78
  begin
72
79
  Timeout::timeout(@options[:timeout], CommunicationTimeoutError) do
73
80
  @socket.send(packet, 0, @url.host, @url.port)
@@ -76,11 +83,8 @@ module Torckapi
76
83
  @communicated_at = Time.now
77
84
  end
78
85
  rescue CommunicationTimeoutError
79
- if (tries += 1) <= @options[:tries]
80
- retry
81
- else
82
- raise CommunicationFailedError
83
- end
86
+ retry if (tries += 1) <= @options[:tries]
87
+ raise CommunicationFailedError
84
88
  end
85
89
 
86
90
  response
@@ -1,3 +1,3 @@
1
1
  module Torckapi
2
- VERSION = "0.0.9"
2
+ VERSION = "0.0.10"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: torckapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dennis Krupenik