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 ++ ) {
2021-03-07 16:30:48 +01:00
const url = await url _generator ( i , times )
2020-07-15 19:28:36 +02:00
try {
const response = await fetch ( url )
console . log ( ` ${ url } exists! ` )
2020-07-21 00:00:09 +02:00
json _object . push ( ` {"website_url":" ${ url } ","response_type":"SUCCESS","response_code":" ${ String ( response . statusCode ) } ","response_details":" ${ String ( response . statusMessage ) } "} ` )
2020-07-15 19:28:36 +02:00
}
catch ( e ) {
if ( e . errno != 'ENOTFOUND' ) {
console . log ( ` ${ url } exists! ` )
2020-07-21 00:00:09 +02:00
json _object . push ( ` {"website_url":" ${ url } ","response_type":"ERROR","response_code":" ${ String ( e . errno ) } ","response_details":" ${ String ( 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 ) {
2020-07-15 19:28:36 +02:00
let result = mode [ Math . round ( Math . random ( ) * ( mode . length - 1 ) ) ] + "://"
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 ) ) }
2020-07-15 19:28:36 +02:00
result += domains [ Math . floor ( Math . random ( ) * domains . length ) ]
2020-07-24 01:37:47 +02:00
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 ) => {
2021-03-07 16:30:48 +01:00
if ( ! url ) return reject ( new Error ( 'URL was not provided' ) ) // Cannot happen; exists just for the sake of it
2020-07-15 19:28:36 +02:00
2020-07-21 00:00:09 +02:00
const { body , method = 'GET' , ... restOptions } = options
2020-07-15 19:28:36 +02:00
const client = url . startsWith ( 'https' ) ? https : http
2020-07-21 00:00:09 +02:00
const request = client . request ( url , { method , ... restOptions } , ( res ) => {
2020-07-15 19:28:36 +02:00
res . setEncoding ( 'utf8' )
2021-03-07 16:30:48 +01:00
res . on ( 'data' , ( chunk ) => { } ) // Do nothing, it must handle receiving data but we do not need the received data
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 ( )
} )
}
const times = process . argv . indexOf ( '-t' ) > - 1 ? Math . round ( Number ( process . argv [ process . argv . indexOf ( '-t' ) + 1 ] ) ) : 3000
2020-07-24 01:37:47 +02:00
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' ]
2020-07-15 19:28:36 +02:00
const mode = process . argv . indexOf ( '-m' ) > - 1 ? process . argv [ process . argv . indexOf ( '-m' ) + 1 ] . split ( ',' ) : [ 'http' ]
const log = process . argv . indexOf ( '-l' ) > - 1
2020-07-24 01:37:47 +02:00
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 second = process . argv . indexOf ( '-s' ) > - 1 ? Math . round ( Number ( process . argv [ process . argv . indexOf ( '-s' ) + 1 ] ) ) : 1
2020-07-15 19:28:36 +02:00
2020-07-21 00:00:09 +02:00
const report _file = "JS_report_" + String ( new Date ( ) . getUTCDate ( ) ) + String ( new Date ( ) . getHours ( ) ) + String ( new Date ( ) . getMinutes ( ) ) + ".json"
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 " )
2020-07-15 19:28:36 +02:00
console . log ( mode )
2020-07-24 01:37:47 +02:00
console . log ( ` and each of them have ${ second } in a 100 chance to have a second level domain. ` )
2020-07-15 19:28:36 +02:00
console . log ( 'Started at ' + String ( new Date ( ) . getHours ( ) ) + 'h' + String ( new Date ( ) . getMinutes ( ) ) + 'm\n' )
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 ( )