-
Notifications
You must be signed in to change notification settings - Fork 291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added SOCKS[45] proxy support #380
Open
mumumu
wants to merge
11
commits into
nahi:master
Choose a base branch
from
mumumu:177_add_socks_proxy_support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f1d3700
- implemented socks proxy functionality
mumumu a56879e
- fixed test server env dependent logger
mumumu e7cb964
- fixed bug socks server emits 'Bad Protocol version' error when auth…
mumumu 43847ba
- changed socks server default loglevel: INFO -> WARN
mumumu b24b9b6
- raise RuntimeError in case of invalid proxy url
mumumu 22f1fb8
- updated README.md
mumumu c709305
- fixed ENV using sample
mumumu e8486e1
applied #386 for fixing CI error.
mumumu 56679f2
Merge branch 'master' of https://github.com/nahi/httpclient into 177_…
mumumu 9aeb8e7
followed origin/master changes
mumumu d9e37ab
fixed JRuby CI error
mumumu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
require 'httpclient/timeout' # TODO: remove this once we drop 1.8 support | ||
require 'httpclient/ssl_config' | ||
require 'httpclient/http' | ||
require 'httpclient/socks' | ||
if defined? JRUBY_VERSION | ||
require 'httpclient/jruby_ssl_socket' | ||
else | ||
|
@@ -92,6 +93,8 @@ def inspect # :nodoc: | |
|
||
# Manages sessions for a HTTPClient instance. | ||
class SessionManager | ||
include Util | ||
|
||
# Name of this client. Used for 'User-Agent' header in HTTP request. | ||
attr_accessor :agent_name | ||
# Owner of this client. Used for 'From' header in HTTP request. | ||
|
@@ -132,6 +135,8 @@ class SessionManager | |
def initialize(client) | ||
@client = client | ||
@proxy = client.proxy | ||
@socks_user = nil | ||
@socks_password = nil | ||
|
||
@agent_name = nil | ||
@from = nil | ||
|
@@ -167,6 +172,10 @@ def proxy=(proxy) | |
@proxy = nil | ||
else | ||
@proxy = Site.new(proxy) | ||
if socks?(proxy) | ||
@socks_user = proxy.user | ||
@socks_password = proxy.password | ||
end | ||
end | ||
end | ||
|
||
|
@@ -218,6 +227,8 @@ def open(uri, via_proxy = false) | |
site = Site.new(uri) | ||
sess = Session.new(@client, site, @agent_name, @from) | ||
sess.proxy = via_proxy ? @proxy : nil | ||
sess.socks_user = @socks_user | ||
sess.socks_password = @socks_password | ||
sess.socket_sync = @socket_sync | ||
sess.tcp_keepalive = @tcp_keepalive | ||
sess.requested_version = @protocol_version if @protocol_version | ||
|
@@ -448,6 +459,9 @@ class Session | |
# Device for dumping log for debugging | ||
attr_accessor :debug_dev | ||
|
||
attr_accessor :socks_user | ||
attr_accessor :socks_password | ||
|
||
attr_accessor :connect_timeout | ||
attr_accessor :connect_retry | ||
attr_accessor :send_timeout | ||
|
@@ -469,6 +483,8 @@ def initialize(client, dest, agent_name, from) | |
@client = client | ||
@dest = dest | ||
@proxy = nil | ||
@socks_user = nil | ||
@socks_password = nil | ||
@socket_sync = true | ||
@tcp_keepalive = false | ||
@requested_version = nil | ||
|
@@ -627,6 +643,32 @@ def create_socket(host, port) | |
socket | ||
end | ||
|
||
def via_socks_proxy? | ||
@proxy && socks?(@proxy) | ||
end | ||
|
||
def via_http_proxy? | ||
@proxy && !socks?(@proxy) | ||
end | ||
|
||
def create_socks_socket(proxy, dest) | ||
if socks4?(proxy) | ||
options = {} | ||
options = { user: @socks_user } if @socks_user | ||
socks_socket = SOCKS4Socket.new(proxy.host, proxy.port, options) | ||
elsif socks5?(proxy) | ||
options = {} | ||
options = { user: @socks_user } if @socks_user | ||
options[:password] = @socks_password if @socks_password | ||
socks_socket = SOCKS5Socket.new(proxy.host, proxy.port, options) | ||
else | ||
raise "invalid proxy url #{proxy}" | ||
end | ||
dest_site = Site.new(dest) | ||
opened_socket = socks_socket.open(dest_site.host, dest_site.port, {}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
opened_socket | ||
end | ||
|
||
def create_loopback_socket(host, port, str) | ||
@debug_dev << "! CONNECT TO #{host}:#{port}\n" if @debug_dev | ||
socket = LoopBackSocket.new(host, port, str) | ||
|
@@ -751,6 +793,8 @@ def connect | |
elsif https?(@dest) | ||
@socket = SSLSocket.create_socket(self) | ||
@ssl_peer_cert = @socket.peer_cert | ||
elsif socks?(@proxy) | ||
@socket = create_socks_socket(@proxy, @dest) | ||
else | ||
@socket = create_socket(site.host, site.port) | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
# Copyright (c) 2008 Jamis Buck | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the 'Software'), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
require 'socket' | ||
require 'resolv' | ||
require 'ipaddr' | ||
|
||
# | ||
# This implementation was borrowed from Net::SSH::Proxy | ||
# | ||
class HTTPClient | ||
# An Standard SOCKS error. | ||
class SOCKSError < SocketError; end | ||
|
||
# Used for reporting proxy connection errors. | ||
class SOCKSConnectError < SOCKSError; end | ||
|
||
# Used when the server doesn't recognize the user's credentials. | ||
class SOCKSUnauthorizedError < SOCKSError; end | ||
|
||
# An implementation of a SOCKS4 proxy. | ||
class SOCKS4Socket | ||
# The SOCKS protocol version used by this class | ||
VERSION = 4 | ||
|
||
# The packet type for connection requests | ||
CONNECT = 1 | ||
|
||
# The status code for a successful connection | ||
GRANTED = 90 | ||
|
||
# The proxy's host name or IP address, as given to the constructor. | ||
attr_reader :proxy_host | ||
|
||
# The proxy's port number. | ||
attr_reader :proxy_port | ||
|
||
# The additional options that were given to the proxy's constructor. | ||
attr_reader :options | ||
|
||
# Create a new proxy connection to the given proxy host and port. | ||
# Optionally, a :user key may be given to identify the username | ||
# with which to authenticate. | ||
def initialize(proxy_host, proxy_port = 1080, options = {}) | ||
@proxy_host = proxy_host | ||
@proxy_port = proxy_port | ||
@options = options | ||
end | ||
|
||
# Return a new socket connected to the given host and port via the | ||
# proxy that was requested when the socket factory was instantiated. | ||
def open(host, port, connection_options) | ||
socket = Socket.tcp(proxy_host, proxy_port, nil, nil, | ||
connect_timeout: connection_options[:timeout]) | ||
ip_addr = IPAddr.new(Resolv.getaddress(host)) | ||
|
||
packet = [ | ||
VERSION, CONNECT, port.to_i, | ||
ip_addr.to_i, options[:user] | ||
].pack('CCnNZ*') | ||
socket.send packet, 0 | ||
|
||
_version, status, _port, _ip = socket.recv(8).unpack('CCnN') | ||
if status != GRANTED | ||
socket.close | ||
raise SOCKSConnectError, "error connecting to socks proxy (#{status})" | ||
end | ||
|
||
socket | ||
end | ||
end | ||
|
||
# An implementation of a SOCKS5 proxy. | ||
class SOCKS5Socket | ||
# The SOCKS protocol version used by this class | ||
VERSION = 5 | ||
|
||
# The SOCKS authentication type for requests without authentication | ||
METHOD_NO_AUTH = 0 | ||
|
||
# The SOCKS authentication type for requests via username/password | ||
METHOD_PASSWD = 2 | ||
|
||
# The SOCKS authentication type for when there are no supported | ||
# authentication methods. | ||
METHOD_NONE = 0xFF | ||
|
||
# The SOCKS packet type for requesting a proxy connection. | ||
CMD_CONNECT = 1 | ||
|
||
# The SOCKS address type for connections via IP address. | ||
ATYP_IPV4 = 1 | ||
|
||
# The SOCKS address type for connections via domain name. | ||
ATYP_DOMAIN = 3 | ||
|
||
# The SOCKS response code for a successful operation. | ||
SUCCESS = 0 | ||
|
||
# The proxy's host name or IP address | ||
attr_reader :proxy_host | ||
|
||
# The proxy's port number | ||
attr_reader :proxy_port | ||
|
||
# The map of options given at initialization | ||
attr_reader :options | ||
|
||
# Create a new proxy connection to the given proxy host and port. | ||
# Optionally, :user and :password options may be given to | ||
# identify the username and password with which to authenticate. | ||
def initialize(proxy_host, proxy_port = 1080, options = {}) | ||
@proxy_host = proxy_host | ||
@proxy_port = proxy_port | ||
@options = options | ||
end | ||
|
||
# Return a new socket connected to the given host and port via the | ||
# proxy that was requested when the socket factory was instantiated. | ||
def open(host, port, connection_options) | ||
socket = Socket.tcp(proxy_host, proxy_port, nil, nil, | ||
connect_timeout: connection_options[:timeout]) | ||
|
||
methods = [METHOD_NO_AUTH] | ||
methods << METHOD_PASSWD if options[:user] | ||
|
||
packet = [VERSION, methods.size, *methods].pack('C*') | ||
socket.send packet, 0 | ||
|
||
version, method = socket.recv(2).unpack('CC') | ||
if version != VERSION | ||
socket.close | ||
raise SOCKSError, "invalid SOCKS version (#{version})" | ||
end | ||
|
||
if method == METHOD_NONE | ||
socket.close | ||
raise SOCKSError, 'no supported authorization methods' | ||
end | ||
|
||
negotiate_password(socket) if method == METHOD_PASSWD | ||
|
||
packet = [VERSION, CMD_CONNECT, 0].pack('C*') | ||
|
||
if host =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/ | ||
packet << [ATYP_IPV4, $1.to_i, $2.to_i, $3.to_i, $4.to_i].pack('C*') | ||
else | ||
packet << [ATYP_DOMAIN, host.length, host].pack('CCA*') | ||
end | ||
|
||
packet << [port].pack('n') | ||
socket.send packet, 0 | ||
|
||
_version, reply, = socket.recv(2).unpack('C*') | ||
socket.recv(1) | ||
address_type = socket.recv(1).getbyte(0) | ||
case address_type | ||
when 1 | ||
socket.recv(4) # get four bytes for IPv4 address | ||
when 3 | ||
len = socket.recv(1).getbyte(0) | ||
_hostname = socket.recv(len) | ||
when 4 | ||
_ipv6addr _hostname = socket.recv(16) | ||
else | ||
socket.close | ||
raise SOCKSConnectError, 'Illegal response type' | ||
end | ||
_portnum = socket.recv(2) | ||
|
||
unless reply == SUCCESS | ||
socket.close | ||
raise SOCKSConnectError, reply.to_s | ||
end | ||
|
||
socket | ||
end | ||
|
||
private | ||
|
||
# Simple username/password negotiation with the SOCKS5 server. | ||
def negotiate_password(socket) | ||
packet = [ | ||
0x01, options[:user].length, options[:user], | ||
options[:password].length, options[:password] | ||
].pack('CCA*CA*') | ||
socket.send packet, 0 | ||
|
||
_version, status = socket.recv(2).unpack('CC') | ||
|
||
return if status == SUCCESS | ||
socket.close | ||
raise SOCKSUnauthorizedError, 'could not authorize user' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would suggest changing this line to
elsif getenv('http_proxy')
. As it now stands, setting "HTTP_PROXY" does not appear to work anymore, only works with "http_proxy"