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
205 206 207 208 209 |
# File 'lib/google_safe_browsing/response_helper.rb', line 205 def self.escape_and_join(values) values.map do |v| ActiveRecord::Base::sanitize(v) end.join(', ') end |
.parse_data_line(line) ⇒ Object
194 195 196 197 198 199 200 201 202 203 |
# File 'lib/google_safe_browsing/response_helper.rb', line 194 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 57 58 59 |
# 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])) else GoogleSafeBrowsing.logger.warn \ "Unknown data response directive: #{vals[0]}" 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
211 212 213 |
# File 'lib/google_safe_browsing/response_helper.rb', line 211 def self.pop_and_join(records) records.pop(10_000).join(', ') end |
.receive_data(url, list) ⇒ Object
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 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/google_safe_browsing/response_helper.rb', line 61 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 GoogleSafeBrowsing.logger.info <<-LOG.gsub(/\n\s*/, '') Added #{counter} host_keys for add chunk number #{line_actions[:chunk_number]} LOG 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 GoogleSafeBrowsing.logger.info <<-LOG.gsub(/\n\s*/, '') Added #{counter} host_keys for sub chunk number #{line_actions[:chunk_number]} LOG end end else GoogleSafeBrowsing.logger.info <<-LOG.gsub(/\n\s*/, '') neither a nor s ================================================= LOG end end end while @add_shavar_values && @add_shavar_values.any? AddShavar.connection.execute <<-SQL INSERT INTO gsb_add_shavars (prefix, host_key, chunk_number, list) VALUES #{pop_and_join @add_shavar_values} SQL end while @sub_shavar_values && @sub_shavar_values.any? SubShavar.connection.execute <<-SQL INSERT INTO gsb_sub_shavars (prefix, host_key, add_chunk_number, chunk_number, list) VALUES #{pop_and_join @sub_shavar_values} SQL end FullHash.delete_subbed @add_shavar_values = [] @sub_shavar_values = [] end |
.record_add_shavar_to_insert(h) ⇒ Object
179 180 181 182 183 |
# File 'lib/google_safe_browsing/response_helper.rb', line 179 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
184 185 186 187 188 189 190 191 192 |
# File 'lib/google_safe_browsing/response_helper.rb', line 184 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 |