1
0

Feature: allow http outbound to set custom headers (#2647)

This commit is contained in:
RommHui 2023-03-31 20:33:41 +08:00 committed by GitHub
parent 7e2974f02f
commit a7252a1576
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,18 +22,20 @@ type Http struct {
user string user string
pass string pass string
tlsConfig *tls.Config tlsConfig *tls.Config
Headers http.Header
} }
type HttpOption struct { type HttpOption struct {
BasicOption BasicOption
Name string `proxy:"name"` Name string `proxy:"name"`
Server string `proxy:"server"` Server string `proxy:"server"`
Port int `proxy:"port"` Port int `proxy:"port"`
UserName string `proxy:"username,omitempty"` UserName string `proxy:"username,omitempty"`
Password string `proxy:"password,omitempty"` Password string `proxy:"password,omitempty"`
TLS bool `proxy:"tls,omitempty"` TLS bool `proxy:"tls,omitempty"`
SNI string `proxy:"sni,omitempty"` SNI string `proxy:"sni,omitempty"`
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"` SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
Headers map[string]string `proxy:"headers,omitempty"`
} }
// StreamConn implements C.ProxyAdapter // StreamConn implements C.ProxyAdapter
@ -82,12 +84,12 @@ func (h *Http) shakeHand(metadata *C.Metadata, rw io.ReadWriter) error {
URL: &url.URL{ URL: &url.URL{
Host: addr, Host: addr,
}, },
Host: addr, Host: addr,
Header: http.Header{ Header: h.Headers.Clone(),
"Proxy-Connection": []string{"Keep-Alive"},
},
} }
req.Header.Add("Proxy-Connection", "Keep-Alive")
if h.user != "" && h.pass != "" { if h.user != "" && h.pass != "" {
auth := h.user + ":" + h.pass auth := h.user + ":" + h.pass
req.Header.Add("Proxy-Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(auth))) req.Header.Add("Proxy-Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(auth)))
@ -134,6 +136,11 @@ func NewHttp(option HttpOption) *Http {
} }
} }
headers := http.Header{}
for name, value := range option.Headers {
headers.Add(name, value)
}
return &Http{ return &Http{
Base: &Base{ Base: &Base{
name: option.Name, name: option.Name,
@ -145,5 +152,6 @@ func NewHttp(option HttpOption) *Http {
user: option.UserName, user: option.UserName,
pass: option.Password, pass: option.Password,
tlsConfig: tlsConfig, tlsConfig: tlsConfig,
Headers: headers,
} }
} }