Module: Watobo::HTTPSocket

Defined in:
lib/watobo/sockets/agent.rb,
lib/watobo/sockets/ntlm_auth.rb,
lib/watobo/sockets/connection.rb,
lib/watobo/sockets/http_socket.rb,
lib/watobo/sockets/client_socket.rb

Overview

:nodoc: all

Defined Under Namespace

Modules: NTLMAuth Classes: Agent_UNUSED, ClientSocket, ClientSocket_ORIG, Connection_UNUSED

Class Method Summary collapse

Class Method Details

.close(socket) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/watobo/sockets/http_socket.rb', line 13

def self.close(socket)
  #  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
      #puts "SHUTDOWN"
      socket.shutdown(Socket::SHUT_RDWR)
    end
    # finally close it
    if socket.respond_to? :close
    socket.close
    end
    return true
  rescue => bang
    puts bang
    puts bang.backtrace if $DEBUG
  end
  false
# end
end

.get_peer_subject(socket) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/watobo/sockets/http_socket.rb', line 148

def self.get_peer_subject(socket)
  begin
    ctx = OpenSSL::SSL::SSLContext.new()
    ctx.tmp_dh_callback = proc { |*args|
      OpenSSL::PKey::DH.new(128)
    }
    ssl_sock = OpenSSL::SSL::SSLSocket.new(socket, ctx)
    subject = ssl_sock.peer_cert.subject
    return subject
  rescue => bang
    puts bang
    puts bang.backtrace
  end
  return nil
end

.get_ssl_cert_cn(host, port) ⇒ Object



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

def self.get_ssl_cert_cn( host, port)
  cn = ""
  begin
    tcp_socket = TCPSocket.new( host, port )
    tcp_socket.setsockopt( Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1)
    tcp_socket.sync = true
    ctx = OpenSSL::SSL::SSLContext.new()

    ctx.tmp_dh_callback = proc { |*args|
      OpenSSL::PKey::DH.new(128)
    }

    socket = OpenSSL::SSL::SSLSocket.new(tcp_socket, ctx)

    socket.connect
    cert = socket.peer_cert

    if cert.subject.to_s =~ /cn=([^\/]*)/i
    cn = $1
    end
    puts "Peer-Cert CN: #{cn}"
    socket.io.shutdown(2)
  rescue => bang
    puts bang
    cn = host
  ensure
    socket.close if socket.respond_to? :close
  end
  cn
end

.read_body(socket, prefs = nil) ⇒ Object



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

def self.read_body(socket, prefs=nil)
  buf = nil
  max_bytes = -1
  unless prefs.nil?
    max_bytes = prefs[:max_bytes] unless prefs[:max_bytes].nil?
  end
  bytes_to_read = max_bytes >= 0 ? max_bytes : 1024

  bytes_read = 0
  while max_bytes < 0 or bytes_to_read > 0
    begin
    #   timeout(5) do
    # puts "<#{bytes_to_read} / #{bytes_read} / #{max_bytes}"
      buf = socket.readpartial(bytes_to_read)
      bytes_read += buf.length
      #   end
    rescue EOFError
      if $DEBUG
        puts "#{buf.class} - #{buf}"
      end
      # unless buf.nil?
      #   yield buf if block_given?
      # end
      #buf = nil
      break
      #return
    rescue Timeout::Error
      puts "!!! Timeout: read_body (max_bytes=#{max_bytes})"
      #puts "* last data seen on socket:"
      # puts buf
      puts $!.backtrace if $DEBUG
      break
    rescue => bang
      print "E!"
      puts bang.backtrace if $DEBUG
    break
    end
    break if buf.nil?
    yield buf if block_given?
    break if max_bytes >= 0 and bytes_read >= max_bytes
    bytes_to_read -= bytes_read if max_bytes >= 0 && bytes_to_read >= bytes_read
  end
  return
end

.read_client_header(socket) ⇒ Object



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
328
329
330
331
332
333
334
335
336
337
# File 'lib/watobo/sockets/http_socket.rb', line 295

def self.read_client_header(socket)
  buf = ''

  while true
    begin
    #Timeout::timeout(1.5) do
      buf = socket.gets
      #end
    rescue EOFError => e
      puts "EOFError: #{e}"
      #puts "!!! EOF: reading header"
      # buf = nil
      return true
    rescue Errno::ECONNRESET => e
      puts "ECONNRESET: #{e}"
      #puts "!!! CONNECTION RESET: reading header"
      #buf = nil
      #return
      #raise
      return false
    rescue Errno::ECONNABORTED => e
      puts "ECONNABORTED: #{e}"
      #raise
      return false
    rescue Timeout::Error => e
      puts "TIMEOUT: #{e}"
      return false
    rescue => bang
    # puts "!!! READING HEADER:"
    # puts buf
      puts bang
      puts bang.backtrace
      raise
    end

    return false if buf.nil?
    
   # puts buf

    yield buf if block_given?
    return if buf.strip.empty?
  end
