socksify-a 1.1.3 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/socksify.rb +20 -12
  2. metadata +2 -2
data/lib/socksify.rb CHANGED
@@ -16,6 +16,7 @@
16
16
  =end
17
17
 
18
18
  require 'socket'
19
+ require 'resolv'
19
20
  require 'socksify_debug'
20
21
 
21
22
  class SOCKSError < RuntimeError
@@ -90,13 +91,13 @@ class SOCKSError < RuntimeError
90
91
  end
91
92
 
92
93
  class TCPSocket
93
- @@socks_version ||= 5
94
+ @@socks_version ||= "5"
94
95
 
95
96
  def self.socks_version
96
- @@socks_version == 4 ? "\004" : "\005"
97
+ (@@socks_version == "4a" or @@socks_version == "4") ? "\004" : "\005"
97
98
  end
98
99
  def self.socks_version=(version)
99
- @@socks_version = version
100
+ @@socks_version = version.to_s
100
101
  end
101
102
  def self.socks_server
102
103
  @@socks_server ||= nil
@@ -129,7 +130,7 @@ class TCPSocket
129
130
  Socksify::debug_notice "Connecting to SOCKS server #{socks_server}:#{socks_port}"
130
131
  initialize_tcp socks_server, socks_port
131
132
 
132
- socks_authenticate unless @@socks_version == 4
133
+ socks_authenticate unless @@socks_version =~ /^4/
133
134
 
134
135
  if host
135
136
  socks_connect(host, port)
@@ -159,12 +160,17 @@ class TCPSocket
159
160
  def socks_connect(host, port)
160
161
  Socksify::debug_debug "Sending destination address"
161
162
  write TCPSocket.socks_version
163
+ Socksify::debug_debug TCPSocket.socks_version.unpack "H*"
162
164
  write "\001"
163
- write "\000" if @@socks_version == 5
164
- write [port].pack('n') if @@socks_version == 4
165
+ write "\000" if @@socks_version == "5"
166
+ write [port].pack('n') if @@socks_version =~ /^4/
165
167
 
168
+ if @@socks_version == "4"
169
+ host = Resolv::DNS.new.getaddress(host).to_s
170
+ end
171
+ Socksify::debug_debug host
166
172
  if host =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/ # to IPv4 address
167
- write "\001" if @@socks_version == 5
173
+ write "\001" if @@socks_version == "5"
168
174
  _ip = [$1.to_i,
169
175
  $2.to_i,
170
176
  $3.to_i,
@@ -175,16 +181,17 @@ class TCPSocket
175
181
  raise "TCP/IPv6 over SOCKS is not yet supported (inet_pton missing in Ruby & not supported by Tor"
176
182
  write "\004"
177
183
  else # to hostname
178
- if @@socks_version == 5
184
+ if @@socks_version == "5"
179
185
  write "\003" + [host.size].pack('C') + host
180
186
  else
181
187
  write "\000\000\000\001"
182
- write "\001\000"
188
+ write "\007\000"
189
+ Socksify::debug_notice host
183
190
  write host
184
191
  write "\000"
185
192
  end
186
193
  end
187
- write [port].pack('n') if @@socks_version == 5
194
+ write [port].pack('n') if @@socks_version == "5"
188
195
 
189
196
  socks_receive_reply
190
197
  Socksify::debug_notice "Connected to #{host}:#{port} over SOCKS"
@@ -193,10 +200,11 @@ class TCPSocket
193
200
  # returns [bind_addr: String, bind_port: Fixnum]
194
201
  def socks_receive_reply
195
202
  Socksify::debug_debug "Waiting for SOCKS reply"
196
- if @@socks_version == 5
203
+ if @@socks_version == "5"
197
204
  connect_reply = recv(4)
205
+ Socksify::debug_debug connect_reply.unpack "H*"
198
206
  if connect_reply[0..0] != "\005"
199
- raise SOCKSError.new("SOCKS version #{connect_reply[0..0]} is not 4 or 5")
207
+ raise SOCKSError.new("SOCKS version #{connect_reply[0..0]} is not 5")
200
208
  end
201
209
  if connect_reply[1..1] != "\000"
202
210
  raise SOCKSError.for_response_code(connect_reply.bytes.to_a[1])
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 1
8
- - 3
9
- version: 1.1.3
8
+ - 4
9
+ version: 1.1.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Andrey Kouznetsov