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 { type HTTPOutboundOptions struct {
DialerOptions DialerOptions
ServerOptions ServerOptions
Username string `json:"username,omitempty"` Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"` Password string `json:"password,omitempty"`
TLS *OutboundTLSOptions `json:"tls,omitempty"` TLS *OutboundTLSOptions `json:"tls,omitempty"`
Path string `json:"path,omitempty"` Path string `json:"path,omitempty"`
Headers map[string]Listable[string] `json:"headers,omitempty"` Headers HTTPHeader `json:"headers,omitempty"`
} }

View file

@ -1,6 +1,7 @@
package option package option
import ( import (
"net/http"
"net/netip" "net/netip"
"strings" "strings"
"time" "time"
@ -235,3 +236,15 @@ func DNSQueryTypeToString(queryType uint16) string {
} }
return F.ToString(queryType) 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 { type V2RayHTTPOptions struct {
Host Listable[string] `json:"host,omitempty"` Host Listable[string] `json:"host,omitempty"`
Path string `json:"path,omitempty"` Path string `json:"path,omitempty"`
Method string `json:"method,omitempty"` Method string `json:"method,omitempty"`
Headers map[string]Listable[string] `json:"headers,omitempty"` Headers HTTPHeader `json:"headers,omitempty"`
IdleTimeout Duration `json:"idle_timeout,omitempty"` IdleTimeout Duration `json:"idle_timeout,omitempty"`
PingTimeout Duration `json:"ping_timeout,omitempty"` PingTimeout Duration `json:"ping_timeout,omitempty"`
} }
type V2RayWebsocketOptions struct { type V2RayWebsocketOptions struct {
Path string `json:"path,omitempty"` Path string `json:"path,omitempty"`
Headers map[string]Listable[string] `json:"headers,omitempty"` Headers HTTPHeader `json:"headers,omitempty"`
MaxEarlyData uint32 `json:"max_early_data,omitempty"` MaxEarlyData uint32 `json:"max_early_data,omitempty"`
EarlyDataHeaderName string `json:"early_data_header_name,omitempty"` EarlyDataHeaderName string `json:"early_data_header_name,omitempty"`
} }
type V2RayQUICOptions struct{} type V2RayQUICOptions struct{}

View file

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

View file

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

View file

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