-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdomain-list.go
159 lines (141 loc) · 3.54 KB
/
domain-list.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
)
var (
proxiedDomains = make(map[string]struct{})
proxiedPatterns []string
blockedDomains = make(map[string]struct{})
blockedPatterns []string
)
func isPattern(s string) bool {
return strings.Contains(s, "*")
}
func checkPattern(pattern, str string) bool {
parts := strings.Split(pattern, "*")
if len(parts) == 1 {
return pattern == str
}
// Begin
if !strings.HasPrefix(str, parts[0]) {
return false
}
str = str[len(parts[0]):]
// Middle
for i := 1; i < len(parts)-1; i++ {
index := strings.Index(str, parts[i])
if index == -1 {
return false
}
str = str[index+len(parts[i]):]
}
// End
return strings.HasSuffix(str, parts[len(parts)-1])
}
func checkPatterns(str string, list []string) string {
for _, pattern := range list {
ok := checkPattern(pattern, str)
if ok {
return pattern
}
}
return ""
}
///////////////////////////////////////////////////////////////////////////////
// a.b.site.com -> site.com
// aboba.ru -> aboba.ru
// localhost -> localhost
// a.test.co.uk -> test.co.uk
// api.x.com -> x.com
// a.b.c.test.com -> test.com
func trimDomain(domain string) string {
parts := strings.Split(domain, ".")
if len(parts) < 3 {
return domain
}
lastTwo := parts[len(parts)-2:]
// Если предпоследняя часть короче 3 символов (например, "co.uk"), берем три последние части
if len(lastTwo[0]) <= 2 && !strings.HasSuffix(domain, ".x.com") {
if len(parts) >= 3 {
return strings.Join(parts[len(parts)-3:], ".")
}
return domain
}
return strings.Join(lastTwo, ".")
}
func readDomains(sources string, fn func(domain string)) {
for _, source := range strings.Split(sources, ";") {
source = strings.TrimSpace(source)
if source == "" {
continue
}
file, err := os.Open(source)
if err != nil {
log.Fatalf(red("Error")+" opening file %s: %v", source, err)
continue
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
domain := scanner.Text()
// Remove comment
if idx := strings.Index(domain, "#"); idx != -1 {
domain = domain[:idx]
}
domain = strings.TrimSpace(domain)
domain, _ = strings.CutPrefix(domain, "https-")
domain, _ = strings.CutPrefix(domain, "https.")
domain, _ = strings.CutPrefix(domain, "http-")
domain, _ = strings.CutPrefix(domain, "http.")
domain, _ = strings.CutPrefix(domain, "0.0.0.0 ")
domain, _ = strings.CutPrefix(domain, "127.0.0.1 ")
domain = strings.TrimSpace(domain)
domain = strings.ToLower(domain)
if domain == "" {
continue
}
fn(domain)
}
if err := scanner.Err(); err != nil {
log.Fatalf(red("Error")+" reading file %s: %v", source, err)
}
}
}
func addProxiedDomain(domain string) {
pattern := checkPatterns(domain, proxiedPatterns)
if pattern != "" {
if args.Verbose {
fmt.Printf("PROXY: %s == %s\n", pattern, domain)
}
return
}
if isPattern(domain) {
proxiedPatterns = append(proxiedPatterns, domain)
return
}
domain = trimDomain(domain)
if _, exists := proxiedDomains[domain]; !exists {
proxiedDomains[domain] = struct{}{}
}
}
func addBlockedDomain(domain string) {
pattern := checkPatterns(domain, blockedPatterns)
if pattern != "" {
if args.Verbose {
fmt.Printf("BLOCK: %s == %s\n", pattern, domain)
}
return
}
if isPattern(domain) {
blockedPatterns = append(blockedPatterns, domain)
println(domain)
return
}
if _, exists := blockedDomains[domain]; !exists {
blockedDomains[domain] = struct{}{}
}
}