Class: Watobo::HTTPSocket::ClientSocket
- Inherits:
-
Object
- Object
- Watobo::HTTPSocket::ClientSocket
- Defined in:
- lib/watobo/sockets/client_socket.rb
Instance Attribute Summary collapse
-
#address ⇒ Object
Returns the value of attribute address.
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#site ⇒ Object
Returns the value of attribute site.
-
#ssl ⇒ Object
Returns the value of attribute ssl.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #flush ⇒ Object
-
#initialize(socket, req = nil) ⇒ ClientSocket
constructor
A new instance of ClientSocket.
- #read_header ⇒ Object
- #request ⇒ Object
- #send_response(response) ⇒ Object
- #ssl? ⇒ Boolean
- #write(data) ⇒ Object
Constructor Details
#initialize(socket, req = nil) ⇒ ClientSocket
Returns a new instance of ClientSocket.
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/watobo/sockets/client_socket.rb', line 117 def initialize(socket, req=nil) @socket = socket @port = nil @address = nil @host = nil @site = nil @ssl = false @initial_request = req @persistent = false # TODO: Fake Certs Should be global accessable end |
Instance Attribute Details
#address ⇒ Object
Returns the value of attribute address.
27 28 29 |
# File 'lib/watobo/sockets/client_socket.rb', line 27 def address @address end |
#host ⇒ Object
Returns the value of attribute host.
28 29 30 |
# File 'lib/watobo/sockets/client_socket.rb', line 28 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
26 27 28 |
# File 'lib/watobo/sockets/client_socket.rb', line 26 def port @port end |
#site ⇒ Object
Returns the value of attribute site.
29 30 31 |
# File 'lib/watobo/sockets/client_socket.rb', line 29 def site @site end |
#ssl ⇒ Object
Returns the value of attribute ssl.
30 31 32 |
# File 'lib/watobo/sockets/client_socket.rb', line 30 def ssl @ssl end |
Class Method Details
.connect(socket) ⇒ Object
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 |
# File 'lib/watobo/sockets/client_socket.rb', line 131 def self.connect(socket) request = [] @fake_certs ||= {} @dh_key ||= Watobo::CA.dh_key ra = socket.remote_address cport = ra.ip_port caddr = ra.ip_address optval = [1, 500_000].pack("I_2") # socket.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval # socket.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) # socket.setsockopt Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1 socket.sync = true session = socket if Watobo::Interceptor::Proxy.transparent? ci = Watobo::Interceptor::Transparent.info({ 'host' => caddr, 'port' => cport } ) unless ci['target'].empty? or ci['cn'].empty? puts "SSL-REQUEST FROM #{caddr}:#{cport}" ctx = Watobo::CertStore.acquire_ssl_ctx ci['target'], ci['cn'] begin ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, ctx) #ssl_socket.setsockopt( Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1) # ssl_socket.sync_close = true ssl_socket.sync = true # puts ssl_socket.methods.sort session = ssl_socket.accept rescue OpenSSL::SSL::SSLError => e puts ">> SSLError" puts e return nil, session rescue => bang puts bang puts bang.backtrace return nil, session end else puts ci['host'] puts ci['cn'] end end begin Watobo::HTTPSocket.read_header(session) do |line| request << line end rescue => bang puts bang puts bang.backtrace if $DEBUG return nil end if Watobo::Interceptor::Proxy.transparent? #puts "> get hostname ..." thn = nil request.each do |l| if l =~ /^Host: (.*)/ thn = $1.strip # puts ">> #{thn}" end end # puts session.class # puts "* fix request line ..." # puts request.first # puts ">>" if session.is_a? OpenSSL::SSL::SSLSocket request.first.gsub!(/(^[^[:space:]]{1,}) (.*) (HTTP.*)/i,"\\1 https://#{thn}\\2 \\3") unless request.first =~ /^[^[:space:]]{1,} http/ else request.first.gsub!(/(^[^[:space:]]{1,}) (.*) (HTTP.*)/i,"\\1 http://#{thn}\\2 \\3") unless request.first =~ /^[^[:space:]]{1,} http/ end #puts request.first end if request.first =~ /^CONNECT (.*):(\d{1,5}) HTTP\/1\./ then target = $1 tport = $2 # puts request.first #print "\n* CONNECT: #{method} #{target} on port #{tport}\n" site = "#{target}:#{tport}" #puts "CONNECT #{site}" socket.print "HTTP/1.0 200 Connection established\r\n" + #"Proxy-connection: Keep-alive\r\n" + "Proxy-agent: WATOBO-Proxy/1.1\r\n" + "\r\n" bscount = 0 # bad handshake counter # puts "* wait for ssl handshake ..." begin # site = "#{target}:#{tport}" unless @fake_certs.has_key? site puts "CREATE NEW CERTIFICATE FOR >> #{site} <<" cn = Watobo::HTTPSocket.get_ssl_cert_cn(target, tport) puts "CN=#{cn}" cert = { :hostname => cn, :type => 'server', :user => 'watobo', :email => 'root@localhost', } cert_file, key_file = Watobo::CA.create_cert cert @fake_certs[site] = { :cert => OpenSSL::X509::Certificate.new(File.read(cert_file)), :key => OpenSSL::PKey::RSA.new(File.read(key_file)) } end ctx = OpenSSL::SSL::SSLContext.new() #ctx.cert = @cert ctx.cert = @fake_certs[site][:cert] # @ctx.key = OpenSSL::PKey::DSA.new(File.read(key_file)) #ctx.key = @key ctx.key = @fake_certs[site][:key] ctx.tmp_dh_callback = proc { |*args| @dh_key } ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE ctx.timeout = 10 ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, ctx) ssl_socket.setsockopt( Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1) # ssl_socket.sync_close = true ssl_socket.sync = true # puts ssl_socket.methods.sort ssl_session = ssl_socket.accept session = ssl_session request = [] Watobo::HTTPSocket.read_header(session) do |line| request << line end return nil if request.empty? return nil if request.first.nil? unless request.first =~ /(^[^[:space:]]{1,}) http/ request.first.gsub!(/(^[^[:space:]]{1,})( )(\/.*)/, "\\1 https://#{site}\\3") end request = Watobo::Request.new(request) rescue => bang puts bang puts bang.backtrace if $DEBUG return nil end else # puts "* create request object" request = Watobo::Request.new(request) site = request.site #puts request end begin unless request.nil? clen = request.content_length if clen > 0 then body = "" Watobo::HTTPSocket.read_body(session) do |data| body += data break if body.length == clen end request << body unless body.empty? end connection = ClientSocket.new(session, request) else connection = ClientSocket.new(session) end connection.ssl = true if session.class.to_s =~ /ssl/i # ra = session.remote_address # connection.port = ra.ip_port # connection.address = ra.ip_address # connection.site = site connection.port = cport connection.address = caddr connection.site = site rescue => bang puts bang puts bang.backtrace end connection end |
Instance Method Details
#close ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/watobo/sockets/client_socket.rb', line 40 def close begin #if socket.class.to_s =~ /SSLSocket/ if @socket.respond_to? :shutdown @socket.shutdown(Socket::SHUT_RDWR) end # finally close it if @socket.respond_to? :close @socket.close elsif @socket.respond_to? :sysclose socket.io.shutdown(Socket::SHUT_RDWR) @socket.sysclose end return true rescue => bang puts bang puts bang.backtrace if $DEBUG end false end |
#flush ⇒ Object
36 37 38 |
# File 'lib/watobo/sockets/client_socket.rb', line 36 def flush @socket.flush end |
#read_header ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/watobo/sockets/client_socket.rb', line 61 def read_header request = [] Watobo::HTTPSocket.read_client_header(@socket) do |line| request << line end return nil if request.empty? unless request.first =~ /(^[^[:space:]]{1,}) http/ request.first.gsub!(/(^[^[:space:]]{1,})( )(\/.*)/, "\\1 https://#{@site}\\3") end Watobo::Request.new(request) end |
#request ⇒ Object
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 |
# File 'lib/watobo/sockets/client_socket.rb', line 79 def request @persistent = false begin unless @initial_request.nil? request = @initial_request.copy @initial_request = nil clean_request request return request end request = read_header return nil if request.nil? @persistent = !request.connection_close? clen = request.content_length if clen > 0 then body = "" Watobo::HTTPSocket.read_body(@socket) do |data| body += data break if body.length == clen end request << body end rescue => bang puts bang end clean_request request request end |
#send_response(response) ⇒ Object
113 114 115 |
# File 'lib/watobo/sockets/client_socket.rb', line 113 def send_response(response) end |
#ssl? ⇒ Boolean
75 76 77 |
# File 'lib/watobo/sockets/client_socket.rb', line 75 def ssl? @ssl == true end |
#write(data) ⇒ Object
31 32 33 34 |
# File 'lib/watobo/sockets/client_socket.rb', line 31 def write(data) @socket.write data @socket.flush end |