From f00b4afabd8df5cb74af129ae019da9e79a5a65b Mon Sep 17 00:00:00 2001 From: Bill Kirtley Date: Thu, 20 Oct 2011 15:28:59 -0400 Subject: [PATCH] Better configuration for whole environment setups Environment variable PROXY_FILTER_LIST is a comma separated list of hosts (or IPs) to not use the proxy with (e.g. '10.0.0.1,10.0.2.1') Environment variable PROXY_FILTER_REGEXP is a regular expression, where if the host matches, it connects directly, not through the proxy (e.g., '(10\..*|localhost)') --- README.md | 17 +++++++++++++++++ lib/proxifier/env.rb | 6 ++++++ lib/proxifier/proxy.rb | 8 ++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 274b9af..68d0839 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,23 @@ socket << "GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n" socket.gets # => "HTTP/1.1 200 OK\r\n" ``` +### Environment - patching TCPSocket + +```ruby +require 'proxifier/env' +ENV['PROXY'] = 'http://10.0.0.44:8888' # How to find the proxy +ENV['PROXY_FILTER_REGEX'] = '(10\..*|localhost)' # Do not use proxy for connections inside internal net or localhost +ENV['PROXY_FILTER_LIST'] = '66.211.168.123,207.97.227.239' +``` + +Afterwards, any new TCPSocket connections will use the proxy, unless they're excluded either by the specific PROXY_FILTER_LIST or the PROXY_FILTER_REGEX. + +PROXY_FILTER_LIST specifies a comma separated list of hostnames or IP addresses (whichever the client code is going to use). + +PROXY_FILTER_REGEX is a regular expression, where if the connection hostname (or IP address) matches the regex, connections will be made directly and not through the proxy. + +Both PROXY_FILTER_LIST and PROXY_FILTER_REGEX are optional, but only one will be used. + ## Supported Proxies diff --git a/lib/proxifier/env.rb b/lib/proxifier/env.rb index 4ce92ea..1cb1064 100644 --- a/lib/proxifier/env.rb +++ b/lib/proxifier/env.rb @@ -36,6 +36,12 @@ def initialize_with_proxy(host, port, options_or_local_host = {}, local_port = n options = options_if_local_host end + if ENV['PROXY_FILTER_REGEX'].present? + options[:no_proxy] = /\A#{ENV['PROXY_FILTER_REGEX']}\z/ + elsif ENV['PROXY_FILTER_LIST'].present? + options[:no_proxy] = ENV['PROXY_FILTER_LIST'] + end + if options[:proxy] && (proxy = Proxifier::Proxy(options.delete(:proxy), options)) && proxy.proxify?(host) initialize_without_proxy(proxy.host, proxy.port, local_host, local_port) begin diff --git a/lib/proxifier/proxy.rb b/lib/proxifier/proxy.rb index 93195fc..87eed39 100644 --- a/lib/proxifier/proxy.rb +++ b/lib/proxifier/proxy.rb @@ -8,8 +8,12 @@ class << self def proxify?(host, no_proxy = nil) return true unless no_proxy - dont_proxy = no_proxy.split(",") - dont_proxy.none? { |h| host =~ /#{h}\Z/ } + if no_proxy.is_a?(Regexp) + (host =~ no_proxy).nil? + else + dont_proxy = no_proxy.split(",") + dont_proxy.none? { |h| host =~ /#{h}\Z/ } + end end end