Skip to content

Commit

Permalink
Merge pull request #21 from wunderio/feature/empty-ua-fix
Browse files Browse the repository at this point in the history
Empty user agent fix
  • Loading branch information
Jancis authored Mar 11, 2024
2 parents 14bd88a + 14ad674 commit 9a82a6c
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ app.use(cors());
*/
app.post('/upscale', async (req, res) => {
try {
blockBadActors(req, res);
if (blockBadActors(req)) {
res.sendStatus(403).end();
return;
}
const ingress = await loadIngressByHostname(req.query.domain);

if (ingress) {
Expand Down Expand Up @@ -128,9 +131,10 @@ app.get('/status', async (req, res) => {

app.get('*', async (req, res) => {
res.setHeader('Cache-Control', 'no-store');

blockBadActors(req, res);

if (blockBadActors(req)) {
res.sendStatus(403).end();
return;
}
try {
// Strip off the port when used locally.
const hostname = req.headers.host.replace(':3000', '');
Expand Down Expand Up @@ -162,12 +166,17 @@ async function loadIngressByHostname(hostname) {
return ingresses.find(ingress => ingress.spec.rules.some(rule => rule.host === hostname));
}

function blockBadActors(req, res) {
function blockBadActors(req) {
if (typeof badUserAgents !== 'undefined'){
badUserAgentsArray.forEach(function(uaString){
if (req.get('User-Agent').includes(uaString)) res.sendStatus(403).end();
if (req.get('User-Agent') && req.get('User-Agent').includes(uaString)) {
// Print ip address of the request and sanitized user agent
console.log(`Blocked request from ${req.ip} with user agent ${req.get('User-Agent').replace(/[^a-zA-Z0-9]/g, '')}`);
return true;
}
});
}
return false;
}

function placeholderPageContent(hostname, ingressName) {
Expand Down

0 comments on commit 9a82a6c

Please sign in to comment.