-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.go
More file actions
66 lines (58 loc) · 1.46 KB
/
Copy pathcheck.go
File metadata and controls
66 lines (58 loc) · 1.46 KB
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
package proxycheck
import (
"fmt"
"net/url"
"time"
)
type CheckResult struct {
Protocols []string
Online bool
Err map[string]error
Speed time.Duration
}
func Check(proxyAddr string, judge Judge) (result CheckResult) {
result.Err = make(map[string]error)
ip, port, err := proxyAddrToIpPort(proxyAddr)
if err != nil {
result.Err[""] = fmt.Errorf("parse addr error: %v", err)
return
}
result.Err = make(map[string]error)
for _, protocol := range []string{"http", "https", "socks4", "socks5"} {
proxyURL, err := url.Parse(fmt.Sprintf("%s://%s:%d", protocol, ip, port))
if err != nil {
result.Err[protocol] = fmt.Errorf("could not parse proxy address as proxyURL: %v", err)
return
}
cpResult := checkProtocol(proxyURL, judge)
if cpResult.Online {
result.Protocols = append(result.Protocols, protocol)
result.Online = true
// @TODO average speed
if result.Speed < cpResult.Speed {
result.Speed = cpResult.Speed
}
} else {
result.Err[protocol] = cpResult.Err
}
}
return
}
type checkProtocolResult struct {
Online bool
Err error
Speed time.Duration
}
func checkProtocol(proxyURL *url.URL, judge Judge) (result checkProtocolResult) {
speedStart := time.Now().UTC()
if _, err := ProxyRequest(judge.TargetURL(), proxyURL, judge.Timeout()); err == nil {
result.Online = true
speed := time.Now().UTC().Sub(speedStart)
if result.Speed < speed {
result.Speed = speed
}
} else {
result.Err = err
}
return
}