2020-07-15 19:28:36 +02:00
const https = require ( 'https' )
const http = require ( 'http' )
const fs = require ( 'fs' )
async function main _loop ( ) {
2020-07-21 00:00:09 +02:00
json _object = [ ]
2020-07-15 19:28:36 +02:00
for ( let i = 0 ; i < times ; i ++ ) {
2022-12-12 21:03:40 +01:00
const url = url _generator ( i , times )
2020-07-15 19:28:36 +02:00
try {
const response = await fetch ( url )
console . log ( ` ${ url } exists! ` )
2022-12-12 21:03:40 +01:00
json _object . push ( ` {"website_url":" ${ url } ","response_type":"SUCCESS","response_code":" ${ response . statusCode } ","response_details":" ${ response . statusMessage } "} ` )
2020-07-15 19:28:36 +02:00
}
catch ( e ) {
2021-06-24 20:11:45 +02:00
if ( e . code != 'ENOTFOUND' ) {
2020-07-15 19:28:36 +02:00
console . log ( ` ${ url } exists! ` )
2022-12-12 21:03:40 +01:00
json _object . push ( ` {"website_url":" ${ url } ","response_type":"ERROR","response_code":" ${ e . code } ","response_details":" ${ e . syscall } "} ` )
2020-07-15 19:28:36 +02:00
}
}
}
2020-07-21 00:00:09 +02:00
fs . appendFile ( report _file , '[' + String ( json _object ) + ']' , function ( err ) { if ( err ) throw err } )
2020-07-15 19:28:36 +02:00
console . log ( '\nFinished at ' + String ( new Date ( ) . getHours ( ) ) + 'h' + String ( new Date ( ) . getMinutes ( ) ) + 'm' )
}
2021-03-07 16:30:48 +01:00
function url _generator ( num _url , times ) {
2022-12-11 20:00:36 +01:00
let result = protocols [ Math . round ( Math . random ( ) * ( protocols . length - 1 ) ) ] + "://"
2020-07-15 19:28:36 +02:00
const characters = "abcdefghijklmnopqrstuvwxyz0123456789"
2020-07-24 01:37:47 +02:00
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 ) ) }
2022-12-12 21:03:40 +01:00
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 ) ] } `
2021-03-07 16:30:48 +01:00
if ( log ) console . log ( ` ${ result } ( ${ num _url + 1 } / ${ times } ) ` )
2020-07-15 19:28:36 +02:00
return result
}
function fetch ( url , options = { } ) {
return new Promise ( ( resolve , reject ) => {
const client = url . startsWith ( 'https' ) ? https : http
2022-12-12 21:03:40 +01:00
const request = client . request ( url , { method : "GET" } , ( res ) => {
2020-07-15 19:28:36 +02:00
res . setEncoding ( 'utf8' )
2022-12-12 21:03:40 +01:00
res . on ( 'data' , ( ) => { } ) // Do nothing, deleting this line actually makes the software exit upon finding a website (wtf)
2020-07-24 01:37:47 +02:00
res . on ( 'end' , ( ) => { resolve ( { statusCode : res . statusCode , statusMessage : res . statusMessage } ) } )
2020-07-15 19:28:36 +02:00
} )
2020-07-24 01:37:47 +02:00
request . on ( 'error' , ( err ) => { reject ( err ) } )
2020-07-15 19:28:36 +02:00
request . end ( )
} )
}
2022-12-12 21:03:40 +01:00
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
2020-07-21 00:00:09 +02:00
2022-12-12 21:03:40 +01:00
const date = new Date ( )
2020-07-24 01:37:47 +02:00
process . stdout . write ( ` \n I am going to look for websites through ${ times } random URLs (min length ${ mini } and max length ${ maxi } ) with the following domains: ` )
2020-07-15 19:28:36 +02:00
console . log ( domains )
2020-07-24 01:37:47 +02:00
process . stdout . write ( "These URLs will use the protocols " )
2022-12-11 20:00:36 +01:00
console . log ( protocols )
2022-12-12 21:03:40 +01:00
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 ( ) )
2020-07-15 19:28:36 +02:00
2022-12-12 21:03:40 +01:00
const report _file = "JS_report_" + String ( date . getUTCDate ( ) ) + String ( date . getHours ( ) ) + String ( date . getMinutes ( ) ) + ".json"
2020-07-21 00:00:09 +02:00
fs . open ( report _file , "w" , function ( err ) { if ( err ) throw err } )
2020-07-15 19:28:36 +02:00
main _loop ( )