Faraday: 覆盖环境代理设置

创建于 2017-11-28  ·  5评论  ·  资料来源: lostisland/faraday

我无法弄清楚法拉第是否有一种方法可以覆盖环境变量代理设置(http_proxy、https_proxy、no_proxy)。 我试过在Faraday.new参数和配置块中设置proxy=nil ,但这似乎不起作用:

> c = Faraday.new('https://www.google.com', proxy: nil)
 => #<Faraday::Connection:0x00007fd1e2176180 @parallel_manager=nil, @headers={"User-Agent"=>"Faraday v0.13.1"}, @params={}, @options=#<Faraday::RequestOptions (empty)>, @ssl=#<Faraday::SSLOptions (empty)>, @default_parallel_manager=nil, @builder=#<Faraday::RackBuilder:0x00007fd1e2175d20 @handlers=[Faraday::Request::UrlEncoded, Faraday::Adapter::NetHttp]>, @url_prefix=#<URI::HTTPS https://www.google.com/>, @proxy=#<Faraday::ProxyOptions uri=#<URI::HTTP http://127.0.0.1:3128/>>, @temp_proxy=#<Faraday::ProxyOptions uri=#<URI::HTTP http://127.0.0.1:3128/>>>
> c = Faraday.new('https://www.google.com') { |c| c.proxy=nil 
c.adapter :net_http
}
(Object doesn't support #inspect)
> c = Faraday.new('https://www.google.com', proxy: '')
 => #<Faraday::Connection:0x00007fd1e28f2300 @parallel_manager=nil, @headers={"User-Agent"=>"Faraday v0.13.1"}, @params={}, @options=#<Faraday::RequestOptions (empty)>, @ssl=#<Faraday::SSLOptions (empty)>, @default_parallel_manager=nil, @builder=#<Faraday::RackBuilder:0x00007fd1e28f1d60 @handlers=[Faraday::Request::UrlEncoded, Faraday::Adapter::NetHttp]>, @url_prefix=#<URI::HTTPS https://www.google.com/>, @proxy=#<Faraday::ProxyOptions uri=#<URI::Generic >>, @temp_proxy=#<Faraday::ProxyOptions uri=#<URI::Generic >>>
> c = Faraday.new('https://www.google.com/') { |co|
2.4.2 :025 >     co.proxy = ''
2.4.2 :026?>   co.adapter :net_http
2.4.2 :027?>   }
 => #<Faraday::Connection:0x00007fe0af1bd4c8 @parallel_manager=nil, @headers={"User-Agent"=>"Faraday v0.13.1"}, @params={}, @options=#<Faraday::RequestOptions (empty)>, @ssl=#<Faraday::SSLOptions (empty)>, @default_parallel_manager=nil, @builder=#<Faraday::RackBuilder:0x00007fe0af1bd1d0 @handlers=[Faraday::Adapter::NetHttp]>, @url_prefix=#<URI::HTTPS https://nexus-gss.uscis.dhs.gov/>, @proxy=#<Faraday::ProxyOptions uri=#<URI::Generic >>, @temp_proxy=#<Faraday::ProxyOptions uri=#<URI::HTTP http://127.0.0.1:3128/>>>

最后一个结果是ProxyOptions.from(URI::Generic)或某种被分配为代理的东西,它仍然产生一个非零代理

https://github.com/lostisland/faraday/blob/master/lib/faraday/adapter/net_http.rb#L88 -L94

似乎并没有真正正确处理这个案子。 有任何想法吗?

(虽然代码潜水似乎选项解析可能不能很好地处理显式 nil 设置)

feature help wanted

最有用的评论

@guille-moe 我计划解决 @jeffb-stell 问题的解决方案应该允许您还专门使这些警告静音。

所有5条评论

嗨@jeffb-stell,

我的理解是你想忽略法拉第的环境代理值。
我真的不明白您为什么要这样做,但显然直到现在才考虑过这种情况。
正如您自己已经发现的那样,如果您通过nil ProxyOptions 会变得有点疯狂,这就是为什么您的解决方案都不起作用的原因。
我可以尝试用 PR 来解决这个问题,但只有在 Faraday 0.14.0 或 0.14.1 发布时才能使用它,所以你需要等待并在时机成熟时更新(可能会审查 PR如果你想贡献!)。

与此同时,一个可能的快速解决方法是在覆盖 ENV 变量的情况下启动您的应用程序。 只需在用于启动它的命令前加上env -u http_proxy即可。 例如:

# For the Ruby console
env -u http_proxy irb

# For Rails
env -u http_proxy rails

@iMacTia

同样的问题,除了no_proxy env 上的warn (https://github.com/lostisland/faraday/blob/4d1ddc130bd224adf25a19eaf0e81fd236a47285/lib/ faraday/connection.rb#L460) 默认情况下,当我在 macOS 上的 docker 容器中使用法拉第(或它的 gem)时,我会得到一个no_proxy env: https :

对我来说最好的解决方案是什么或对法拉第的改进是什么? (可以很快提出 PR)

它可以被称为像ENV['FARADAY_SILENCE_INTERNAL_WARNINGS']这样明确的东西吗?

或者,@guille-moe 是RUBYOPT=-W0 - 始终静音warn ,在您的用例中不实用?

@guille-moe 我计划解决 @jeffb-stell 问题的解决方案应该允许您还专门使这些警告静音。

@guille-moe @jeffb-stell
我在#754 中引入了一个新设置,您现在可以像这样禁用环境代理检测:

Faraday.ignore_env_proxy = true # new setting, defaults to false
ENV['http_proxy'] = 'http://test.proxy.com:80'

conn = Faraday::Connection.new(....)
# conn will NOT use the env proxy

我还没有合并 PR,如果你能花一些时间在你的应用程序中测试它,那会让我更有信心合并它 😄
也欢迎提供额外测试的建议👍

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

mvastola picture mvastola  ·  4评论

jordansissel picture jordansissel  ·  5评论

t3hk0d3 picture t3hk0d3  ·  3评论

mattmill30 picture mattmill30  ·  4评论

yusefu picture yusefu  ·  3评论