Skip to content

Commit c44949b

Browse files
committed
feat: Allow providers to set individual proxy and headers
1 parent 19f7220 commit c44949b

File tree

12 files changed

+102
-91
lines changed

12 files changed

+102
-91
lines changed

adapter/provider/parser.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@ type proxyProviderSchema struct {
4444
Type string `provider:"type"`
4545
Path string `provider:"path,omitempty"`
4646
URL string `provider:"url,omitempty"`
47+
Proxy string `provider:"proxy,omitempty"`
4748
Interval int `provider:"interval,omitempty"`
4849
Filter string `provider:"filter,omitempty"`
4950
ExcludeFilter string `provider:"exclude-filter,omitempty"`
5051
ExcludeType string `provider:"exclude-type,omitempty"`
5152
DialerProxy string `provider:"dialer-proxy,omitempty"`
5253

53-
HealthCheck healthCheckSchema `provider:"health-check,omitempty"`
54-
Override OverrideSchema `provider:"override,omitempty"`
54+
HealthCheck healthCheckSchema `provider:"health-check,omitempty"`
55+
Override OverrideSchema `provider:"override,omitempty"`
56+
Header map[string][]string `provider:"header,omitempty"`
5557
}
5658

5759
func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvider, error) {
@@ -86,16 +88,14 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide
8688
path := C.Path.Resolve(schema.Path)
8789
vehicle = resource.NewFileVehicle(path)
8890
case "http":
91+
path := C.Path.GetPathByHash("proxies", schema.URL)
8992
if schema.Path != "" {
90-
path := C.Path.Resolve(schema.Path)
93+
path = C.Path.Resolve(schema.Path)
9194
if !features.CMFA && !C.Path.IsSafePath(path) {
9295
return nil, fmt.Errorf("%w: %s", errSubPath, path)
9396
}
94-
vehicle = resource.NewHTTPVehicle(schema.URL, path)
95-
} else {
96-
path := C.Path.GetPathByHash("proxies", schema.URL)
97-
vehicle = resource.NewHTTPVehicle(schema.URL, path)
9897
}
98+
vehicle = resource.NewHTTPVehicle(schema.URL, path, schema.Proxy, schema.Header)
9999
default:
100100
return nil, fmt.Errorf("%w: %s", errVehicleType, schema.Type)
101101
}

