File: dump-peers.rb

package info (click to toggle)
libtorrent-ruby 0.3-2
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 260 kB
  • ctags: 330
  • sloc: ruby: 2,751; makefile: 1
file content (45 lines) | stat: -rw-r--r-- 1,472 bytes parent folder | download | duplicates (3)
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
## dump-peers.rb -- command-line peer lister
## Copyright 2004 William Morgan.
##
## This file is part of RubyTorrent. RubyTorrent is free software;
## you can redistribute it and/or modify it under the terms of version
## 2 of the GNU General Public License as published by the Free
## Software Foundation.
##
## RubyTorrent is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License (in the file COPYING) for more details.

require "rubytorrent"

def die(x); $stderr << "#{x}\n" && exit(-1); end
def dump_peer(p)
  "#{(p.peer_id.nil? ? '<not specified>' : p.peer_id.inspect)} on #{p.ip}:#{p.port}"
end
  
fn = ARGV.shift or raise "first argument must be .torrent file"

mi = nil
begin
  mi = RubyTorrent::MetaInfo.from_location(fn)
rescue RubyTorrent::MetaInfoFormatError, RubyTorrent::BEncodingError => e
  die "error parsing metainfo file #{fn}---maybe not a .torrent?"
end

# complete abuse
mi.trackers.each do |track|
  puts "#{track}:"

  tc = RubyTorrent::TrackerConnection.new(track, mi.info.sha1, mi.info.total_length, 9999, "rubytorrent.dumppeer") # complete abuse, i know
  begin
    tc.force_refresh
    puts "<no peers>" if tc.peers.length == 0
    tc.peers.each do |p|
      puts dump_peer(p)
    end
  rescue RubyTorrent::TrackerError => e
    puts "error connecting to tracker: #{e.message}"
  end
end