-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracker.rb
141 lines (121 loc) · 5.99 KB
/
tracker.rb
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
##################################################################
## Ruby BitTorrent Tracker ##
## ##
## ##
## Copyright 2008 Noah ##
## Released under the Creative Commons Attribution License ##
##################################################################
# Require RubyGems
require 'rubygems'
# Require the bencode gem from http://github.com/dasch/ruby-bencode-bindings/tree/master
require 'bencode'
# Require the mysql gem
require 'mysql'
# Require the memcache gem
require 'memcache'
# Require our cache-abstraction class
require 'cache'
# Require the sinatra gem
require 'sinatra'
# Require YAML to parse config files
require 'yaml'
configure do
# Load the config
$config = YAML::load( open('config.yml') )
# Connect to MySQL
$db = Mysql::new($config[:mysql][:host], $config[:mysql][:user], $config[:mysql][:pass], $config[:mysql][:database])
whitelist = $db.query( "SELECT Peer_ID, Client FROM whitelist" )
$whitelist = Array.new
whitelist.each_hash { |client| $whitelist << { :regex => /#{client['Peer_ID']}/, :name => client['Client'] } } # Put a RegEx of each peerid into $whitelist
end
get '/:passkey/announce' do
begin
# Set instance variables for all the query parameters and make sure they exist
required_params = ['passkey', 'info_hash', 'peer_id', 'port', 'uploaded', 'downloaded', 'left']
optional_params = ['event', 'compact', 'no_peer_id', 'ip', 'numwant']
(required_params + optional_params).each do |param|
error "Bad Announce" if (params[param].nil? or params[param].empty?) and required_params.include?(param)
self.instance_variable_set("@" + param, escape(params[param]))
end
@event ||= 'none'
@ip ||= escape(@ip)
@numwant ||= 50
# Make sure client is whitelisted
whitelisted = $whitelist.map { |client| @peer_id =~ client[:regex] }.include? 0
error "Your client is banned. Go to #{$config[:whitelist_url]}" unless whitelisted
# Instantiate a cache object for this request
cache = Cache.new(:host => $config[:cache][:host], :namespace => $config[:cache][:namespace], :mysql => $db, :passkey => @passkey, :peer_id => @peer_id, :info_hash => @info_hash)
# Find our user
user = cache.user
error "Account not found" unless user.exists?
error "Leeching Disabled" unless user.can_leech?
# Find our torrent
torrent = cache.torrent
error "Torrent not found" unless torrent.exists?
# Find peers
peers = torrent.peers[0, @numwant.to_i]
# Log Announce
$db.query( "INSERT INTO tracker_announce_log (UserID, TorrentID, IP, Port, Event, Uploaded, Downloaded, tracker_announce_log.Left, PeerID, RequestURI, Time) VALUES (#{user['ID']}, #{torrent['ID']}, '#{@ip}', #{@port}, '#{@event}', #{@uploaded}, #{@downloaded}, #{@left}, '#{@peer_id}', '#{escape request.env['QUERY_STRING']}', NOW())" ) if $config[:log_announces]
# Generate Peerlist
if @compact == '1' # Compact Mode
peer_list = ''
peers.each do |peer| # Go through each peer
ip = peer['IP'].split('.').collect { |octet| octet.to_i }.pack('C*')
port = [peer['Port'].to_i].pack('n*')
peer_list << ip + port
end
else
peer_list = []
peers.each do |peer| # Go through each peer
peer_hash = { 'ip' => peer['IP'], 'port' => peer['Port'] }
peer_hash.update( { 'peer id' => peer['PeerID'] } ) unless @no_peer_id == 1
peer_list << peer_hash
end
end
@resp = { 'interval' => $config[:announce_int], 'min interval' => $config[:min_announce_int], 'peers' => peer_list }
# End peerlist generation
# Update database / cache
# Update values specific to each event
case @event
when 'started'
# Add the user to the torrents peerlist, then update the seeder / leecher count
torrent.new_peer(@ip, @port, @left, @peer_id)
$db.query( "UPDATE torrents SET #{@left.to_i > 0 ? 'Leechers = Leechers + 1' : 'Seeders = Seeders + 1'} WHERE ID = #{torrent['ID']}" )
when 'completed'
$db.query( "INSERT INTO tracker_snatches (UserID, TorrentID, IP, Port, Uploaded, Downloaded, PeerID) VALUES (#{user['ID']}, #{torrent['ID']}, '#{@ip}', #{@port}, #{@uploaded}, #{@downloaded}, '#{@peer_id}')" )
$db.query( "UPDATE torrents SET Seeders = Seeders + 1, Leechers = Leechers - 1, Snatched = Snatched + 1 WHERE ID = #{torrent['ID']}" )
when 'stopped'
# Update Seeder / Leecher count for torrent, and update snatched list with final upload / download counts, then delete the user from the torrents peerlist
$db.query( "UPDATE torrents AS t, tracker_snatches AS s SET #{@left.to_i > 0 ? 't.Leechers = t.Leechers - 1' : 't.Seeders = t.Seeders - 1'}, s.Uploaded = #{@uploaded}, s.Downloaded = #{@downloaded} WHERE t.ID = #{torrent['ID']} AND (s.UserID = #{user['ID']} AND s.TorrentID = #{torrent['ID']})" )
torrent.delete_peer
end
# Add user to the update queue
user.update(@uploaded, @downloaded, @left)
@resp.bencode
rescue TrackerError => e
e.message
end
end
get '/:passkey/scrape' do
begin
error "Bad Scrape" if params['info_hash'].nil? or params['info_hash'].empty?
cache = MemCache::new $config[:cache][:host], :namespace => $config[:cache][:namespace]
torrent = Cache::Torrent.new(cache, $db, params['info_hash'])
{ params['info_hash'] => { 'complete' => torrent['Seeders'], 'incomplete' => torrent['Leechers'], 'downloaded' => torrent['Snatched'], 'name' => torrent['Name'] } }.bencode
rescue TrackerError => e
e.message
end
end
get '/whitelist' do
$whitelist.collect { |client| client[:name] }.join('<br />')
end
helpers do
def error reason
raise TrackerError, { 'failure reason' => reason }.bencode
end
def escape string
Mysql::escape_string(string.to_s)
end
end
# Error class
class TrackerError < RuntimeError; end # Used to clearly identify errors