Class: PhantomJSProxy::PhantomJSServer

Inherits:
Object
  • Object
show all
Defined in:
lib/phantom_proxy/phantomjsserver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePhantomJSServer



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/phantom_proxy/phantomjsserver.rb', line 21

def initialize
  @control_panel = PhantomJSProxy::PhantomJSControlPanel.new
  
  #load key
  @hmac_activated = false
    @hmac = nil
  if File.directory?("/tmp/phantom_proxy")
      if File.exists?("/tmp/phantom_proxy/key")
        key = File.open("/tmp/phantom_proxy/key", "r").read
        #puts "HMAC_KEY: #{key}"
        @hmac_activated = true
        @hmac = HMAC::MD5.new key
      end
    end
end

Instance Attribute Details

#control_panelObject

Returns the value of attribute control_panel.



37
38
39
# File 'lib/phantom_proxy/phantomjsserver.rb', line 37

def control_panel
  @control_panel
end

#hmacObject

Returns the value of attribute hmac.



38
39
40
# File 'lib/phantom_proxy/phantomjsserver.rb', line 38

def hmac
  @hmac
end

#hmac_activatedObject

Returns the value of attribute hmac_activated.



39
40
41
# File 'lib/phantom_proxy/phantomjsserver.rb', line 39

def hmac_activated
  @hmac_activated
end

Instance Method Details

#call(env) ⇒ Object



114
115
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
# File 'lib/phantom_proxy/phantomjsserver.rb', line 114

def call(env)
  control_panel.add_request
  
  req = Rack::Request.new(env)
  
  request_parameters = env.collect { |k, v| "\t#{k} : #{v}\n" }.join
  env['rack.errors'].write("The request: "+req.url()+"\nGET: "+request_parameters+"\n")
  
  if hmac_activated && hmac && !check_request_security(req, env)
      resp = Rack::Response.new([], 503,  {
                                              'Content-Type' => 'text/html'
                                          }) { |r|
        r.write("Security ERROR")
      }
      return resp.finish
    end
  
  https_request = false
  if /\:443/.match(req.url())
   https_request = true
  end
  
  params = req.params.collect { |k, v| "#{k}=#{v}&" }.join
  env['rack.errors'].write("Paramas: "+params+"\n")
    
    #this routes the request to the outgoing server incase its not html that we want to load
    type = check_for_route(req.url)#env['REQUEST_URI'])
    if type == "control_panel"
      return control_panel.show()
    elsif type != "none" and type != "base64"
      control_panel.add_special_request "@forward_requests"
      return route(env, type)
    else        
      #Fetch the Webpage with PhantomJS
      phJS = PhantomJS.new
      
      env['rack.errors'].write("Extract the uri\n")

      loadOptions = Options.new(env)

      puts "Options: #{loadOptions.picture?}, #{loadOptions.loadFrames?}"
      
      url = prepareUrl(env, params, req, https_request, type)
      
      phJS.getUrl(url, loadOptions.picture?, loadOptions.loadFrames?)

      #Create the response
      if loadOptions.picture?
        control_panel.add_special_request "@image_requests"
        resp = Rack::Response.new([], 200,  {
                                                'Content-Type' => 'image/png'
                                            }) { |r|
          r.write(phJS.image)
        }
        resp.finish
      elsif phJS.ready != 200
        if !/favicon\.ico/.match(req.url())
          env['rack.errors'].write("Request FAILED\n")
          control_panel.add_special_request "@failed_requests"
        else
          control_panel.add_special_request "@favicon_requests"
        end
        resp = Rack::Response.new([], phJS.ready > 0 ? phJS.ready : 404 ,  {
                                                'Content-Type' => 'text/html'
                                            }) { |r|
          r.write(phJS.dom)
        }
        resp.finish
      else
        control_panel.add_special_request "@html_requests"
        resp = Rack::Response.new([], 200,  {
                                                'Content-Type' => 'text/html'
                                            }) { |r|
          r.write(phJS.dom)
        }
        resp.finish
      end
    end
end

#check_for_route(url) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/phantom_proxy/phantomjsserver.rb', line 41

def check_for_route(url)
  if /\.js/i.match(url) and !/\.jsp/i.match(url)
    return 'text/html'
  end
  if /\.css/i.match(url)
    return 'text/css'
  end
  if /\.png/i.match(url) or /\.jpg/i.match(url) or /\.jpeg/i.match(url) or /\.gif/i.match(url)
    return 'image/*'
  end
  if /phantom_proxy_control_panel/.match(url)
    return 'control_panel'
  end
  if /phantomProxy\.get/.match(url)
    return "base64"
  end
  "none"
end

#check_request_security(req, env) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/phantom_proxy/phantomjsserver.rb', line 78

def check_request_security req, env
  if !env['HTTP_HMAC_KEY'] || !env['HTTP_HMAC_TIME']
    return false
  end
  
  client_key = env['HTTP_HMAC_KEY']
  client_time= Time.parse(env['HTTP_HMAC_TIME'])
  remote_time= Time.now
  remote_key = hmac.update(env['REQUEST_URI']+env['HTTP_HMAC_TIME']).hexdigest

  if (client_key != remote_key || (remote_time-client_time).abs > 120)
    control_panel.add_special_request "@did not pass security check"
    return false
  end
  return true
end

#prepareUrl(env, params, req, https_request, type) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/phantom_proxy/phantomjsserver.rb', line 95

def prepareUrl(env, params, req, https_request, type)
  if type == "none"
    url = req.url#env['REQUEST_URI']
      puts "URL is: #{url}"
      if https_request
        url['http'] = 'https' if url['http']
        url[':443'] = '' if url[':443']
      end
      
      if params.length > 0
        url += '?'+params;
      end
      return url
  end
  url = Base64.decode64(req.params["address"])
  env['rack.errors'].write("After Base64 decoding: "+url+"\n")
  return url
end

#route(env, type) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/phantom_proxy/phantomjsserver.rb', line 60

def route(env, type)
  _req = Net::HTTP::Get.new(env['REQUEST_URI'])
  
  _req['User-Agent'] = env['HTTP_USER_AGENT']
  
  _res = Net::HTTP.start(env['HTTP_HOST'], env['SERVER_PORT']) {|http|
    #http.request(_req)
    http.get(env['REQUEST_URI'])
  }
  
  #env['rack.errors'].write("Response is:"+_res.body+"\n")
  
  resp = Rack::Response.new([], 200,  {'Content-Type' => type}) { |r|
    r.write(_res.body)
  }
  resp.finish
end