Class: VOODOO::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/voodoo/collector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port = 0, close_browser: nil) ⇒ Collector

Returns a new instance of Collector.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/voodoo/collector.rb', line 12

def initialize(port = 0, close_browser: nil)
    @chunks = []
    @close_browser = close_browser
    if port == 0
        tmp_server = TCPServer.open('127.0.0.1', 0)
        @port = tmp_server.addr[1]
        tmp_server.close
    else
        @port = port
    end
    @token = SecureRandom.uuid
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



8
9
10
# File 'lib/voodoo/collector.rb', line 8

def port
  @port
end

#threadObject (readonly)

Returns the value of attribute thread.



9
10
11
# File 'lib/voodoo/collector.rb', line 9

def thread
  @thread
end

#tokenObject (readonly)

Returns the value of attribute token.



10
11
12
# File 'lib/voodoo/collector.rb', line 10

def token
  @token
end

Instance Method Details

#on_jsonObject



29
30
31
32
33
34
35
36
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
# File 'lib/voodoo/collector.rb', line 29

def on_json
   @thread = Thread.new do
        server = TCPServer.new('127.0.0.1', @port)

        loop {
            begin
                socket = server.accept
                headers = {}
                method, path = socket.gets.split

                unless path.include? @token
                    socket.puts("HTTP/1.1 400 OK\r\n\r\n")
                    socket.close
                    next
                end
                
                while line = socket.gets.split(" ", 2)
                    break if line[0] == ""
                    headers[line[0].chop] = line[1].strip
                end

                post_body = socket.read(headers["Content-Length"].to_i)
                socket.puts("HTTP/1.1 204 OK\r\n\r\n")
                socket.close
                
                jsonData = JSON.parse(post_body, {:symbolize_names => true})

                if jsonData[:log]
                    puts jsonData[:log]
                end

                if jsonData[:chunk]
                    @chunks << jsonData[:payload][1]
                    if jsonData[:payload][0] == @chunks.length
                        payload = {
                            payload: @chunks.join('')
                        }
                        @chunks = []
                        yield payload
                    end
                    return
                end

                if jsonData[:kill] == true
                    if jsonData[:close_browser] && @close_browser != nil
                        @close_browser.call()
                    end
                    self.thread.kill
                    return
                end
                
                yield jsonData
            rescue
            end
        }
    end
end

#urlObject



25
26
27
# File 'lib/voodoo/collector.rb', line 25

def url
    return "http://localhost:#{@port}/?token=#{@token}"
end