4

I try to use my compound.js-application as a (transparent) proxy-server. When a user tries to request a external website, the application will check, if the user with that ip-address was authenticated before.

If so, the external site will be shown, if not, the user will be encouraged to login. The problem is, that the response is not processed, when there is an access to the database-object "User". When I comment out the database section and just use the code inside the anonymous function, the programm works as expected.

action('exturl', function () {
    User.all({ where: { ipaddress: req.ip }}, function(err, users) {
      if (users.length > 0) {                                                                                              
          this.user = user[0];                                                                                            

          var proxy = http.createClient(80, req.headers['host'])
          var proxy_request = proxy.request(req.method, req.url, req.headers);
          proxy_request.addListener('response', function (proxy_response) {
              proxy_response.addListener('data', function(chunk) {
                  res.write(chunk, 'binary');
              });
              proxy_response.addListener('end', function() {
                  res.end();
              });
              res.writeHead(proxy_response.statusCode, proxy_response.headers);
          });
          req.addListener('data', function(chunk) {
              proxy_request.write(chunk, 'binary');
          });
          req.addListener('end', function() {
              proxy_request.end();
          });
      } else {                                                                                                            
          redirect(path_to.login);                                                                                        
      }                                                                                                                   
    });
});

Is there a failure inside my code? I don't know what I am doing wrong.

David Geh
  • 307
  • 1
  • 2
  • 12
  • Can you please clarify which portion of the above code you comment out to make this work? – story Jun 03 '13 at 01:56

0 Answers0