Class: Tor::CachedDesc
- Inherits:
-
Object
- Object
- Tor::CachedDesc
- Defined in:
- lib/tor_extend.rb
Overview
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 = Tor::CachedDesc.new
Tor::CachedDesc.dbconnect
Tor::CachedDesc.dbconnect( { :adapter => "sqlite3",:database => "db.sqlite3"} )
Tor::CachedDesc.readall("cached_descriptor_filename")
Tor::CachedDesc.ors
Tor::CachedDesc.ors.all
Instance Method Summary collapse
-
#dbconnect(*dbconfig) ⇒ Object
Tor::dbconnect() method connects using the optional database config passed as argument.
-
#get_geoiprecord(ipaddr) ⇒ Object
This returns the Geoip record from one of the database files used in Tor::CachedDesc.
-
#initialize(geoip_path) ⇒ CachedDesc
constructor
This initialises the stat attribute of the CachedDesc class that can be used to get data from Tor::Router.
-
#ors ⇒ Object
Returns the Router database class.
-
#readall(filename) ⇒ Object
Read a file and put all the ralated information into the Tor::Router database.
-
#stat ⇒ Object
This initialises the stat attribute of the CachedDesc class that can be used to get data from Tor::Router It returns a Tor::StatsObj.
Constructor Details
#initialize(geoip_path) ⇒ CachedDesc
This initialises the stat attribute of the CachedDesc class that can be used to get data from Tor::Router
57 58 59 60 61 62 63 |
# 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 |
Instance Method Details
#dbconnect(*dbconfig) ⇒ Object
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.
<b>Connecting to a database</b>
Tor::CachedDesc.dbconnect
Tor::CachedDesc.dbconnect( {:adapter => "sqlite3",:database => "db_name_here"} )
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 |
# 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. end create_table :bridges do |t| t.string :ipaddr t.integer :port t.decimal :lat t.decimal :lng t. end end end end |
#get_geoiprecord(ipaddr) ⇒ Object
This returns the Geoip record from one of the database files used in Tor::CachedDesc.
120 121 122 123 124 |
# 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 |
#ors ⇒ Object
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
113 114 115 |
# File 'lib/tor_extend.rb', line 113 def ors Router end |
#readall(filename) ⇒ Object
Read a file and put all the ralated information into the Tor::Router database.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# 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/i osplatform ="WINDOWS" when /SunOS/i osplatform ="SUN" when /Linux/i,/Ubuntu/i osplatform ="LINUX" when /Unix/i, /BSD/i,/DragonFly/i,/Darwin/i 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 |