Skip to content

Commit b4a7cad

Browse files
committed
Fix custom Host headers in core downloads
Go's HTTP client sends the request authority from Request.Host rather than from the ordinary header map. Apply Host overrides to that dedicated field while preserving normal handling for every other forwarded header. Cover both the authority override and an ordinary header with a focused regression test.
1 parent d607494 commit b4a7cad

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

‎libv2ray_utils.go‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,7 @@ func (x *CoreController) getURL(url string, outboundTag string, headersJSON stri
199199
if err := json.Unmarshal([]byte(headersJSON), &headers); err != nil {
200200
return nil, fmt.Errorf("failed to parse request headers: %w", err)
201201
}
202-
for key, value := range headers {
203-
req.Header.Set(key, value)
204-
}
202+
applyRequestHeaders(req, headers)
205203
}
206204

207205
resp, err := (&http.Client{Transport: tr, Timeout: timeout}).Do(req)
@@ -219,6 +217,16 @@ func (x *CoreController) getURL(url string, outboundTag string, headersJSON stri
219217
return resp, nil
220218
}
221219

220+
func applyRequestHeaders(req *http.Request, headers map[string]string) {
221+
for key, value := range headers {
222+
if strings.EqualFold(key, "Host") {
223+
req.Host = value
224+
continue
225+
}
226+
req.Header.Set(key, value)
227+
}
228+
}
229+
222230
// MeasureOutboundDelay measures the outbound delay for a given configuration and URL
223231
func MeasureOutboundDelay(ConfigureFileContent string, url string) (int64, error) {
224232
config, err := coreserial.LoadJSONConfig(strings.NewReader(ConfigureFileContent))

‎libv2ray_utils_test.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package libv2ray
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
)
7+
8+
func TestApplyRequestHeaders(t *testing.T) {
9+
req, err := http.NewRequest(http.MethodGet, "https://example.com", nil)
10+
if err != nil {
11+
t.Fatal(err)
12+
}
13+
14+
applyRequestHeaders(req, map[string]string{
15+
"host": "origin.example",
16+
"X-Test": "value",
17+
})
18+
19+
if req.Host != "origin.example" {
20+
t.Fatalf("Host = %q, want %q", req.Host, "origin.example")
21+
}
22+
if got := req.Header.Get("Host"); got != "" {
23+
t.Fatalf("Header[Host] = %q, want empty", got)
24+
}
25+
if got := req.Header.Get("X-Test"); got != "value" {
26+
t.Fatalf("X-Test = %q, want %q", got, "value")
27+
}
28+
}

0 commit comments

Comments
 (0)