Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom User-Agent for http requests send by xrayhelper #21

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ XrayHelper 使用 yml 格式的配置文件,默认使用`/data/adb/xray/xrayhe
- `runDir`必填,用于存储运行时所产生的文件,例如核心的 pid 值,核心日志等
- `proxyTag`默认值`proxy`,使用 XrayHelper 进行节点切换时,将进行替换的出站代理 Tag
- `subList`可选,数组,节点订阅链接(SIP002/v2rayNg/Hysteria/Hysteria2),也支持 clash 订阅链接(需要在订阅链接前添加`clash+`前缀)
- `userAgent`可选,自定义 XrayHelper http 请求的 User-Agent
- proxy
- `method`默认值`tproxy`,代理模式,可选`tproxy`、`tun`、`tun2socks`,使用 tun 模式时,请确保你的核心支持 tun 并正确配置它;使用 tun2socks 模式时,需要提前下载 tun2socks 二进制文件(可使用命令`xrayhelper update tun2socks`)
- `tproxyPort`默认值`65535`,透明代理端口,该值需要与核心的 tproxy 入站代理端口相对应,`tproxy`模式需要
Expand Down
2 changes: 2 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ xrayHelper:
subList:
- https://testsuburl.com
- clash+https://testclashsuburl.com
# Optional, custom User-Agent for http requests send by xrayhelper
userAgent: 'ClashMeta'
proxy:
# Required, Default value: tproxy, proxy method you want to use, support tproxy, tun, tun2socks
# If you use tun mode, please make sure your core support tun, and configure it correctly
Expand Down
1 change: 1 addition & 0 deletions main/builds/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var Config struct {
RunDir string `yaml:"runDir"`
ProxyTag string `default:"proxy" yaml:"proxyTag"`
SubList []string `yaml:"subList"`
UserAgent string `yaml:"userAgent"`
} `yaml:"xrayHelper"`
Proxy struct {
Method string `default:"tproxy" yaml:"method"`
Expand Down
15 changes: 13 additions & 2 deletions main/common/network.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"XrayHelper/main/builds"
e "XrayHelper/main/errors"
"bytes"
"context"
Expand Down Expand Up @@ -141,7 +142,12 @@ func getExternalIPv6Addr() ([]string, error) {
// DownloadFile download file from url, and save to filepath
func DownloadFile(filepath string, url string) error {
// get file from url
response, err := getHttpClient(dns, timeout*time.Millisecond).Get(url)
client := getHttpClient(dns, timeout*time.Millisecond)
request, _ := http.NewRequest("GET", url, nil)
if len(builds.Config.XrayHelper.UserAgent) > 0 {
request.Header.Set("User-Agent", builds.Config.XrayHelper.UserAgent)
}
response, err := client.Do(request)
if err != nil {
return e.New("cannot get file "+url+", ", err).WithPrefix(tagNetwork)
}
Expand All @@ -168,7 +174,12 @@ func DownloadFile(filepath string, url string) error {

// GetRawData get raw data from a url
func GetRawData(url string) ([]byte, error) {
response, err := getHttpClient(dns, timeout*time.Millisecond).Get(url)
client := getHttpClient(dns, timeout*time.Millisecond)
request, _ := http.NewRequest("GET", url, nil)
if len(builds.Config.XrayHelper.UserAgent) > 0 {
request.Header.Set("User-Agent", builds.Config.XrayHelper.UserAgent)
}
response, err := client.Do(request)
if err != nil {
return nil, e.New("cannot get url "+url+", ", err).WithPrefix(tagNetwork)
}
Expand Down