github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/outbound/http.go (about)

     1  package outbound
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  	"net/http"
     7  	"os"
     8  
     9  	"github.com/inazumav/sing-box/adapter"
    10  	"github.com/inazumav/sing-box/common/dialer"
    11  	"github.com/inazumav/sing-box/common/tls"
    12  	C "github.com/inazumav/sing-box/constant"
    13  	"github.com/inazumav/sing-box/log"
    14  	"github.com/inazumav/sing-box/option"
    15  	"github.com/sagernet/sing/common"
    16  	M "github.com/sagernet/sing/common/metadata"
    17  	N "github.com/sagernet/sing/common/network"
    18  	sHTTP "github.com/sagernet/sing/protocol/http"
    19  )
    20  
    21  var _ adapter.Outbound = (*HTTP)(nil)
    22  
    23  type HTTP struct {
    24  	myOutboundAdapter
    25  	client *sHTTP.Client
    26  }
    27  
    28  func NewHTTP(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.HTTPOutboundOptions) (*HTTP, error) {
    29  	outboundDialer, err := dialer.New(router, options.DialerOptions)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	detour, err := tls.NewDialerFromOptions(ctx, router, outboundDialer, options.Server, common.PtrValueOrDefault(options.TLS))
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	var headers http.Header
    38  	if options.Headers != nil {
    39  		headers = make(http.Header)
    40  		for key, values := range options.Headers {
    41  			headers[key] = values
    42  		}
    43  	}
    44  	return &HTTP{
    45  		myOutboundAdapter{
    46  			protocol:     C.TypeHTTP,
    47  			network:      []string{N.NetworkTCP},
    48  			router:       router,
    49  			logger:       logger,
    50  			tag:          tag,
    51  			dependencies: withDialerDependency(options.DialerOptions),
    52  		},
    53  		sHTTP.NewClient(sHTTP.Options{
    54  			Dialer:   detour,
    55  			Server:   options.ServerOptions.Build(),
    56  			Username: options.Username,
    57  			Password: options.Password,
    58  			Path:     options.Path,
    59  			Headers:  headers,
    60  		}),
    61  	}, nil
    62  }
    63  
    64  func (h *HTTP) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
    65  	ctx, metadata := adapter.AppendContext(ctx)
    66  	metadata.Outbound = h.tag
    67  	metadata.Destination = destination
    68  	h.logger.InfoContext(ctx, "outbound connection to ", destination)
    69  	return h.client.DialContext(ctx, network, destination)
    70  }
    71  
    72  func (h *HTTP) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
    73  	return nil, os.ErrInvalid
    74  }
    75  
    76  func (h *HTTP) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
    77  	return NewConnection(ctx, h, conn, metadata)
    78  }
    79  
    80  func (h *HTTP) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
    81  	return os.ErrInvalid
    82  }