From a19bd277ea4a86e5b311570fe0dee63d643bd8fc Mon Sep 17 00:00:00 2001 From: shelepuginivan Date: Sun, 5 Jan 2025 19:58:32 +0300 Subject: [PATCH 1/6] style(tests): run `go fmt` on `test.go` --- tests/test.go | 113 ++++++++++++++++++++++++-------------------------- 1 file changed, 55 insertions(+), 58 deletions(-) diff --git a/tests/test.go b/tests/test.go index 3926f7c..029b3c2 100644 --- a/tests/test.go +++ b/tests/test.go @@ -1,23 +1,22 @@ package main import ( + "crypto/tls" "fmt" "io" - "os" "log" - "time" "net/http" - "crypto/tls" + "os" + "time" ) -var Red = "\033[31m" +var Red = "\033[31m" var Reset = "\033[0m" var Green = "\033[32m" var Yellow = "\033[33m" func Mode0(url string) { - - fmt.Println(Yellow + "[*] Testing URL:", url + Reset) + fmt.Println(Yellow+"[*] Testing URL:", url+Reset) fmt.Println(Yellow + "[*] Mode: 0 (Status Code)" + Reset) transport := &http.Transport{ @@ -25,35 +24,34 @@ func Mode0(url string) { MinVersion: tls.VersionTLS12, }, } - + client := &http.Client{ Timeout: 85 * time.Second, Transport: transport, } req, err := http.NewRequest("GET", url, nil) - if err!= nil { - log.Fatal(err) - } + if err != nil { + log.Fatal(err) + } req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0") res, err := client.Do(req) - if err!= nil { - log.Fatal(err) - } + if err != nil { + log.Fatal(err) + } defer res.Body.Close() - fmt.Println(Green + "[+] Response:", res.Status + Reset) - fmt.Println(Green + "[+] Response URL:", res.Request.URL) + fmt.Println(Green+"[+] Response:", res.Status+Reset) + fmt.Println(Green+"[+] Response URL:", res.Request.URL) os.Exit(0) } func Mode1(url string) { - - fmt.Println(Yellow + "[*] Testing URL:", url + Reset) + fmt.Println(Yellow+"[*] Testing URL:", url+Reset) fmt.Println(Yellow + "[*] Mode: 1 (Response Body)" + Reset) transport := &http.Transport{ @@ -61,41 +59,41 @@ func Mode1(url string) { MinVersion: tls.VersionTLS12, }, } - + client := &http.Client{ Timeout: 85 * time.Second, Transport: transport, } req, err := http.NewRequest("GET", url, nil) - if err!= nil { - log.Fatal(err) - } + if err != nil { + log.Fatal(err) + } req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0") res, err := client.Do(req) - if err!= nil { - log.Fatal(err) - } + if err != nil { + log.Fatal(err) + } defer res.Body.Close() body, err := io.ReadAll(res.Body) - if err!= nil { - log.Fatal(err) - } + if err != nil { + log.Fatal(err) + } os.WriteFile("response.txt", body, os.FileMode(os.O_WRONLY)) - fmt.Println(Green + "[+] Response:", res.Status + Reset) - fmt.Println(Green + "[+] Response URL:", res.Request.URL) + fmt.Println(Green+"[+] Response:", res.Status+Reset) + fmt.Println(Green+"[+] Response URL:", res.Request.URL) fmt.Println(Green + "[+] Saved response to response.txt" + Reset) - os.Exit(0) + os.Exit(0) } func Mode2(url string) { - fmt.Println(Yellow + "[*] Testing URL:", url + Reset) + fmt.Println(Yellow+"[*] Testing URL:", url+Reset) fmt.Println(Yellow + "[*] Mode: 2 (Status Code Without Following Redirects)" + Reset) transport := &http.Transport{ @@ -103,7 +101,7 @@ func Mode2(url string) { MinVersion: tls.VersionTLS12, }, } - + client := &http.Client{ Timeout: 85 * time.Second, Transport: transport, @@ -114,27 +112,26 @@ func Mode2(url string) { } req, err := http.NewRequest("GET", url, nil) - if err!= nil { - log.Fatal(err) - } + if err != nil { + log.Fatal(err) + } req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0") res, err := client.Do(req) - if err!= nil { - log.Fatal(err) - } + if err != nil { + log.Fatal(err) + } defer res.Body.Close() - fmt.Println(Green + "[+] Response:", res.Status + Reset) - fmt.Println(Green + "[+] Response URL:", res.Request.URL) + fmt.Println(Green+"[+] Response:", res.Status+Reset) + fmt.Println(Green+"[+] Response URL:", res.Request.URL) os.Exit(0) } func Mode3(url string) { - - fmt.Println(Yellow + "[*] Testing URL:", url + Reset) + fmt.Println(Yellow+"[*] Testing URL:", url+Reset) fmt.Println(Yellow + "[*] Mode: 3 (Response Body Without Following Redirects)" + Reset) transport := &http.Transport{ @@ -142,7 +139,7 @@ func Mode3(url string) { MinVersion: tls.VersionTLS12, }, } - + client := &http.Client{ Timeout: 85 * time.Second, Transport: transport, @@ -153,27 +150,27 @@ func Mode3(url string) { } req, err := http.NewRequest("GET", url, nil) - if err!= nil { - log.Fatal(err) - } + if err != nil { + log.Fatal(err) + } req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0") res, err := client.Do(req) - if err!= nil { - log.Fatal(err) - } + if err != nil { + log.Fatal(err) + } defer res.Body.Close() body, err := io.ReadAll(res.Body) - if err!= nil { - log.Fatal(err) - } + if err != nil { + log.Fatal(err) + } os.WriteFile("response.txt", body, os.FileMode(os.O_WRONLY)) - fmt.Println(Green + "[+] Response:", res.Status + Reset) - fmt.Println(Green + "[+] Response URL:", res.Request.URL) + fmt.Println(Green+"[+] Response:", res.Status+Reset) + fmt.Println(Green+"[+] Response URL:", res.Request.URL) fmt.Println(Green + "[+] Saved response to response.txt" + Reset) os.Exit(0) @@ -186,16 +183,16 @@ func main() { if len(os.Args) != 3 { fmt.Println(Red + "Usage: gosearch \nIssues: https://github.com/ibnaleem/gosearch/issues" + Reset) - os.Exit(1) + os.Exit(1) } else if len(os.Args) == 2 { fmt.Println(Red + "Mode not provided. Please provide either 0, 1, 2, or 3. Exiting..." + Reset) - os.Exit(1) + os.Exit(1) } else if len(os.Args) == 1 { fmt.Println(Yellow + "Welcome to GoSearch's testing binary." + Reset) fmt.Println(Yellow + "First, find a url containing a username." + Red + "Eg. https://instagram.com/zuck" + Reset) fmt.Println(Yellow + "Then, provide the mode number you want to test against." + Red + "Eg. ./test https://instagram.com/zuck 0" + Reset) fmt.Println(Yellow + "Modes:\n0: Status Code - Check if a website throws any status code errors for invalid usernames") - fmt.Println(Yellow + "1: Response Body - Check if the response body contains any errors for invalid usernames (e.g 'username not found')") + fmt.Println(Yellow + "1: Response Body - Check if the response body contains any errors for invalid usernames (e.g 'username not found')") fmt.Println(Yellow + "2: Status Code (No Redirects) - Check if a website throws any status code errors for invalid usernames without following redirects") fmt.Println(Yellow + "3: Response Body (No Redirects) - Check if the response body contains any errors for invalid usernames (e.g 'username not found') without following redirects") os.Exit(1) @@ -211,6 +208,6 @@ func main() { Mode3(url) } else { fmt.Println(Red + "Invalid mode. Please provide either 0, 1, 2, or 3. Exiting..." + Reset) - os.Exit(1) + os.Exit(1) } } From 44bea9b0e75cbc392f32d4bec50692bfc0210ccb Mon Sep 17 00:00:00 2001 From: shelepuginivan Date: Sun, 5 Jan 2025 20:12:05 +0300 Subject: [PATCH 2/6] refactor(tests): use constants * Replace hard-coded HTTP methods with built-in constants * Use `const` instead of `var` for ANSI-escape codes * Add a common `UserAgent` constant for all modes --- tests/test.go | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/tests/test.go b/tests/test.go index 029b3c2..2543593 100644 --- a/tests/test.go +++ b/tests/test.go @@ -10,10 +10,16 @@ import ( "time" ) -var Red = "\033[31m" -var Reset = "\033[0m" -var Green = "\033[32m" -var Yellow = "\033[33m" +// Color output constants. +const ( + Red = "\033[31m" + Reset = "\033[0m" + Green = "\033[32m" + Yellow = "\033[33m" +) + +// User-Agent header used in requests. +const UserAgent = "Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0" func Mode0(url string) { fmt.Println(Yellow+"[*] Testing URL:", url+Reset) @@ -30,12 +36,12 @@ func Mode0(url string) { Transport: transport, } - req, err := http.NewRequest("GET", url, nil) + req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { log.Fatal(err) } - req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0") + req.Header.Set("User-Agent", UserAgent) res, err := client.Do(req) if err != nil { @@ -65,12 +71,12 @@ func Mode1(url string) { Transport: transport, } - req, err := http.NewRequest("GET", url, nil) + req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { log.Fatal(err) } - req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0") + req.Header.Set("User-Agent", UserAgent) res, err := client.Do(req) if err != nil { @@ -111,12 +117,12 @@ func Mode2(url string) { return http.ErrUseLastResponse } - req, err := http.NewRequest("GET", url, nil) + req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { log.Fatal(err) } - req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0") + req.Header.Set("User-Agent", UserAgent) res, err := client.Do(req) if err != nil { @@ -149,12 +155,12 @@ func Mode3(url string) { return http.ErrUseLastResponse } - req, err := http.NewRequest("GET", url, nil) + req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { log.Fatal(err) } - req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0") + req.Header.Set("User-Agent", UserAgent) res, err := client.Do(req) if err != nil { From 4d42c099eb226b7cc7acce5a3095bbe0eef84607 Mon Sep 17 00:00:00 2001 From: shelepuginivan Date: Sun, 5 Jan 2025 20:15:26 +0300 Subject: [PATCH 3/6] refactor(tests): remove redundant `os.Exit` calls --- tests/test.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tests/test.go b/tests/test.go index 2543593..fe6fee5 100644 --- a/tests/test.go +++ b/tests/test.go @@ -52,8 +52,6 @@ func Mode0(url string) { fmt.Println(Green+"[+] Response:", res.Status+Reset) fmt.Println(Green+"[+] Response URL:", res.Request.URL) - - os.Exit(0) } func Mode1(url string) { @@ -94,8 +92,6 @@ func Mode1(url string) { fmt.Println(Green+"[+] Response:", res.Status+Reset) fmt.Println(Green+"[+] Response URL:", res.Request.URL) fmt.Println(Green + "[+] Saved response to response.txt" + Reset) - - os.Exit(0) } func Mode2(url string) { @@ -133,7 +129,6 @@ func Mode2(url string) { fmt.Println(Green+"[+] Response:", res.Status+Reset) fmt.Println(Green+"[+] Response URL:", res.Request.URL) - os.Exit(0) } func Mode3(url string) { @@ -178,9 +173,6 @@ func Mode3(url string) { fmt.Println(Green+"[+] Response:", res.Status+Reset) fmt.Println(Green+"[+] Response URL:", res.Request.URL) fmt.Println(Green + "[+] Saved response to response.txt" + Reset) - - os.Exit(0) - } func main() { From 28a5775a3f23c9846b1310d44c51cd63b0790fc7 Mon Sep 17 00:00:00 2001 From: shelepuginivan Date: Sun, 5 Jan 2025 20:16:43 +0300 Subject: [PATCH 4/6] fix(tests): set proper permissions for output file On Linux, resulting file could not be read by user. This commit fixes this behavior by setting proper file permissions of `response.txt`, allowing user to read it and write to it. --- tests/test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test.go b/tests/test.go index fe6fee5..bc978e3 100644 --- a/tests/test.go +++ b/tests/test.go @@ -88,7 +88,7 @@ func Mode1(url string) { log.Fatal(err) } - os.WriteFile("response.txt", body, os.FileMode(os.O_WRONLY)) + os.WriteFile("response.txt", body, os.ModePerm) fmt.Println(Green+"[+] Response:", res.Status+Reset) fmt.Println(Green+"[+] Response URL:", res.Request.URL) fmt.Println(Green + "[+] Saved response to response.txt" + Reset) @@ -169,7 +169,7 @@ func Mode3(url string) { log.Fatal(err) } - os.WriteFile("response.txt", body, os.FileMode(os.O_WRONLY)) + os.WriteFile("response.txt", body, os.ModePerm) fmt.Println(Green+"[+] Response:", res.Status+Reset) fmt.Println(Green+"[+] Response URL:", res.Request.URL) fmt.Println(Green + "[+] Saved response to response.txt" + Reset) From f1bceb64fda95e954c33bbb391fc8ff6c974a089 Mon Sep 17 00:00:00 2001 From: shelepuginivan Date: Sun, 5 Jan 2025 20:22:48 +0300 Subject: [PATCH 5/6] fix(tests): check cli arguments before usage Executing `go run .` (or the `tests` binary) resulted in the following error: `panic: runtime error: index out of range [1] with length 1`. This commit fixes this behavior by ensuring correct arguments before using them. --- tests/test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test.go b/tests/test.go index bc978e3..315c6ec 100644 --- a/tests/test.go +++ b/tests/test.go @@ -176,9 +176,6 @@ func Mode3(url string) { } func main() { - url := os.Args[1] - mode := os.Args[2] - if len(os.Args) != 3 { fmt.Println(Red + "Usage: gosearch \nIssues: https://github.com/ibnaleem/gosearch/issues" + Reset) os.Exit(1) @@ -196,6 +193,9 @@ func main() { os.Exit(1) } + url := os.Args[1] + mode := os.Args[2] + if mode == "0" { Mode0(url) } else if mode == "1" { From 7fab6354e4f5c139da828c703b3b850ca4deff79 Mon Sep 17 00:00:00 2001 From: shelepuginivan Date: Sun, 5 Jan 2025 20:30:26 +0300 Subject: [PATCH 6/6] fix(tests): reorder usage messages When an invalid number of arguments were provided, `tests` binary would print usage string (`Usage: gosearch ...`) and exit immediately, without printing a detailed usage message (`Welcome to GoSearch's testing binary ...`). --- tests/test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/test.go b/tests/test.go index 315c6ec..48b3bb2 100644 --- a/tests/test.go +++ b/tests/test.go @@ -176,13 +176,7 @@ func Mode3(url string) { } func main() { - if len(os.Args) != 3 { - fmt.Println(Red + "Usage: gosearch \nIssues: https://github.com/ibnaleem/gosearch/issues" + Reset) - os.Exit(1) - } else if len(os.Args) == 2 { - fmt.Println(Red + "Mode not provided. Please provide either 0, 1, 2, or 3. Exiting..." + Reset) - os.Exit(1) - } else if len(os.Args) == 1 { + if len(os.Args) == 1 { fmt.Println(Yellow + "Welcome to GoSearch's testing binary." + Reset) fmt.Println(Yellow + "First, find a url containing a username." + Red + "Eg. https://instagram.com/zuck" + Reset) fmt.Println(Yellow + "Then, provide the mode number you want to test against." + Red + "Eg. ./test https://instagram.com/zuck 0" + Reset) @@ -191,6 +185,12 @@ func main() { fmt.Println(Yellow + "2: Status Code (No Redirects) - Check if a website throws any status code errors for invalid usernames without following redirects") fmt.Println(Yellow + "3: Response Body (No Redirects) - Check if the response body contains any errors for invalid usernames (e.g 'username not found') without following redirects") os.Exit(1) + } else if len(os.Args) == 2 { + fmt.Println(Red + "Mode not provided. Please provide either 0, 1, 2, or 3. Exiting..." + Reset) + os.Exit(1) + } else if len(os.Args) > 3 { + fmt.Println(Red + "Usage: gosearch \nIssues: https://github.com/ibnaleem/gosearch/issues" + Reset) + os.Exit(1) } url := os.Args[1]