Class: SocksHandler::TCP

Inherits:
Object
  • Object
show all
Extended by:
SocksHandler
Defined in:
lib/socks_handler/tcp.rb

Constant Summary

Constants included from SocksHandler

PROTOCOL_VERSION, VERSION

Class Method Summary collapse

Class Method Details

.desocksifynil

Returns:

  • (nil)


61
62
63
64
# File 'lib/socks_handler/tcp.rb', line 61

def desocksify
  @rules = []
  nil
end

.establish_connection(socket, remote_host, remote_port, username = nil, password = nil) ⇒ nil

Connects a host through a socks server

Examples:

socket = TCPSocket.new("127.0.0.1", 1080) # or Socket.tcp("127.0.0.1", 1080)
# "nginx" is an HTTP server only the socks server can access
SocksHandler::TCP.establish_connection(socket, "nginx", 80)

socket.write(<<~REQUEST.gsub("\n", "\r\n"))
  HEAD / HTTP/1.1
  Host: nginx

REQUEST
puts socket.gets #=> HTTP/1.1 200 OK

Parameters:

  • socket (Socket, TCPSocket)

    a socket that has connected to a socks server

  • remote_host (String)
  • remote_port (Integer, String)

    a port number or service name such as “http”

  • username (String, nil) (defaults to: nil)
  • password (String, nil) (defaults to: nil)

Returns:

  • (nil)


92
93
94
95
96
# File 'lib/socks_handler/tcp.rb', line 92

def establish_connection(socket, remote_host, remote_port, username = nil, password = nil)
  negotiate(socket, username, password)
  send_details(socket,  Command::CONNECT, remote_host, remote_port)
  nil
end

.find_rule(host) ⇒ DirectAccessRule, ...

Parameters:

  • host (String)

    a domain name or IP address of a remote host

Returns:



68
69
70
# File 'lib/socks_handler/tcp.rb', line 68

def find_rule(host)
  rules.find { |r| r.match?(host) }
end

.socksify(rules) ⇒ nil

Socksifies all TCP connections created by TCPSocket.new or Socket.tcp

Examples:

SocksHandler::TCP.socksify([
  # Access 127.0.0.1, ::1 or hosts that end in ".local" directly
  SocksHandler::DirectAccessRule.new(host_patterns: %w[127.0.0.1 ::1] + [/\.local\z/]),

  # Access hosts that end in ".ap-northeast-1.compute.internal" through 127.0.0.1:1080
  SocksHandler::ProxyAccessRule.new(
    host_patterns: [/\.ap-northeast-1\.compute\.internal\z/],
    socks_server: "127.0.0.1:1080",
  ),

  # Access hosts that end in ".ec2.internal" through 127.0.0.1:1081
  SocksHandler::ProxyAccessRule.new(
    host_patterns: [/\.ec2\.internal\z/],
    socks_server: "127.0.0.1:1081",
  ),

  # Access others hosts through 127.0.0.1:1082 with username/password auth
  SocksHandler::ProxyAccessRule.new(
    host_patterns: [//],
    socks_server: "127.0.0.1:1082",
    username: "user",
    password: ENV["SOCKS_SERVER_PASSWORD"],
  ),
])

Parameters:

Returns:

  • (nil)


46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/socks_handler/tcp.rb', line 46

def socksify(rules)
  @rules = rules

  unless TCPSocket.ancestors.include?(SocksHandler::TCPSocketSocksify)
    TCPSocket.prepend(SocksHandler::TCPSocketSocksify)
  end

  unless Socket.singleton_class.ancestors.include?(SocksHandler::SocketSocksify)
    Socket.singleton_class.prepend(SocksHandler::SocketSocksify)
  end

  nil
end