Skip to content

Commit

Permalink
style: refactor code and make sure each match quit
Browse files Browse the repository at this point in the history
  • Loading branch information
louonezime committed Jul 16, 2024
1 parent 80bb47e commit 075062f
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 69 deletions.
6 changes: 4 additions & 2 deletions src/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

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

func main() {
Expand All @@ -18,7 +18,9 @@ func main() {
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;
fmt.Println("\nuse flag -header=\"<key:value>\"")
fmt.Println("use flag -body=\"<string-in-body>\"")
return
}
matcher.MatchParser("http://example.com", *statusPtr, *headerPtr, *bodyPtr)
}
6 changes: 3 additions & 3 deletions src/matching/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package matcher

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

func matchHeaders(resp *http.Response, criteria MatchCriteria) (bool, string) {
for key, value := range criteria.Headers {
if resp.Header.Get(key) != value {
return false, fmt.Sprintf("header mismatch: %s=%s", key, value)
return false, fmt.Sprintf("header mismatch: %s=%s\nheaders: %s", key, value, resp.Header)
}
}
return true, "headers match"
Expand Down
39 changes: 25 additions & 14 deletions src/matching/matcher.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package matcher

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

type MatchCriteria struct {
Expand All @@ -14,6 +13,9 @@ type MatchCriteria struct {
}

func matchResponse(url string, criteria MatchCriteria) (bool, string) {
status := true
matchError := ""

resp, err := http.Get(url)
if err != nil {
return false, err.Error()
Expand All @@ -25,18 +27,27 @@ func matchResponse(url string, criteria MatchCriteria) (bool, string) {
return false, err.Error()
}

matchStatusCode(resp, criteria.StatusCodes)
matchHeaders(resp, criteria)
matchContents(body, criteria)
status, matchError = matchStatusCode(resp, criteria.StatusCodes)
if !status {
return false, matchError
}
status, matchError = matchHeaders(resp, criteria)
if !status {
return false, matchError
}
status, matchError = matchContents(body, criteria)
if !status {
return false, matchError
}

return false, fmt.Sprintf("status code is %d", resp.StatusCode)
return true, "Matched successfully for " + url
}

func MatchParser(url string, statuses string, headers string, bodyContains string) {
matchCodes, err := parseStatusCodes(statuses)
if err != nil {
log.Fatal("Error parsing status codes:", err)
}
log.Fatal("Error parsing status codes:", err)
}

matchHeaders := parseHeaders(headers)
criteria := MatchCriteria{
Expand All @@ -46,9 +57,9 @@ func MatchParser(url string, statuses string, headers string, bodyContains strin
}

matched, msg := matchResponse(url, criteria)
if matched {
log.Println("Matched:", msg)
} else {
log.Println("Not matched:", msg)
}
if matched {
log.Println("Matched:", msg)
} else {
log.Println("Not matched:", msg)
}
}
50 changes: 50 additions & 0 deletions src/matching/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package matcher

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

func matchStatusCode(resp *http.Response, matchCodes []int) (bool, string) {
isAll := false

if matchCodes[0] == 0 {
isAll = !isAll
} else {
log.Printf("Matching status codes %d...", matchCodes)
}
for _, code := range matchCodes {
if resp.StatusCode == code || isAll {
return true, fmt.Sprintf("status code is %d", resp.StatusCode)
}
}
return false, fmt.Sprintf("status code is %d", resp.StatusCode)
}

func parseStatusCodes(statusCodeList string) ([]int, error) {
codeStrs := strings.Split(statusCodeList, ",")
if statusCodeList == "all" {
log.Println("Matching all status codes")
return []int{0}, nil
}

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
}
50 changes: 0 additions & 50 deletions src/matching/status_codes.go

This file was deleted.

0 comments on commit 075062f

Please sign in to comment.