Browse Source

Improve HTTP headers option

septs 2 years ago
parent
commit
41fd1778a7
6 changed files with 31 additions and 32 deletions
  1. 5 5
      option/simple.go
  2. 13 0
      option/types.go
  3. 10 10
      option/v2ray_transport.go
  4. 1 9
      outbound/http.go
  5. 1 4
      transport/v2rayhttp/client.go
  6. 1 4
      transport/v2rayhttp/server.go

+ 5 - 5
option/simple.go

@@ -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"`
 }

+ 13 - 0
option/types.go

@@ -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
+}

+ 10 - 10
option/v2ray_transport.go

@@ -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{}

+ 1 - 9
outbound/http.go

@@ -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
 }

+ 1 - 4
transport/v2rayhttp/client.go

@@ -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
 }

+ 1 - 4
transport/v2rayhttp/server.go

@@ -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,