Class: Aspera::ProxyAutoConfig
- Inherits:
-
Object
- Object
- Aspera::ProxyAutoConfig
- Defined in:
- lib/aspera/proxy_auto_config.rb
Overview
Evaluate a proxy auto config script
Instance Attribute Summary collapse
-
#proxy_pass ⇒ Object
writeonly
Sets the attribute proxy_pass.
-
#proxy_user ⇒ Object
writeonly
Sets the attribute proxy_user.
Instance Method Summary collapse
-
#find_proxy_for_url(service_url) ⇒ String?
execute proxy auto config script for the given URL : https://en.wikipedia.org/wiki/Proxy_auto-config.
-
#get_proxies(service_url) ⇒ Array<URI>
used to replace URI::Generic.find_proxy.
-
#initialize(proxy_auto_config) ⇒ ProxyAutoConfig
constructor
A new instance of ProxyAutoConfig.
- #register_uri_generic ⇒ Object
Constructor Details
#initialize(proxy_auto_config) ⇒ ProxyAutoConfig
Returns a new instance of ProxyAutoConfig.
70 71 72 73 74 75 76 |
# File 'lib/aspera/proxy_auto_config.rb', line 70 def initialize(proxy_auto_config) # user provided javascript with FindProxyForURL function @proxy_auto_config = proxy_auto_config # avoid multiple execution, this does not support load balancing @cache = {} @proxy_user = @proxy_pass = @pac_functions = nil end |
Instance Attribute Details
#proxy_pass=(value) ⇒ Object (writeonly)
Sets the attribute proxy_pass
67 68 69 |
# File 'lib/aspera/proxy_auto_config.rb', line 67 def proxy_pass=(value) @proxy_pass = value end |
#proxy_user=(value) ⇒ Object (writeonly)
Sets the attribute proxy_user
67 68 69 |
# File 'lib/aspera/proxy_auto_config.rb', line 67 def proxy_user=(value) @proxy_user = value end |
Instance Method Details
#find_proxy_for_url(service_url) ⇒ String?
execute proxy auto config script for the given URL : https://en.wikipedia.org/wiki/Proxy_auto-config
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/aspera/proxy_auto_config.rb', line 86 def find_proxy_for_url(service_url) uri = URI.parse(service_url) simple_url = "#{uri.scheme}://#{uri.host}" if !@cache.key?(simple_url) Log.log.debug { "PAC: starting javascript for #{service_url}" } # require at runtime, in case there is no js engine require 'execjs' # read template lib @pac_functions = File.read(PAC_FUNCTIONS_FILE).freeze if @pac_functions.nil? # to be executed is dns + utils + user function js_to_execute = "#{pac_dns_functions(uri.host)}#{@pac_functions}#{@proxy_auto_config}" executable_js = ExecJS.compile(js_to_execute) @cache[simple_url] = executable_js.call(PAC_MAIN_FUNCTION, simple_url, uri.host) Log.log.debug { "PAC: result: #{@cache[simple_url]}" } end return @cache[simple_url] end |
#get_proxies(service_url) ⇒ Array<URI>
used to replace URI::Generic.find_proxy
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 |
# File 'lib/aspera/proxy_auto_config.rb', line 106 def get_proxies(service_url) # prepare result uri_list = [] # execute PAC script proxy_list_str = find_proxy_for_url(service_url) if !proxy_list_str.is_a?(String) Log.log.warn { "PAC: did not return a String, returned #{proxy_list_str.class}" } return uri_list end proxy_list_str.strip! proxy_list_str.gsub!(/\s+/, ' ') proxy_list_str.split(';').each do |item| # strip and split by space parts = item.strip.split proxy_type = parts.shift case proxy_type when 'DIRECT' Aspera.assert(parts.empty?, 'DIRECT has no param', type: Aspera::ParameterError) Log.log.debug('ignoring proxy DIRECT') when 'PROXY' addr_port = parts.shift Aspera.assert_type(addr_port, String, type: Aspera::ParameterError) { 'PROXY address' } Aspera.assert(parts.empty?, 'PROXY shall have one param', type: Aspera::ParameterError) begin # PAC proxy addresses are <host>:<port> if /:[0-9]+$/.match?(addr_port) uri = URI.parse("proxy://#{addr_port}") # ruby v>2.6 allows uri.user = @proxy_user uri.password = @proxy_pass uri_list.push(uri) else Log.log.warn { "PAC: PROXY must be <address>:<port>, ignoring #{addr_port}" } end rescue StandardError => e Log.log.warn { "PAC: cannot parse #{addr_port} #{e}" } end else Log.log.warn { "PAC: ignoring proxy type #{proxy_type}: not supported" } end end Log.log.debug { "Proxies: #{uri_list}" } return uri_list end |
#register_uri_generic ⇒ Object
78 79 80 81 82 |
# File 'lib/aspera/proxy_auto_config.rb', line 78 def register_uri_generic URI::Generic.register_proxy_finder { |url_str| get_proxies(url_str).first } # allow chaining return self end |