Class: HTTPX::Resolver::HTTPS
- Extended by:
- Forwardable
- Defined in:
- lib/httpx/resolver/https.rb,
sig/resolver/https.rbs
Overview
Implementation of a DoH name resolver (https://www.youtube.com/watch?v=unMXvnY2FNM). It wraps an HTTPX::Connection object which integrates with the main session in the same manner as other performed HTTP requests.
Defined Under Namespace
Modules: DNSExtensions
Constant Summary collapse
- NAMESERVER =
"https://1.1.1.1/dns-query"- DEFAULTS =
{ uri: NAMESERVER, use_get: false, }.freeze
- FAMILY_TYPES =
Constants inherited from Resolver
Constants included from Loggable
Loggable::COLORS, Loggable::USE_DEBUG_LOG
Instance Attribute Summary collapse
-
#family ⇒ ip_family
readonly
Returns the value of attribute family.
-
#pool ⇒ Object
writeonly
Sets the attribute pool.
Attributes inherited from Resolver
#current_selector, #current_session, #multi, #options
Instance Method Summary collapse
- #<<(connection) ⇒ void
- #build_request(hostname) ⇒ Request
- #call ⇒ void
- #close ⇒ void
- #close_or_resolve(should_deactivate = false) ⇒ void
-
#connecting? ⇒ Boolean
delegated to resolver connection.
- #deactivate ⇒ void
- #decode_response_body(response) ⇒ dns_decoding_response
- #handle_socket_timeout ⇒ void
- #inflight? ⇒ Boolean
-
#initialize(_, options) ⇒ HTTPS
constructor
A new instance of HTTPS.
- #on_response(request, response) ⇒ void
- #parse(request, response) ⇒ void
- #parse_addresses(answers, request) ⇒ void
- #reset_hostname(hostname, reset_candidates: true) ⇒ Connection?
- #resolver_connection ⇒ Connection
- #send_request(hostname, connection) ⇒ void
- #state ⇒ Object
- #terminate ⇒ void
- #to_io ⇒ IO
Methods inherited from Resolver
#closed?, #disconnect, #each_connection, #early_resolve, #emit_addresses, #emit_connection_error, #emit_resolve_error, #emit_resolved_connection, #empty?, #force_close, #handle_error, #initial_call, #lazy_resolve, multi?, #on_error, #on_io_error, #resolve_connection, #resolve_error
Methods included from Loggable
#log, #log_exception, log_identifiers, #log_redact, #log_redact_body, #log_redact_headers
Methods included from _Selectable
#force_close, #initial_call, #interests, #on_error, #on_io_error, #timeout
Constructor Details
#initialize(_, options) ⇒ HTTPS
Returns a new instance of HTTPS.
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/httpx/resolver/https.rb', line 37 def initialize(_, ) super @resolver_options = DEFAULTS.merge(@options.) @queries = {} @requests = {} @_timeouts = Array(@resolver_options[:timeouts]) @timeouts = Hash.new { |timeouts, host| timeouts[host] = @_timeouts.dup } @uri = URI(@resolver_options[:uri]) @name = @uri_addresses = nil @resolver = Resolv::DNS.new @resolver.timeouts = @_timeouts.empty? ? Resolver::RESOLVE_TIMEOUT : @_timeouts @resolver.lazy_initialize end |
Instance Attribute Details
#family ⇒ ip_family (readonly)
Returns the value of attribute family.
11 12 13 |
# File 'sig/resolver/https.rbs', line 11 def family @family end |
#pool=(value) ⇒ Object (writeonly)
Sets the attribute pool
25 26 27 |
# File 'sig/resolver/https.rbs', line 25 def pool=(value) @pool = value end |
Instance Method Details
#<<(connection) ⇒ void
This method returns an undefined value.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/httpx/resolver/https.rb', line 55 def <<(connection) return if @uri.origin == connection.peer.to_s @uri_addresses ||= @options.resolver_cache.resolve(@uri.host) || @resolver.getaddresses(@uri.host) if @uri_addresses.empty? ex = ResolveError.new("Can't resolve DNS server #{@uri.host}") ex.set_backtrace(caller) connection.force_close throw(:resolve_error, ex) end resolve(connection) end |
#build_request(hostname) ⇒ Request
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/httpx/resolver/https.rb', line 261 def build_request(hostname) uri = @uri.dup rklass = @options.request_class payload = Resolver.encode_dns_query(hostname, type: @record_type) timeouts = @timeouts[hostname] request_timeout = timeouts.shift = @options.merge(timeout: { request_timeout: request_timeout }) if @resolver_options[:use_get] params = URI.decode_www_form(uri.query.to_s) params << ["type", FAMILY_TYPES[@record_type]] params << ["dns", Base64.urlsafe_encode64(payload, padding: false)] uri.query = URI.encode_www_form(params) request = rklass.new("GET", uri, ) else request = rklass.new("POST", uri, , body: [payload]) request.headers["content-type"] = "application/dns-message" end request.headers["accept"] = "application/dns-message" request end |
#call ⇒ void
This method returns an undefined value.
34 |
# File 'sig/resolver/https.rbs', line 34
def call: () -> void
|
#close ⇒ void
This method returns an undefined value.
36 |
# File 'sig/resolver/https.rbs', line 36
def close: () -> void
|
#close_or_resolve(should_deactivate = false) ⇒ void
This method returns an undefined value.
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/httpx/resolver/https.rb', line 308 def close_or_resolve(should_deactivate = false) # drop already closed connections @connections.shift until @connections.empty? || @connections.first.state != :closed if (@connections - @queries.values).empty? # the same resolver connection may be serving different https resolvers (AAAA and A). return if inflight? if should_deactivate deactivate else disconnect end else resolve end end |
#connecting? ⇒ Boolean
delegated to resolver connection
30 |
# File 'sig/resolver/https.rbs', line 30
def connecting?: () -> bool
|
#deactivate ⇒ void
This method returns an undefined value.
40 |
# File 'sig/resolver/https.rbs', line 40
def deactivate: () -> void
|
#decode_response_body(response) ⇒ dns_decoding_response
283 284 285 286 287 288 289 290 291 |
# File 'lib/httpx/resolver/https.rb', line 283 def decode_response_body(response) case response.headers["content-type"] when "application/dns-udpwireformat", "application/dns-message" Resolver.decode_dns_answer(response.to_s) else raise Error, "unsupported DNS mime-type (#{response.headers["content-type"]})" end end |
#handle_socket_timeout ⇒ void
This method returns an undefined value.
46 |
# File 'sig/resolver/https.rbs', line 46
def handle_socket_timeout: (interval interval) -> void
|
#inflight? ⇒ Boolean
44 |
# File 'sig/resolver/https.rbs', line 44
def inflight?: () -> bool
|
#on_response(request, response) ⇒ void
This method returns an undefined value.
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 |
# File 'lib/httpx/resolver/https.rb', line 128 def on_response(request, response) if (e = response.error) hostname = @requests.delete(request) connection = reset_hostname(hostname) emit_resolve_error(connection, connection.peer.host, e) close_or_resolve else # @type var response: HTTPX::Response if response.status.between?(300, 399) && response.headers.key?("location") hostname = @requests[request] connection = @queries[hostname] location_uri = URI(response.headers["location"]) location_uri = response.uri.merge(location_uri) if location_uri.relative? # we assume that the DNS server URI changed permanently and move on @uri = location_uri send_request(hostname, connection) return end parse(request, response) end ensure @requests.delete(request) end |
#parse(request, response) ⇒ void
This method returns an undefined value.
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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/httpx/resolver/https.rb', line 159 def parse(request, response) hostname = @name @name = nil code, result = decode_response_body(response) case code when :ok parse_addresses(result, request) when :no_domain_found # Indicates no such domain was found. host = @requests.delete(request) connection = reset_hostname(host, reset_candidates: false) unless @queries.value?(connection) emit_resolve_error(connection) close_or_resolve return end resolve when :retriable_error timeouts = @timeouts[hostname] unless timeouts.empty? log { "resolver #{FAMILY_TYPES[@record_type]}: failed, but will retry..." } connection = @queries[hostname] resolve(connection, hostname) return end host = @requests.delete(request) connection = reset_hostname(host) emit_resolve_error(connection) close_or_resolve when :dns_error host = @requests.delete(request) connection = reset_hostname(host) emit_resolve_error(connection) close_or_resolve when :decode_error host = @requests.delete(request) connection = reset_hostname(host) emit_resolve_error(connection, connection.peer.host, result) close_or_resolve end end |
#parse_addresses(answers, request) ⇒ void
This method returns an undefined value.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/httpx/resolver/https.rb', line 213 def parse_addresses(answers, request) if answers.empty? # no address found, eliminate candidates host = @requests.delete(request) connection = reset_hostname(host) emit_resolve_error(connection) close_or_resolve return else answers = answers.group_by { |answer| answer["name"] } answers.each do |hostname, addresses| addresses = addresses.flat_map do |address| if address.key?("alias") alias_address = answers[address["alias"]] if alias_address.nil? reset_hostname(address["name"]) if early_resolve(connection, hostname: address["alias"]) @connections.delete(connection) else resolve(connection, address["alias"]) return # rubocop:disable Lint/NonLocalExitFromIterator end else alias_address end else address end end.compact next if addresses.empty? hostname.delete_suffix!(".") if hostname.end_with?(".") connection = reset_hostname(hostname, reset_candidates: false) next unless connection # probably a retried query for which there's an answer @connections.delete(connection) # eliminate other candidates @queries.delete_if { |_, conn| connection == conn } @options.resolver_cache.set(hostname, @family, addresses) if @resolver_options[:cache] catch(:coalesced) { emit_addresses(connection, @family, addresses.map { |a| Resolver::Entry.new(a["data"], a["TTL"]) }) } end end close_or_resolve(true) end |
#reset_hostname(hostname, reset_candidates: true) ⇒ Connection?
293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/httpx/resolver/https.rb', line 293 def reset_hostname(hostname, reset_candidates: true) @timeouts.delete(hostname) connection = @queries.delete(hostname) return connection unless connection && reset_candidates # eliminate other candidates candidates = @queries.select { |_, conn| connection == conn }.keys @queries.delete_if { |h, _| candidates.include?(h) } # reset timeouts @timeouts.delete_if { |h, _| candidates.include?(h) } connection end |
#resolver_connection ⇒ Connection
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/httpx/resolver/https.rb', line 70 def resolver_connection # TODO: leaks connection object into the pool @resolver_connection ||= @current_session.find_connection( @uri, @current_selector, @options.merge(resolver_class: :system, ssl: { alpn_protocols: %w[h2] }) ).tap do |conn| emit_addresses(conn, @family, @uri_addresses) unless conn.addresses conn.on(:force_closed, &method(:force_close)) end end |
#send_request(hostname, connection) ⇒ void
This method returns an undefined value.
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/httpx/resolver/https.rb', line 114 def send_request(hostname, connection) request = build_request(hostname) request.on(:response, &method(:on_response).curry(2)[request]) request.on(:promise, &method(:on_promise)) @requests[request] = hostname resolver_connection.send(request) @connections << connection rescue ResolveError, Resolv::DNS::EncodeError => e reset_hostname(hostname) throw(:resolve_error, e) if connection.pending.empty? emit_resolve_error(connection, connection.peer.host, e) close_or_resolve end |
#state ⇒ Object
51 52 53 |
# File 'lib/httpx/resolver/https.rb', line 51 def state @resolver_connection ? @resolver_connection.state : :idle end |
#terminate ⇒ void
This method returns an undefined value.
42 |
# File 'sig/resolver/https.rbs', line 42
def terminate: () -> void
|
#to_io ⇒ IO
32 |
# File 'sig/resolver/https.rbs', line 32
def to_io: () -> IO
|