Skip to content

Commit

Permalink
Merge pull request #19 from shelepuginivan/main
Browse files Browse the repository at this point in the history
Refactor and enhance test file
  • Loading branch information
ibnaleem authored Jan 5, 2025
2 parents d42155a + 7fab635 commit 61c2f48
Showing 1 changed file with 81 additions and 86 deletions.
167 changes: 81 additions & 86 deletions tests/test.go
Original file line number Diff line number Diff line change
@@ -1,109 +1,109 @@
package main

import (
"crypto/tls"
"fmt"
"io"
"os"
"log"
"time"
"net/http"
"crypto/tls"
"os"
"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)
fmt.Println(Yellow+"[*] Testing URL:", url+Reset)
fmt.Println(Yellow + "[*] Mode: 0 (Status Code)" + Reset)

transport := &http.Transport{
TLSClientConfig: &tls.Config{
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)
}
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 {
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)

os.Exit(0)
fmt.Println(Green+"[+] Response:", res.Status+Reset)
fmt.Println(Green+"[+] Response URL:", res.Request.URL)
}

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{
TLSClientConfig: &tls.Config{
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)
}
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 {
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)
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)

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{
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
},
}

client := &http.Client{
Timeout: 85 * time.Second,
Transport: transport,
Expand All @@ -113,36 +113,34 @@ func Mode2(url string) {
return http.ErrUseLastResponse
}

req, err := http.NewRequest("GET", url, nil)
if err!= nil {
log.Fatal(err)
}
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 {
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)
os.Exit(0)
fmt.Println(Green+"[+] Response:", res.Status+Reset)
fmt.Println(Green+"[+] Response URL:", res.Request.URL)
}

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{
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
},
}

client := &http.Client{
Timeout: 85 * time.Second,
Transport: transport,
Expand All @@ -152,55 +150,52 @@ func Mode3(url string) {
return http.ErrUseLastResponse
}

req, err := http.NewRequest("GET", url, nil)
if err!= nil {
log.Fatal(err)
}
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 {
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)
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)

os.Exit(0)

}

func main() {
url := os.Args[1]
mode := os.Args[2]

if len(os.Args) != 3 {
fmt.Println(Red + "Usage: gosearch <url> <mode>\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)
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)
} 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 <url> <mode>\nIssues: https://github.com/ibnaleem/gosearch/issues" + Reset)
os.Exit(1)
}

url := os.Args[1]
mode := os.Args[2]

if mode == "0" {
Mode0(url)
} else if mode == "1" {
Expand All @@ -211,6 +206,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)
}
}

0 comments on commit 61c2f48

Please sign in to comment.