Class: Aspera::ProxyAutoConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/proxy_auto_config.rb

Overview

Evaluate a proxy auto config script

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy_auto_config) ⇒ ProxyAutoConfig

Returns a new instance of ProxyAutoConfig.

Parameters:

  • proxy_auto_config

    the proxy auto config script to be evaluated



60
61
62
63
64
65
66
# File 'lib/aspera/proxy_auto_config.rb', line 60

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

Parameters:

  • value

    the value to set the attribute proxy_pass to.



57
58
59
# File 'lib/aspera/proxy_auto_config.rb', line 57

def proxy_pass=(value)
  @proxy_pass = value
end

#proxy_user=(value) ⇒ Object (writeonly)

Sets the attribute proxy_user

Parameters:

  • value

    the value to set the attribute proxy_user to.



57
58
59
# File 'lib/aspera/proxy_auto_config.rb', line 57

def proxy_user=(value)
  @proxy_user = value
end

Instance Method Details

#find_proxy_for_url(service_url) ⇒ Object

execute proxy auto config script for the given URL : en.wikipedia.org/wiki/Proxy_auto-config

Returns:

  • either nil, or a String formatted following PAC standard



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/aspera/proxy_auto_config.rb', line 76

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) ⇒ Object

used to replace URI::Generic.find_proxy

Returns:

  • Array of URI, possibly empty



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
# File 'lib/aspera/proxy_auto_config.rb', line 96

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
    case parts.shift
    when 'DIRECT'
      Aspera.assert(parts.empty?){'DIRECT has no param'}
      Log.log.debug('ignoring proxy DIRECT')
    when 'PROXY'
      addr_port = parts.shift
      Aspera.assert_type(addr_port, String)
      Aspera.assert(parts.empty?){'PROXY shall have one param'}
      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 #{parts.first}: not supported"}
    end
  end
  Log.log.debug{"Proxies: #{uri_list}"}
  return uri_list
end

#register_uri_genericObject



68
69
70
71
72
# File 'lib/aspera/proxy_auto_config.rb', line 68

def register_uri_generic
  URI::Generic.register_proxy_finder{|url_str|get_proxies(url_str).first}
  # allow chaining
  return self
end