Skip to content

Commit

Permalink
Improve HTTP headers option
Browse files Browse the repository at this point in the history
  • Loading branch information
septs authored and nekohasekai committed Oct 30, 2023
1 parent ac930cf commit 41fd177
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 32 deletions.
10 changes: 5 additions & 5 deletions option/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 13 additions & 0 deletions option/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package option

import (
"net/http"
"net/netip"
"strings"
"time"
Expand Down Expand Up @@ -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
}
20 changes: 10 additions & 10 deletions option/v2ray_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
10 changes: 1 addition & 9 deletions outbound/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package outbound
import (
"context"
"net"
"net/http"
"os"

"github.com/sagernet/sing-box/adapter"
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
Expand Down
5 changes: 1 addition & 4 deletions transport/v2rayhttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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
}
Expand Down
5 changes: 1 addition & 4 deletions transport/v2rayhttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,14 @@ 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"
}
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,
Expand Down

0 comments on commit 41fd177

Please sign in to comment.