Search code examples
ruby-on-railsrubycloud-foundryfaradayfaraday-oauth

Faraday - Can I use outbound proxy for requests?


My app works great on localhost. Our Cloud Foundry setup somehow does not have outbound connectivity... so I have been told I can get outbound connectivity through a proxy.

What do I need to change in my Faraday requests to use that proxy?

My code and how I am supposed to connect.

url = "#{@config[:host]}#{@config[:login_path]}"
c = Faraday.new(:url => url,
                :request => { :timeout => 20, :open_timeout => 5},
                :ssl => {:verify => false}) do |f|
  f.request :url_encoded
  f.adapter Faraday.default_adapter
  f.response :json, :content_type => /\bjson$/
end

  @resp = c.post(url, {username: username, password: password,
    deviceId: @config[:device_id], deviceName: @config[:device_name], deviceType: @config[:device_type],
    appId: @config[:app_id], scope: @config[:scope]})

This is how I am supposed to use the proxy that was set up.

"Once your service is bound, the application will have service definition in JSON format in VCAP_SERVICES environment variable you can see the format using cf env x-explorer command. Your app will need to parse and setup proxy using the given credentials.

Here is an example of the credentials. You can use only uri or individual parameters: host, port, username and password"

uri: http://2abcf906-127e-495c-ae71-c69712862292:616e714aea0cfc81de67ad1445cf1448e38d6a8db345a452c0e1ddc830b8f132@f.services.g1.app.cloud.my_company.net:49349
host: f.services.g1.app.cloud.my_company.net
port: 49349
username: 2abcf906-127e-495c-ae71-c69712862292
password: 616e714aea0cfc81de67ad1445cf1448e38d6a8db345a452c0e1ddc830b8f132

Again, app works great on localhost. No outbound connectivity with company Cloud Foundry setup. I have bound the service, so that is fine.

What do I add to have my outbound requests go through the proxy?


Solution

  • Try this

    url = "#{@config[:host]}#{@config[:login_path]}"
    c = Faraday.new(:url => url,
                    :request => { :timeout => 20, :open_timeout => 5},
                    :ssl => {:verify => false}) do |f|
      f.request :url_encoded
      f.adapter Faraday.default_adapter
      f.response :json, :content_type => /\bjson$/url = "#{@config[:host]}#{@config[:login_path]}"
    c = Faraday.new(:url => url,
                    :request => { :timeout => 20, :open_timeout => 5},
                    :ssl => {:verify => false}) do |f|
      f.request :url_encoded
      f.adapter Faraday.default_adapter
      f.response :json, :content_type => /\bjson$/
      f.proxy = "http://2abcf906-127e-495c-ae71-c69712862292:616e714aea0cfc81de67ad1445cf1448e38d6a8db345a452c0e1ddc830b8f132@f.services.g1.app.cloud.my_company.net:49349"
    end
    
      @resp = c.post(url, {username: username, password: password,
        deviceId: @config[:device_id], deviceName: @config[:device_name], deviceType: @config[:device_type],
        appId: @config[:app_id], scope: @config[:scope]})
    
    end
    
      @resp = c.post(url, {username: username, password: password,
        deviceId: @config[:device_id], deviceName: @config[:device_name], deviceType: @config[:device_type],
        appId: @config[:app_id], scope: @config[:scope]})
    

    Here they says how to set up a proxy on faraday request doing

    conn = Faraday.new(url, ssl: {verify:false}) do |conn|
      # middleware ...
      conn.adapter <adapter>
    
      conn.proxy "http://localhost:80"
    end
    

    https://github.com/lostisland/faraday/issues/221