Class: GoogleSafeBrowsing::ResponseHelper
- Inherits:
-
Object
- Object
- GoogleSafeBrowsing::ResponseHelper
- Defined in:
- lib/google_safe_browsing/response_helper.rb
Class Method Summary collapse
- .escape_and_join(values) ⇒ Object
- .parse_data_line(line) ⇒ Object
- .parse_data_response(response) ⇒ Object
- .parse_full_hash_response(response) ⇒ Object
- .pop_and_join(records) ⇒ Object
- .receive_data(url, list) ⇒ Object
- .record_add_shavar_to_insert(h) ⇒ Object
- .record_sub_shavar_to_insert(h) ⇒ Object
Class Method Details
.escape_and_join(values) ⇒ Object
192 193 194 195 196 |
# File 'lib/google_safe_browsing/response_helper.rb', line 192 def self.escape_and_join(values) values.map do |v| ActiveRecord::Base::sanitize(v) end.join(', ') end |
.parse_data_line(line) ⇒ Object
181 182 183 184 185 186 187 188 189 190 |
# File 'lib/google_safe_browsing/response_helper.rb', line 181 def self.parse_data_line(line) split_line = line.split(':') ret = {} ret[ :action ] = split_line[0] ret[ :chunk_number ] = split_line[1].to_i ret[ :hash_length ] = split_line[2].to_i ret[ :chunk_length ] = split_line[3].to_i ret end |
.parse_data_response(response) ⇒ Object
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 |
# File 'lib/google_safe_browsing/response_helper.rb', line 20 def self.parse_data_response(response) ret = {} ret[:lists] = [] ret[:data_urls] = {} current_list = '' response.split("\n").each do |line| vals = line.split(':') case vals[0] when 'n' ret[:delay_seconds] = vals[1].to_i @delay_seconds = ret[:delay_seconds].to_i when 'i' ret[:lists] << vals[1] current_list = vals[1] ret[:data_urls][current_list] = [] when 'u' ret[:data_urls][current_list] << vals[1] when 'r' # reset (delete all data and try again) when 'ad' # vals[1] is a CHUNKLIST representing add chunks to delete # we can also delete the associated Shavar Hashes # we no longer have to report that we received these chunks AddShavar.delete_chunks_from_list(current_list, ChunkList.new(vals[1])) when 'sd' # vals[1] is a CHUNKLIST representing sub chunks to delete # we no longer have to report that we received these chunks SubShavar.delete_chunks_from_list(current_list, ChunkList.new(vals[1])) end end ret end |
.parse_full_hash_response(response) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/google_safe_browsing/response_helper.rb', line 3 def self.parse_full_hash_response(response) f = StringIO.new(response) full_hashes = [] while(! f.eof? ) hash = {} = f.gets.chomp.split(':') hash[:list] = [0] hash[:add_chunk_num] = [1] hash[:full_hash] = f.read([2].to_i).unpack('H*')[0] full_hashes << hash end full_hashes end |
.pop_and_join(records) ⇒ Object
198 199 200 |
# File 'lib/google_safe_browsing/response_helper.rb', line 198 def self.pop_and_join(records) records.pop(10000).join(', ') end |
.receive_data(url, list) ⇒ Object
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/google_safe_browsing/response_helper.rb', line 58 def self.receive_data(url, list) urls = url.split(',') url = urls[0] mac = urls[1] open(url) do |f| body = f.read unless mac.blank? || HttpHelper.valid_mac?(body, mac) raise InvalidMACVerification end f.rewind while(line = f.gets) line_actions = parse_data_line(line) chunk = f.read(line_actions[:chunk_length]) # f iterator is now set for next chunk add_attrs = { :chunk_number => line_actions[:chunk_number], :list => list, :prefix => nil, :host_key => nil } case line_actions[:action] when 'a' if line_actions[:chunk_length] == 0 record_add_shavar_to_insert(add_attrs) else chunk_iterator = chunk.bytes.to_enum counter = 0 begin while true add_attrs[:host_key] = BinaryHelper.read_bytes_as_hex(chunk_iterator, 4) count = chunk_iterator.next if count > 0 count.times do |i| add_attrs[:prefix] = BinaryHelper.read_bytes_as_hex(chunk_iterator, line_actions[:hash_length]) record_add_shavar_to_insert(add_attrs) end else add_attrs[:prefix] = add_attrs[:host_key] record_add_shavar_to_insert(add_attrs) end counter += 1 end rescue StopIteration puts "Added #{counter} host_keys for add chunk number #{line_actions[:chunk_number]}" end end when 's' sub_attrs = add_attrs.merge({ :add_chunk_number => nil }) if line_actions[:chunk_length] == 0 record_sub_shavar_to_insert(sub_attrs) else chunk_iterator = chunk.bytes.to_enum counter = 0 begin while true sub_attrs[:host_key] = BinaryHelper.read_bytes_as_hex(chunk_iterator, 4) count = chunk_iterator.next if count > 0 count.times do |i| sub_attrs[:add_chunk_number] = BinaryHelper.unpack_add_chunk_num(BinaryHelper.read_bytes_from(chunk_iterator, 4)) sub_attrs[:prefix] = BinaryHelper.read_bytes_as_hex(chunk_iterator, line_actions[:hash_length]) record_sub_shavar_to_insert(sub_attrs) end else sub_attrs[:add_chunk_number] = BinaryHelper.unpack_add_chunk_num(BinaryHelper.read_bytes_from(chunk_iterator, 4)) sub_attrs[:prefix] = sub_attrs[:host_key] record_sub_shavar_to_insert(sub_attrs) end counter += 1 end rescue StopIteration puts "Added #{counter} host_keys for sub chunk number #{line_actions[:chunk_number]}" end end else puts "neither a nor s =======================================================" end end end while @add_shavar_values && @add_shavar_values.any? AddShavar.connection.execute " INSERT INTO gsb_add_shavars (prefix, host_key, chunk_number, list)\n VALUES \#{pop_and_join @add_shavar_values}\n SQL\n end\n while @sub_shavar_values && @sub_shavar_values.any?\n SubShavar.connection.execute <<-SQL\n INSERT INTO gsb_sub_shavars (prefix,\n host_key,\n add_chunk_number,\n chunk_number,\n list)\n VALUES \#{pop_and_join @sub_shavar_values}\n SQL\n end\n\n FullHash.delete_subbed\n\n @add_shavar_values = []\n @sub_shavar_values = []\nend\n" |
.record_add_shavar_to_insert(h) ⇒ Object
166 167 168 169 170 |
# File 'lib/google_safe_browsing/response_helper.rb', line 166 def self.record_add_shavar_to_insert(h) @add_shavar_values ||= [] values = [ h[:prefix], h[:host_key], h[:chunk_number], h[:list] ] @add_shavar_values << "(#{escape_and_join values})" end |
.record_sub_shavar_to_insert(h) ⇒ Object
171 172 173 174 175 176 177 178 179 |
# File 'lib/google_safe_browsing/response_helper.rb', line 171 def self.record_sub_shavar_to_insert(h) @sub_shavar_values ||= [] values = [ h[:prefix], h[:host_key], h[:add_chunk_number], h[:chunk_number], h[:list] ] @sub_shavar_values << "(#{escape_and_join values})" end |