Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<table>
Expand Down
6 changes: 6 additions & 0 deletions lib/proxifier/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions lib/proxifier/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down