forked from SagerNet/sing-geoip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
255 lines (236 loc) · 6.45 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"context"
"io"
"net"
"net/http"
"os"
"path/filepath"
"sort"
"strings"
"github.com/google/go-github/v45/github"
"github.com/maxmind/mmdbwriter"
"github.com/maxmind/mmdbwriter/inserter"
"github.com/maxmind/mmdbwriter/mmdbtype"
"github.com/oschwald/geoip2-golang"
"github.com/oschwald/maxminddb-golang"
"github.com/sagernet/sing-box/common/srs"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
)
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) {
fixedRelease := os.Getenv("FIXED_RELEASE")
names := strings.SplitN(from, "/", 2)
if fixedRelease != "" {
latestRelease, _, err := githubClient.Repositories.GetReleaseByTag(context.Background(), names[0], names[1], fixedRelease)
if err != nil {
return nil, err
}
return latestRelease, err
} else {
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) {
log.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) {
geoipAsset := common.Find(release.Assets, func(it *github.ReleaseAsset) bool {
return *it.Name == "Country.mmdb"
})
if geoipAsset == nil {
return nil, E.New("Country.mmdb not found in upstream release ", release.GetTagName())
}
return get(geoipAsset.BrowserDownloadURL)
}
func parse(binary []byte) (metadata maxminddb.Metadata, countryMap map[string][]*net.IPNet, err error) {
database, err := maxminddb.FromBytes(binary)
if err != nil {
return
}
metadata = database.Metadata
networks := database.Networks(maxminddb.SkipAliasedNetworks)
countryMap = make(map[string][]*net.IPNet)
var country geoip2.Enterprise
var ipNet *net.IPNet
for networks.Next() {
ipNet, err = networks.Network(&country)
if err != nil {
return
}
// idk why
code := strings.ToLower(country.Country.IsoCode)
countryMap[code] = append(countryMap[code], ipNet)
}
err = networks.Err()
return
}
func newWriter(metadata maxminddb.Metadata, codes []string) (*mmdbwriter.Tree, error) {
return mmdbwriter.New(mmdbwriter.Options{
DatabaseType: "sing-geoip",
Languages: codes,
IPVersion: int(metadata.IPVersion),
RecordSize: int(metadata.RecordSize),
Inserter: inserter.ReplaceWith,
DisableIPv4Aliasing: true,
IncludeReservedNetworks: true,
})
}
func open(path string, codes []string) (*mmdbwriter.Tree, error) {
reader, err := maxminddb.Open(path)
if err != nil {
return nil, err
}
if reader.Metadata.DatabaseType != "sing-geoip" {
return nil, E.New("invalid sing-geoip database")
}
reader.Close()
return mmdbwriter.Load(path, mmdbwriter.Options{
Languages: append(reader.Metadata.Languages, common.Filter(codes, func(it string) bool {
return !common.Contains(reader.Metadata.Languages, it)
})...),
Inserter: inserter.ReplaceWith,
})
}
func write(writer *mmdbwriter.Tree, dataMap map[string][]*net.IPNet, output string, codes []string) error {
if len(codes) == 0 {
codes = make([]string, 0, len(dataMap))
for code := range dataMap {
codes = append(codes, code)
}
}
sort.Strings(codes)
codeMap := make(map[string]bool)
for _, code := range codes {
codeMap[code] = true
}
for code, data := range dataMap {
if !codeMap[code] {
continue
}
for _, item := range data {
err := writer.Insert(item, mmdbtype.String(code))
if err != nil {
return err
}
}
}
outputFile, err := os.Create(output)
if err != nil {
return err
}
defer outputFile.Close()
_, err = writer.WriteTo(outputFile)
return err
}
func release(source string, destination string, output string, ruleSetOutput string) error {
sourceRelease, err := fetch(source)
if err != nil {
return err
}
destinationRelease, err := fetch(destination)
if err != nil {
log.Warn("missing destination latest release")
} else {
if os.Getenv("NO_SKIP") != "true" && strings.Contains(destinationRelease.GetTagName(), sourceRelease.GetTagName()) {
log.Info("already latest")
setActionOutput("skip", "true")
return nil
}
}
binary, err := download(sourceRelease)
if err != nil {
return err
}
metadata, countryMap, err := parse(binary)
if err != nil {
return err
}
allCodes := make([]string, 0, len(countryMap))
for code := range countryMap {
allCodes = append(allCodes, code)
}
writer, err := newWriter(metadata, allCodes)
if err != nil {
return err
}
err = write(writer, countryMap, output, nil)
if err != nil {
return err
}
writer, err = newWriter(metadata, []string{"cn"})
if err != nil {
return err
}
err = write(writer, countryMap, "geoip-cn.db", []string{"cn"})
if err != nil {
return err
}
os.RemoveAll(ruleSetOutput)
err = os.MkdirAll(ruleSetOutput, 0o755)
if err != nil {
return err
}
for countryCode, ipNets := range countryMap {
var headlessRule option.DefaultHeadlessRule
headlessRule.IPCIDR = make([]string, 0, len(ipNets))
for _, cidr := range ipNets {
headlessRule.IPCIDR = append(headlessRule.IPCIDR, cidr.String())
}
var plainRuleSet option.PlainRuleSet
plainRuleSet.Rules = []option.HeadlessRule{
{
Type: C.RuleTypeDefault,
DefaultOptions: headlessRule,
},
}
srsPath, _ := filepath.Abs(filepath.Join(ruleSetOutput, "geoip-"+countryCode+".srs"))
os.Stderr.WriteString("write " + srsPath + "\n")
outputRuleSet, err := os.Create(srsPath)
if err != nil {
return err
}
err = srs.Write(outputRuleSet, plainRuleSet)
if err != nil {
outputRuleSet.Close()
return err
}
outputRuleSet.Close()
}
setActionOutput("tag", sourceRelease.GetTagName())
return nil
}
func setActionOutput(name string, content string) {
os.Stdout.WriteString("::set-output name=" + name + "::" + content + "\n")
}
func main() {
err := release("Loyalsoldier/geoip", "sunls2/sing-geoip", "geoip.db", "rule-set")
if err != nil {
log.Fatal(err)
}
}