This example demonstrates using both device detection and IP intelligence engines in a single web application pipeline.
This example demonstrates using both device detection and IP intelligence engines in a single web application pipeline. The User-Agent and IP address of the requesting client are used as evidence and details about the device, network, and location are displayed.
This example can be configured using the 51d.json file. The paths to the data files are taken from this configuration, and can be changed by editing the file. Where a configured data file cannot be found, the project directories are searched for a file with the same name.
const path = require('path');
const require51 = (requestedPackage) => {
try {
return require(path.join(__dirname, '/../../../../node_modules/', requestedPackage));
} catch (e) {
try {
return require(requestedPackage);
} catch (e2) {
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 core = require51('fiftyone.pipeline.core');
const { DATA_FILE_AGE_WARNING,
ExampleUtils, NEEDS_ENTERPRISE } =
require(path.join(__dirname, '/../../exampleUtils'));
let pipeline;
const fixDataFilePaths = (options) => {
for (const element of options.PipelineOptions.Elements) {
const parameters = element.elementParameters;
if (parameters === undefined ||
parameters.dataFilePath === undefined) {
continue;
}
if (!fs.existsSync(parameters.dataFilePath)) {
const fileName = path.basename(parameters.dataFilePath);
const newDataFilePath = fileName.toLowerCase().endsWith('.hash')
if (newDataFilePath !== undefined) {
parameters.dataFilePath = newDataFilePath;
console.warn('Failed to find a data file at the specified ' +
`path. Using '${newDataFilePath}' instead.`);
} else {
throw 'Failed to find a data file at ' +
`'${parameters.dataFilePath}'. Ensure that the filename is ` +
'correct in 51d.json. If using the lite files, then make sure ' +
'the cxx/data submodule has been fetched by running ' +
'\'git submodule update --init --recursive\'.';
}
}
}
};
const setPipeline = (options) => {
fixDataFilePaths(options);
pipeline = new core.PipelineBuilder().buildFromConfiguration(options);
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 deviceEvidence =
pipeline.getElement('device').evidenceKeyFilter.filterEvidence(
allEvidence);
const ipEvidence =
pipeline.getElement('ip').evidenceKeyFilter.filterEvidence(
allEvidence);
const evidenceUsed = Object.assign({}, deviceEvidence, ipEvidence);
const inputIpAddress =
(allEvidence['query.client-ip'] ||
allEvidence['header.x-forwarded-for'] || '');
const deviceEngine = pipeline.getElement('device');
const ipEngine = pipeline.getElement('ip');
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(
{
dataFilePublishedTime: new Date().getTime(),
dataFileAgeWarning: DATA_FILE_AGE_WARNING,
evidenceUsed,
allEvidence,
inputIpAddress,
areas,
mapAreas: located(areas),
isMobile:
ExampleUtils.getEnterpriseValue(deviceEngine, flowData.device,
'ismobile'),
platformName:
ExampleUtils.getEnterpriseValue(deviceEngine, flowData.device,
'platformname'),
platformVersion:
ExampleUtils.getEnterpriseValue(deviceEngine, flowData.device,
'platformversion'),
browserName:
ExampleUtils.getEnterpriseValue(deviceEngine, flowData.device,
'browsername'),
browserVersion:
ExampleUtils.getEnterpriseValue(deviceEngine, flowData.device,
'browserversion'),
deviceType:
ExampleUtils.getEnterpriseValue(deviceEngine, flowData.device,
'devicetype'),
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'),
town:
ExampleUtils.getEnterpriseValue(ipEngine, flowData.ip,
'town'),
latitude,
longitude,
mapLatitude: located(latitude),
mapLongitude: located(longitude)
})
);
});
});
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