Tor::CachedDesc is a class used to read cached descriptors into a database. The ip address, fingerprint, platform, country, latitude and longitude.
@example Create database and read cached desciptor into it cached_descriptor = ::new #dbconnect #dbconnect( { :adapter => “sqlite3”,:database => “db.sqlite3”} ) #readall(“cached_descriptor_filename”) #ors #ors.all
This initialises the stat attribute of the CachedDesc class that can be used to get data from Tor::Router
# File lib/tor_extend.rb, line 57 def initialize(geoip_path) @stat = StatsObj.new # Tor::CachedDesc#geoipdb is an instance of GeoIP with the active database @geoipdb = GeoIP.new(geoip_path[0]) # Tor::CachedDesc#geoipdb2 acts as a backup if records are not present in Tor::CachedDesc#geoipdb @geoipdb2 = GeoIP.new(geoip_path[1]) end
Tor::dbconnect() method connects using the optional database config passed as argument. It creates an sqlite3 file by default called db.sqlite3, and a table called routers. A table called routers is created in an existing database if it does not already exist.
Connecting to a database
Tor::CachedDesc.dbconnect Tor::CachedDesc.dbconnect( {:adapter => "sqlite3",:database => "db_name_here"} )
# File lib/tor_extend.rb, line 79 def dbconnect(*dbconfig) dbconfig = [{:adapter => "sqlite3",:database => "db.sqlite3"}] if dbconfig.empty? ActiveRecord::Base.establish_connection(dbconfig[0]) if !ActiveRecord::Base.connection.table_exists?('routers') ActiveRecord::Schema.define do create_table :routers do |t| t.string :ipaddr t.text :platform t.string :fingerprint t.string :city t.string :country t.string :country_code t.string :continent t.decimal :lat t.decimal :lng t.datetime :published_at t.timestamps end create_table :bridges do |t| t.string :ipaddr t.integer :port t.decimal :lat t.decimal :lng t.timestamps end end end end
This returns the Geoip record from one of the database files used in Tor::CachedDesc.
# File lib/tor_extend.rb, line 120 def get_geoiprecord(ipaddr) or_geoip=@geoipdb.city(ipaddr) or_geoip=@geoipdb2.city(ipaddr) if or_geoip==nil or_geoip end
Returns the Router database class. Allows interation with the database as an instance of Active::Base if you know what you’re doing.
Display on ORs in the database. Cacheddec.or.all
# File lib/tor_extend.rb, line 113 def ors Router end
Read a file and put all the ralated information into the Tor::Router database.
# File lib/tor_extend.rb, line 129 def readall(filename) cacheddesc = File.new(filename,'r:ISO-8859-1') loopbreaker=false until loopbreaker==true fileline = cacheddesc.gets fileline.chomp! if fileline != nil case fileline when /^published / time_published = Time.parse fileline.sub("published ",'') # downloaded_at when /^router / or_ipaddr = fileline.scan(/\d+\.\d+\.\d+.\d+/)[0] when /^platform / case fileline when /Windows/ osplatform ="WINDOWS" when /SunOS/ osplatform ="SUN" when /Linux/,/Ubuntu/ osplatform ="LINUX" when /Unix/, /BSD/,/DragonFly/,/Darwin/ osplatform ="UNIX" else osplatform ="OTHER" end when /^opt fingerprint / or_fingerprint=fileline.split("opt fingerprint ")[1].split.join # the fingerprint # puts or_ipaddr or_geoip=@geoipdb.city(or_ipaddr) or_geoip=@geoipdb2.city(or_ipaddr) if or_geoip==nil # noticed that the august database returns nil for an address like 208.64.240.182, and smaller in size than July if or_geoip.nil? puts "#{or_ipaddr} not found in both geoip databases, try get an updated one, or older one." else or_city = or_geoip.city_name or_country = or_geoip.country_name orcountry_code = or_geoip.country_code2 orcontinent = or_geoip.continent_code lng = or_geoip.longitude.to_f lat = or_geoip.latitude.to_f tmpr = Router.where(:ipaddr=>or_ipaddr).first if tmpr.nil? Router.create(:ipaddr=>or_ipaddr, :fingerprint=>or_fingerprint, :lat=>lat, :lng=>lng, :platform=>osplatform, :city=>or_city, :country=>or_country, :continent=>orcontinent,:country_code=>orcountry_code, :published_at=>time_published) elsif time_published > tmpr.published_at tmpr.fingerprint = or_fingerprint # Update fingerprint - Make sure you read newer files last tmpr.lat = lat tmpr.lng = lng tmpr.platform=osplatform tmpr.country=or_country tmpr.save end end # if orgeoip.nil? when nil # gets returns nil for EOF loopbreaker=true cacheddesc.close end end # end of read file until eof end
This initialises the stat attribute of the CachedDesc class that can be used to get data from Tor::Router It returns a Tor::StatsObj
# File lib/tor_extend.rb, line 68 def stat @stat end