Snippet #4: LineRate and GeoIP Node.js module

GeoIP allows you to find the geological location of a host based on its IP address. The information typically includes the country, city, latitude, and longitude. The types of data you get and their granularity and accuracy depend on database you use. This article demonstrates how we can utilize the geoip-lite Node.js module on LineRate. In this example, a LineRate VirtualServer injects two headers containing GeoIP information to the client requests in order to provide the information to the back-end HTTP servers for use in their applications.

To install the geoip-lite Node.js module, execute the following LineRate command.

scripting npm install geoip-lite

If you have received an error message, please refer to the section on LRS-26399 in LineRate Release Notes, Version 2.5.0.

Here is the script. Make sure to change the virtual server ID in the 2nd argument of vsm.on (line #18).

'use strict';
var geoip = require('geoip-lite');
var vsm = require('lrs/virtualServerModule');

var proc = function(servReq, servResp, cliReq) {
  var g = geoip.lookup(servReq.connection.remoteAddress);
  if (g) {
    if (g['ll']) {
      servReq.addHeader('Geo-Position', g['ll'].join(';'));
    }
    if (g['country']) {
      servReq.addHeader('Geo-Country', g['country']);
    }
  }
  cliReq();
}

vsm.on('exist', 'vs1', function(vso) {
  vso.on('request', proc);
}; 
Updated Jun 06, 2023
Version 2.0

Was this article helpful?

No CommentsBe the first to comment