forked from Johnshall/sing-geosite
-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
221 lines (209 loc) · 6.15 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"context"
"crypto/sha256"
"encoding/hex"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/google/go-github/v45/github"
"github.com/sagernet/sing-box/common/geosite"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sirupsen/logrus"
"github.com/v2fly/v2ray-core/v5/app/router/routercommon"
"google.golang.org/protobuf/proto"
)
var githubClient *github.Client
func init() {
accessToken, loaded := os.LookupEnv("ACCESS_TOKEN")
if !loaded {
githubClient = github.NewClient(nil)
return
}
transport := &github.BasicAuthTransport{
Username: accessToken,
}
githubClient = github.NewClient(transport.Client())
}
func fetch(from string) (*github.RepositoryRelease, error) {
names := strings.SplitN(from, "/", 2)
latestRelease, _, err := githubClient.Repositories.GetLatestRelease(context.Background(), names[0], names[1])
if err != nil {
return nil, err
}
return latestRelease, err
}
func get(downloadURL *string) ([]byte, error) {
logrus.Info("download ", *downloadURL)
response, err := http.Get(*downloadURL)
if err != nil {
return nil, err
}
defer response.Body.Close()
return io.ReadAll(response.Body)
}
func download(release *github.RepositoryRelease) ([]byte, error) {
geositeAsset := common.Find(release.Assets, func(it *github.ReleaseAsset) bool {
return *it.Name == "geosite.dat"
})
geositeChecksumAsset := common.Find(release.Assets, func(it *github.ReleaseAsset) bool {
return *it.Name == "geosite.dat.sha256sum"
})
if geositeAsset == nil {
return nil, E.New("geosite asset not found in upstream release ", release.Name)
}
if geositeChecksumAsset == nil {
return nil, E.New("geosite asset not found in upstream release ", release.Name)
}
data, err := get(geositeAsset.BrowserDownloadURL)
if err != nil {
return nil, err
}
remoteChecksum, err := get(geositeChecksumAsset.BrowserDownloadURL)
if err != nil {
return nil, err
}
checksum := sha256.Sum256(data)
if hex.EncodeToString(checksum[:]) != string(remoteChecksum[:64]) {
return nil, E.New("checksum mismatch")
}
return data, nil
}
func parse(vGeositeData []byte) (map[string][]geosite.Item, error) {
vGeositeList := routercommon.GeoSiteList{}
err := proto.Unmarshal(vGeositeData, &vGeositeList)
if err != nil {
return nil, err
}
domainMap := make(map[string][]geosite.Item)
for _, vGeositeEntry := range vGeositeList.Entry {
code := strings.ToLower(vGeositeEntry.CountryCode)
domains := make([]geosite.Item, 0, len(vGeositeEntry.Domain)*2)
attributes := make(map[string][]*routercommon.Domain)
for _, domain := range vGeositeEntry.Domain {
if len(domain.Attribute) > 0 {
for _, attribute := range domain.Attribute {
attributes[attribute.Key] = append(attributes[attribute.Key], domain)
}
}
switch domain.Type {
case routercommon.Domain_Plain:
domains = append(domains, geosite.Item{
Type: geosite.RuleTypeDomainKeyword,
Value: domain.Value,
})
case routercommon.Domain_Regex:
domains = append(domains, geosite.Item{
Type: geosite.RuleTypeDomainRegex,
Value: domain.Value,
})
case routercommon.Domain_RootDomain:
if strings.Contains(domain.Value, ".") {
domains = append(domains, geosite.Item{
Type: geosite.RuleTypeDomain,
Value: domain.Value,
})
}
domains = append(domains, geosite.Item{
Type: geosite.RuleTypeDomainSuffix,
Value: "." + domain.Value,
})
case routercommon.Domain_Full:
domains = append(domains, geosite.Item{
Type: geosite.RuleTypeDomain,
Value: domain.Value,
})
}
}
domainMap[code] = common.Uniq(domains)
for attribute, attributeEntries := range attributes {
attributeDomains := make([]geosite.Item, 0, len(attributeEntries)*2)
for _, domain := range attributeEntries {
switch domain.Type {
case routercommon.Domain_Plain:
attributeDomains = append(attributeDomains, geosite.Item{
Type: geosite.RuleTypeDomainKeyword,
Value: domain.Value,
})
case routercommon.Domain_Regex:
attributeDomains = append(attributeDomains, geosite.Item{
Type: geosite.RuleTypeDomainRegex,
Value: domain.Value,
})
case routercommon.Domain_RootDomain:
if strings.Contains(domain.Value, ".") {
attributeDomains = append(attributeDomains, geosite.Item{
Type: geosite.RuleTypeDomain,
Value: domain.Value,
})
}
attributeDomains = append(attributeDomains, geosite.Item{
Type: geosite.RuleTypeDomainSuffix,
Value: "." + domain.Value,
})
case routercommon.Domain_Full:
attributeDomains = append(attributeDomains, geosite.Item{
Type: geosite.RuleTypeDomain,
Value: domain.Value,
})
}
}
domainMap[code+"@"+attribute] = common.Uniq(attributeDomains)
}
}
return domainMap, nil
}
func generate(release *github.RepositoryRelease, output string) error {
outputFile, err := os.Create(output)
if err != nil {
return err
}
defer outputFile.Close()
vData, err := download(release)
if err != nil {
return err
}
domainMap, err := parse(vData)
if err != nil {
return err
}
outputPath, _ := filepath.Abs(output)
os.Stderr.WriteString("write " + outputPath + "\n")
return geosite.Write(outputFile, domainMap)
}
func setActionOutput(name string, content string) {
os.Stdout.WriteString("::set-output name=" + name + "::" + content + "\n")
}
func release(source string, destination string, output string) error {
sourceRelease, err := fetch(source)
if err != nil {
return err
}
destinationRelease, err := fetch(destination)
if err != nil {
logrus.Warn("missing destination latest release")
} else {
if os.Getenv("NO_SKIP") != "true" && strings.Contains(*destinationRelease.Name, *sourceRelease.Name) {
logrus.Info("already latest")
setActionOutput("skip", "true")
return nil
}
}
err = generate(sourceRelease, output)
if err != nil {
return err
}
// Remove "Released on" from Loyalsoldier/v2ray-rules-dat
tagName := *sourceRelease.Name
setActionOutput("tag", tagName[12:])
return nil
}
func main() {
err := release("Loyalsoldier/v2ray-rules-dat", "soffchen/sing-geosite", "geosite.db")
if err != nil {
logrus.Fatal(err)
}
}