Class: Net::SSH::Proxy::SOCKS4

Inherits:
Object
  • Object
show all
Defined in:
lib/net/ssh/proxy/socks4.rb

Overview

An implementation of a SOCKS4 proxy. To use it, instantiate it, then pass the instantiated object via the :proxy key to Net::SSH.start:

require 'net/ssh/proxy/socks4'

proxy = Net::SSH::Proxy::SOCKS4.new('proxy.host', proxy_port, :user => 'user')
Net::SSH.start('host', 'user', :proxy => proxy) do |ssh|
  ...
end

Constant Summary collapse

VERSION =

The SOCKS protocol version used by this class

4
CONNECT =

The packet type for connection requests

1
GRANTED =

The status code for a successful connection

90

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy_host, proxy_port = 1080, options = {}) ⇒ SOCKS4

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.



40
41
42
43
44
# File 'lib/net/ssh/proxy/socks4.rb', line 40

def initialize(proxy_host, proxy_port = 1080, options = {})
  @proxy_host = proxy_host
  @proxy_port = proxy_port
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

The additional options that were given to the proxy’s constructor.



35
36
37
# File 'lib/net/ssh/proxy/socks4.rb', line 35

def options
  @options
end

#proxy_hostObject (readonly)

The proxy’s host name or IP address, as given to the constructor.



29
30
31
# File 'lib/net/ssh/proxy/socks4.rb', line 29

def proxy_host
  @proxy_host
end

#proxy_portObject (readonly)

The proxy’s port number.



32
33
34
# File 'lib/net/ssh/proxy/socks4.rb', line 32

def proxy_port
  @proxy_port
end

Instance Method Details

#open(host, port, connection_options) ⇒ Object

Return a new socket connected to the given host and port via the proxy that was requested when the socket factory was instantiated.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/net/ssh/proxy/socks4.rb', line 48

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 ConnectError, "error connecting to proxy (#{status})"
  end

  return socket
end