Module: ProxyHelper

Defined in:
lib/fluent/plugin/proxy_helper.rb

Class Method Summary collapse

Class Method Details

.configure_rest_client_proxy(url, http_proxy_param = nil, logger = nil) ⇒ Object

Configure RestClient proxy for a given URL



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fluent/plugin/proxy_helper.rb', line 75

def self.configure_rest_client_proxy(url, http_proxy_param = nil, logger = nil)
  proxy_url = get_proxy_for_url(url, http_proxy_param, logger)
  
  if proxy_url.nil?
    RestClient.proxy = nil
    logger&.debug("Proxy disabled for URL: #{url}")
  else
    RestClient.proxy = proxy_url
    logger&.debug("Proxy enabled for URL: #{url}, using: #{proxy_url}")
  end
end

.get_proxy_for_url(url, http_proxy_param = nil, logger = nil) ⇒ Object

Get the proxy URL to use for a given target URL Returns nil if proxy should be bypassed, otherwise returns the proxy URL



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fluent/plugin/proxy_helper.rb', line 47

def self.get_proxy_for_url(url, http_proxy_param = nil, logger = nil)
  # Check if this URL should bypass proxy
  if should_bypass_proxy?(url, logger)
    return nil
  end

  # Return proxy URL in order of precedence
  if http_proxy_param && !http_proxy_param.empty?
    logger&.debug("Using http_proxy param for request. Proxy url: #{http_proxy_param}")
    return http_proxy_param
  elsif ENV['HTTP_PROXY'] && !ENV['HTTP_PROXY'].empty?
    logger&.debug("Using 'HTTP_PROXY' environment variable for request. Proxy url: #{ENV['HTTP_PROXY']}")
    return ENV['HTTP_PROXY']
  elsif ENV['http_proxy'] && !ENV['http_proxy'].empty?
    logger&.debug("Using 'http_proxy' environment variable for request. Proxy url: #{ENV['http_proxy']}")
    return ENV['http_proxy']
  elsif ENV['HTTPS_PROXY'] && !ENV['HTTPS_PROXY'].empty?
    logger&.debug("Using 'HTTPS_PROXY' environment variable for request. Proxy url: #{ENV['HTTPS_PROXY']}")
    return ENV['HTTPS_PROXY']
  elsif ENV['https_proxy'] && !ENV['https_proxy'].empty?
    logger&.debug("Using 'https_proxy' environment variable for request. Proxy url: #{ENV['https_proxy']}")
    return ENV['https_proxy']
  end

  nil
end

.should_bypass_proxy?(url, logger = nil) ⇒ Boolean

Check if a host should bypass the proxy based on NO_PROXY environment variable

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fluent/plugin/proxy_helper.rb', line 7

def self.should_bypass_proxy?(url, logger = nil)
  no_proxy = ENV['NO_PROXY'] || ENV['no_proxy']
  return false if no_proxy.nil? || no_proxy.empty?

  begin
    target_host = URI.parse(url).host
    return false if target_host.nil?

    no_proxy_hosts = no_proxy.split(',').map(&:strip)

    no_proxy_hosts.each do |pattern|
      next if pattern.empty?

      # Remove leading dot if present (e.g., ".example.com" -> "example.com")
      # Downcase both for case-insensitive matching (domain names are case-insensitive per RFC 1035)
      pattern = pattern.sub(/^\./, '').downcase
      target_host_lower = target_host.downcase

      # Check for exact match or subdomain match
      if target_host_lower == pattern || target_host_lower.end_with?(".#{pattern}")
        logger&.debug("Host '#{target_host}' matches NO_PROXY pattern '#{pattern}', bypassing proxy")
        return true
      end

      # Check for wildcard
      if pattern == '*'
        logger&.debug("NO_PROXY contains '*', bypassing proxy for all hosts")
        return true
      end
    end
  rescue URI::InvalidURIError => e
    logger&.warn("Failed to parse URL '#{url}': #{e.message}")
    return false
  end

  false
end