Class: Watobo::HTTPSocket::ClientSocket

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket, req = nil) ⇒ ClientSocket

Returns a new instance of ClientSocket.



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/watobo/http_socket/client_socket.rb', line 103

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.



27
28
29
# File 'lib/watobo/http_socket/client_socket.rb', line 27

def address
  @address
end

#hostObject

Returns the value of attribute host.



28
29
30
# File 'lib/watobo/http_socket/client_socket.rb', line 28

def host
  @host
end

#portObject

Returns the value of attribute port.



26
27
28
# File 'lib/watobo/http_socket/client_socket.rb', line 26

def port
  @port
end

#siteObject

Returns the value of attribute site.



29
30
31
# File 'lib/watobo/http_socket/client_socket.rb', line 29

def site
  @site
end

#sslObject

Returns the value of attribute ssl.



30
31
32
# File 'lib/watobo/http_socket/client_socket.rb', line 30

def ssl
  @ssl
end

Class Method Details

.connect(socket) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
# File 'lib/watobo/http_socket/client_socket.rb', line 116

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

  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
  
 # puts "#{self} reading header"
  Watobo::HTTPSocket.read_header(session) do |line|         
    request << line
  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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/watobo/http_socket/client_socket.rb', line 37

def close
  begin
  #if socket.class.to_s =~ /SSLSocket/
    if @socket.respond_to? :sysclose
    #socket.io.shutdown(2)
    @socket.sysclose
    elsif @socket.respond_to? :shutdown
    @socket.shutdown(2)
    elsif @socket.respond_to? :close
    @socket.close
    end
    return true
  rescue => bang
    puts bang
    puts bang.backtrace if $DEBUG
  end
  false
end

#read_headerObject



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/watobo/http_socket/client_socket.rb', line 56

def read_header
  request = []
  Watobo::HTTPSocket.read_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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/watobo/http_socket/client_socket.rb', line 74

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

  request = read_header
  puts request
  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

  request
end

#ssl?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/watobo/http_socket/client_socket.rb', line 70

def ssl?
  @ssl == true
end

#write(data) ⇒ Object



33
34
35
# File 'lib/watobo/http_socket/client_socket.rb', line 33

def write(data)
  @socket.write data
end