Skip to content

Commit f3e23b1

Browse files
xishang0128wwqgtxx
authored andcommitted
feat: Allow providers to set individual proxy and headers
1 parent 19f7220 commit f3e23b1

File tree

7 files changed

+96
-81
lines changed

7 files changed

+96
-81
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
}

component/http/http.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ import (
1717
)
1818

1919
func HttpRequest(ctx context.Context, url, method string, header map[string][]string, body io.Reader) (*http.Response, error) {
20-
UA := C.UA
20+
return HttpRequestWithProxy(ctx, url, method, header, body, "")
21+
}
22+
23+
func HttpRequestWithProxy(ctx context.Context, url, method string, header map[string][]string, body io.Reader, specialProxy string) (*http.Response, error) {
2124
method = strings.ToUpper(method)
2225
urlRes, err := URL.Parse(url)
2326
if err != nil {
@@ -32,7 +35,7 @@ func HttpRequest(ctx context.Context, url, method string, header map[string][]st
3235
}
3336

3437
if _, ok := header["User-Agent"]; !ok {
35-
req.Header.Set("User-Agent", UA)
38+
req.Header.Set("User-Agent", C.UA)
3639
}
3740

3841
if err != nil {
@@ -54,7 +57,7 @@ func HttpRequest(ctx context.Context, url, method string, header map[string][]st
5457
TLSHandshakeTimeout: 10 * time.Second,
5558
ExpectContinueTimeout: 1 * time.Second,
5659
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
57-
if conn, err := inner.HandleTcp(address); err == nil {
60+
if conn, err := inner.HandleTcp(address, specialProxy); err == nil {
5861
return conn, nil
5962
} else {
6063
d := net.Dialer{}
@@ -66,5 +69,4 @@ func HttpRequest(ctx context.Context, url, method string, header map[string][]st
6669

6770
client := http.Client{Transport: transport}
6871
return client.Do(req)
69-
7072
}

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.HttpRequestWithProxy(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: "",

0 commit comments

Comments
 (0)