2024-04-23 02:47:53 -04:00
|
|
|
package common
|
2023-09-12 06:40:24 -04:00
|
|
|
|
|
|
|
import (
|
2023-09-17 03:52:37 -04:00
|
|
|
"crypto/sha256"
|
2023-09-12 06:40:24 -04:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-04-24 00:51:37 -04:00
|
|
|
"net/http"
|
2023-09-12 06:40:24 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-09-12 12:46:17 -04:00
|
|
|
"sync"
|
2023-09-12 06:40:24 -04:00
|
|
|
"time"
|
2024-09-17 01:10:13 -04:00
|
|
|
|
|
|
|
"github.com/nitezs/sub2clash/config"
|
2023-09-12 06:40:24 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var subsDir = "subs"
|
2023-09-12 12:46:17 -04:00
|
|
|
var fileLock sync.RWMutex
|
2023-09-12 06:40:24 -04:00
|
|
|
|
2024-07-15 09:01:22 -04:00
|
|
|
func LoadSubscription(url string, refresh bool, userAgent string) ([]byte, error) {
|
2023-09-12 06:40:24 -04:00
|
|
|
if refresh {
|
2024-07-15 09:01:22 -04:00
|
|
|
return FetchSubscriptionFromAPI(url, userAgent)
|
2023-09-12 06:40:24 -04:00
|
|
|
}
|
2023-09-17 03:52:37 -04:00
|
|
|
hash := sha256.Sum224([]byte(url))
|
2023-09-12 12:46:17 -04:00
|
|
|
fileName := filepath.Join(subsDir, hex.EncodeToString(hash[:]))
|
2023-09-12 06:40:24 -04:00
|
|
|
stat, err := os.Stat(fileName)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-15 09:01:22 -04:00
|
|
|
return FetchSubscriptionFromAPI(url, userAgent)
|
2023-09-12 06:40:24 -04:00
|
|
|
}
|
2024-08-11 11:55:47 -04:00
|
|
|
lastGetTime := stat.ModTime().Unix()
|
2023-09-13 01:47:22 -04:00
|
|
|
if lastGetTime+config.Default.CacheExpire > time.Now().Unix() {
|
2023-09-12 06:40:24 -04:00
|
|
|
file, err := os.Open(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func(file *os.File) {
|
2023-09-22 13:31:04 -04:00
|
|
|
if file != nil {
|
|
|
|
_ = file.Close()
|
|
|
|
}
|
2023-09-12 06:40:24 -04:00
|
|
|
}(file)
|
2023-09-12 12:46:17 -04:00
|
|
|
fileLock.RLock()
|
|
|
|
defer fileLock.RUnlock()
|
2023-09-12 06:40:24 -04:00
|
|
|
subContent, err := io.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return subContent, nil
|
|
|
|
}
|
2024-07-15 09:01:22 -04:00
|
|
|
return FetchSubscriptionFromAPI(url, userAgent)
|
2023-09-12 06:40:24 -04:00
|
|
|
}
|
|
|
|
|
2024-07-15 09:01:22 -04:00
|
|
|
func FetchSubscriptionFromAPI(url string, userAgent string) ([]byte, error) {
|
2023-09-17 03:52:37 -04:00
|
|
|
hash := sha256.Sum224([]byte(url))
|
2023-09-12 12:46:17 -04:00
|
|
|
fileName := filepath.Join(subsDir, hex.EncodeToString(hash[:]))
|
2024-07-15 09:01:22 -04:00
|
|
|
resp, err := Get(url, WithUserAgent(userAgent))
|
2023-09-12 06:40:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-24 00:51:37 -04:00
|
|
|
defer func(resp *http.Response) {
|
|
|
|
if resp != nil && resp.Body != nil {
|
|
|
|
_ = resp.Body.Close()
|
2023-09-22 13:31:04 -04:00
|
|
|
}
|
2024-04-24 00:51:37 -04:00
|
|
|
}(resp)
|
2023-09-12 06:40:24 -04:00
|
|
|
data, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read response body: %w", err)
|
|
|
|
}
|
|
|
|
file, err := os.Create(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func(file *os.File) {
|
2023-09-22 13:31:04 -04:00
|
|
|
if file != nil {
|
|
|
|
_ = file.Close()
|
|
|
|
}
|
2023-09-12 06:40:24 -04:00
|
|
|
}(file)
|
2023-09-12 12:46:17 -04:00
|
|
|
fileLock.Lock()
|
|
|
|
defer fileLock.Unlock()
|
2023-09-12 06:40:24 -04:00
|
|
|
_, err = file.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to write to sub.yaml: %w", err)
|
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|