forked from shadowsocks/go-shadowsocks2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfwlist.go
74 lines (64 loc) · 1.47 KB
/
gfwlist.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
package main
import (
"bufio"
"encoding/base64"
"net/http"
"regexp"
"strings"
"time"
)
var sources = []string{
"https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt",
"https://bitbucket.org/gfwlist/gfwlist/raw/HEAD/gfwlist.txt",
"https://gitlab.com/gfwlist/gfwlist/raw/master/gfwlist.txt",
}
// FetchBlockedAddrs download blocked addrs from the gfwlist
func FetchBlockedAddrs() ([]string, error) {
timeout := time.Duration(5 * time.Second)
client := http.Client{
Timeout: timeout,
}
var err error
var resp *http.Response
for _, url := range sources {
if resp, err = client.Get(url); err == nil {
break
}
}
if err != nil {
return nil, err
}
defer resp.Body.Close()
decoder := base64.NewDecoder(base64.StdEncoding, resp.Body)
scanner := bufio.NewScanner(decoder)
res := []*regexp.Regexp{
regexp.MustCompile("^\\|+"),
regexp.MustCompile("https?://"),
regexp.MustCompile("^\\."),
regexp.MustCompile("^\\*.*?\\."),
}
scanner.Scan() // skip [AutoProxy 0.2.9]
var addrs = []string{}
for scanner.Scan() {
line := scanner.Text()
if len(line) == 0 || line[0] == '!' || line[0] == '@' {
continue
}
if strings.IndexByte(line, '/') > 0 {
continue
}
if strings.IndexByte(line, '*') > 0 {
continue
}
for _, re := range res {
line = re.ReplaceAllString(line, "")
}
if strings.IndexByte(line, '.') > 0 {
addrs = append(addrs, line)
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return addrs, nil
}