Skip to content

Commit

Permalink
Merge pull request #35 from arusuki/main
Browse files Browse the repository at this point in the history
🔧  customize HTTP user-agent for fetching subscription from API.
  • Loading branch information
nitezs authored Aug 4, 2024
2 parents 0946412 + fd22cd1 commit 7158ad4
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 9 deletions.
4 changes: 2 additions & 2 deletions api/handler/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
template = query.Template
}
if strings.HasPrefix(template, "http") {
templateBytes, err = common.LoadSubscription(template, query.Refresh)
templateBytes, err = common.LoadSubscription(template, query.Refresh, query.UserAgent)
if err != nil {
logger.Logger.Debug(
"load template failed", zap.String("template", template), zap.Error(err),
Expand Down Expand Up @@ -61,7 +61,7 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
var proxyList []model.Proxy
// 加载订阅
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 := ""
if strings.Contains(query.Subs[i], "#") {
subName = query.Subs[i][strings.LastIndex(query.Subs[i], "#")+1:]
Expand Down
7 changes: 7 additions & 0 deletions api/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ <h2>sub2clash</h2>
<label for="proxy">节点分享链接:</label>
<textarea class="form-control" id="proxy" name="proxy" placeholder="每行输入一个节点分享链接" rows="5"></textarea>
</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 -->
<div class="form-check mb-3">
<input class="form-check-input" id="refresh" name="refresh" type="checkbox" />
Expand Down Expand Up @@ -172,4 +178,5 @@ <h2>sub2clash</h2>
</div>
</body>
<script src="./static/index.js"></script>

</html>
5 changes: 5 additions & 0 deletions api/static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ function generateURI() {
// alert("订阅链接和节点分享链接不能同时为空!");
return "";
}

// 获取订阅user-agent标识
const userAgent = document.getElementById("user-agent").value;
queryParams.push(`userAgent=${userAgent}`)

// 获取复选框的值
const refresh = document.getElementById("refresh").checked;
queryParams.push(`refresh=${refresh ? "true" : "false"}`);
Expand Down
21 changes: 20 additions & 1 deletion common/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@ import (
"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
haveTried := 0
retryDelay := time.Second // 延迟1秒再重试
getConfig := GetConfig{}
for _, option := range options {
option(&getConfig)
}
for haveTried < retryTimes {
client := &http.Client{}
//client.Timeout = time.Second * 10
Expand All @@ -20,6 +36,9 @@ func Get(url string) (resp *http.Response, err error) {
time.Sleep(retryDelay)
continue
}
if getConfig.userAgent != "" {
req.Header.Set("User-Agent", getConfig.userAgent)
}
get, err := client.Do(req)
if err != nil {
haveTried++
Expand Down
12 changes: 6 additions & 6 deletions common/sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
var subsDir = "subs"
var fileLock sync.RWMutex

func LoadSubscription(url string, refresh bool) ([]byte, error) {
func LoadSubscription(url string, refresh bool, userAgent string) ([]byte, error) {
if refresh {
return FetchSubscriptionFromAPI(url)
return FetchSubscriptionFromAPI(url, userAgent)
}
hash := sha256.Sum224([]byte(url))
fileName := filepath.Join(subsDir, hex.EncodeToString(hash[:]))
Expand All @@ -27,7 +27,7 @@ func LoadSubscription(url string, refresh bool) ([]byte, error) {
if !os.IsNotExist(err) {
return nil, err
}
return FetchSubscriptionFromAPI(url)
return FetchSubscriptionFromAPI(url, userAgent)
}
lastGetTime := stat.ModTime().Unix() // 单位是秒
if lastGetTime+config.Default.CacheExpire > time.Now().Unix() {
Expand All @@ -48,13 +48,13 @@ func LoadSubscription(url string, refresh bool) ([]byte, error) {
}
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))
fileName := filepath.Join(subsDir, hex.EncodeToString(hash[:]))
resp, err := Get(url)
resp, err := Get(url, WithUserAgent(userAgent))
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions validator/sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type SubValidator struct {
ReplaceTo []string `form:"-" binding:""`
NodeListMode bool `form:"nodeList,default=false" binding:""`
IgnoreCountryGrooup bool `form:"ignoreCountryGroup,default=false" binding:""`
UserAgent string `form:"userAgent" binding:""`
}

type RuleProviderStruct struct {
Expand Down

0 comments on commit 7158ad4

Please sign in to comment.