-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77e9452
commit f0894c2
Showing
5 changed files
with
222 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* Service that will be available to all docker apps on the network to get Host information | ||
* Host Public IP | ||
* Host Unique Identifier | ||
* Host Geolocation | ||
*/ | ||
|
||
const config = require('config'); | ||
const log = require('../lib/log'); | ||
const messageHelper = require('./messageHelper'); | ||
const geolocationService = require('./geolocationService'); | ||
const fluxNetworkHelper = require('./fluxNetworkHelper'); | ||
const generalService = require('./generalService'); | ||
const dockerService = require('./dockerService'); | ||
|
||
const express = require('express'); | ||
|
||
let server = null; | ||
|
||
async function getHostInfo(req, res) { | ||
try { | ||
const app = await dockerService.getAppNameByContainerIp(req.socket.remoteAddress); | ||
if (!app) { | ||
const errMessage = messageHelper.errUnauthorizedMessage(); | ||
res.json(errMessage); | ||
} else { | ||
const hostInfo = {}; | ||
const nodeCollateralInfo = await generalService.obtainNodeCollateralInformation().catch(() => { throw new Error('Host Identifier information not available at the moment'); }); | ||
hostInfo.id = nodeCollateralInfo.txhash + nodeCollateralInfo.txindex; | ||
const myIP = await fluxNetworkHelper.getMyFluxIPandPort(); | ||
if (myIP) { | ||
hostInfo.ip = myIP.split(':')[0]; | ||
const myGeo = await geolocationService.getNodeGeolocation(); | ||
if (myGeo) { | ||
delete myGeo.ip; | ||
delete myGeo.org; | ||
hostInfo.geo = myGeo; | ||
} else { | ||
throw new Error('Geolocation information not available at the moment'); | ||
} | ||
} else { | ||
throw new Error('Host IP information not available at the moment'); | ||
} | ||
|
||
const message = messageHelper.createDataMessage(hostInfo); | ||
res.json(message); | ||
} | ||
} catch (error) { | ||
log.error(`getHostInfo: ${error}`); | ||
const errorResponse = messageHelper.createErrorMessage( | ||
error.message || error, | ||
error.name, | ||
error.code, | ||
); | ||
res.json(errorResponse); | ||
} | ||
} | ||
|
||
function handleError(middleware, req, res, next) { | ||
// eslint-disable-next-line consistent-return | ||
middleware(req, res, (err) => { | ||
if (err instanceof SyntaxError && err.status === 400 && 'body' in err) { | ||
res.statusMessage = err.message; | ||
return res.sendStatus(400); | ||
} | ||
if (err) { | ||
log.error(err); | ||
return res.sendStatus(400); | ||
} | ||
|
||
next(); | ||
}); | ||
} | ||
|
||
function start() { | ||
if (server) return; | ||
|
||
const app = express(); | ||
app.use((req, res, next) => { | ||
handleError(express.json(), req, res, next); | ||
}); | ||
app.get('/hostinfo', getHostInfo); | ||
app.all('*', (_, res) => res.status(404).end()); | ||
|
||
const bindAddress = config.server.fluxNodeServiceAddress; | ||
server = app.listen(80, bindAddress, () => { | ||
log.info(`Server listening on port: 80 address: ${bindAddress}`); | ||
}); | ||
} | ||
|
||
function stop() { | ||
if (server) { | ||
server.close(); | ||
server = null; | ||
} | ||
} | ||
|
||
module.exports = { | ||
start, | ||
stop, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters