-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlFinder.go
124 lines (104 loc) · 2.49 KB
/
urlFinder.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
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"bufio"
"crypto/tls"
"flag"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
"github.com/schollz/progressbar/v3"
)
func checkURL(url string, results chan string, sem chan struct{}, bar *progressbar.ProgressBar, timeout time.Duration) {
defer func() {
<-sem
}()
client := http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Timeout: timeout * time.Millisecond,
}
resp, err := client.Get(url)
if err != nil {
if strings.Contains(err.Error(), url+"/") {
results <- fmt.Sprintf("%s,301", url)
}
results <- fmt.Sprintf("%s,无响应", url)
bar.Add(1)
return
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
fmt.Println(err)
}
}(resp.Body)
results <- fmt.Sprintf("%s,%d", url, resp.StatusCode)
bar.Add(1)
}
func worker(jobs <-chan string, results chan string, sem chan struct{}, bar *progressbar.ProgressBar, timeout time.Duration) {
for url := range jobs {
sem <- struct{}{}
go checkURL(url, results, sem, bar, timeout)
}
}
func main() {
maxWorkersPtr := flag.Int("n", 100, "最大工作线程数")
timeoutPtr := flag.Int("t", 1000, "超时时间(毫秒)")
urlsFilePtr := flag.String("f", "urls.txt", "包含URL的文件")
flag.Parse()
urls := readURLsFromFile(*urlsFilePtr)
jobs := make(chan string, len(urls))
results := make(chan string, len(urls))
var sem = make(chan struct{}, *maxWorkersPtr)
bar := progressbar.Default(int64(len(urls)))
for i := 0; i < *maxWorkersPtr; i++ {
go worker(jobs, results, sem, bar, time.Duration(*timeoutPtr))
}
for _, url := range urls {
jobs <- url
}
close(jobs)
file, err := os.Create("results.csv")
if err != nil {
fmt.Println("创建文件时出错:", err)
return
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
fmt.Println(err)
}
}(file)
for range urls {
res := <-results
_, _ = file.WriteString(res + "\n")
}
fmt.Println("所有任务已完成!")
}
func readURLsFromFile(filename string) []string {
var urls []string
file, err := os.Open(filename)
if err != nil {
fmt.Println("打开文件时出错:", err)
return urls
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
fmt.Println(err)
// 处理文件关闭错误
}
}(file)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
urls = append(urls, strings.TrimSpace(scanner.Text()))
}
if err := scanner.Err(); err != nil {
fmt.Println("读取文件时出错:", err)
}
return urls
}