class Torrenter::Tracker::UDPTracker
Attributes
interval[R]
response[R]
socket[R]
Public Class Methods
new(tracker, params)
click to toggle source
# File lib/torrenter/tracker/udp_tracker.rb, line 7 def initialize(tracker, params) @url = tracker[/(?<=udp\:\/\/).+(?=\:\d+)/] @port = tracker[/\d+$/].to_i @socket = UDPSocket.new @connection_id = [0x41727101980].pack("Q>") @params = params end
Public Instance Methods
announce_input()
click to toggle source
# File lib/torrenter/tracker/udp_tracker.rb, line 94 def announce_input @connection_id + segment(1) + @transaction_id + @params[:info_hash] + PEER_ID end
announce_msg()
click to toggle source
# File lib/torrenter/tracker/udp_tracker.rb, line 102 def announce_msg announce_input + (segment(0, "Q>") * 3) + (segment(0) * 3) + segment(-1) + segment(@socket.addr[1], ">S") end
bound_peers()
click to toggle source
# File lib/torrenter/tracker/udp_tracker.rb, line 52 def bound_peers @connection_id = @response[-8..-1] @transaction_id = [rand(10000)].pack("I>") send_msg(announce_msg) read_response parse_announce if @response[0..3] == segment(1) end
connect()
click to toggle source
# File lib/torrenter/tracker/udp_tracker.rb, line 23 def connect @transaction_id = segment(rand(10000), "I>") @socket.connect(ip_addr, @port) begin send_msg(connect_msg) read_response bound_peers rescue false end return self end
connect_match?()
click to toggle source
# File lib/torrenter/tracker/udp_tracker.rb, line 90 def connect_match? data[0] == (segment(0) + @transaction_id + @connection_id) end
connect_msg()
click to toggle source
# File lib/torrenter/tracker/udp_tracker.rb, line 98 def connect_msg @connection_id + segment(0) + @transaction_id end
connected?()
click to toggle source
# File lib/torrenter/tracker/udp_tracker.rb, line 44 def connected? @response end
interval?()
click to toggle source
# File lib/torrenter/tracker/udp_tracker.rb, line 15 def interval? interval % Time.now.to_i == 0 end
ip_addr()
click to toggle source
# File lib/torrenter/tracker/udp_tracker.rb, line 48 def ip_addr Socket.getaddrinfo(@url, @port)[0][3] end
parse_announce()
click to toggle source
# File lib/torrenter/tracker/udp_tracker.rb, line 66 def parse_announce if @response[4..7] == @transaction_id @interval = @response[8..11].unpack("I>").first res = @response.slice!(0..11) @leechers = @response.slice!(0..3).unpack("I>").first @seeders = @response.slice!(0..3).unpack("I>").first end end
peer_list()
click to toggle source
# File lib/torrenter/tracker/udp_tracker.rb, line 62 def peer_list format_peers(@response) end
read_response()
click to toggle source
# File lib/torrenter/tracker/udp_tracker.rb, line 82 def read_response begin @response = @socket.recv(1028) rescue Exception => e e end end
segment(e, enc="I>")
click to toggle source
# File lib/torrenter/tracker/udp_tracker.rb, line 19 def segment(e, enc="I>") [e].pack(enc) end
send_message()
click to toggle source
# File lib/torrenter/tracker/udp_tracker.rb, line 75 def send_message begin @socket.send(@msg, 0) rescue *EXCEPTIONS end end
send_msg(msg)
click to toggle source
# File lib/torrenter/tracker/udp_tracker.rb, line 36 def send_msg(msg) begin @socket.send(msg, 0) rescue false end end