mirror of
https://github.com/SagerNet/sing-geoip.git
synced 2025-12-16 14:23:17 +08:00
Add rule-set releases
This commit is contained in:
87
main.go
87
main.go
@@ -6,20 +6,22 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/sagernet/sing/common"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
"github.com/sagernet/sing/common/rw"
|
||||
|
||||
"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/sirupsen/logrus"
|
||||
"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
|
||||
@@ -46,7 +48,7 @@ func fetch(from string) (*github.RepositoryRelease, error) {
|
||||
}
|
||||
|
||||
func get(downloadURL *string) ([]byte, error) {
|
||||
logrus.Info("download ", *downloadURL)
|
||||
log.Info("download ", *downloadURL)
|
||||
response, err := http.Get(*downloadURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -160,38 +162,17 @@ func write(writer *mmdbwriter.Tree, dataMap map[string][]*net.IPNet, output stri
|
||||
return err
|
||||
}
|
||||
|
||||
func local(input string, output string, codes []string) error {
|
||||
binary, err := os.ReadFile(input)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
metadata, countryMap, err := parse(binary)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var writer *mmdbwriter.Tree
|
||||
if rw.FileExists(output) {
|
||||
writer, err = open(output, codes)
|
||||
} else {
|
||||
writer, err = newWriter(metadata, codes)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return write(writer, countryMap, output, codes)
|
||||
}
|
||||
|
||||
func release(source string, destination string) error {
|
||||
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 {
|
||||
logrus.Warn("missing destination latest release")
|
||||
log.Warn("missing destination latest release")
|
||||
} else {
|
||||
if os.Getenv("NO_SKIP") != "true" && strings.Contains(*destinationRelease.Name, *sourceRelease.Name) {
|
||||
logrus.Info("already latest")
|
||||
log.Info("already latest")
|
||||
setActionOutput("skip", "true")
|
||||
return nil
|
||||
}
|
||||
@@ -212,20 +193,41 @@ func release(source string, destination string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = write(writer, countryMap, "geoip.db", nil)
|
||||
err = write(writer, countryMap, output, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
writer, err = newWriter(metadata, []string{"cn"})
|
||||
|
||||
os.RemoveAll(ruleSetOutput)
|
||||
err = os.MkdirAll(ruleSetOutput, 0o755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = write(writer, countryMap, "geoip-cn.db", []string{"cn"})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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.Name)
|
||||
return nil
|
||||
@@ -236,13 +238,8 @@ func setActionOutput(name string, content string) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
if len(os.Args) >= 3 {
|
||||
err = local(os.Args[1], os.Args[2], os.Args[3:])
|
||||
} else {
|
||||
err = release("Dreamacro/maxmind-geoip", "sagernet/sing-geoip")
|
||||
}
|
||||
err := release("Dreamacro/maxmind-geoip", "sagernet/sing-geoip", "geoip.db", "rule-set")
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user