This example demonstrates how to use the 51Degrees on-premise IP intelligence engine as part of a simple web server.
This example demonstrates how to use the 51Degrees on-premise IP intelligence engine as part of a simple web server. The IP address of the requesting client is used as evidence and details about the associated network, and location are displayed.
This example can be configured using the 51d.json file. The path to the data file is taken from this configuration, and can be changed by editing the file.
const path = require('path');
const require51 = (requestedPackage) => {
try {
return require(path.join(__dirname, '/../../../node_modules/', requestedPackage));
} catch (e) {
return require(requestedPackage);
}
};
const fs = require('fs');
const http = require('http');
const pug = require('pug');
const compiledFunction =
pug.compileFile(path.join(__dirname, '/index.pug'));
const ipi = require51('fiftyone.ipintelligence.onpremise');
const translation = require51('fiftyone.ipintelligence.translation');
const optionsExtension =
require51('fiftyone.ipintelligence.shared').optionsExtension;
const dataExtension =
require51('fiftyone.ipintelligence.shared').dataExtension;
const { DATA_FILE_AGE_WARNING,
ExampleUtils, NEEDS_ENTERPRISE } =
require(path.join(__dirname, '/../exampleUtils'));
let pipeline;
const setPipeline = (options) => {
let dataFilePath = optionsExtension.getDataFilePath(options);
if (!dataFilePath) {
throw 'A data file must be specified in the 51d.json file.';
}
if (!fs.existsSync(dataFilePath)) {
const newDataFilePath =
if (newDataFilePath !== undefined) {
dataFilePath = newDataFilePath;
console.warn('Failed to find an IP intelligence data file ' +
`at the specified path '${dataFilePath}'. Use '${newDataFilePath}' ` +
'instead.');
} else {
throw 'Failed to find an IP intelligence data file at ' +
`'${dataFilePath}'. If using the lite file, then make sure the ` +
'cxx/data submodule has been fetched by running ' +
'\'git submodule update --init --recursive\'. Otherwise, ensure ' +
'that the filename is correct in 51d.json.';
}
}
pipeline = new ipi.IpIntelligenceOnPremisePipelineBuilder({
performanceProfile: 'Balanced',
dataFile: dataFilePath,
shareUsage: false,
autoUpdate: false,
updateOnStart: false,
fileSystemWatcher: false,
suppressProcessExceptions: true
})
.add(new translation.CountryCodeTranslationEngineBuilder().build())
.add(new translation.CountriesTranslationEngineBuilder().build())
.build();
pipeline.on('error', console.error);
};
const publicDir = path.join(__dirname, '/public');
const server = http.createServer((req, res) => {
return;
}
const flowData = pipeline.createFlowData();
flowData.evidence.addFromRequest(req);
flowData.process().then(function () {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
const allEvidence = flowData.evidence.getAll();
const evidences =
pipeline.getElement('ip').evidenceKeyFilter.filterEvidence(
allEvidence);
const inputIpAddress =
(allEvidence['query.client-ip'] ||
allEvidence['header.x-forwarded-for'] || '');
const allList = (key) => {
try {
const value = flowData.get('countrynamestranslated').get(key);
return value && value.hasValue ? value.value : [];
} catch (e) {
return [];
}
};
const ipList = (key) => {
try {
const value = flowData.ip[key];
if (value && value.hasValue && Array.isArray(value.value)) {
return value.value.map((item) => dataExtension.formatValue(item));
}
return [];
} catch (e) {
return [];
}
};
const ipEngine = pipeline.getElement('ip');
const countriesNeedEnterprise =
!
ExampleUtils.
hasProperty(ipEngine,
'CountryCodesGeographical');
const countries = (list) =>
countriesNeedEnterprise ? [NEEDS_ENTERPRISE] : list;
const areas =
ExampleUtils.
getEnterpriseValue(ipEngine, flowData.ip,
'areas');
const latitude =
ExampleUtils.getEnterpriseValue(ipEngine, flowData.ip,
'latitude');
const longitude =
ExampleUtils.getEnterpriseValue(ipEngine, flowData.ip,
'longitude');
const located = (value) => value === NEEDS_ENTERPRISE ? '' : value;
res.end(compiledFunction(
{
countryCodesGeographical: countries(ipList('countrycodesgeographical')),
countryCodesPopulation: countries(ipList('countrycodespopulation')),
countryCodesGeographicalAll:
countries(allList('countrycodesgeographicalall')),
countryNamesGeographicalAllTranslated:
countries(allList('countrynamesgeographicalalltranslated')),
countryCodesPopulationAll:
countries(allList('countrycodespopulationall')),
countryNamesPopulationAllTranslated:
countries(allList('countrynamespopulationalltranslated')),
dataFilePublishedTime: new Date().getTime(),
dataFileAgeWarning: DATA_FILE_AGE_WARNING,
evidenceUsed: evidences,
allEvidence,
inputIpAddress,
areas,
mapAreas: located(areas),
registeredName:
ExampleUtils.getEnterpriseValue(ipEngine, flowData.ip,
'registeredname'),
registeredOwner:
ExampleUtils.getEnterpriseValue(ipEngine, flowData.ip,
'registeredowner'),
registeredCountry:
ExampleUtils.getEnterpriseValue(ipEngine, flowData.ip,
'registeredcountry'),
ipRangeStart:
ExampleUtils.getEnterpriseValue(ipEngine, flowData.ip,
'iprangestart'),
ipRangeEnd:
ExampleUtils.getEnterpriseValue(ipEngine, flowData.ip,
'iprangeend'),
country:
ExampleUtils.getEnterpriseValue(ipEngine, flowData.ip,
'country'),
countryCode:
ExampleUtils.getEnterpriseValue(ipEngine, flowData.ip,
'countrycode'),
countryCode3:
ExampleUtils.getEnterpriseValue(ipEngine, flowData.ip,
'countrycode3'),
region:
ExampleUtils.getEnterpriseValue(ipEngine, flowData.ip,
'region'),
state:
ExampleUtils.getEnterpriseValue(ipEngine, flowData.ip,
'state'),
town:
ExampleUtils.getEnterpriseValue(ipEngine, flowData.ip,
'town'),
latitude,
longitude,
mapLatitude: located(latitude),
mapLongitude: located(longitude),
accuracyRadius:
ExampleUtils.getEnterpriseValue(ipEngine, flowData.ip,
'accuracyradiusmin'),
timeZoneOffset:
ExampleUtils.getEnterpriseValue(ipEngine, flowData.ip,
'timezoneoffset')
})
);
});
});
if (process.env.JEST_WORKER_ID === undefined) {
const options = JSON.parse(fs.readFileSync(path.join(__dirname, '/51d.json')));
setPipeline(options);
const port = 3001;
const hostname = 'localhost';
server.listen(port, hostname);
console.log(`Server listening on: http:
};
module.exports = {
server,
setPipeline
};
Definition fiftyone.ipintelligence.onpremise/examples/onpremise/exampleUtils.js:203