mirror of
https://github.com/SagerNet/sing-geosite.git
synced 2025-12-16 14:23:16 +08:00
Add rule-set releases
This commit is contained in:
62
main.go
62
main.go
@@ -10,11 +10,15 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/google/go-github/v45/github"
|
||||
"github.com/sagernet/sing-box/common/geosite"
|
||||
"github.com/sagernet/sing-box/common/srs"
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
"github.com/sagernet/sing/common"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/google/go-github/v45/github"
|
||||
"github.com/sagernet/sing-box/log"
|
||||
"github.com/v2fly/v2ray-core/v5/app/router/routercommon"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
@@ -43,7 +47,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
|
||||
@@ -165,7 +169,7 @@ func parse(vGeositeData []byte) (map[string][]geosite.Item, error) {
|
||||
return domainMap, nil
|
||||
}
|
||||
|
||||
func generate(release *github.RepositoryRelease, output string) error {
|
||||
func generate(release *github.RepositoryRelease, output string, ruleSetOutput string) error {
|
||||
outputFile, err := os.Create(output)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -181,29 +185,65 @@ func generate(release *github.RepositoryRelease, output string) error {
|
||||
}
|
||||
outputPath, _ := filepath.Abs(output)
|
||||
os.Stderr.WriteString("write " + outputPath + "\n")
|
||||
return geosite.Write(outputFile, domainMap)
|
||||
err = geosite.Write(outputFile, domainMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
os.RemoveAll(ruleSetOutput)
|
||||
err = os.MkdirAll(ruleSetOutput, 0o755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for code, domains := range domainMap {
|
||||
var headlessRule option.DefaultHeadlessRule
|
||||
defaultRule := geosite.Compile(domains)
|
||||
headlessRule.Domain = defaultRule.Domain
|
||||
headlessRule.DomainSuffix = defaultRule.DomainSuffix
|
||||
headlessRule.DomainKeyword = defaultRule.DomainKeyword
|
||||
headlessRule.DomainRegex = defaultRule.DomainRegex
|
||||
var plainRuleSet option.PlainRuleSet
|
||||
plainRuleSet.Rules = []option.HeadlessRule{
|
||||
{
|
||||
Type: C.RuleTypeDefault,
|
||||
DefaultOptions: headlessRule,
|
||||
},
|
||||
}
|
||||
srsPath, _ := filepath.Abs(filepath.Join(ruleSetOutput, "geosite-"+code+".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()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func setActionOutput(name string, content string) {
|
||||
os.Stdout.WriteString("::set-output name=" + name + "::" + content + "\n")
|
||||
}
|
||||
|
||||
func release(source string, destination string, output 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
|
||||
}
|
||||
}
|
||||
err = generate(sourceRelease, output)
|
||||
err = generate(sourceRelease, output, ruleSetOutput)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -212,8 +252,8 @@ func release(source string, destination string, output string) error {
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := release("v2fly/domain-list-community", "sagernet/sing-geosite", "geosite.db")
|
||||
err := release("v2fly/domain-list-community", "sagernet/sing-geosite", "geosite.db", "rule-set")
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user