From 0897f353bec3f23ea1c637a11bbdadeb5f1d0a70 Mon Sep 17 00:00:00 2001 From: bebiksik Date: Mon, 8 May 2023 21:00:14 +0200 Subject: [PATCH] -status-codes flag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I made a modification to include the status code in the output when using the -status-code flag. Here's an example of the new output: ``` ▶ cat recon/example/domains.txt | httprobe -status-code 200 http://example.com 403 http://admin.example.com 302 http://redirect.example.com ``` --- main.go | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index 60e5004..d3b2e5b 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,9 @@ import ( "bufio" "crypto/tls" "flag" - "fmt" "io" "io/ioutil" + "fmt" "net" "net/http" "os" @@ -48,6 +48,10 @@ func main() { var preferHTTPS bool flag.BoolVar(&preferHTTPS, "prefer-https", false, "only try plain HTTP if HTTPS fails") + // show status codes + var showStatusCodes bool + flag.BoolVar(&showStatusCodes, "status-codes", false, "show status codes in output") + // HTTP method to use var method string flag.StringVar(&method, "method", "GET", "HTTP method to use") @@ -96,8 +100,13 @@ func main() { // always try HTTPS first withProto := "https://" + url - if isListening(client, withProto, method) { - output <- withProto + if code, ok := isListening(client, withProto, method); ok { + if showStatusCodes { + output <- fmt.Sprintf("%d %s", code, withProto) + } else { + output <- withProto + } + // skip trying HTTP if --prefer-https is set if preferHTTPS { @@ -120,8 +129,12 @@ func main() { go func() { for url := range httpURLs { withProto := "http://" + url - if isListening(client, withProto, method) { - output <- withProto + if code, ok := isListening(client, withProto, method); ok { + if showStatusCodes { + output <- fmt.Sprintf("%d %s", code, withProto) + } else { + output <- withProto + } continue } } @@ -210,11 +223,10 @@ func main() { outputWG.Wait() } -func isListening(client *http.Client, url, method string) bool { - +func isListening(client *http.Client, url string, method string) (int, bool) { req, err := http.NewRequest(method, url, nil) if err != nil { - return false + return 0, false } req.Header.Add("Connection", "close") @@ -224,11 +236,8 @@ func isListening(client *http.Client, url, method string) bool { if resp != nil { io.Copy(ioutil.Discard, resp.Body) resp.Body.Close() + return resp.StatusCode, true } - if err != nil { - return false - } - - return true + return 0, false }