Module: Html2rss::RequestService::FaradayStrategy::PeerIpValidator

Defined in:
lib/html2rss/request_service/faraday_strategy.rb

Overview

Validates the remote socket peer IP during Net::HTTP start.

Class Method Summary collapse

Class Method Details

.install!(http, policy:) ⇒ void

This method returns an undefined value.

Parameters:

  • http (Net::HTTP)

    connection to configure

  • policy (Policy)

    request policy



77
78
79
80
81
82
83
84
85
# File 'lib/html2rss/request_service/faraday_strategy.rb', line 77

def install!(http, policy:)
  orig_start = http.method(:start)
  http.define_singleton_method(:start) do |&block|
    orig_start.call do |opened_http|
      PeerIpValidator.validate!(opened_http, policy:)
      block.call(opened_http)
    end
  end
end

.peer_ip_for(opened_http) ⇒ String?

Parameters:

  • opened_http (Net::HTTP)

Returns:

  • (String, nil)


98
99
100
101
102
103
# File 'lib/html2rss/request_service/faraday_strategy.rb', line 98

def peer_ip_for(opened_http)
  sock = opened_http.instance_variable_get(:@socket)
  io = sock.respond_to?(:io) ? sock.io : sock
  peeraddr = io.respond_to?(:peeraddr) ? io.peeraddr : nil
  peeraddr&.[](3) || peeraddr&.[](2)
end

.validate!(opened_http, policy:) ⇒ void

This method returns an undefined value.

Parameters:

  • opened_http (Net::HTTP)

    active connection

  • policy (Policy)

    request policy



90
91
92
93
94
# File 'lib/html2rss/request_service/faraday_strategy.rb', line 90

def validate!(opened_http, policy:)
  scheme = opened_http.use_ssl? ? 'https' : 'http'
  url = Html2rss::Url.from_absolute("#{scheme}://#{opened_http.address}:#{opened_http.port}")
  policy.validate_remote_ip!(ip: peer_ip_for(opened_http), url:)
end