Skip to content

Commit

Permalink
refactor: implementation of query package and utils with temporary main
Browse files Browse the repository at this point in the history
  • Loading branch information
louonezime committed Jul 17, 2024
1 parent 2e47a15 commit 570b74e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 45 deletions.
11 changes: 9 additions & 2 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bruteforce/src/matching"
"bruteforce/src/query"
"flag"
"fmt"
)
Expand All @@ -24,7 +25,13 @@ func main() {
}

criteria := matcher.MatchParser(*statusPtr, *headerPtr, *bodyPtr)
// Here look to implement queryExecute
// matcher.MatchResponse can be called with criteria (above) in parameter
fmt.Println(criteria)

data := query.ForceData{
Worker: 3,
WordList: "../wordList/rootList",
Url: "http://localhost:3333",
}
// matcher.MatchResponse can be called with criteria (above) in parameter
query.MainRequest(data, criteria) // maybe like this?
}
41 changes: 21 additions & 20 deletions src/query/callWorker.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
package main
package query

import (
"time"
"bruteforce/src/matching"
"bruteforce/src/utils"
"time"
)

type forceData struct {
worker int
wordList string
url string
type ForceData struct {
Worker int
WordList string
Url string
}

func executeQueryFromFile(data forceData, currentPath chan string) {
for taskData := range currentPath{
queryExecute(data, taskData, "POST")
}
func executeQueryFromFile(data ForceData, currentPath chan string) {
for taskData := range currentPath {
QueryExecute(data, taskData, "POST")
}
}

func mainRequest(data forceData) {
channel := make(chan string)
wordArray := GetFileContent("../wordList/rootList")
func MainRequest(data ForceData, criteria matcher.MatchCriteria) {
channel := make(chan string)
wordArray := utils.GetFileContent(data.WordList)

for i := 0 ;i < data.worker; i++ {
for i := 0; i < data.Worker; i++ {
go executeQueryFromFile(data, channel)
}
for i := 0; i < len(wordArray); i++ {
channel <- wordArray[i]
}
time.Sleep(1 * time.Second)
close(channel)
for i := 0; i < len(wordArray); i++ {
channel <- wordArray[i]
}
time.Sleep(1 * time.Second)
close(channel)
}

21 changes: 9 additions & 12 deletions src/query/queryExecute.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
package main
package query

import (
"fmt"
"io"
"log"
"io/ioutil"
"net/http"
)

func queryExecute(data forceData, path string, method string) {

func QueryExecute(data ForceData, path string, method string) {
client := &http.Client{}
req, err := http.NewRequest(method, data.url + path, nil)
req, err := http.NewRequest(method, data.Url+path, nil)
if err != nil {
log.Fatal(err)
log.Fatal(err)
}

q := req.URL.Query()

req.URL.RawQuery = q.Encode()

resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
fmt.Println(err)
}

defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
log.Fatal(err)
}

fmt.Println(string(body))
}
}
21 changes: 10 additions & 11 deletions src/utils/getFile.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package main
package utils

import (
"os"
"fmt"
"log"
"log"
"os"
"strings"
)

func GetFileContent(filePath string) []string{
body, err := os.ReadFile(filePath)
if err != nil {
log.Fatalf("unable to read file: %v", err)
}
func GetFileContent(filePath string) []string {
body, err := os.ReadFile(filePath)
if err != nil {
log.Fatalf("Unable to read file: %v", err)
}

dataTab := strings.Split(string(body), "\n")
fmt.Println(dataTab[0])
return dataTab
}
}

0 comments on commit 570b74e

Please sign in to comment.