Skip to content

Commit 227ed00

Browse files
authored
Merge pull request #55 from evgeni/ssl-proxies
implement talking SSL to the proxy too
2 parents bd8f570 + ae2d83f commit 227ed00

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

lib/net/http.rb

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ class << HTTP
11031103
# For proxy-defining arguments +p_addr+ through +p_no_proxy+,
11041104
# see {Proxy Server}[rdoc-ref:Net::HTTP@Proxy+Server].
11051105
#
1106-
def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_no_proxy = nil)
1106+
def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_no_proxy = nil, p_use_ssl = nil)
11071107
http = super address, port
11081108

11091109
if proxy_class? then # from Net::HTTP::Proxy()
@@ -1112,6 +1112,7 @@ def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_p
11121112
http.proxy_port = @proxy_port
11131113
http.proxy_user = @proxy_user
11141114
http.proxy_pass = @proxy_pass
1115+
http.proxy_use_ssl = @proxy_use_ssl
11151116
elsif p_addr == :ENV then
11161117
http.proxy_from_env = true
11171118
else
@@ -1123,6 +1124,7 @@ def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_p
11231124
http.proxy_port = p_port || default_port
11241125
http.proxy_user = p_user
11251126
http.proxy_pass = p_pass
1127+
http.proxy_use_ssl = p_use_ssl
11261128
end
11271129

11281130
http
@@ -1190,6 +1192,7 @@ def initialize(address, port = nil) # :nodoc:
11901192
@proxy_port = nil
11911193
@proxy_user = nil
11921194
@proxy_pass = nil
1195+
@proxy_use_ssl = nil
11931196

11941197
@use_ssl = false
11951198
@ssl_context = nil
@@ -1324,6 +1327,7 @@ def response_body_encoding=(value)
13241327
# Sets the proxy password;
13251328
# see {Proxy Server}[rdoc-ref:Net::HTTP@Proxy+Server].
13261329
attr_writer :proxy_pass
1330+
attr_writer :proxy_use_ssl
13271331

13281332
# Returns the IP address for the connection.
13291333
#
@@ -1668,7 +1672,13 @@ def connect
16681672
debug "opened"
16691673
if use_ssl?
16701674
if proxy?
1671-
plain_sock = BufferedIO.new(s, read_timeout: @read_timeout,
1675+
if @proxy_use_ssl
1676+
proxy_sock = OpenSSL::SSL::SSLSocket.new(s)
1677+
ssl_socket_connect(proxy_sock, @open_timeout)
1678+
else
1679+
proxy_sock = s
1680+
end
1681+
proxy_sock = BufferedIO.new(proxy_sock, read_timeout: @read_timeout,
16721682
write_timeout: @write_timeout,
16731683
continue_timeout: @continue_timeout,
16741684
debug_output: @debug_output)
@@ -1679,8 +1689,8 @@ def connect
16791689
buf << "Proxy-Authorization: Basic #{credential}\r\n"
16801690
end
16811691
buf << "\r\n"
1682-
plain_sock.write(buf)
1683-
HTTPResponse.read_new(plain_sock).value
1692+
proxy_sock.write(buf)
1693+
HTTPResponse.read_new(proxy_sock).value
16841694
# assuming nothing left in buffers after successful CONNECT response
16851695
end
16861696

@@ -1788,13 +1798,14 @@ def do_finish
17881798
@proxy_port = nil
17891799
@proxy_user = nil
17901800
@proxy_pass = nil
1801+
@proxy_use_ssl = nil
17911802

17921803
# Creates an \HTTP proxy class which behaves like \Net::HTTP, but
17931804
# performs all access via the specified proxy.
17941805
#
17951806
# This class is obsolete. You may pass these same parameters directly to
17961807
# \Net::HTTP.new. See Net::HTTP.new for details of the arguments.
1797-
def HTTP.Proxy(p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil) #:nodoc:
1808+
def HTTP.Proxy(p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_use_ssl = nil) #:nodoc:
17981809
return self unless p_addr
17991810

18001811
Class.new(self) {
@@ -1812,6 +1823,7 @@ def HTTP.Proxy(p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil) #:nodoc:
18121823

18131824
@proxy_user = p_user
18141825
@proxy_pass = p_pass
1826+
@proxy_use_ssl = p_use_ssl
18151827
}
18161828
end
18171829

@@ -1836,6 +1848,9 @@ def proxy_class?
18361848
# Returns the password for accessing the proxy, or +nil+ if none;
18371849
# see Net::HTTP@Proxy+Server.
18381850
attr_reader :proxy_pass
1851+
1852+
# Use SSL when talking to the proxy. If Net::HTTP does not use a proxy, nil.
1853+
attr_reader :proxy_use_ssl
18391854
end
18401855

18411856
# Returns +true+ if a proxy server is defined, +false+ otherwise;

test/net/http/test_https_proxy.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,53 @@ def test_https_proxy_authentication
4343
assert_join_threads([client_thread, server_thread])
4444
}
4545
end
46+
47+
def test_https_proxy_ssl_connection
48+
begin
49+
OpenSSL
50+
rescue LoadError
51+
omit 'autoload problem. see [ruby-dev:45021][Bug #5786]'
52+
end
53+
54+
tcpserver = TCPServer.new("127.0.0.1", 0)
55+
ctx = OpenSSL::SSL::SSLContext.new
56+
ctx.key = OpenSSL::PKey::RSA.new 2048
57+
ctx.cert = OpenSSL::X509::Certificate.new
58+
ctx.cert.subject = OpenSSL::X509::Name.new [['CN', 'localhost']]
59+
ctx.cert.issuer = ctx.cert.subject
60+
ctx.cert.public_key = ctx.key
61+
ctx.cert.not_before = Time.now
62+
ctx.cert.not_after = Time.now + 60 * 60 * 24
63+
ctx.cert.sign ctx.key, OpenSSL::Digest::SHA1.new
64+
serv = OpenSSL::SSL::SSLServer.new(tcpserver, ctx)
65+
66+
_, port, _, _ = serv.addr
67+
client_thread = Thread.new {
68+
proxy = Net::HTTP.Proxy("127.0.0.1", port, 'user', 'password', true)
69+
http = proxy.new("foo.example.org", 8000)
70+
http.use_ssl = true
71+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
72+
begin
73+
http.start
74+
rescue EOFError
75+
end
76+
}
77+
server_thread = Thread.new {
78+
sock = serv.accept
79+
begin
80+
proxy_request = sock.gets("\r\n\r\n")
81+
assert_equal(
82+
"CONNECT foo.example.org:8000 HTTP/1.1\r\n" +
83+
"Host: foo.example.org:8000\r\n" +
84+
"Proxy-Authorization: Basic dXNlcjpwYXNzd29yZA==\r\n" +
85+
"\r\n",
86+
proxy_request,
87+
"[ruby-core:96672]")
88+
ensure
89+
sock.close
90+
end
91+
}
92+
assert_join_threads([client_thread, server_thread])
93+
end
4694
end if defined?(OpenSSL)
4795

0 commit comments

Comments
 (0)