twtfakelinks/index.js
2022-12-18 18:21:06 -06:00

44 lines
1.2 KiB
JavaScript

const express = require('express')
const app = express()
const port = 4003
app.get('/', (req, res, next) => {
if (req.headers['user-agent'].toLowerCase().startsWith('twitterbot/')) {
console.log('found a twitter request lol')
res.redirect('https://twitter.com/elonmusk')
return
}
next()
})
app.use(express.static('static'))
function addProtocolIfMissing(str) {
if (!str.startsWith('https://') && !str.startsWith('http://')) {
str = 'https://'+str
}
return str
}
app.get('/:base64forTheWorld/:base64forTwitter/:randomtextforcache?', (req, res) => {
let linkForTheWorld
let linkForTwitter
try {
linkForTheWorld = addProtocolIfMissing(Buffer.from(req.params.base64forTheWorld, 'base64').toString('utf-8'))
linkForTwitter = addProtocolIfMissing(Buffer.from(req.params.base64forTwitter, 'base64').toString('utf-8'))
} catch (error) {
res.status(400).send('idk what to do with this url! someone broke something.')
return
}
if (req.headers['user-agent'].toLowerCase().startsWith('twitterbot/')) {
console.log('found a twitter request lol')
res.redirect(linkForTwitter)
return
}
res.redirect(linkForTheWorld)
})
app.listen(port, '127.0.0.1', () => {
console.log(`Example app listening on port ${port}`)
})