Make use of defaults.json (except C# for now)

This commit is contained in:
Taevas 2022-12-12 21:03:40 +01:00
parent a8bda4704a
commit 8656fd134f
6 changed files with 102 additions and 68 deletions

View file

@ -5,16 +5,16 @@ const fs = require('fs')
async function main_loop() {
json_object = []
for (let i = 0; i < times; i++) {
const url = await url_generator(i, times)
const url = url_generator(i, times)
try {
const response = await fetch(url)
console.log(`${url} exists!`)
json_object.push(`{"website_url":"${url}","response_type":"SUCCESS","response_code":"${String(response.statusCode)}","response_details":"${String(response.statusMessage)}"}`)
json_object.push(`{"website_url":"${url}","response_type":"SUCCESS","response_code":"${response.statusCode}","response_details":"${response.statusMessage}"}`)
}
catch(e) {
if (e.code != 'ENOTFOUND') {
console.log(`${url} exists!`)
json_object.push(`{"website_url":"${url}","response_type":"ERROR","response_code":"${String(e.code)}","response_details":"${String(e.syscall)}"}`)
json_object.push(`{"website_url":"${url}","response_type":"ERROR","response_code":"${e.code}","response_details":"${e.syscall}"}`)
}
}
}
@ -27,21 +27,18 @@ function url_generator(num_url, times) {
const characters = "abcdefghijklmnopqrstuvwxyz0123456789"
const url_length = Math.floor(Math.random() * (maxi - mini) + mini)
for (let i = 0; i < url_length; i++) {result += characters.charAt(Math.floor(Math.random() * characters.length))}
result += domains[Math.floor(Math.random() * domains.length)]
if (Math.floor(Math.random() * (100 - 1) + 1) <= second) result += domains[Math.floor(Math.random() * domains.length)]
result += `.${domains[Math.floor(Math.random() * domains.length)]}`
if (Math.floor(Math.random() * (100 - 1) + 1) <= second) result += `.${domains[Math.floor(Math.random() * domains.length)]}`
if (log) console.log(`${result} (${num_url + 1}/${times})`)
return result
}
function fetch(url, options = {}) {
return new Promise((resolve, reject) => {
if (!url) return reject(new Error('URL was not provided')) // Cannot happen; exists just for the sake of it
const {body, method = 'GET', ...restOptions} = options
const client = url.startsWith('https') ? https : http
const request = client.request(url, {method, ...restOptions}, (res) => {
const request = client.request(url, {method: "GET"}, (res) => {
res.setEncoding('utf8')
res.on('data', (chunk) => {}) // Do nothing, it must handle receiving data but we do not need the received data
res.on('data', () => {}) // Do nothing, deleting this line actually makes the software exit upon finding a website (wtf)
res.on('end', () => {resolve({statusCode: res.statusCode, statusMessage: res.statusMessage})})
})
request.on('error', (err) => {reject(err)})
@ -49,22 +46,23 @@ function fetch(url, options = {}) {
})
}
const times = process.argv.indexOf('-t') > -1 ? Math.round(Number(process.argv[process.argv.indexOf('-t') + 1])) : 3000
const protocols = process.argv.indexOf('-p') > -1 ? process.argv[process.argv.indexOf('-p') + 1].split(',') : ['http']
const domains = process.argv.indexOf('-d') > -1 ? process.argv[process.argv.indexOf('-d') + 1].split(',') : ['.co', '.com', '.net', '.edu', '.gov', '.cn', '.org', '.cc', '.us', '.mil', '.ac', '.it', '.de']
const second = process.argv.indexOf('-s') > -1 ? Math.round(Number(process.argv[process.argv.indexOf('-s') + 1])) : 1
const log = process.argv.indexOf('-l') > -1
const mini = process.argv.indexOf('-min') > -1 ? Math.round(Number(process.argv[process.argv.indexOf('-min') + 1])) : 2
const maxi = process.argv.indexOf('-max') > -1 ? Math.round(Number(process.argv[process.argv.indexOf('-max') + 1])) : 50
const report_file = "JS_report_" + String(new Date().getUTCDate()) + String(new Date().getHours()) + String(new Date().getMinutes()) + ".json"
const defaults = require("../defaults.json")
const times = process.argv.indexOf('-t') > -1 ? Math.round(Number(process.argv[process.argv.indexOf('-t') + 1])) : defaults.times
const protocols = process.argv.indexOf('-p') > -1 ? process.argv[process.argv.indexOf('-p') + 1].split(',') : defaults.protocols
const domains = process.argv.indexOf('-d') > -1 ? process.argv[process.argv.indexOf('-d') + 1].split(',') : defaults.domains
const second = process.argv.indexOf('-s') > -1 ? Math.round(Number(process.argv[process.argv.indexOf('-s') + 1])) : defaults.second
const log = process.argv.indexOf('-l') > -1 ? true : defaults.log
const mini = process.argv.indexOf('-min') > -1 ? Math.round(Number(process.argv[process.argv.indexOf('-min') + 1])) : defaults.min
const maxi = process.argv.indexOf('-max') > -1 ? Math.round(Number(process.argv[process.argv.indexOf('-max') + 1])) : defaults.max
const date = new Date()
process.stdout.write(`\nI am going to look for websites through ${times} random URLs (min length ${mini} and max length ${maxi}) with the following domains: `)
console.log(domains)
process.stdout.write("These URLs will use the protocols ")
console.log(protocols)
console.log(`and each of them have ${second} in a 100 chance to have a second level domain.`)
console.log('Started at ' + String(new Date().getHours()) + 'h' + String(new Date().getMinutes()) + 'm\n')
console.log(`and each of them have ${second} in a 100 chance to have a second level domain`)
console.log("Started at %dh%dm\n", date.getHours(), date.getMinutes())
const report_file = "JS_report_" + String(date.getUTCDate()) + String(date.getHours()) + String(date.getMinutes()) + ".json"
fs.open(report_file, "w", function(err) {if (err) throw err})
main_loop()