mirror of
https://github.com/bestnite/sub2clash.git
synced 2025-07-03 11:32:35 +08:00
update
This commit is contained in:
16
utils/os.go
Normal file
16
utils/os.go
Normal file
@ -0,0 +1,16 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func MKDir(dir string) error {
|
||||
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||
err := os.MkdirAll(dir, os.ModePerm)
|
||||
if err != nil {
|
||||
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
@ -34,7 +34,7 @@ var skipGroups = map[string]bool{
|
||||
"应用进化": true,
|
||||
}
|
||||
|
||||
func AddProxy(sub *model.Subscription, proxies ...model.Proxy) {
|
||||
func AddProxy(sub *model.Subscription, autotest bool, lazy bool, proxies ...model.Proxy) {
|
||||
newCountryGroupNames := make([]string, 0)
|
||||
|
||||
for _, proxy := range proxies {
|
||||
@ -57,11 +57,25 @@ func AddProxy(sub *model.Subscription, proxies ...model.Proxy) {
|
||||
}
|
||||
|
||||
if !haveProxyGroup {
|
||||
newGroup := model.ProxyGroup{
|
||||
Name: countryName,
|
||||
Type: "select",
|
||||
Proxies: []string{proxy.Name},
|
||||
IsCountryGrop: true,
|
||||
var newGroup model.ProxyGroup
|
||||
if !autotest {
|
||||
newGroup = model.ProxyGroup{
|
||||
Name: countryName,
|
||||
Type: "select",
|
||||
Proxies: []string{proxy.Name},
|
||||
IsCountryGrop: true,
|
||||
}
|
||||
} else {
|
||||
newGroup = model.ProxyGroup{
|
||||
Name: countryName,
|
||||
Type: "url-test",
|
||||
Proxies: []string{proxy.Name},
|
||||
IsCountryGrop: true,
|
||||
Url: "http://www.gstatic.com/generate_204",
|
||||
Interval: 300,
|
||||
Tolerance: 50,
|
||||
Lazy: lazy,
|
||||
}
|
||||
}
|
||||
sub.ProxyGroups = append(sub.ProxyGroups, newGroup)
|
||||
newCountryGroupNames = append(newCountryGroupNames, countryName)
|
||||
|
@ -2,53 +2,49 @@ package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gopkg.in/yaml.v3"
|
||||
"io"
|
||||
"strings"
|
||||
"sub2clash/model"
|
||||
)
|
||||
|
||||
func AddRulesByUrl(sub *model.Subscription, url string, proxy string) {
|
||||
get, err := Get(url)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
err := Body.Close()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}(get.Body)
|
||||
bytes, err := io.ReadAll(get.Body)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
var payload model.Payload
|
||||
err = yaml.Unmarshal(bytes, &payload)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
for i := range payload.Rules {
|
||||
payload.Rules[i] = payload.Rules[i] + "," + proxy
|
||||
}
|
||||
AddRules(sub, payload.Rules...)
|
||||
}
|
||||
|
||||
func AddRuleProvider(
|
||||
sub *model.Subscription, providerName string, proxy string, provider model.RuleProvider,
|
||||
func PrependRuleProvider(
|
||||
sub *model.Subscription, providerName string, group string, provider model.RuleProvider,
|
||||
) {
|
||||
if sub.RuleProviders == nil {
|
||||
sub.RuleProviders = make(map[string]model.RuleProvider)
|
||||
}
|
||||
sub.RuleProviders[providerName] = provider
|
||||
AddRules(
|
||||
PrependRules(
|
||||
sub,
|
||||
fmt.Sprintf("RULE-SET,%s,%s", providerName, proxy),
|
||||
fmt.Sprintf("RULE-SET,%s,%s", providerName, group),
|
||||
)
|
||||
}
|
||||
|
||||
func AddRules(sub *model.Subscription, rules ...string) {
|
||||
func AppenddRuleProvider(
|
||||
sub *model.Subscription, providerName string, group string, provider model.RuleProvider,
|
||||
) {
|
||||
if sub.RuleProviders == nil {
|
||||
sub.RuleProviders = make(map[string]model.RuleProvider)
|
||||
}
|
||||
sub.RuleProviders[providerName] = provider
|
||||
AppendRules(sub, fmt.Sprintf("RULE-SET,%s,%s", providerName, group))
|
||||
}
|
||||
|
||||
func PrependRules(sub *model.Subscription, rules ...string) {
|
||||
if sub.Rules == nil {
|
||||
sub.Rules = make([]string, 0)
|
||||
}
|
||||
sub.Rules = append(rules, sub.Rules...)
|
||||
}
|
||||
|
||||
func AppendRules(sub *model.Subscription, rules ...string) {
|
||||
if sub.Rules == nil {
|
||||
sub.Rules = make([]string, 0)
|
||||
}
|
||||
matchRule := sub.Rules[len(sub.Rules)-1]
|
||||
if strings.Contains(matchRule, "MATCH") {
|
||||
sub.Rules = append(sub.Rules[:len(sub.Rules)-1], rules...)
|
||||
sub.Rules = append(sub.Rules, matchRule)
|
||||
return
|
||||
}
|
||||
sub.Rules = append(sub.Rules, rules...)
|
||||
}
|
||||
|
18
utils/sub.go
18
utils/sub.go
@ -7,6 +7,7 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sub2clash/config"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@ -20,7 +21,6 @@ func LoadSubscription(url string, refresh bool) ([]byte, error) {
|
||||
}
|
||||
hash := md5.Sum([]byte(url))
|
||||
fileName := filepath.Join(subsDir, hex.EncodeToString(hash[:]))
|
||||
const refreshInterval = 500 * 60 // 5分钟
|
||||
stat, err := os.Stat(fileName)
|
||||
if err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
@ -29,16 +29,13 @@ func LoadSubscription(url string, refresh bool) ([]byte, error) {
|
||||
return FetchSubscriptionFromAPI(url)
|
||||
}
|
||||
lastGetTime := stat.ModTime().Unix() // 单位是秒
|
||||
if lastGetTime+refreshInterval > time.Now().Unix() {
|
||||
if lastGetTime+config.Default.CacheExpire > time.Now().Unix() {
|
||||
file, err := os.Open(fileName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func(file *os.File) {
|
||||
err := file.Close()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
_ = file.Close()
|
||||
}(file)
|
||||
fileLock.RLock()
|
||||
defer fileLock.RUnlock()
|
||||
@ -58,7 +55,9 @@ func FetchSubscriptionFromAPI(url string) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response body: %w", err)
|
||||
@ -68,10 +67,7 @@ func FetchSubscriptionFromAPI(url string) ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
defer func(file *os.File) {
|
||||
err := file.Close()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
_ = file.Close()
|
||||
}(file)
|
||||
fileLock.Lock()
|
||||
defer fileLock.Unlock()
|
||||
|
@ -2,7 +2,6 @@ package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@ -10,27 +9,24 @@ import (
|
||||
|
||||
// LoadTemplate 加载模板
|
||||
// template 模板文件名
|
||||
func LoadTemplate(template string) (string, error) {
|
||||
func LoadTemplate(template string) ([]byte, error) {
|
||||
tPath := filepath.Join("templates", template)
|
||||
if _, err := os.Stat(tPath); err == nil {
|
||||
file, err := os.Open(tPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
defer func(file *os.File) {
|
||||
err := file.Close()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
_ = file.Close()
|
||||
}(file)
|
||||
result, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
return string(result), nil
|
||||
return result, nil
|
||||
}
|
||||
return "", errors.New("模板文件不存在")
|
||||
return nil, errors.New("模板文件不存在")
|
||||
}
|
||||
|
Reference in New Issue
Block a user