-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.go
84 lines (70 loc) · 1.49 KB
/
check.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
package main
import (
"fmt"
"net/http"
"net/url"
"os"
"time"
)
func ControlRestart() bool {
for {
if restart == false {
continue
} else {
if os.Getenv("DEBUG") == "true" {
fmt.Println("1")
}
return true
}
}
}
func Control() {
for {
sources := Select(dtbs)
for _, source := range sources {
go Check(source)
}
if ControlRestart() == true {
restart = false
continue
}
}
}
func Check(source Source) {
for {
var client *http.Client
if source.Proxy != "" {
proxyStr := source.Proxy
proxyURL, _ := url.Parse(proxyStr)
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}
client = &http.Client{
Transport: transport,
Timeout: 30 * time.Second,
}
} else {
transport := &http.Transport{}
client = &http.Client{
Transport: transport,
Timeout: 10 * time.Second,
}
}
request, err := http.NewRequest(source.Method, source.Host, nil)
if err != nil {
fmt.Println("Cannot do request: " + source.Host + " with proxy: " + source.Proxy)
source.LastCode = 0
Update(dtbs, source)
}
response, err := client.Do(request)
if err != nil {
fmt.Println("Cannot reach address: " + source.Host + " with proxy: " + source.Proxy)
source.LastCode = 0
Update(dtbs, source)
} else {
source.LastCode = response.StatusCode
Update(dtbs, source)
}
time.Sleep(time.Duration(source.Interval) * time.Second)
}
}