[Fix]GeoSite.dat initial in logic rule

This commit is contained in:
Clash-Mini
2022-02-06 04:41:34 +08:00
parent 7465eaafa1
commit ca8ed0a01b
3 changed files with 36 additions and 5 deletions

View File

@ -3,11 +3,13 @@ package logic
import (
"fmt"
"github.com/Dreamacro/clash/common/collections"
"github.com/Dreamacro/clash/config"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
RC "github.com/Dreamacro/clash/rule/common"
"github.com/Dreamacro/clash/rule/provider"
"io"
"net/http"
"os"
"regexp"
"strings"
)
@ -55,7 +57,7 @@ func payloadToRule(subPayload string) (C.Rule, error) {
return parseRule(tp, payload, nil)
}
if tp == "GEOSITE" {
if err := config.InitGeoSite(); err != nil {
if err := initGeoSite(); err != nil {
log.Errorln("can't initial GeoSite: %w", err)
}
}
@ -173,3 +175,32 @@ func findSubRuleRange(payload string, ruleRanges []Range) []Range {
return subRuleRange
}
func downloadGeoSite(path string) (err error) {
resp, err := http.Get("https://cdn.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/geosite.dat")
if err != nil {
return
}
defer resp.Body.Close()
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, resp.Body)
return err
}
func initGeoSite() error {
if _, err := os.Stat(C.Path.GeoSite()); os.IsNotExist(err) {
log.Infoln("Need GeoSite but can't find GeoSite.dat, start download")
if err := downloadGeoSite(C.Path.GeoSite()); err != nil {
return fmt.Errorf("can't download GeoSite.dat: %s", err.Error())
}
log.Infoln("Download GeoSite.dat finish")
}
return nil
}