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