stone/server.js

183 lines
4.1 KiB
JavaScript
Raw Normal View History

2017-09-11 10:13:57 +00:00
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
2017-10-01 09:51:10 +00:00
// const config = require('./config');
2017-09-11 10:13:57 +00:00
const log4js = require('log4js');
const logger = log4js.getLogger();
const URL = require('url');
const http = require('http');
2017-09-11 11:19:14 +00:00
const https = require('https');
2017-09-11 14:29:31 +00:00
const apicache = require('apicache');
2017-09-11 10:13:57 +00:00
2018-06-13 21:50:57 +00:00
const ipfilter = require('express-ipfilter').IpFilter;
2018-06-13 22:12:12 +00:00
const IpDeniedError = require('express-ipfilter').IpDeniedError;
2018-06-13 21:50:57 +00:00
2018-01-23 17:00:29 +00:00
const port = process.env.PORT || 8080;
2017-10-01 09:51:10 +00:00
2017-09-11 10:13:57 +00:00
logger.level = 'debug';
// app.use(compression());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ 'extended': true }));
2017-09-11 14:36:12 +00:00
apicache.options({ 'debug': true });
2017-09-11 14:29:31 +00:00
const cache = apicache.middleware;
2018-06-13 22:12:12 +00:00
// app.use(cache('15 minutes'));
2017-09-11 10:13:57 +00:00
2018-06-13 22:14:25 +00:00
const ips = ['212.71.255.44', '82.35.75.161'];
2018-06-13 21:57:24 +00:00
app.use(ipfilter(ips, { 'mode': 'allow' }));
2018-06-13 22:12:12 +00:00
app.use((err, req, res, _next) => {
console.log('Error handler', err);
if(err instanceof IpDeniedError)
res.status(401);
else
res.status(err.status || 500);
/* res.render('error', {
'message': 'You shall not pass',
'error': err
});*/
res.status(403).end();
});
2017-10-01 12:37:31 +00:00
const bouncer = ['phpmyadmin',
'phpMyadmin',
'phpMyAdmin',
'phpmyAdmin',
'phpmyadmin2',
'phpmyadmin3',
'phpmyadmin4',
'2phpmyadmin',
'phpmy',
'phppma',
'myadmin',
'shopdb',
'MyAdmin',
'program',
'PMA',
'dbadmin',
'pma',
'db',
'admin',
'mysql',
'database',
'sqlmanager',
'mysqlmanager',
'php-myadmin',
'phpmy-admin',
'mysqladmin',
'mysql-admin',
'phpMyAdmin2',
'phpMyAdmin3',
'phpMyAdmin4',
'phpMyAdmin-3',
'php-my-admin',
'PMA2011',
'PMA2012',
'PMA2013',
'PMA2014',
'PMA2015',
'PMA2016',
'PMA2017',
'PMA2018',
'pma2011',
'pma2012',
'pma2013',
'pma2014',
'pma2015',
'pma2016',
'pma2017',
'pma2018',
'phpmyadmin2011',
'phpmyadmin2012',
'phpmyadmin2013',
'phpmyadmin2014',
'phpmyadmin2015',
'phpmyadmin2016',
'phpmyadmin2017',
'phpmyadmin2018',
'phpmanager'];
2017-09-11 10:13:57 +00:00
function getUrl (req, res) {
const theUrl = req.params.encoded_id;
logger.info(`IP:${req.ip}`);
2017-09-11 10:13:57 +00:00
logger.debug('Want', theUrl);
2017-10-01 12:54:26 +00:00
if (theUrl === undefined || bouncer.indexOf(theUrl) !== -1 || theUrl === '') {
2017-10-01 12:37:31 +00:00
logger.warn(`You're not getting in ${theUrl}`);
2017-10-01 12:59:43 +00:00
res.status(400).send('');
2017-10-01 12:37:31 +00:00
return;
}
2017-09-11 10:13:57 +00:00
const options = URL.parse(theUrl);
2017-09-11 10:50:04 +00:00
options.followAllRedirects = true;
2017-09-11 10:43:04 +00:00
options.headers = {
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'
};
2017-09-11 10:13:57 +00:00
2017-09-11 11:19:14 +00:00
// console.log('Options', options);
2018-06-13 21:57:24 +00:00
logger.info(`>> getting ${theUrl}`);
2017-09-11 10:13:57 +00:00
function urlQuery (callback) {
try {
2017-09-11 10:52:17 +00:00
let count = 0;
2017-09-11 11:19:14 +00:00
http.request(options, responseHandler).end();
function responseHandler(response) {
2017-09-11 10:47:34 +00:00
response.setEncoding('utf8');
2017-09-11 11:19:14 +00:00
if (response.statusCode === 302 || response.statusCode === 301) {
2017-09-11 10:52:17 +00:00
body = [];
2017-09-11 11:19:14 +00:00
const rUrl = URL.parse(response.headers.location);
rUrl.followAllRedirects = true;
rUrl.headers = {
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'
};
logger.debug('>> follow', response.headers.location, count);
2018-01-23 17:00:29 +00:00
2017-09-11 10:52:17 +00:00
count++;
if (rUrl.protocol === 'https:')
2017-09-11 11:19:14 +00:00
https.request(rUrl, responseHandler).end();
else
2017-09-11 11:19:14 +00:00
http.request(rUrl, responseHandler).end();
2017-09-11 10:52:17 +00:00
}
2017-09-11 10:13:57 +00:00
let data = '';
response.on('data', chunk => {
data += chunk;
});
response.on('end', () => {
if (response.statusCode !== 302 && response.statusCode !== 301)
2017-09-11 11:19:14 +00:00
callback(data);
2017-09-11 10:13:57 +00:00
});
response.on('error', e => {
logger.error(e);
});
2017-09-11 11:19:14 +00:00
}
2017-09-11 10:13:57 +00:00
}
catch (e) {
logger.error(e);
}
}
2017-09-11 10:43:04 +00:00
urlQuery(a => {
2017-09-11 10:13:57 +00:00
// logger.info(a);
2017-09-11 11:19:14 +00:00
logger.info('Got result');
2017-09-11 10:13:57 +00:00
// res.setHeader('Content-Type', 'application/json');
2017-09-11 11:19:14 +00:00
res.send(a);
2017-09-11 10:13:57 +00:00
});
}
2018-06-13 22:14:25 +00:00
app.get('/:encoded_id', cache('15 minutes'), getUrl);
2017-09-11 10:13:57 +00:00
2017-10-01 09:51:10 +00:00
const server = app.listen(port, () => {
logger.info(`Server listening on port ${port}`);
2017-09-11 10:13:57 +00:00
});