forked from gosom/scrapemate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.go
57 lines (44 loc) · 888 Bytes
/
proxy.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
package scrapemate
import (
"fmt"
"net/url"
"strings"
)
// Proxy is a struct for proxy
type Proxy struct {
URL string
Username string
Password string
}
func NewProxy(u string) (Proxy, error) {
if !strings.Contains(u, "://") {
u = "socks5://" + u
}
pu, err := url.Parse(u)
if err != nil {
return Proxy{}, err
}
supportedSchemes := []string{"socks5", "http", "https"}
scheme := strings.ToLower(pu.Scheme)
var valid bool
for _, s := range supportedSchemes {
if s == scheme {
valid = true
break
}
}
if !valid {
return Proxy{}, fmt.Errorf("invalid proxy type: %s", scheme)
}
var username, password string
if pu.User != nil {
username = pu.User.Username()
password, _ = pu.User.Password()
}
cleanURL := fmt.Sprintf("%s://%s", scheme, pu.Host)
return Proxy{
URL: cleanURL,
Username: username,
Password: password,
}, nil
}