️ Improve

This commit is contained in:
2024-08-11 23:55:47 +08:00
parent dedbf2bc03
commit 3cfa4bdf24
24 changed files with 204 additions and 304 deletions

View File

@ -13,16 +13,14 @@ import (
var DB *bbolt.DB
func ConnectDB() error {
// 确保数据目录存在
path := filepath.Join("data", "sub2clash.db")
// 打开或创建数据库文件
db, err := bbolt.Open(path, 0600, nil)
if err != nil {
return err
}
DB = db
// 确保存储桶存在
return db.Update(func(tx *bbolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte("ShortLinks"))
return err

View File

@ -22,7 +22,7 @@ func WithUserAgent(userAgent string) GetOption {
func Get(url string, options ...GetOption) (resp *http.Response, err error) {
retryTimes := config.Default.RequestRetryTimes
haveTried := 0
retryDelay := time.Second // 延迟1秒再重试
retryDelay := time.Second
getConfig := GetConfig{}
for _, option := range options {
option(&getConfig)
@ -45,7 +45,7 @@ func Get(url string, options ...GetOption) (resp *http.Response, err error) {
time.Sleep(retryDelay)
continue
} else {
// 如果文件大小大于设定,直接返回错误
if get != nil && get.ContentLength > config.Default.RequestMaxFileSize {
return nil, errors.New("文件过大")
}

View File

@ -11,7 +11,7 @@ import (
)
func GetContryName(countryKey string) string {
// 创建一个切片包含所有的国家映射
countryMaps := []map[string]string{
model.CountryFlag,
model.CountryChineseName,
@ -19,11 +19,9 @@ func GetContryName(countryKey string) string {
model.CountryEnglishName,
}
// 对每一个映射进行检查
for i, countryMap := range countryMaps {
if i == 2 {
// 对ISO匹配做特殊处理
// 根据常用分割字符分割字符串
splitChars := []string{"-", "_", " "}
key := make([]string, 0)
for _, splitChar := range splitChars {
@ -34,9 +32,9 @@ func GetContryName(countryKey string) string {
}
}
}
// 对每一个分割后的字符串进行检查
for _, v := range key {
// 如果匹配到了国家
if country, ok := countryMap[strings.ToUpper(v)]; ok {
return country
}
@ -56,7 +54,7 @@ func AddProxy(
lazy bool, clashType model.ClashType, proxies ...model.Proxy,
) {
proxyTypes := model.GetSupportProxyTypes(clashType)
// 添加节点
for _, proxy := range proxies {
if !proxyTypes[proxy.Type] {
continue
@ -106,7 +104,7 @@ func ParseProxy(proxies ...string) []model.Proxy {
if proxy != "" {
var proxyItem model.Proxy
var err error
// 解析节点
if strings.HasPrefix(proxy, constant.ShadowsocksPrefix) {
proxyItem, err = parser.ParseShadowsocks(proxy)
}

View File

@ -3,7 +3,7 @@ package common
import "math/rand"
func RandomString(length int) string {
// 生成随机字符串
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var result []byte
for i := 0; i < length; i++ {

View File

@ -29,7 +29,7 @@ func LoadSubscription(url string, refresh bool, userAgent string) ([]byte, error
}
return FetchSubscriptionFromAPI(url, userAgent)
}
lastGetTime := stat.ModTime().Unix() // 单位是秒
lastGetTime := stat.ModTime().Unix()
if lastGetTime+config.Default.CacheExpire > time.Now().Unix() {
file, err := os.Open(fileName)
if err != nil {

View File

@ -7,8 +7,6 @@ import (
"path/filepath"
)
// LoadTemplate 加载模板
// templates 模板文件名
func LoadTemplate(template string) ([]byte, error) {
tPath := filepath.Join("templates", template)
if _, err := os.Stat(tPath); err == nil {