Commit b5927643 authored by dmknght's avatar dmknght

All first development code

parents
Pipeline #1130 failed
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "[Test]Show help banner",
"type": "shell",
"command": "nim c -r src/test/testcli.nim"
}
]
}
\ No newline at end of file
# Package
version = "0.1.0"
author = "Nong Hoang Tu"
description = "A replacement of old dirb with threadpool in Nim"
license = "GPL-3.0"
srcDir = "src"
bin = @["dirbnim"]
# Dependencies
requires "nim >= 1.2.6"
# This is just an example to get you started. A typical binary package
# uses this file as the main entry point of the application.
when isMainModule:
echo("Hello, World!")
switch("threads", "on")
switch("define", "ssl")
\ No newline at end of file
# import os
import utils
proc helpBanner*() =
# let appName = getAppFileName()
showHelpCmd(keyword = "-w", value = "<path_to_wordlist>", descr = "Define custom wordlist")
showHelpCmd(keyword = "-a", value = "<browser agent>", descr = "Custom user-agent")
showHelpCmd(keyword = "-c", value = "<cookie>", descr = "Custom HTTP cookie")
\ No newline at end of file
proc showHelpCmd*(app = "dirbnim", keyword = "help", value = "", descr = "") =
#[
Make color for command syntax in help bannner
Print them in help
Syntax: <command> <keyword> <value> [<description>]
command -> light green
keyword -> red
value (optional) -> yellow
description (optional) -> blue
]#
var cmdOutput = ""
cmdOutput &= "\e[32m" & app & "\e[0m " # Green color for command
cmdOutput &= "\e[31m" & keyword & "\e[0m " # Red color for keyword
if value != "":
cmdOutput &= "\e[33m" & value & "\e[0m "
if descr != "":
cmdOutput &= "[\e[36m" & descr & "\e[0m]"
echo cmdOutput
import cpuinfo, times, math, threadpool
# Bench script
# ----------------------------------------------------------------------------------------------------
template benchmark*(benchName: string, body: untyped) =
# When multithreading, make sure to measure wall-clock time
# If you use CPU time you might measure the cumulated CPU time on each processor.
let start = epochTime()
body
let stop = epochTime()
let elapsed = stop-start
echo "Wall time for ", benchName, ": ", round(elapsed, 3), " s"
# OpenMP
# ----------------------------------------------------------------------------------------------------
# Add OpenMP to compilation flags
{.passC:"-fopenmp".}
{.passL:"-fopenmp".}
# Nim native threading
# ----------------------------------------------------------------------------------------------------
template parallelChunks*(start, stop: int, chunkOffset, chunkSize: untyped{ident}, body: untyped): untyped =
## In-place declare and define "chunkOffset" and "chunkSize"
## That corresponds to a slice of the start..stop range
## that will be processed on the same core
let
numIters = (stop - start)
numChunks = countProcessors()
baseChunkSize = numIters div numChunks
remainder = numIters mod numChunks
# The following simple chunking scheme can lead to severe load imbalance
#
# `chunkOffset`{.inject.} = chunkSize * threadId
# `chunkSize`{.inject.} = if threadId < nb_chunks - 1: chunkSize
# else: numIters - chunkOffset # remainder if division isn't exact
#
# For example dividing 40 items on 12 threads will lead to
# a base_chunk_size of 40/12 = 3 so work on the first 11 threads
# will be 3 * 11 = 33, and the remainder 7 on the last thread.
# Instead of dividing 40 work items on 12 cores into:
# 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 7 = 3*11 + 7 = 40
# the following scheme will divide into
# 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3 = 4*4 + 3*8 = 40
var chunkOffset {.inject.}, chunkSize {.inject.}: Natural
for threadID in 0 ..< numChunks:
if threadID < remainder:
chunkOffset = start + (baseChunkSize + 1) * threadID
chunkSize = baseChunkSize + 1
else:
chunkOffset = start + baseChunkSize * threadID + remainder
chunkSize = baseChunkSize
block: body
import httpclient
import terminal
import strutils
import bench
import threadpool
const
url = "http://testphp.vulnweb.com/"
dict = "/usr/share/dirb/wordlists/common.txt"
# dict = "list.txt"
# TODO handle url format
{.experimental: "parallel".}
proc simProgressBar(subPath: string) =
stdout.eraseLine()
stdout.write(subPath)
stdout.flushFile()
proc checkPath(client: HttpClient, subPath, fullURL: string) =
if not isEmptyOrWhitespace(subPath):
simProgressBar(subPath)
let fullURL = url & subPath
let resp = client.get(fullURL)
if resp.status != "404 Not Found":
stdout.eraseLine()
echo fullURL & " " & resp.status
proc createTest() =
var client = newHttpClient()
# benchmark "Normal loop":
# for subPath in lines(dict):
# if isEmptyOrWhitespace(subPath):
# continue
# simProgressBar(subPath)
# let fullURL = url & subPath
# let resp = client.get(fullURL)
# if resp.status != "404 Not Found":
# stdout.eraseLine()
# echo fullURL & " " & resp.status
benchmark "ThreadPool loop":
for subPath in lines(dict):
let fullURL = url & subPath
spawn checkPath(client, subPath, fullURL)
client.close()
stdout.eraseLine()
createtest()
switch("threads", "on")
switch("define", "ssl")
\ No newline at end of file
import ../ modules / cli / help
helpBanner()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment