mirror of
https://github.com/nitezs/sub2clash.git
synced 2024-12-23 22:14:41 -05:00
🔧 customize HTTP user-agent for fetching subscription from API.
This commit is contained in:
parent
0946412ea7
commit
fd22cd1499
@ -32,7 +32,7 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
|
|||||||
template = query.Template
|
template = query.Template
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(template, "http") {
|
if strings.HasPrefix(template, "http") {
|
||||||
templateBytes, err = common.LoadSubscription(template, query.Refresh)
|
templateBytes, err = common.LoadSubscription(template, query.Refresh, query.UserAgent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Logger.Debug(
|
logger.Logger.Debug(
|
||||||
"load template failed", zap.String("template", template), zap.Error(err),
|
"load template failed", zap.String("template", template), zap.Error(err),
|
||||||
@ -61,7 +61,7 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
|
|||||||
var proxyList []model.Proxy
|
var proxyList []model.Proxy
|
||||||
// 加载订阅
|
// 加载订阅
|
||||||
for i := range query.Subs {
|
for i := range query.Subs {
|
||||||
data, err := common.LoadSubscription(query.Subs[i], query.Refresh)
|
data, err := common.LoadSubscription(query.Subs[i], query.Refresh, query.UserAgent)
|
||||||
subName := ""
|
subName := ""
|
||||||
if strings.Contains(query.Subs[i], "#") {
|
if strings.Contains(query.Subs[i], "#") {
|
||||||
subName = query.Subs[i][strings.LastIndex(query.Subs[i], "#")+1:]
|
subName = query.Subs[i][strings.LastIndex(query.Subs[i], "#")+1:]
|
||||||
|
@ -75,6 +75,12 @@
|
|||||||
<label for="proxy">节点分享链接:</label>
|
<label for="proxy">节点分享链接:</label>
|
||||||
<textarea class="form-control" id="proxy" name="proxy" placeholder="每行输入一个节点分享链接" rows="5"></textarea>
|
<textarea class="form-control" id="proxy" name="proxy" placeholder="每行输入一个节点分享链接" rows="5"></textarea>
|
||||||
</div>
|
</div>
|
||||||
|
<!-- User Agent -->
|
||||||
|
<div class="form-group mb-3">
|
||||||
|
<label for="user-agent">ua标识:</label>
|
||||||
|
<textarea class="form-control" id="user-agent" name="user-agent"
|
||||||
|
placeholder="用于获取订阅的http请求中的user-agent标识(可选)" rows="1"></textarea>
|
||||||
|
</div>
|
||||||
<!-- Refresh -->
|
<!-- Refresh -->
|
||||||
<div class="form-check mb-3">
|
<div class="form-check mb-3">
|
||||||
<input class="form-check-input" id="refresh" name="refresh" type="checkbox" />
|
<input class="form-check-input" id="refresh" name="refresh" type="checkbox" />
|
||||||
@ -172,4 +178,5 @@
|
|||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
<script src="./static/index.js"></script>
|
<script src="./static/index.js"></script>
|
||||||
|
|
||||||
</html>
|
</html>
|
@ -66,6 +66,11 @@ function generateURI() {
|
|||||||
// alert("订阅链接和节点分享链接不能同时为空!");
|
// alert("订阅链接和节点分享链接不能同时为空!");
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取订阅user-agent标识
|
||||||
|
const userAgent = document.getElementById("user-agent").value;
|
||||||
|
queryParams.push(`userAgent=${userAgent}`)
|
||||||
|
|
||||||
// 获取复选框的值
|
// 获取复选框的值
|
||||||
const refresh = document.getElementById("refresh").checked;
|
const refresh = document.getElementById("refresh").checked;
|
||||||
queryParams.push(`refresh=${refresh ? "true" : "false"}`);
|
queryParams.push(`refresh=${refresh ? "true" : "false"}`);
|
||||||
|
@ -7,10 +7,26 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Get(url string) (resp *http.Response, err error) {
|
type GetConfig struct {
|
||||||
|
userAgent string
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetOption func(*GetConfig)
|
||||||
|
|
||||||
|
func WithUserAgent(userAgent string) GetOption {
|
||||||
|
return func(config *GetConfig) {
|
||||||
|
config.userAgent = userAgent
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Get(url string, options ...GetOption) (resp *http.Response, err error) {
|
||||||
retryTimes := config.Default.RequestRetryTimes
|
retryTimes := config.Default.RequestRetryTimes
|
||||||
haveTried := 0
|
haveTried := 0
|
||||||
retryDelay := time.Second // 延迟1秒再重试
|
retryDelay := time.Second // 延迟1秒再重试
|
||||||
|
getConfig := GetConfig{}
|
||||||
|
for _, option := range options {
|
||||||
|
option(&getConfig)
|
||||||
|
}
|
||||||
for haveTried < retryTimes {
|
for haveTried < retryTimes {
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
//client.Timeout = time.Second * 10
|
//client.Timeout = time.Second * 10
|
||||||
@ -20,6 +36,9 @@ func Get(url string) (resp *http.Response, err error) {
|
|||||||
time.Sleep(retryDelay)
|
time.Sleep(retryDelay)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if getConfig.userAgent != "" {
|
||||||
|
req.Header.Set("User-Agent", getConfig.userAgent)
|
||||||
|
}
|
||||||
get, err := client.Do(req)
|
get, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
haveTried++
|
haveTried++
|
||||||
|
@ -16,9 +16,9 @@ import (
|
|||||||
var subsDir = "subs"
|
var subsDir = "subs"
|
||||||
var fileLock sync.RWMutex
|
var fileLock sync.RWMutex
|
||||||
|
|
||||||
func LoadSubscription(url string, refresh bool) ([]byte, error) {
|
func LoadSubscription(url string, refresh bool, userAgent string) ([]byte, error) {
|
||||||
if refresh {
|
if refresh {
|
||||||
return FetchSubscriptionFromAPI(url)
|
return FetchSubscriptionFromAPI(url, userAgent)
|
||||||
}
|
}
|
||||||
hash := sha256.Sum224([]byte(url))
|
hash := sha256.Sum224([]byte(url))
|
||||||
fileName := filepath.Join(subsDir, hex.EncodeToString(hash[:]))
|
fileName := filepath.Join(subsDir, hex.EncodeToString(hash[:]))
|
||||||
@ -27,7 +27,7 @@ func LoadSubscription(url string, refresh bool) ([]byte, error) {
|
|||||||
if !os.IsNotExist(err) {
|
if !os.IsNotExist(err) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return FetchSubscriptionFromAPI(url)
|
return FetchSubscriptionFromAPI(url, userAgent)
|
||||||
}
|
}
|
||||||
lastGetTime := stat.ModTime().Unix() // 单位是秒
|
lastGetTime := stat.ModTime().Unix() // 单位是秒
|
||||||
if lastGetTime+config.Default.CacheExpire > time.Now().Unix() {
|
if lastGetTime+config.Default.CacheExpire > time.Now().Unix() {
|
||||||
@ -48,13 +48,13 @@ func LoadSubscription(url string, refresh bool) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
return subContent, nil
|
return subContent, nil
|
||||||
}
|
}
|
||||||
return FetchSubscriptionFromAPI(url)
|
return FetchSubscriptionFromAPI(url, userAgent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchSubscriptionFromAPI(url string) ([]byte, error) {
|
func FetchSubscriptionFromAPI(url string, userAgent string) ([]byte, error) {
|
||||||
hash := sha256.Sum224([]byte(url))
|
hash := sha256.Sum224([]byte(url))
|
||||||
fileName := filepath.Join(subsDir, hex.EncodeToString(hash[:]))
|
fileName := filepath.Join(subsDir, hex.EncodeToString(hash[:]))
|
||||||
resp, err := Get(url)
|
resp, err := Get(url, WithUserAgent(userAgent))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,7 @@ type SubValidator struct {
|
|||||||
ReplaceTo []string `form:"-" binding:""`
|
ReplaceTo []string `form:"-" binding:""`
|
||||||
NodeListMode bool `form:"nodeList,default=false" binding:""`
|
NodeListMode bool `form:"nodeList,default=false" binding:""`
|
||||||
IgnoreCountryGrooup bool `form:"ignoreCountryGroup,default=false" binding:""`
|
IgnoreCountryGrooup bool `form:"ignoreCountryGroup,default=false" binding:""`
|
||||||
|
UserAgent string `form:"userAgent" binding:""`
|
||||||
}
|
}
|
||||||
|
|
||||||
type RuleProviderStruct struct {
|
type RuleProviderStruct struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user