Add headers option for HTTP outbound

This commit is contained in:
PuerNya 2023-04-11 17:49:37 +08:00 committed by GitHub
parent f20642d6fd
commit 70e47df295
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 6 deletions

View file

@ -27,7 +27,8 @@ 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"`
Headers map[string]Listable[string] `json:"headers,omitempty"`
} }

View file

@ -3,6 +3,7 @@ package outbound
import ( import (
"context" "context"
"net" "net"
"net/http"
"os" "os"
"github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/adapter"
@ -14,14 +15,14 @@ import (
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
M "github.com/sagernet/sing/common/metadata" M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network" N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/protocol/http" sHTTP "github.com/sagernet/sing/protocol/http"
) )
var _ adapter.Outbound = (*HTTP)(nil) var _ adapter.Outbound = (*HTTP)(nil)
type HTTP struct { type HTTP struct {
myOutboundAdapter myOutboundAdapter
client *http.Client client *sHTTP.Client
} }
func NewHTTP(router adapter.Router, logger log.ContextLogger, tag string, options option.HTTPOutboundOptions) (*HTTP, error) { func NewHTTP(router adapter.Router, logger log.ContextLogger, tag string, options option.HTTPOutboundOptions) (*HTTP, error) {
@ -29,6 +30,13 @@ func NewHTTP(router adapter.Router, logger log.ContextLogger, tag string, option
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,
@ -37,7 +45,7 @@ func NewHTTP(router adapter.Router, logger log.ContextLogger, tag string, option
logger: logger, logger: logger,
tag: tag, tag: tag,
}, },
http.NewClient(detour, options.ServerOptions.Build(), options.Username, options.Password, nil), sHTTP.NewClient(detour, options.ServerOptions.Build(), options.Username, options.Password, headers),
}, nil }, nil
} }