1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
# frozen_string_literal: true
require 'spec_helper'
require 'support/fake_socks5_server'
require 'webrick'
require 'webrick/https'
describe 'SOCKS5 proxy support' do
# --- Unit tests for proxy string parsing ---
describe Excon::SOCKS5 do
let(:parser) { Class.new { include Excon::SOCKS5 }.new }
describe '#parse_socks5_proxy' do
it 'parses host:port' do
host, port, user, pass = parser.send(:parse_socks5_proxy, 'proxy.example.com:1080')
expect(host).to eq('proxy.example.com')
expect(port).to eq('1080')
expect(user).to be_nil
expect(pass).to be_nil
end
it 'defaults port to 1080' do
host, port, _, _ = parser.send(:parse_socks5_proxy, 'proxy.example.com')
expect(host).to eq('proxy.example.com')
expect(port).to eq('1080')
end
it 'parses user:pass@host:port' do
host, port, user, pass = parser.send(:parse_socks5_proxy, 'myuser:mypass@proxy.example.com:1080')
expect(host).to eq('proxy.example.com')
expect(port).to eq('1080')
expect(user).to eq('myuser')
expect(pass).to eq('mypass')
end
it 'parses socks5://host:port' do
host, port, user, pass = parser.send(:parse_socks5_proxy, 'socks5://proxy.example.com:1080')
expect(host).to eq('proxy.example.com')
expect(port).to eq('1080')
expect(user).to be_nil
expect(pass).to be_nil
end
it 'parses socks5://user:pass@host:port' do
host, port, user, pass = parser.send(:parse_socks5_proxy, 'socks5://myuser:mypass@proxy.example.com:9050')
expect(host).to eq('proxy.example.com')
expect(port).to eq('9050')
expect(user).to eq('myuser')
expect(pass).to eq('mypass')
end
it 'handles passwords containing colons' do
_, _, user, pass = parser.send(:parse_socks5_proxy, 'user:p:a:ss@host:1080')
expect(user).to eq('user')
expect(pass).to eq('p:a:ss')
end
end
end
# --- Validation: :socks5_proxy is a valid connection key ---
describe 'connection parameter validation' do
it 'accepts :socks5_proxy as a valid connection key' do
expect {
Excon.new('http://example.com', socks5_proxy: 'localhost:1080', mock: true)
}.not_to raise_error
end
end
# --- Class hierarchy ---
describe 'class hierarchy' do
it 'SOCKS5Socket inherits from Excon::Socket' do
expect(Excon::SOCKS5Socket.superclass).to eq(Excon::Socket)
end
it 'SOCKS5SSLSocket inherits from Excon::SSLSocket' do
expect(Excon::SOCKS5SSLSocket.superclass).to eq(Excon::SSLSocket)
end
end
# --- Integration tests ---
context 'integration' do
cert_dir = File.expand_path('../../../tests/data', __FILE__)
before(:all) do
@cert_dir = File.expand_path('../../../tests/data', __FILE__)
# HTTP backend
@webrick = WEBrick::HTTPServer.new(
Port: 0,
Logger: WEBrick::Log.new('/dev/null'),
AccessLog: []
)
@webrick.mount_proc('/content-length/100') do |_req, res|
res.body = 'x' * 100
end
@webrick.mount_proc('/echo') do |req, res|
res.body = req.body || ''
end
@backend_port = @webrick.config[:Port]
@webrick_thread = Thread.new { @webrick.start }
# HTTPS backend
@webrick_ssl = WEBrick::HTTPServer.new(
Port: 0,
Logger: WEBrick::Log.new('/dev/null'),
AccessLog: [],
SSLEnable: true,
SSLCertName: [['CN', '127.0.0.1']],
SSLCertificate: OpenSSL::X509::Certificate.new(File.read(File.join(@cert_dir, '127.0.0.1.cert.crt'))),
SSLPrivateKey: OpenSSL::PKey::RSA.new(File.read(File.join(@cert_dir, '127.0.0.1.cert.key')))
)
@webrick_ssl.mount_proc('/content-length/100') do |_req, res|
res.body = 'x' * 100
end
@webrick_ssl.mount_proc('/echo') do |req, res|
res.body = req.body || ''
end
@ssl_port = @webrick_ssl.config[:Port]
@webrick_ssl_thread = Thread.new { @webrick_ssl.start }
# Shared no-auth SOCKS5 proxy (used by most tests)
@socks5 = Excon::Test::FakeSOCKS5Server.new
@socks5.start
end
after(:all) do
@socks5&.stop
@webrick&.shutdown
@webrick_thread&.join(2)
@webrick_ssl&.shutdown
@webrick_ssl_thread&.join(2)
end
# --- HTTP through SOCKS5 (no auth) ---
it 'makes a successful GET request' do
conn = Excon.new(
"http://127.0.0.1:#{@backend_port}",
socks5_proxy: "127.0.0.1:#{@socks5.port}"
)
response = conn.request(method: :get, path: '/content-length/100')
expect(response.status).to eq(200)
expect(response.body).to eq('x' * 100)
end
it 'tracks remote_ip from the proxy connection' do
conn = Excon.new(
"http://127.0.0.1:#{@backend_port}",
socks5_proxy: "127.0.0.1:#{@socks5.port}"
)
response = conn.request(method: :get, path: '/content-length/100')
expect(response.remote_ip).to eq('127.0.0.1')
end
it 'forwards POST request bodies' do
conn = Excon.new(
"http://127.0.0.1:#{@backend_port}",
socks5_proxy: "127.0.0.1:#{@socks5.port}"
)
response = conn.request(method: :post, path: '/echo', body: 'hello SOCKS5')
expect(response.status).to eq(200)
expect(response.body).to eq('hello SOCKS5')
end
it 'works with nonblock: false' do
conn = Excon.new(
"http://127.0.0.1:#{@backend_port}",
socks5_proxy: "127.0.0.1:#{@socks5.port}",
nonblock: false
)
response = conn.request(method: :get, path: '/content-length/100')
expect(response.status).to eq(200)
expect(response.body).to eq('x' * 100)
end
it 'supports multiple requests on a persistent connection' do
conn = Excon.new(
"http://127.0.0.1:#{@backend_port}",
socks5_proxy: "127.0.0.1:#{@socks5.port}",
persistent: true
)
r1 = conn.request(method: :get, path: '/content-length/100')
r2 = conn.request(method: :post, path: '/echo', body: 'second')
expect(r1.status).to eq(200)
expect(r1.body).to eq('x' * 100)
expect(r2.status).to eq(200)
expect(r2.body).to eq('second')
conn.reset
end
# --- HTTP through SOCKS5 (with auth) ---
context 'with username/password authentication' do
before(:all) do
@socks5_auth = Excon::Test::FakeSOCKS5Server.new(
username: 'testuser',
password: 'testpass'
)
@socks5_auth.start
end
after(:all) { @socks5_auth&.stop }
it 'authenticates and makes a successful request' do
conn = Excon.new(
"http://127.0.0.1:#{@backend_port}",
socks5_proxy: "testuser:testpass@127.0.0.1:#{@socks5_auth.port}"
)
response = conn.request(method: :get, path: '/content-length/100')
expect(response.status).to eq(200)
expect(response.body).to eq('x' * 100)
end
it 'works with socks5:// URI scheme' do
conn = Excon.new(
"http://127.0.0.1:#{@backend_port}",
socks5_proxy: "socks5://testuser:testpass@127.0.0.1:#{@socks5_auth.port}"
)
response = conn.request(method: :get, path: '/content-length/100')
expect(response.status).to eq(200)
end
it 'raises an error with wrong credentials' do
conn = Excon.new(
"http://127.0.0.1:#{@backend_port}",
socks5_proxy: "wrong:creds@127.0.0.1:#{@socks5_auth.port}"
)
expect { conn.request(method: :get, path: '/') }.to raise_error(
Excon::Errors::SocketError, /SOCKS5 proxy authentication failed/
)
end
end
# --- HTTPS through SOCKS5 ---
it 'makes a successful HTTPS request through the SOCKS5 proxy' do
conn = Excon.new(
"https://127.0.0.1:#{@ssl_port}",
socks5_proxy: "127.0.0.1:#{@socks5.port}",
ssl_verify_peer: false
)
response = conn.request(method: :get, path: '/content-length/100')
expect(response.status).to eq(200)
expect(response.body).to eq('x' * 100)
end
it 'forwards POST bodies over HTTPS' do
conn = Excon.new(
"https://127.0.0.1:#{@ssl_port}",
socks5_proxy: "127.0.0.1:#{@socks5.port}",
ssl_verify_peer: false
)
response = conn.request(method: :post, path: '/echo', body: 'encrypted payload')
expect(response.status).to eq(200)
expect(response.body).to eq('encrypted payload')
end
it 'verifies the SSL peer when ssl_verify_peer is true' do
conn = Excon.new(
"https://127.0.0.1:#{@ssl_port}",
socks5_proxy: "127.0.0.1:#{@socks5.port}",
ssl_verify_peer: true,
ssl_ca_file: File.join(cert_dir, '127.0.0.1.cert.crt')
)
response = conn.request(method: :get, path: '/content-length/100')
expect(response.status).to eq(200)
end
# --- Error cases ---
context 'when proxy rejects CONNECT' do
before(:all) do
@socks5_reject = Excon::Test::FakeSOCKS5Server.new(reject_connect: true)
@socks5_reject.start
end
after(:all) { @socks5_reject&.stop }
it 'raises an error' do
conn = Excon.new(
"http://127.0.0.1:#{@backend_port}",
socks5_proxy: "127.0.0.1:#{@socks5_reject.port}"
)
expect { conn.request(method: :get, path: '/') }.to raise_error(
Excon::Errors::SocketError, /SOCKS5 proxy connect failed/
)
end
end
it 'raises an error when proxy is unreachable' do
conn = Excon.new(
"http://127.0.0.1:#{@backend_port}",
socks5_proxy: '127.0.0.1:1',
connect_timeout: 1
)
expect { conn.request(method: :get, path: '/') }.to raise_error(Excon::Error)
end
it 'raises an error when hostname exceeds 255 bytes' do
long_host = 'a' * 256 + '.example.com'
conn = Excon::Connection.new(
host: long_host,
hostname: long_host,
port: @backend_port,
scheme: 'http',
socks5_proxy: "127.0.0.1:#{@socks5.port}"
)
expect { conn.request(method: :get, path: '/') }.to raise_error(
Excon::Errors::SocketError, /hostname exceeds maximum length/
)
end
# --- Edge cases ---
it 'gives SOCKS5 precedence when both :proxy and :socks5_proxy are set' do
conn = Excon.new(
"http://127.0.0.1:#{@backend_port}",
proxy: 'http://bogus-proxy.invalid:9999',
socks5_proxy: "127.0.0.1:#{@socks5.port}"
)
response = conn.request(method: :get, path: '/content-length/100')
expect(response.status).to eq(200)
end
it 'clears :proxy from data after connect so requests use relative URIs' do
conn = Excon.new(
"http://127.0.0.1:#{@backend_port}",
socks5_proxy: "127.0.0.1:#{@socks5.port}"
)
conn.request(method: :get, path: '/content-length/100')
expect(conn.data[:proxy]).to be_nil
end
end
end
|