mirror of
https://github.com/SagerNet/sing-box.git
synced 2026-11-13 06:30:54 +08:00
103 lines
3.4 KiB
Go
103 lines
3.4 KiB
Go
package http
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
"github.com/sagernet/sing-box/adapter/outbound"
|
|
"github.com/sagernet/sing-box/common/dialer"
|
|
C "github.com/sagernet/sing-box/constant"
|
|
"github.com/sagernet/sing-box/log"
|
|
"github.com/sagernet/sing-box/option"
|
|
"github.com/sagernet/sing-box/transport/http"
|
|
"github.com/sagernet/sing/common"
|
|
"github.com/sagernet/sing/common/bufio"
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
"github.com/sagernet/sing/common/logger"
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
N "github.com/sagernet/sing/common/network"
|
|
)
|
|
|
|
func RegisterOutbound(registry *outbound.Registry) {
|
|
outbound.Register[option.HTTPOutboundOptions](registry, C.TypeHTTP, NewOutbound)
|
|
}
|
|
|
|
var _ adapter.OutboundWithMultiplex = (*Outbound)(nil)
|
|
|
|
type Outbound struct {
|
|
outbound.Adapter
|
|
logger logger.ContextLogger
|
|
client *http.Client
|
|
}
|
|
|
|
func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.HTTPOutboundOptions) (adapter.Outbound, error) {
|
|
outboundDialer, err := dialer.New(ctx, options.DialerOptions, options.ServerIsDomain())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
headers := options.Headers.Build()
|
|
client, err := http.NewClientWithTLS(ctx, logger, outboundDialer, options.ServerOptions, common.PtrValueOrDefault(options.TLS), http.ClientOptions{
|
|
Username: options.Username,
|
|
Password: options.Password,
|
|
Path: options.Path,
|
|
Headers: headers,
|
|
Version: http.ResolveVersion(options.Version, options.Path, headers.Get("Host")),
|
|
DisableVersionFallback: options.DisableVersionFallback,
|
|
HTTP2Options: options.HTTP2Options,
|
|
HTTP3Options: options.HTTP3Options,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Outbound{
|
|
Adapter: outbound.NewAdapterWithDialerOptions(C.TypeHTTP, tag, []string{N.NetworkTCP, N.NetworkUDP}, options.DialerOptions),
|
|
logger: logger,
|
|
client: client,
|
|
}, nil
|
|
}
|
|
|
|
func (h *Outbound) MultiplexEnabled() bool {
|
|
return h.client.MultiplexEnabled()
|
|
}
|
|
|
|
func (h *Outbound) InterfaceUpdated(ctx context.Context) {
|
|
h.client.ResetConnections()
|
|
}
|
|
|
|
func (h *Outbound) Start(stage adapter.StartStage, scope *adapter.Scope) error {
|
|
if stage != adapter.StartStateInitialize {
|
|
return nil
|
|
}
|
|
scope.Add(h.client.Close)
|
|
return nil
|
|
}
|
|
|
|
func (h *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
|
ctx, metadata := adapter.ExtendContext(ctx)
|
|
metadata.Outbound = h.Tag()
|
|
metadata.Destination = destination
|
|
switch N.NetworkName(network) {
|
|
case N.NetworkTCP:
|
|
h.logger.InfoContext(ctx, "outbound connection to ", destination)
|
|
return h.client.DialContext(ctx, network, destination)
|
|
case N.NetworkUDP:
|
|
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
|
|
packetConn, err := h.client.ListenPacket(ctx, destination)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return bufio.NewBindPacketConn(packetConn, destination), nil
|
|
default:
|
|
return nil, E.Extend(N.ErrUnknownNetwork, network)
|
|
}
|
|
}
|
|
|
|
func (h *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
|
ctx, metadata := adapter.ExtendContext(ctx)
|
|
metadata.Outbound = h.Tag()
|
|
metadata.Destination = destination
|
|
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
|
|
return h.client.ListenPacket(ctx, destination)
|
|
}
|