Logging now tells you how many URLs the script has checked so far

This commit is contained in:
Taevas 2021-03-07 16:30:48 +01:00
parent b0ec265695
commit 829c833f50
4 changed files with 14 additions and 14 deletions

View file

@ -5,7 +5,7 @@ const fs = require('fs')
async function main_loop() {
json_object = []
for (let i = 0; i < times; i++) {
const url = await url_generator()
const url = await url_generator(i, times)
try {
const response = await fetch(url)
console.log(`${url} exists!`)
@ -22,26 +22,26 @@ async function main_loop() {
console.log('\nFinished at ' + String(new Date().getHours()) + 'h' + String(new Date().getMinutes()) + 'm')
}
function url_generator() {
function url_generator(num_url, times) {
let result = mode[Math.round(Math.random() * (mode.length - 1))] + "://"
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)]
if (log) console.log(result)
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, line may end up getting removed
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) => {
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', (chunk) => {}) // Do nothing, it must handle receiving data but we do not need the received data
res.on('end', () => {resolve({statusCode: res.statusCode, statusMessage: res.statusMessage})})
})
request.on('error', (err) => {reject(err)})

View file

@ -6,7 +6,7 @@ import urllib.request
def main_loop():
json_object = []
for i in range(times):
url = url_generator()
url = url_generator(i, times)
try:
response = urllib.request.urlopen(url)
print(url + " exists!")
@ -21,14 +21,14 @@ def main_loop():
f.close()
print("Finished at " + str(datetime.datetime.now().time())[0:5].replace(":", "h") + "m")
def url_generator():
def url_generator(num_url, times):
result = mode[random.randint(0, len(mode) - 1)] + "://"
characters = "abcdefghijklmnopqrstuvwxyz0123456789"
url_length = random.randint(mini, maxi)
result += ''.join(random.choice(characters) for i in range(url_length))
result += domains[random.randint(0, len(domains) - 1)]
if random.randint(1, 100) <= second: result += domains[random.randint(0, len(domains) - 1)]
if log: print(result)
if log: print(result + " (" + str(num_url + 1) + "/" + str(times) + ")")
return result
times = int(sys.argv[sys.argv.index('-t') + 1]) if '-t' in sys.argv else 3000
@ -36,7 +36,7 @@ domains = sys.argv[sys.argv.index('-d') + 1].split(",") if '-d' in sys.argv else
mode = sys.argv[sys.argv.index('-m') + 1].split(",") if '-m' in sys.argv else ['http']
log = '-l' in sys.argv
mini = int(sys.argv[sys.argv.index('-MIN') + 1]) if '-MIN' in sys.argv else 2
maxi = int(sys.argv[sys.argv.index('-MAX') + 1]) if '-MAX' in sys.argv else 50 #Python cannot look for URLs longer than 50ish, so be careful!
maxi = int(sys.argv[sys.argv.index('-MAX') + 1]) if '-MAX' in sys.argv else 50 # Python cannot look for URLs longer than 50ish, so be careful!
second = int(sys.argv[sys.argv.index('-s') + 1]) if '-s' in sys.argv else 1
print("\nI am going to look for websites through " + str(times) + " random URLs (min length " + str(mini) + " and max length " + str(maxi) + ") with the following domains: " + str(domains))

View file

@ -11,7 +11,7 @@ Each script has its own requirements.
* index.py, the Python script, requires [Python 3](https://www.python.org/downloads/)
* index.js, the Node.js script, requires [Node.js](https://nodejs.org/en/download/)
* index.rb, the Ruby script, requires [Ruby](https://rubyinstaller.org/downloads/)
* index.html, the Javascript script within a HTML webpage, only requires a web browser supporting [JS](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_is_JavaScript)
* index.html, which runs a Javascript script within a HTML webpage, only requires a web browser supporting [JS](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_is_JavaScript)
## HOW TO RUN
@ -46,7 +46,7 @@ You can use arguments by launching the scripts through the command-line.
* "-t" defaults to 3000.
* "-d" defaults to a lot of popular top-level domains.
* "-m" defaults to "http", but the Javascript script defaults to "https" due to [requests made with the "http" application protocol being blocked when not run locally](https://developer.mozilla.org/en-US/docs/Web/Security/Mixed_content).
* "-l" makes it so URLs will be logged.
* "-l" is off by default.
* "-s" defaults to 1.
* "-MIN" defaults to 2.
* "-MAX" defaults to 50.

View file

@ -3,14 +3,14 @@ require 'json'
def main_loop
json_object = []
TIMES.times do
TIMES.times do |i|
url = url_generator()
puts(url) if LOG
puts("#{url} (#{i + 1}/#{TIMES})") if LOG
begin
response = Net::HTTP.get_response(URI(url))
puts("#{url} exists!")
json_object << Hash["website_url" => url, "response_type" => "SUCCESS", "response_code" => response.code, "response_details" => response.message]
rescue Exception => e # Unlike JS/PY, the number of existing websites that raise exceptions is small
rescue Exception => e # Unlike Node/PY, the number of existing websites that raise exceptions is small
if e.class != SocketError
puts("#{url} exists!")
json_object << Hash["website_url" => url, "response_type" => "ERROR", "response_code" => e.class.to_s, "response_details" => e.to_s]