Improve HTTP headers option

This commit is contained in:
septs 2023-10-21 12:00:00 +08:00 committed by 世界
parent ac930cf1aa
commit 41fd1778a7
No known key found for this signature in database
GPG Key ID: CD109927C34A63C4
6 changed files with 31 additions and 32 deletions

View File

@ -27,9 +27,9 @@ type SocksOutboundOptions struct {
type HTTPOutboundOptions struct {
DialerOptions
ServerOptions
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
TLS *OutboundTLSOptions `json:"tls,omitempty"`
Path string `json:"path,omitempty"`
Headers map[string]Listable[string] `json:"headers,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
TLS *OutboundTLSOptions `json:"tls,omitempty"`
Path string `json:"path,omitempty"`
Headers HTTPHeader `json:"headers,omitempty"`
}

View File

@ -1,6 +1,7 @@
package option
import (
"net/http"
"net/netip"
"strings"
"time"
@ -235,3 +236,15 @@ func DNSQueryTypeToString(queryType uint16) string {
}
return F.ToString(queryType)
}
type HTTPHeader map[string]Listable[string]
func (h HTTPHeader) Build() http.Header {
header := make(http.Header)
for name, values := range h {
for _, value := range values {
header.Add(name, value)
}
}
return header
}

View File

@ -61,19 +61,19 @@ func (o *V2RayTransportOptions) UnmarshalJSON(bytes []byte) error {
}
type V2RayHTTPOptions struct {
Host Listable[string] `json:"host,omitempty"`
Path string `json:"path,omitempty"`
Method string `json:"method,omitempty"`
Headers map[string]Listable[string] `json:"headers,omitempty"`
IdleTimeout Duration `json:"idle_timeout,omitempty"`
PingTimeout Duration `json:"ping_timeout,omitempty"`
Host Listable[string] `json:"host,omitempty"`
Path string `json:"path,omitempty"`
Method string `json:"method,omitempty"`
Headers HTTPHeader `json:"headers,omitempty"`
IdleTimeout Duration `json:"idle_timeout,omitempty"`
PingTimeout Duration `json:"ping_timeout,omitempty"`
}
type V2RayWebsocketOptions struct {
Path string `json:"path,omitempty"`
Headers map[string]Listable[string] `json:"headers,omitempty"`
MaxEarlyData uint32 `json:"max_early_data,omitempty"`
EarlyDataHeaderName string `json:"early_data_header_name,omitempty"`
Path string `json:"path,omitempty"`
Headers HTTPHeader `json:"headers,omitempty"`
MaxEarlyData uint32 `json:"max_early_data,omitempty"`
EarlyDataHeaderName string `json:"early_data_header_name,omitempty"`
}
type V2RayQUICOptions struct{}

View File

@ -3,7 +3,6 @@ package outbound
import (
"context"
"net"
"net/http"
"os"
"github.com/sagernet/sing-box/adapter"
@ -34,13 +33,6 @@ func NewHTTP(ctx context.Context, router adapter.Router, logger log.ContextLogge
if err != nil {
return nil, err
}
var headers http.Header
if options.Headers != nil {
headers = make(http.Header)
for key, values := range options.Headers {
headers[key] = values
}
}
return &HTTP{
myOutboundAdapter{
protocol: C.TypeHTTP,
@ -56,7 +48,7 @@ func NewHTTP(ctx context.Context, router adapter.Router, logger log.ContextLogge
Username: options.Username,
Password: options.Password,
Path: options.Path,
Headers: headers,
Headers: options.Headers.Build(),
}),
}, nil
}

View File

@ -64,7 +64,7 @@ func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, opt
serverAddr: serverAddr,
host: options.Host,
method: options.Method,
headers: make(http.Header),
headers: options.Headers.Build(),
transport: transport,
http2: tlsConfig != nil,
}
@ -83,9 +83,6 @@ func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, opt
if err != nil {
return nil, E.New("failed to set path: " + err.Error())
}
for key, valueList := range options.Headers {
client.headers[key] = valueList
}
client.url = &uri
return client, nil
}

View File

@ -55,7 +55,7 @@ func NewServer(ctx context.Context, options option.V2RayHTTPOptions, tlsConfig t
host: options.Host,
path: options.Path,
method: options.Method,
headers: make(http.Header),
headers: options.Headers.Build(),
}
if server.method == "" {
server.method = "PUT"
@ -63,9 +63,6 @@ func NewServer(ctx context.Context, options option.V2RayHTTPOptions, tlsConfig t
if !strings.HasPrefix(server.path, "/") {
server.path = "/" + server.path
}
for key, value := range options.Headers {
server.headers[key] = value
}
server.httpServer = &http.Server{
Handler: server,
ReadHeaderTimeout: C.TCPTimeout,