Skip to content

Instantly share code, notes, and snippets.

@slaughtr
Last active October 20, 2021 21:52
Show Gist options
  • Save slaughtr/abf3c4ba855a31f30b29f4985ea1ec7e to your computer and use it in GitHub Desktop.
Save slaughtr/abf3c4ba855a31f30b29f4985ea1ec7e to your computer and use it in GitHub Desktop.
Check RewriteRules - find redirect chains, status codes, final URLs
// USAGE: node apacheRedirectCheck.js /path/to/file.conf.in https://yourdomain.example
const lineReader = require('line-by-line')
const util = require('util');
const exec = util.promisify(require('child_process').exec);
// get our file
const [ ,, fileName, host ] = process.argv
const lr = new lineReader(fileName)
const results = { success: [], regex: [], failure: []}
// Linereader boilerplate
lr.on('error', console.error)
lr.on('line', async line => {
// Hold on, async operations...
lr.pause()
try{
// This is very naive
const fromURL = line.split('^')[1].split(' ')[0].replace('(.*)', '').replace('$', '')
// Just skip regex'd ones for now
if (fromURL.includes('(') || fromURL.includes('[') || fromURL.includes('?')) results.regex.push(fromURL)
else {
try {
// I was originally trying to do this all via shell scripts
// But turns out I'd rather fight JS than sh
const { stdout,stderr } = await exec(`curl -Ls -o /dev/null -w "End URL: %{url_effective} | Redirects: %{num_redirects} | Status code: %{http_code}" ${host}${fromURL}`)
results.success.push({fromURL, stdout})
console.log(stdout, stderr)
} catch(e) {
console.error(e)
// Figure it out later
results.failure.push(fromURL)
}
}
} catch(outerE) {
// Figure it out later
results.failure.push(line)
}
// Next line!
lr.resume()
});
lr.on('end', function () {
// All lines are read, write the file
require('fs').writeFileSync('./results.json', JSON.stringify(results, null, 2))
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment