sing-box/inbound/http.go

120 lines
3.8 KiB
Go
Raw Normal View History

2022-07-01 19:34:02 +08:00
package inbound
import (
std_bufio "bufio"
"context"
"net"
"github.com/sagernet/sing-box/adapter"
2022-09-09 18:45:10 +08:00
"github.com/sagernet/sing-box/common/tls"
"github.com/sagernet/sing-box/common/uot"
2022-07-01 19:34:02 +08:00
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
2022-07-02 14:07:50 +08:00
"github.com/sagernet/sing-box/option"
2022-07-25 08:14:09 +08:00
"github.com/sagernet/sing/common"
2022-07-08 23:03:57 +08:00
"github.com/sagernet/sing/common/auth"
2022-07-30 22:00:04 +08:00
E "github.com/sagernet/sing/common/exceptions"
2022-07-17 15:11:26 +08:00
N "github.com/sagernet/sing/common/network"
2022-07-08 23:03:57 +08:00
"github.com/sagernet/sing/protocol/http"
2022-07-01 19:34:02 +08:00
)
2022-08-29 19:43:13 +08:00
var (
2024-10-21 23:38:34 +08:00
_ adapter.Inbound = (*HTTP)(nil)
_ adapter.TCPInjectableInbound = (*HTTP)(nil)
2022-08-29 19:43:13 +08:00
)
2022-07-01 19:34:02 +08:00
type HTTP struct {
myInboundAdapter
authenticator *auth.Authenticator
2022-09-09 18:45:10 +08:00
tlsConfig tls.ServerConfig
2022-07-01 19:34:02 +08:00
}
2022-07-25 08:14:09 +08:00
func NewHTTP(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.HTTPMixedInboundOptions) (*HTTP, error) {
2022-07-01 19:34:02 +08:00
inbound := &HTTP{
2022-07-25 08:14:09 +08:00
myInboundAdapter: myInboundAdapter{
protocol: C.TypeHTTP,
2022-07-30 00:29:22 +08:00
network: []string{N.NetworkTCP},
ctx: ctx,
router: uot.NewRouter(router, logger),
logger: logger,
tag: tag,
listenOptions: options.ListenOptions,
setSystemProxy: options.SetSystemProxy,
2022-07-01 19:34:02 +08:00
},
2022-07-25 08:14:09 +08:00
authenticator: auth.NewAuthenticator(options.Users),
}
if options.TLS != nil {
2023-08-29 13:43:42 +08:00
tlsConfig, err := tls.NewServer(ctx, logger, common.PtrValueOrDefault(options.TLS))
2022-07-25 08:14:09 +08:00
if err != nil {
return nil, err
}
inbound.tlsConfig = tlsConfig
2022-07-01 19:34:02 +08:00
}
inbound.connHandler = inbound
2022-07-25 08:14:09 +08:00
return inbound, nil
2022-07-01 19:34:02 +08:00
}
2022-07-30 22:00:04 +08:00
func (h *HTTP) Start() error {
if h.tlsConfig != nil {
err := h.tlsConfig.Start()
if err != nil {
return E.Cause(err, "create TLS config")
}
}
return h.myInboundAdapter.Start()
}
func (h *HTTP) Close() error {
return common.Close(
&h.myInboundAdapter,
2022-09-09 18:45:10 +08:00
h.tlsConfig,
2022-07-30 22:00:04 +08:00
)
}
2024-10-21 23:38:34 +08:00
func (h *HTTP) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
err := h.newConnection(ctx, conn, metadata, onClose)
N.CloseOnHandshakeFailure(conn, onClose, err)
if err != nil {
h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
}
}
func (h *HTTP) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) error {
2022-09-30 11:27:18 +08:00
var err error
2022-07-25 08:14:09 +08:00
if h.tlsConfig != nil {
2022-09-30 11:27:18 +08:00
conn, err = tls.ServerHandshake(ctx, conn, h.tlsConfig)
if err != nil {
return err
}
2022-07-25 08:14:09 +08:00
}
2024-10-21 23:38:34 +08:00
return http.HandleConnectionEx(ctx, conn, std_bufio.NewReader(conn), h.authenticator, nil, h.upstreamUserHandlerEx(metadata), metadata.Source, onClose)
2022-08-29 19:43:13 +08:00
}
2024-10-21 23:38:34 +08:00
func (a *myInboundAdapter) upstreamUserHandlerEx(metadata adapter.InboundContext) adapter.UpstreamHandlerAdapterEx {
return adapter.NewUpstreamHandlerEx(metadata, a.newUserConnection, a.streamUserPacketConnection)
2022-07-17 15:11:26 +08:00
}
2024-10-21 23:38:34 +08:00
func (a *myInboundAdapter) newUserConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
2022-07-17 15:11:26 +08:00
user, loaded := auth.UserFromContext[string](ctx)
if !loaded {
a.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
2024-10-21 23:38:34 +08:00
a.router.RouteConnectionEx(ctx, conn, metadata, onClose)
return
2022-07-17 15:11:26 +08:00
}
metadata.User = user
a.logger.InfoContext(ctx, "[", user, "] inbound connection to ", metadata.Destination)
2024-10-21 23:38:34 +08:00
a.router.RouteConnectionEx(ctx, conn, metadata, onClose)
2022-07-17 15:11:26 +08:00
}
2024-10-21 23:38:34 +08:00
func (a *myInboundAdapter) streamUserPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
2022-12-18 15:50:22 +08:00
user, loaded := auth.UserFromContext[string](ctx)
if !loaded {
a.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
2024-10-21 23:38:34 +08:00
a.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
return
2022-12-18 15:50:22 +08:00
}
metadata.User = user
a.logger.InfoContext(ctx, "[", user, "] inbound packet connection to ", metadata.Destination)
2024-10-21 23:38:34 +08:00
a.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
2022-07-01 19:34:02 +08:00
}