-
Notifications
You must be signed in to change notification settings - Fork 12
/
antizapret.go
108 lines (91 loc) · 2.66 KB
/
antizapret.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
package geosite_antizapret
import (
"bufio"
"fmt"
"log"
"net"
"net/netip"
"regexp"
"strings"
)
const (
AntizapretPACGeneratorLightUpstreamBaseURL = "https://bitbucket.org/anticensority/antizapret-pac-generator-light/raw/master/config/"
ExcludeHostsByIPsDist = "exclude-hosts-by-ips-dist.txt"
ExcludeHostsDist = "exclude-hosts-dist.txt"
ExcludeIPsDist = "exclude-ips-dist.txt"
ExcludeRegexpDist = "exclude-regexp-dist.awk"
IncludeHostsDist = "include-hosts-dist.txt"
IncludeIPsDist = "include-ips-dist.txt"
)
type AntizapretConfigType string
const (
IPs AntizapretConfigType = "ips"
Hosts AntizapretConfigType = "hosts"
HostsByIPs AntizapretConfigType = "hosts_by_ips"
Regexp AntizapretConfigType = "regexp"
)
type AntizapretConfig struct {
Type AntizapretConfigType
Exclude bool
URL string
}
type Configs struct {
ExcludeHosts []string
ExcludeIPs []netip.Addr
ExcludeRegexp []*regexp.Regexp
IncludeHosts []string
IncludeIPs []*net.IPNet
}
func (g *Generator) fetchAntizapretConfigs() (*Configs, error) {
cfgs := []AntizapretConfig{
{HostsByIPs, true, AntizapretPACGeneratorLightUpstreamBaseURL + ExcludeHostsByIPsDist},
{Hosts, true, AntizapretPACGeneratorLightUpstreamBaseURL + ExcludeHostsDist},
{Regexp, true, AntizapretPACGeneratorLightUpstreamBaseURL + ExcludeRegexpDist},
{Hosts, false, AntizapretPACGeneratorLightUpstreamBaseURL + IncludeHostsDist},
}
configs := &Configs{}
for _, cfg := range cfgs {
resp, err := g.httpClient.Get(cfg.URL)
if err != nil {
return nil, fmt.Errorf("cannot get antizapret exclude config: %w", err)
}
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
switch cfg.Type {
case Hosts:
if cfg.Exclude {
configs.ExcludeHosts = append(configs.ExcludeHosts, scanner.Text())
} else {
configs.IncludeHosts = append(configs.IncludeHosts, scanner.Text())
}
case HostsByIPs:
if cfg.Exclude {
ipStr := scanner.Text()
ipStr = strings.ReplaceAll(ipStr, "\\", "")
ipStr = strings.Replace(ipStr, "^", "", 1)
ipStr = strings.Replace(ipStr, ";", "", 1)
addr, err := netip.ParseAddr(ipStr)
if err != nil {
log.Println(err)
continue
}
configs.ExcludeIPs = append(configs.ExcludeIPs, addr)
}
case Regexp:
if cfg.Exclude {
rxStr := scanner.Text()
rxStr = strings.Replace(rxStr, "/) {next}", "", 1)
rxStr = strings.Replace(rxStr, "(/", "", 1)
rx, err := regexp.Compile(rxStr)
if err != nil {
log.Println(err)
continue
}
configs.ExcludeRegexp = append(configs.ExcludeRegexp, rx)
}
}
}
}
return configs, nil
}