Make an attempt to fix bug 1024.
[tor/rransom.git] / contrib / auto-naming / process-consensus
blobdc9d207e43aeb26bc3168d9dfd4c46e575c517e9
1 #!/usr/bin/ruby
3 # process-consensus - read a current consensus document, inserting the
4 # information into a database then calling
5 # update-named-status.rb to update the name-binding
6 # flags
8 # Copyright (c) 2007 Peter Palfrader
10 # Permission is hereby granted, free of charge, to any person obtaining a copy
11 # of this software and associated documentation files (the "Software"), to deal
12 # in the Software without restriction, including without limitation the rights
13 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 # copies of the Software, and to permit persons to whom the Software is
15 # furnished to do so, subject to the following conditions:
17 # The above copyright notice and this permission notice shall be included in
18 # all copies or substantial portions of the Software.
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 # SOFTWARE.
28 require "yaml"
30 require 'db'
31 require 'db-config'
32 require 'update-named-status'
34 $db = Db.new($CONFIG['database']['dbhost'], $CONFIG['database']['dbname'], $CONFIG['database']['user'], $CONFIG['database']['password'])
36 $router_cache = {}
37 $nickname_cache = {}
39 def parse_consensus consensus
40 ts = nil
41 routers = []
42 consensus.each do |line|
43 (key, value) = line.split(' ',2)
44 case key
45 when "valid-after", "published": ts = DateTime.parse(value)
46 when "r":
47 (nick, fpr, _) = value.split(' ', 3)
48 nick.downcase!
49 next if nick == 'unnamed'
50 routers << {
51 'nick' => nick,
52 'fingerprint' => (fpr+'=').unpack('m').first.unpack('H*').first
54 end
55 end
56 throw "Did not find a timestamp" unless ts
57 throw "Did not find any routers" unless routers.size > 0
58 return ts, routers
59 end
61 def insert_routers_into_db(router, table, field, value)
62 pk = table+'_id'
63 row = $db.query_row("SELECT #{pk} FROM #{table} WHERE #{field}=?", value)
64 if row
65 return row[pk]
66 else
67 r = { field => value }
68 $db.insert_row( table, r )
69 return r[pk]
70 end
71 end
73 def handle_one_consensus(c)
74 puts "parsing..." if $verbose
75 timestamp, routers = parse_consensus c
76 puts "storing..." if $verbose
78 routers.each do |router|
79 fpr = router['fingerprint']
80 nick = router['nick']
81 $router_cache[fpr] = router_id = ($router_cache[fpr] or insert_routers_into_db(router, 'router', 'fingerprint', router['fingerprint']))
82 $nickname_cache[nick] = nickname_id = ($nickname_cache[nick] or insert_routers_into_db(router, 'nickname', 'nick', router['nick']))
84 row = $db.update(
85 'router_claims_nickname',
86 { 'last_seen' => timestamp.to_s },
87 { 'router_id' => router_id, 'nickname_id' => nickname_id} )
88 case row
89 when 0:
90 $db.insert('router_claims_nickname',
92 'first_seen' => timestamp.to_s,
93 'last_seen' => timestamp.to_s,
94 'router_id' => router_id, 'nickname_id' => nickname_id} )
95 when 1:
96 else
97 throw "Update of router_claims_nickname returned unexpected number of affected rows(#{row})"
98 end
99 end
102 $db.transaction_begin
103 if ARGV.first == '-v'
104 $verbose = true
105 ARGV.shift
108 if ARGV.size == 0
109 handle_one_consensus STDIN.readlines
110 do_update $verbose
111 else
112 ARGV.each do |filename|
113 puts filename if $verbose
114 handle_one_consensus File.new(filename).readlines
115 puts "updating..." if $verbose
116 do_update $verbose
119 $db.transaction_commit