-
Notifications
You must be signed in to change notification settings - Fork 0
/
URL_Status_Checker.go
112 lines (100 loc) · 2.81 KB
/
URL_Status_Checker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"bufio"
"crypto/tls"
"flag"
"fmt"
"log"
"net"
"net/http"
"net/url"
"os"
"sync"
"time"
)
func main() {
concurrencyPtr := flag.Int("t", 8, "Number of threads to utilize. Default is 8.")
timeoutPtr := flag.Int("timeout", 8, "Timeout for each request in seconds. Default is 8 seconds.")
retryPtr := flag.Int("retry", 3, "Number of retries for failed requests. Default is 3.")
retrySleepPtr := flag.Int("retry-sleep", 1, "Sleep duration between retries in seconds. Default is 1 second.")
flag.Parse()
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DialContext: (&net.Dialer{Timeout: time.Duration(*timeoutPtr) * time.Second}).DialContext,
TLSHandshakeTimeout: time.Duration(*timeoutPtr) * time.Second,
},
}
fmt.Println("Please type or paste the URLs:")
work := make(chan string)
urlCount := 0
go func() {
s := bufio.NewScanner(os.Stdin)
for {
fmt.Print("Input URL: ")
if !s.Scan() {
break
}
urlText := s.Text()
if urlText == "" {
continue
}
if _, err := url.ParseRequestURI(urlText); err != nil {
fmt.Printf("Invalid URL: %s\n", urlText)
continue
}
work <- urlText
urlCount++
}
if s.Err() != nil {
log.Printf("Error reading from stdin: %v\n", s.Err())
}
close(work)
}()
wg := &sync.WaitGroup{}
for i := 0; i < *concurrencyPtr; i++ {
wg.Add(1)
go doWork(work, client, wg, *retryPtr, *retrySleepPtr)
}
wg.Wait()
// Check if URLs were provided
if urlCount == 0 {
fmt.Println("No valid URLs provided. Exiting.")
} else {
// Wait for user input before closing
fmt.Println("Press Enter to exit...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
}
}
func doWork(work chan string, client *http.Client, wg *sync.WaitGroup, retries int, retrySleep int) {
defer wg.Done()
for urlText := range work {
var resp *http.Response
var err error
// Retry loop
for attempts := 0; attempts <= retries; attempts++ {
req, reqErr := http.NewRequest("GET", urlText, nil)
if reqErr != nil {
log.Printf("Error creating request: %v, URL: %s\n", reqErr, urlText)
break
}
req.Header.Set("Connection", "close")
resp, err = client.Do(req)
if err == nil || attempts == retries {
if err != nil {
log.Printf("Request failed: %v, URL: %s\n", err, urlText)
} else {
// Print success status code 5 times
for i := 0; i < 5; i++ {
log.Printf("Response status: %d, URL: %s\n", resp.StatusCode, urlText)
}
resp.Body.Close()
}
break
}
// Log retry attempt and sleep before retrying
log.Printf("Retry %d for URL: %s\n", attempts+1, urlText)
time.Sleep(time.Duration(retrySleep) * time.Second)
}
}
}