end

.read_header(socket) ⇒ Object



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

def self.read_header(socket)
  buf = ''

  while true
    begin
      buf = socket.gets
    rescue EOFError
      puts "!!! EOF: reading header"
      # buf = nil
      return
    rescue Errno::ECONNRESET
    #puts "!!! CONNECTION RESET: reading header"
    #buf = nil
    #return
      raise
    rescue Errno::ECONNABORTED
      raise
    rescue Timeout::Error
    #puts "!!! TIMEOUT: reading header"
    #return
      raise
    rescue => bang
    # puts "!!! READING HEADER:"
    # puts buf
      puts bang
      puts bang.backtrace
      raise
    end

    return if buf.nil?

    yield buf if block_given?
    return if buf.strip.empty?
  end
end

.readChunkedBody(socket, &block) ⇒ Object



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

def self.readChunkedBody(socket, &block)
  buf = nil
  while (chunk_size = socket.gets)
    
    if chunk_size.strip.empty?
      yield chunk_size
      next 
    end
    next unless chunk_size.strip =~/^[a-fA-F0-9]+$/
    yield "#{chunk_size.strip}\n" if block_given?
    bytes_to_read = num_bytes = chunk_size.strip.hex
    # puts "> chunk-length: 0x#{chunk_size.strip}(#{num_bytes})"
    return if num_bytes == 0
    bytes_read = 0
    while bytes_read < num_bytes
      begin
      # timeout(5) do
        bytes_to_read = num_bytes - bytes_read
        # puts bytes_to_read.to_s
        buf = socket.readpartial(bytes_to_read)
        bytes_read += buf.length
        # puts bytes_read.to_s
        # end
      rescue EOFError
      # yield buf if buf
        return
      rescue Timeout::Error
        puts "!!! Timeout: readChunkedBody (bytes_to_read=#{bytes_to_read}"
        #puts "* last data seen on socket:"
        # puts buf
        return
      rescue => bang
      # puts "!!! Error (???) reading body:"
      # puts bang
      # puts bang.class
      # puts bang.backtrace.join("\n")
      # puts "* last data seen on socket:"
      # puts buf
        print "E!"
      return
      end
      # puts bytes_read.to_s
      yield buf if block_given?
    #return if max_bytes > 0 and bytes_read >= max_bytes
    end
    yield "\r\n" if block_given?
  end
#  end
end

.siteAlive?(chat) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/watobo/sockets/http_socket.rb', line 37

def self.siteAlive?(chat)
  #puts chat.class
  site = nil
  host = nil
  port = nil

  site = chat.request.site

  #return @sites_online[site] if @sites_online.has_key?(site)

  proxy = Watobo::ForwardingProxy.get site

  unless proxy.nil?
    Watobo.print_debug("Using Proxy","#{proxy.to_yaml}") if $DEBUG

    puts "* testing proxy connection:"
    puts "#{proxy.name} (#{proxy.host}:#{proxy.port})"

  host = proxy.host
  port = proxy.port

  else
    print "* check if site is alive (#{site}) ... "
  host = chat.request.host
  port = chat.request.port

  end

  return false if host.nil? or port.nil?

  begin
    tcp_socket = nil
    #  timeout(6) do

    tcp_socket = TCPSocket.new( host, port)
    tcp_socket.setsockopt( Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1)
    tcp_socket.sync = true

    socket = tcp_socket

    if socket.class.to_s =~ /SSLSocket/
    socket.io.shutdown(2)
    else
    socket.shutdown(2)
    end
    socket.close
    print "[OK]\n"

    return true
  rescue Errno::ECONNREFUSED
    p "* connection refused (#{host}:#{port})"
  rescue Errno::ECONNRESET
    puts "* connection reset"
  rescue Errno::EHOSTUNREACH
    p "* host unreachable (#{host}:#{port})"

  rescue Timeout::Error
    p "* TimeOut (#{host}:#{port})\n"

  rescue Errno::ETIMEDOUT
    p "* TimeOut (#{host}:#{port})"

  rescue Errno::ENOTCONN
    puts "!!!ENOTCONN"
  rescue OpenSSL::SSL::SSLError
    p "* ssl error"
    socket = nil
    #  puts "!!! SSL-Error"
    print "E"
  rescue => bang
  #  puts host
  #  puts port
    puts bang
    puts bang.backtrace if $DEBUG
  end
  print "[FALSE]\n"

  return false
end