Class: Watobo::HTTPSocket::ClientSocket_ORIG

Inherits:
Object
  • Object
show all
Defined in:
lib/watobo/sockets/client_socket.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket, req = nil) ⇒ ClientSocket_ORIG

Returns a new instance of ClientSocket_ORIG.



428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/watobo/sockets/client_socket.rb', line 428

def initialize(socket, req=nil)
  @socket = socket
  @port = nil
  @address = nil
  @host = nil
  @site = nil
  @ssl = false
  @initial_request = req

# TODO: Fake Certs Should be global accessable

end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



346
347
348
# File 'lib/watobo/sockets/client_socket.rb', line 346

def address
  @address
end

#hostObject

Returns the value of attribute host.



347
348
349
# File 'lib/watobo/sockets/client_socket.rb', line 347

def host
  @host
end

#portObject

Returns the value of attribute port.



345
346
347
# File 'lib/watobo/sockets/client_socket.rb', line 345

def port
  @port
end

#siteObject

Returns the value of attribute site.



348
349
350
# File 'lib/watobo/sockets/client_socket.rb', line 348

def site
  @site
end

#sslObject

Returns the value of attribute ssl.



349
350
351
# File 'lib/watobo/sockets/client_socket.rb', line 349

def ssl
  @ssl
end

Class Method Details

.connect(socket) ⇒ Object



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# File 'lib/watobo/sockets/client_socket.rb', line 441

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
    rescue => bang
      puts bang
      puts bang.backtrace if $DEBUG

      return nil

    end
    session = ssl_session
    request = nil
  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

#closeObject



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/watobo/sockets/client_socket.rb', line 359

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

#flushObject



355
356
357
# File 'lib/watobo/sockets/client_socket.rb', line 355

def flush
  @socket.flush
end

#read_headerObject



380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/watobo/sockets/client_socket.rb', line 380

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

#requestObject



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/watobo/sockets/client_socket.rb', line 398

def request
  begin
    unless @initial_request.nil?
      request = @initial_request.copy
      @initial_request = nil
    return request
    end

    request = read_header

    return nil if request.nil?

    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

  puts request

  request
end

#ssl?Boolean

Returns:

  • (Boolean)


394
395
396
# File 'lib/watobo/sockets/client_socket.rb', line 394

def ssl?
  @ssl == true
end

#write(data) ⇒ Object



350
351
352
353
# File 'lib/watobo/sockets/client_socket.rb', line 350

def write(data)
  @socket.write data
  @socket.flush
end