Class: Watobo::HTTPSocket::ClientSocket

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

Returns a new instance of ClientSocket.



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/watobo/sockets/client_socket.rb', line 110

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

#addressObject

Returns the value of attribute address.



6
7
8
# File 'lib/watobo/sockets/client_socket.rb', line 6

def address
  @address
end

#hostObject

Returns the value of attribute host.



7
8
9
# File 'lib/watobo/sockets/client_socket.rb', line 7

def host
  @host
end

#portObject

Returns the value of attribute port.



5
6
7
# File 'lib/watobo/sockets/client_socket.rb', line 5

def port
  @port
end

#siteObject

Returns the value of attribute site.



8
9
10
# File 'lib/watobo/sockets/client_socket.rb', line 8

def site
  @site
end

#sslObject

Returns the value of attribute ssl.



9
10
11
# File 'lib/watobo/sockets/client_socket.rb', line 9

def ssl
  @ssl
end

Class Method Details

.connect(socket) ⇒ Object



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
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
# File 'lib/watobo/sockets/client_socket.rb', line 124

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
  #puts cport
  #puts caddr

  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.nil? or 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
    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

  return nil if request.empty?

  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: #{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
      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

  #puts "CLIENT REQUEST:"
  #puts request

  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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/watobo/sockets/client_socket.rb', line 20

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



16
17
18
# File 'lib/watobo/sockets/client_socket.rb', line 16

def flush
  @socket.flush
end

#read_headerObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/watobo/sockets/client_socket.rb', line 41

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
102
103
104
# File 'lib/watobo/sockets/client_socket.rb', line 59

def request
  @persistent = false
  begin
    unless @initial_request.nil?
      request = @initial_request.copy

      #puts "\n>> Request RAW:"
      #puts request
      #puts "\n>> Request RAW (HEX):"
      #puts request.join.unpack("H*")[0]

      @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

      puts "* CLEN = #{clen} - read body (#{body.length})"
      request << body
    end
  rescue => bang
    puts bang
  end

  puts "\n>> Request RAW:"
  puts request
  puts "\n>> Request RAW (HEX):"
#  puts request.unpack("H*")[0]

  clean_request request

  request
end

#send_response(response) ⇒ Object



106
107
108
# File 'lib/watobo/sockets/client_socket.rb', line 106

def send_response(response)

end

#ssl?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/watobo/sockets/client_socket.rb', line 55

def ssl?
  @ssl == true
end

#write(data) ⇒ Object



11
12
13
14
# File 'lib/watobo/sockets/client_socket.rb', line 11

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