Skip to content

Commit

Permalink
feat(matcher): parse through status code as argument
Browse files Browse the repository at this point in the history
prase and match functions
  • Loading branch information
louonezime committed Jun 25, 2024
1 parent 1ab304d commit b3158a4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
15 changes: 6 additions & 9 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@ package main

import (
"flag"
"log"
"fmt"
"bruteforce/src/matching"
)

func main() {
matchPtr := flag.String("match", "", "a string")
usagePtr := flag.Bool("help", false, "a bool")
statusPtr := flag.String("status-codes", "200,401,403,404,429,500", "Comma-separated list of status codes to match")

flag.Parse()
if *usagePtr {
fmt.Println("Usage: use flag match=<option>")
fmt.Println(" option status[100-599|all]")
fmt.Println("Usage for matching options:\n\nuse flag -status-codes=\"<status-options>\"")
fmt.Println("\tstatus-options\t200,202,400,404 (Comma-separated list)")
fmt.Println("\t\t\tall")
fmt.Println("\t\t\t[By default: 200,401,403,404,429,500]")
return;
}
if *matchPtr != "" {
matcher.MatchParser(*matchPtr)
} else {
log.Fatal("No match indicated.")
}
matcher.MatchParser(*statusPtr)
}
51 changes: 48 additions & 3 deletions src/matching/matcher.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,52 @@
package matcher

import "fmt"
import (
"fmt"
"strings"
"log"
"net/http"
)

func MatchParser(matchType string) {
fmt.Println("matching option:", matchType)
func matchStatusCode(url string, matchCodes []int) (bool, string) {
resp, err := http.Get(url)
if err != nil {
return false, ""
}
defer resp.Body.Close()

for _, code := range matchCodes {
if resp.StatusCode == code {
return true, fmt.Sprintf("Status Code: %d", code)
}
}
return false, fmt.Sprintf("Status Code: %d", resp.StatusCode)
}

func parseStatusCodes(statusCodeList string) ([]int, error) {
codeStrs := strings.Split(statusCodeList, ",")
var codes []int
for _, codeStr := range codeStrs {
var code int
if _, err := fmt.Sscanf(codeStr, "%d", &code); err != nil {
return nil, err
}
if code < 600 && code >= 100 {
codes = append(codes, code)
} else {
fmt.Printf("[WARN] `%d` not considered, invalid status code.", code)
}
}
if len(codes) == 1 && codes[0] == 0 {
return nil, fmt.Errorf("no valid status code given")
}
return codes, nil
}

func MatchParser(statuses string) {
log.Println("Matching using the following status codes:", statuses)
matchCodes, err := parseStatusCodes(statuses)
if err != nil {
log.Fatal("Error parsing status codes:", err)
}
log.Println("Matching status codes:", matchCodes)
}

0 comments on commit b3158a4

Please sign in to comment.