|
| 1 | +# Copyright (c) 2008 Jamis Buck |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the 'Software'), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in all |
| 11 | +# copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +# SOFTWARE. |
| 20 | + |
| 21 | +require 'socket' |
| 22 | +require 'resolv' |
| 23 | +require 'ipaddr' |
| 24 | + |
| 25 | +# |
| 26 | +# This implementation was borrowed from Net::SSH::Proxy |
| 27 | +# |
| 28 | +class HTTPClient |
| 29 | + # An Standard SOCKS error. |
| 30 | + class SOCKSError < SocketError; end |
| 31 | + |
| 32 | + # Used for reporting proxy connection errors. |
| 33 | + class SOCKSConnectError < SOCKSError; end |
| 34 | + |
| 35 | + # Used when the server doesn't recognize the user's credentials. |
| 36 | + class SOCKSUnauthorizedError < SOCKSError; end |
| 37 | + |
| 38 | + # An implementation of a SOCKS4 proxy. |
| 39 | + class SOCKS4Socket |
| 40 | + # The SOCKS protocol version used by this class |
| 41 | + VERSION = 4 |
| 42 | + |
| 43 | + # The packet type for connection requests |
| 44 | + CONNECT = 1 |
| 45 | + |
| 46 | + # The status code for a successful connection |
| 47 | + GRANTED = 90 |
| 48 | + |
| 49 | + # The proxy's host name or IP address, as given to the constructor. |
| 50 | + attr_reader :proxy_host |
| 51 | + |
| 52 | + # The proxy's port number. |
| 53 | + attr_reader :proxy_port |
| 54 | + |
| 55 | + # The additional options that were given to the proxy's constructor. |
| 56 | + attr_reader :options |
| 57 | + |
| 58 | + # Create a new proxy connection to the given proxy host and port. |
| 59 | + # Optionally, a :user key may be given to identify the username |
| 60 | + # with which to authenticate. |
| 61 | + def initialize(proxy_host, proxy_port = 1080, options = {}) |
| 62 | + @proxy_host = proxy_host |
| 63 | + @proxy_port = proxy_port |
| 64 | + @options = options |
| 65 | + end |
| 66 | + |
| 67 | + # Return a new socket connected to the given host and port via the |
| 68 | + # proxy that was requested when the socket factory was instantiated. |
| 69 | + def open(host, port, connection_options) |
| 70 | + socket = Socket.tcp(proxy_host, proxy_port, nil, nil, |
| 71 | + connect_timeout: connection_options[:timeout]) |
| 72 | + ip_addr = IPAddr.new(Resolv.getaddress(host)) |
| 73 | + |
| 74 | + packet = [ |
| 75 | + VERSION, CONNECT, port.to_i, |
| 76 | + ip_addr.to_i, options[:user] |
| 77 | + ].pack('CCnNZ*') |
| 78 | + socket.send packet, 0 |
| 79 | + |
| 80 | + _version, status, _port, _ip = socket.recv(8).unpack('CCnN') |
| 81 | + if status != GRANTED |
| 82 | + socket.close |
| 83 | + raise SOCKSConnectError, "error connecting to socks proxy (#{status})" |
| 84 | + end |
| 85 | + |
| 86 | + socket |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + # An implementation of a SOCKS5 proxy. |
| 91 | + class SOCKS5Socket |
| 92 | + # The SOCKS protocol version used by this class |
| 93 | + VERSION = 5 |
| 94 | + |
| 95 | + # The SOCKS authentication type for requests without authentication |
| 96 | + METHOD_NO_AUTH = 0 |
| 97 | + |
| 98 | + # The SOCKS authentication type for requests via username/password |
| 99 | + METHOD_PASSWD = 2 |
| 100 | + |
| 101 | + # The SOCKS authentication type for when there are no supported |
| 102 | + # authentication methods. |
| 103 | + METHOD_NONE = 0xFF |
| 104 | + |
| 105 | + # The SOCKS packet type for requesting a proxy connection. |
| 106 | + CMD_CONNECT = 1 |
| 107 | + |
| 108 | + # The SOCKS address type for connections via IP address. |
| 109 | + ATYP_IPV4 = 1 |
| 110 | + |
| 111 | + # The SOCKS address type for connections via domain name. |
| 112 | + ATYP_DOMAIN = 3 |
| 113 | + |
| 114 | + # The SOCKS response code for a successful operation. |
| 115 | + SUCCESS = 0 |
| 116 | + |
| 117 | + # The proxy's host name or IP address |
| 118 | + attr_reader :proxy_host |
| 119 | + |
| 120 | + # The proxy's port number |
| 121 | + attr_reader :proxy_port |
| 122 | + |
| 123 | + # The map of options given at initialization |
| 124 | + attr_reader :options |
| 125 | + |
| 126 | + # Create a new proxy connection to the given proxy host and port. |
| 127 | + # Optionally, :user and :password options may be given to |
| 128 | + # identify the username and password with which to authenticate. |
| 129 | + def initialize(proxy_host, proxy_port = 1080, options = {}) |
| 130 | + @proxy_host = proxy_host |
| 131 | + @proxy_port = proxy_port |
| 132 | + @options = options |
| 133 | + end |
| 134 | + |
| 135 | + # Return a new socket connected to the given host and port via the |
| 136 | + # proxy that was requested when the socket factory was instantiated. |
| 137 | + def open(host, port, connection_options) |
| 138 | + socket = Socket.tcp(proxy_host, proxy_port, nil, nil, |
| 139 | + connect_timeout: connection_options[:timeout]) |
| 140 | + |
| 141 | + methods = [METHOD_NO_AUTH] |
| 142 | + methods << METHOD_PASSWD if options[:user] |
| 143 | + |
| 144 | + packet = [VERSION, methods.size, *methods].pack('C*') |
| 145 | + socket.send packet, 0 |
| 146 | + |
| 147 | + version, method = socket.recv(2).unpack('CC') |
| 148 | + if version != VERSION |
| 149 | + socket.close |
| 150 | + raise SOCKSError, "invalid SOCKS version (#{version})" |
| 151 | + end |
| 152 | + |
| 153 | + if method == METHOD_NONE |
| 154 | + socket.close |
| 155 | + raise SOCKSError, 'no supported authorization methods' |
| 156 | + end |
| 157 | + |
| 158 | + negotiate_password(socket) if method == METHOD_PASSWD |
| 159 | + |
| 160 | + packet = [VERSION, CMD_CONNECT, 0].pack('C*') |
| 161 | + |
| 162 | + if host =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/ |
| 163 | + packet << [ATYP_IPV4, $1.to_i, $2.to_i, $3.to_i, $4.to_i].pack('C*') |
| 164 | + else |
| 165 | + packet << [ATYP_DOMAIN, host.length, host].pack('CCA*') |
| 166 | + end |
| 167 | + |
| 168 | + packet << [port].pack('n') |
| 169 | + socket.send packet, 0 |
| 170 | + |
| 171 | + _version, reply, = socket.recv(2).unpack('C*') |
| 172 | + socket.recv(1) |
| 173 | + address_type = socket.recv(1).getbyte(0) |
| 174 | + case address_type |
| 175 | + when 1 |
| 176 | + socket.recv(4) # get four bytes for IPv4 address |
| 177 | + when 3 |
| 178 | + len = socket.recv(1).getbyte(0) |
| 179 | + _hostname = socket.recv(len) |
| 180 | + when 4 |
| 181 | + _ipv6addr _hostname = socket.recv(16) |
| 182 | + else |
| 183 | + socket.close |
| 184 | + raise SOCKSConnectError, 'Illegal response type' |
| 185 | + end |
| 186 | + _portnum = socket.recv(2) |
| 187 | + |
| 188 | + unless reply == SUCCESS |
| 189 | + socket.close |
| 190 | + raise SOCKSConnectError, reply.to_s |
| 191 | + end |
| 192 | + |
| 193 | + socket |
| 194 | + end |
| 195 | + |
| 196 | + private |
| 197 | + |
| 198 | + # Simple username/password negotiation with the SOCKS5 server. |
| 199 | + def negotiate_password(socket) |
| 200 | + packet = [ |
| 201 | + 0x01, options[:user].length, options[:user], |
| 202 | + options[:password].length, options[:password] |
| 203 | + ].pack('CCA*CA*') |
| 204 | + socket.send packet, 0 |
| 205 | + |
| 206 | + _version, status = socket.recv(2).unpack('CC') |
| 207 | + |
| 208 | + return if status == SUCCESS |
| 209 | + socket.close |
| 210 | + raise SOCKSUnauthorizedError, 'could not authorize user' |
| 211 | + end |
| 212 | + end |
| 213 | +end |
0 commit comments