2024-03-17 16:02:52 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-03-17 16:37:52 -05:00
|
|
|
"flag"
|
2024-03-17 16:02:52 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-03-17 16:37:52 -05:00
|
|
|
// Define flags
|
|
|
|
urlPtr := flag.String("url", "", "URL of the page to fetch (required)")
|
|
|
|
methodPtr := flag.String("X", "GET", "HTTP method for the request (default: GET)")
|
|
|
|
outputPtr := flag.String("o", "", "Output file path (default: print to console)")
|
|
|
|
followRedirectsPtr := flag.Bool("L", false, "Follow HTTP redirects (default: false)")
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
// Check for required flag (-url)
|
|
|
|
if *urlPtr == "" {
|
|
|
|
fmt.Println("Error: -url flag is required")
|
|
|
|
flag.PrintDefaults()
|
2024-03-17 16:02:52 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an HTTP client
|
2024-03-17 16:37:52 -05:00
|
|
|
client := &http.Client{
|
|
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
|
|
if *followRedirectsPtr { // Use the boolean variable
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("too many redirects") // Or another error
|
|
|
|
},
|
|
|
|
}
|
2024-03-17 16:02:52 -05:00
|
|
|
|
|
|
|
// Create a new request
|
2024-03-17 16:37:52 -05:00
|
|
|
req, err := http.NewRequest(*methodPtr, *urlPtr, nil)
|
2024-03-17 16:02:52 -05:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error creating request:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the request and get the response
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error fetching content:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close() // Ensure body is closed
|
|
|
|
|
|
|
|
// Check for successful response
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
fmt.Println("Error:", resp.StatusCode)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the response body
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error reading response body:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-17 16:37:52 -05:00
|
|
|
// Handle output
|
|
|
|
if *outputPtr != "" {
|
|
|
|
// Write content to file
|
|
|
|
err = ioutil.WriteFile(*outputPtr, body, 0644)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error writing to file:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println("Content saved to:", *outputPtr)
|
|
|
|
} else {
|
|
|
|
// Print content to console
|
|
|
|
fmt.Println(string(body))
|
|
|
|
}
|
2024-03-17 16:02:52 -05:00
|
|
|
}
|