adapter/provider/provider.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (pp *proxySetProvider) getSubscriptionInfo() {
125125
ctx, cancel := context.WithTimeout(context.Background(), time.Second*90)
126126
defer cancel()
127127
resp, err := mihomoHttp.HttpRequest(ctx, pp.Vehicle().(*resource.HTTPVehicle).Url(),
128-
http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil)
128+
http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil, "")
129129
if err != nil {
130130
return
131131
}
@@ -134,7 +134,7 @@ func (pp *proxySetProvider) getSubscriptionInfo() {
134134
userInfoStr := strings.TrimSpace(resp.Header.Get("subscription-userinfo"))
135135
if userInfoStr == "" {
136136
resp2, err := mihomoHttp.HttpRequest(ctx, pp.Vehicle().(*resource.HTTPVehicle).Url(),
137-
http.MethodGet, http.Header{"User-Agent": {"Quantumultx"}}, nil)
137+
http.MethodGet, http.Header{"User-Agent": {"Quantumultx"}}, nil, "")
138138
if err != nil {
139139
return
140140
}

component/geodata/init.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func InitGeoSite() error {
4747
func downloadGeoSite(path string) (err error) {
4848
ctx, cancel := context.WithTimeout(context.Background(), time.Second*90)
4949
defer cancel()
50-
resp, err := mihomoHttp.HttpRequest(ctx, C.GeoSiteUrl, http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil)
50+
resp, err := mihomoHttp.HttpRequest(ctx, C.GeoSiteUrl, http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil, "")
5151
if err != nil {
5252
return
5353
}
@@ -66,7 +66,7 @@ func downloadGeoSite(path string) (err error) {
6666
func downloadGeoIP(path string) (err error) {
6767
ctx, cancel := context.WithTimeout(context.Background(), time.Second*90)
6868
defer cancel()
69-
resp, err := mihomoHttp.HttpRequest(ctx, C.GeoIpUrl, http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil)
69+
resp, err := mihomoHttp.HttpRequest(ctx, C.GeoIpUrl, http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil, "")
7070
if err != nil {
7171
return
7272
}

component/http/http.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ import (
1616
"github.com/metacubex/mihomo/listener/inner"
1717
)
1818

19-
func HttpRequest(ctx context.Context, url, method string, header map[string][]string, body io.Reader) (*http.Response, error) {
20-
UA := C.UA
19+
func HttpRequest(ctx context.Context, url, method string, header map[string][]string, body io.Reader, specialProxy string) (*http.Response, error) {
2120
method = strings.ToUpper(method)
2221
urlRes, err := URL.Parse(url)
2322
if err != nil {
@@ -32,7 +31,7 @@ func HttpRequest(ctx context.Context, url, method string, header map[string][]st
3231
}
3332

3433
if _, ok := header["User-Agent"]; !ok {
35-
req.Header.Set("User-Agent", UA)
34+
req.Header.Set("User-Agent", C.UA)
3635
}
3736

3837
if err != nil {
@@ -54,7 +53,7 @@ func HttpRequest(ctx context.Context, url, method string, header map[string][]st
5453
TLSHandshakeTimeout: 10 * time.Second,
5554
ExpectContinueTimeout: 1 * time.Second,
5655
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
57-
if conn, err := inner.HandleTcp(address); err == nil {
56+
if conn, err := inner.HandleTcp(address, specialProxy); err == nil {
5857
return conn, nil
5958
} else {
6059
d := net.Dialer{}
@@ -66,5 +65,4 @@ func HttpRequest(ctx context.Context, url, method string, header map[string][]st
6665

6766
client := http.Client{Transport: transport}
6867
return client.Do(req)
69-
7068
}

component/mmdb/mmdb.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func IPInstance() IPReader {
8282
func DownloadMMDB(path string) (err error) {
8383
ctx, cancel := context.WithTimeout(context.Background(), time.Second*90)
8484
defer cancel()
85-
resp, err := mihomoHttp.HttpRequest(ctx, C.MmdbUrl, http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil)
85+
resp, err := mihomoHttp.HttpRequest(ctx, C.MmdbUrl, http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil, "")
8686
if err != nil {
8787
return
8888
}
@@ -115,7 +115,7 @@ func ASNInstance() ASNReader {
115115
func DownloadASN(path string) (err error) {
116116
ctx, cancel := context.WithTimeout(context.Background(), time.Second*90)
117117
defer cancel()
118-
resp, err := mihomoHttp.HttpRequest(ctx, C.ASNUrl, http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil)
118+
resp, err := mihomoHttp.HttpRequest(ctx, C.ASNUrl, http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil, "")
119119
if err != nil {
120120
return
121121
}

component/resource/vehicle.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ func NewFileVehicle(path string) *FileVehicle {
3333
}
3434

3535
type HTTPVehicle struct {
36-
url string
37-
path string
36+
url string
37+
path string
38+
proxy string
39+
header http.Header
3840
}
3941

4042
func (h *HTTPVehicle) Url() string {
@@ -52,7 +54,7 @@ func (h *HTTPVehicle) Path() string {
5254
func (h *HTTPVehicle) Read() ([]byte, error) {
5355
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
5456
defer cancel()
55-
resp, err := mihomoHttp.HttpRequest(ctx, h.url, http.MethodGet, nil, nil)
57+
resp, err := mihomoHttp.HttpRequest(ctx, h.url, http.MethodGet, h.header, nil, h.proxy)
5658
if err != nil {
5759
return nil, err
5860
}
@@ -67,6 +69,6 @@ func (h *HTTPVehicle) Read() ([]byte, error) {
6769
return buf, nil
6870
}
6971

70-
func NewHTTPVehicle(url string, path string) *HTTPVehicle {
71-
return &HTTPVehicle{url, path}
72+
func NewHTTPVehicle(url string, path string, proxy string, header http.Header) *HTTPVehicle {
73+
return &HTTPVehicle{url, path, proxy, header}
7274
}

config/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ func UnmarshalRawConfig(buf []byte) (*RawConfig, error) {
413413
ProxyGroup: []map[string]any{},
414414
TCPConcurrent: false,
415415
FindProcessMode: P.FindProcessStrict,
416-
GlobalUA: "clash.meta",
416+
GlobalUA: "clash.meta/" + C.Version,
417417
Tun: RawTun{
418418
Enable: false,
419419
Device: "",

config/utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
func downloadForBytes(url string) ([]byte, error) {
2121
ctx, cancel := context.WithTimeout(context.Background(), time.Second*90)
2222
defer cancel()
23-
resp, err := mihomoHttp.HttpRequest(ctx, url, http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil)
23+
resp, err := mihomoHttp.HttpRequest(ctx, url, http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil, "")
2424
if err != nil {
2525
return nil, err
2626
}

0 commit comments

Comments
 (0)