a little error handling
This commit is contained in:
parent
355ff20898
commit
3ec2ee2dfd
24
server.js
24
server.js
@ -30,6 +30,8 @@ const cache = apicache.middleware;
|
|||||||
|
|
||||||
const ips = ['212.71.255.44', '82.35.75.161'];
|
const ips = ['212.71.255.44', '82.35.75.161'];
|
||||||
|
|
||||||
|
const complexUrls = new Map();
|
||||||
|
|
||||||
// app.use(ipfilter(ips, { 'mode': 'allow' }));
|
// app.use(ipfilter(ips, { 'mode': 'allow' }));
|
||||||
|
|
||||||
app.use((err, req, res, _next) => {
|
app.use((err, req, res, _next) => {
|
||||||
@ -109,8 +111,12 @@ const bouncer = ['phpmyadmin',
|
|||||||
const httpStart = RegExp('(http|ftp|https):\\/\\/');
|
const httpStart = RegExp('(http|ftp|https):\\/\\/');
|
||||||
|
|
||||||
function getUrl (req, res) {
|
function getUrl (req, res) {
|
||||||
const theUrl = req.params.encoded_id;
|
let final;
|
||||||
logger.debug('Want', theUrl);
|
const firstUrl = req.params.encoded_id;
|
||||||
|
// let theUrl;
|
||||||
|
logger.debug('Want', firstUrl);
|
||||||
|
const theUrl = complexUrls.get(firstUrl) || firstUrl;
|
||||||
|
|
||||||
if (theUrl === undefined || bouncer.indexOf(theUrl) !== -1 || theUrl === '') {
|
if (theUrl === undefined || bouncer.indexOf(theUrl) !== -1 || theUrl === '') {
|
||||||
logger.warn(`You're not getting in ${theUrl}`);
|
logger.warn(`You're not getting in ${theUrl}`);
|
||||||
res.status(400).send('');
|
res.status(400).send('');
|
||||||
@ -129,6 +135,11 @@ function getUrl (req, res) {
|
|||||||
function urlQuery (callback) {
|
function urlQuery (callback) {
|
||||||
try {
|
try {
|
||||||
let count = 0;
|
let count = 0;
|
||||||
|
|
||||||
|
final = options.href;
|
||||||
|
if (options.protocol === 'https:')
|
||||||
|
https.request(options, responseHandler).end();
|
||||||
|
else
|
||||||
http.request(options, responseHandler).end();
|
http.request(options, responseHandler).end();
|
||||||
|
|
||||||
function responseHandler(response) {
|
function responseHandler(response) {
|
||||||
@ -161,6 +172,7 @@ function getUrl (req, res) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
count++;
|
count++;
|
||||||
|
final = rUrl.href;
|
||||||
if (rUrl.protocol === 'https:')
|
if (rUrl.protocol === 'https:')
|
||||||
https.request(rUrl, responseHandler).end();
|
https.request(rUrl, responseHandler).end();
|
||||||
else
|
else
|
||||||
@ -188,12 +200,18 @@ function getUrl (req, res) {
|
|||||||
urlQuery(a => {
|
urlQuery(a => {
|
||||||
// logger.info(a);
|
// logger.info(a);
|
||||||
logger.info(`Got result for ${theUrl}`);
|
logger.info(`Got result for ${theUrl}`);
|
||||||
|
logger.info(`Actually used: ${final}`);
|
||||||
|
|
||||||
|
if (theUrl !== final)
|
||||||
|
complexUrls.set(theUrl, final);
|
||||||
|
|
||||||
// res.setHeader('Content-Type', 'application/json');
|
// res.setHeader('Content-Type', 'application/json');
|
||||||
res.send(a);
|
res.send(a);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
app.get('/:encoded_id', cache('15 minutes'), getUrl);
|
// app.get('/:encoded_id', cache('15 minutes'), getUrl);
|
||||||
|
app.get('/:encoded_id', getUrl);
|
||||||
|
|
||||||
process.on('uncaughtException', function (err) {
|
process.on('uncaughtException', function (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
Loading…
Reference in New Issue
Block a user