init commit of peertracker with ruby extension

This commit is contained in:
Mateusz Zawisza 2012-08-15 01:34:25 +02:00 committed by Johannes Dewender
parent 4b12f2f6cf
commit c378525e08
15 changed files with 754 additions and 550 deletions

View File

@ -97,10 +97,12 @@ $_GET['info_hash'] = peertracker::$api->escape_sql($_GET['info_hash']);
$_GET['peer_id'] = peertracker::$api->escape_sql($_GET['peer_id']);
// announce peers
peertracker::peers();
// need to pass as_code and country
$json = file_get_contents("http://localhost:4567/as_info/123.123.123.123.json");
peertracker::peers($as_code, $country);
// track client
peertracker::event();
peertracker::event($as_code, $country);
// garbage collection
peertracker::clean();

View File

@ -4,7 +4,7 @@
/*
* PeerTracker - OpenSource BitTorrent Tracker
* Revision - $Id: tracker.mysql.php 164 2010-01-23 22:08:58Z trigunflame $
* Revision - $Id: tracker.mysql.php 148 2009-11-16 23:18:28Z trigunflame $
* Copyright (C) 2009 PeerTracker Team
*
* PeerTracker is free software: you can redistribute it and/or modify
@ -407,6 +407,7 @@ class peertracker
// dotted decimal string ip, integer port, integer state and unix timestamp updated
"'{$_GET['ip']}', {$_GET['port']}, {$_SERVER['tracker']['seeding']}, " . time() . '); '
) OR tracker_error('failed to add new peer data');
exit;
}
// full peer update
@ -452,7 +453,7 @@ class peertracker
}
// tracker event handling
public static function event()
public static function event(as_code, country)
{
// execute peer select
$pState = self::$api->fetch_once(
@ -495,7 +496,7 @@ class peertracker
}
// tracker peer list
public static function peers()
public static function peers($as_code, $country)
{
// fetch peer total
$total = self::$api->fetch_once(
@ -518,12 +519,13 @@ class peertracker
($total[0] <= $_GET['numwant'] ? ';' :
// if the total peers count is low, use SQL RAND
($total[0] <= $_SERVER['tracker']['random_limit'] ?
" ORDER BY RAND() LIMIT {$_GET['numwant']};" :
// " ORDER BY RAND() LIMIT {$_GET['numwant']};" :
" ORDER BY as_code='{$as_code}' DESC, country='{$country}' DESC LIMIT {$_GET['numwant']};" :
// use a more efficient but less accurate RAND
" LIMIT {$_GET['numwant']} OFFSET " .
mt_rand(0, ($total[0]-$_GET['numwant']))
)
);
);
// begin response
$response = 'd8:intervali' . $_SERVER['tracker']['announce_interval'] .
@ -627,7 +629,7 @@ class peertracker
case 'xml':
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="ISO-8859-1"?>' .
'<tracker version="$Id: tracker.mysql.php 164 2010-01-23 22:08:58Z trigunflame $">' .
'<tracker version="$Id: tracker.mysql.php 148 2009-11-16 23:18:28Z trigunflame $">' .
'<peers>' . number_format($stats[0] + $stats[1]) . '</peers>' .
'<seeders>' . number_format($stats[0]) . '</seeders>' .
'<leechers>' . number_format($stats[1]) . '</leechers>' .
@ -637,7 +639,7 @@ class peertracker
// json
case 'json':
header('Content-Type: text/javascript');
echo '{"tracker":{"version":"$Id: tracker.mysql.php 164 2010-01-23 22:08:58Z trigunflame $",' .
echo '{"tracker":{"version":"$Id: tracker.mysql.php 148 2009-11-16 23:18:28Z trigunflame $",' .
'"peers": "' . number_format($stats[0] + $stats[1]) . '",' .
'"seeders":"' . number_format($stats[0]) . '",' .
'"leechers":"' . number_format($stats[1]) . '",' .
@ -647,12 +649,12 @@ class peertracker
// standard
default:
echo '<!doctype html><html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8">' .
'<title>PeerTracker: $Id: tracker.mysql.php 164 2010-01-23 22:08:58Z trigunflame $</title>' .
'<title>PeerTracker: $Id: tracker.mysql.php 148 2009-11-16 23:18:28Z trigunflame $</title>' .
'<body><pre>' . number_format($stats[0] + $stats[1]) .
' peers (' . number_format($stats[0]) . ' seeders + ' . number_format($stats[1]) .
' leechers) in ' . number_format($stats[2]) . ' torrents</pre></body></html>';
}
}
}
}
?>

7
ruby/Gemfile Normal file
View File

@ -0,0 +1,7 @@
source "http://rubygems.org"
gem 'activerecord', :require => "active_record"
gem 'sinatra'
gem 'mysql2'
gem "rspec"
gem "json"

48
ruby/Gemfile.lock Normal file
View File

@ -0,0 +1,48 @@
GEM
remote: http://rubygems.org/
specs:
activemodel (3.2.8)
activesupport (= 3.2.8)
builder (~> 3.0.0)
activerecord (3.2.8)
activemodel (= 3.2.8)
activesupport (= 3.2.8)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activesupport (3.2.8)
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.2)
builder (3.0.0)
diff-lcs (1.1.3)
i18n (0.6.0)
json (1.7.4)
multi_json (1.3.6)
mysql2 (0.3.11)
rack (1.4.1)
rack-protection (1.2.0)
rack
rspec (2.11.0)
rspec-core (~> 2.11.0)
rspec-expectations (~> 2.11.0)
rspec-mocks (~> 2.11.0)
rspec-core (2.11.1)
rspec-expectations (2.11.2)
diff-lcs (~> 1.1.3)
rspec-mocks (2.11.1)
sinatra (1.3.2)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
tilt (1.3.3)
tzinfo (0.3.33)
PLATFORMS
ruby
DEPENDENCIES
activerecord
json
mysql2
rspec
sinatra

4
ruby/bin/get_as_info.rb Executable file
View File

@ -0,0 +1,4 @@
#! /usr/bin/ruby
require File.join(File.dirname(__FILE__),'../lib/as_info.rb')
Peertracker::ASInfo.set_as_info

View File

@ -0,0 +1,7 @@
Bundler.require
require File.join(File.dirname(__FILE__),'../lib/as_info.rb')
get '/as_info/:ip_address.json' do |ip_address|
response = Peertracker::ASInfo.new.set_as_info_for_ip(ip_address)
{:as_code => response[0], :country => response[1]}.to_json
end

7
ruby/config/database.rb Normal file
View File

@ -0,0 +1,7 @@
ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:host => "localhost",
:username => "root",
:password => "howareyou",
:database => "peertracker"
)

16
ruby/config/migration.rb Normal file
View File

@ -0,0 +1,16 @@
Bundler.require
require File.join(File.dirname(__FILE__), "database.rb")
class AddASInfoToPeers < ActiveRecord::Migration
def up
add_column :pt_peers, :as_code, :string
add_column :pt_peers, :country, :string
end
def down
remove_column :pt_peers, :as_code
remove_column :pt_peers, :country
end
end
AddASInfoToPeers.new.up

30
ruby/lib/as_info.rb Normal file
View File

@ -0,0 +1,30 @@
Bundler.require
require File.join(File.dirname(__FILE__), "../config/database.rb")
require File.join(File.dirname(__FILE__), "./model/peer.rb")
require File.join(File.dirname(__FILE__), "./url_creator.rb")
require File.join(File.dirname(__FILE__), "./http_request.rb")
require File.join(File.dirname(__FILE__), "./as_parser.rb")
module Peertracker
class ASInfo
def set_as_info_for_ip(ip_address)
request = HTTPRequest.new(URLCreator.new("ip.searchwww.com", ip_address).create_url)
ASParser.new.parse_response(request.perform_request)
end
class << self
def set_as_info
Peer.without_as_info.each do |peer|
begin
puts "Parsing #{peer.ip}"
peer.as_code, peer.country = self.new.set_as_info_for_ip(peer.ip)
peer.save
rescue => e
puts "Could not fetch AS for #{peer.ip}"
puts "#{e.to_s}"
end
end
end
end
end
end

23
ruby/lib/as_parser.rb Normal file
View File

@ -0,0 +1,23 @@
class ASParser
def parse_response(response)
[parse_as(response),parse_country(response)]
end
def parse_as(response)
begin
response.scan(/Provider:<\/b><p>(\w*)/)[0][0]
rescue => e
puts "Could not parse AS"
nil
end
end
def parse_country(response)
begin
response.scan(/Location:<\/b><p>(.*)<p>/)[0][0].split(",").last.strip
rescue => e
puts "Could not parse Country"
nil
end
end
end

14
ruby/lib/http_request.rb Normal file
View File

@ -0,0 +1,14 @@
module Peertracker
class HTTPRequest
def initialize(host, path = "")
@host = host
@path = path
end
def perform_request
uri = URI("http://#{@host}#{@path}/")
response = Net::HTTP.get_response(uri)
response.body
end
end
end

5
ruby/lib/model/peer.rb Normal file
View File

@ -0,0 +1,5 @@
class Peer < ActiveRecord::Base
self.table_name = "pt_peers"
self.primary_key = "peer_id"
scope :without_as_info, lambda{ where("pt_peers.as_code IS NULL") }
end

18
ruby/lib/url_creator.rb Normal file
View File

@ -0,0 +1,18 @@
require "net/http"
module Peertracker
class URLCreator
def initialize(url_host, ip_address)
@ip_address = ip_address
@url_host = url_host
end
def create_subdomain
"%02x%02x%02x%02x" % @ip_address.scan(/(.*)\.(.*)\.(.*)\.(.*)/)[0]
end
def create_url
"#{create_subdomain}.#{@url_host}"
end
end
end

View File

@ -0,0 +1,20 @@
require File.join(File.dirname(__FILE__), "../../spec/spec_helper.rb")
describe "ASInfo" do
context "when given IP address" do
describe "sets as info for ip address" do
let(:ip_address) {"12.12.12.12"}
it "stores as_info to database" do
ASInfo.new.set_as_info_for_ip(ip_address)
Peer.find_by_ip_address(ip_address).as.should_not be_null
end
end
end
describe ".set_as_info" do
it "stores as info for all addresses"
ASInfo.set_as_info
Peer.all.each {|peer| peer.as.should_not be_null}
end
end

1
ruby/spec/spec_helper.rb Normal file
View File

@ -0,0 +1 @@
require File.join(File.dirname(__FILE__), "../lib/as_info.rb")