sing-box/inbound/http.go

108 lines
3.2 KiB
Go
Raw Normal View History

2022-07-01 11:34:02 +00:00
package inbound
import (
std_bufio "bufio"
"context"
"net"
2022-08-29 11:43:13 +00:00
"os"
2022-07-01 11:34:02 +00:00
"github.com/sagernet/sing-box/adapter"
2022-09-09 10:45:10 +00:00
"github.com/sagernet/sing-box/common/tls"
2022-07-01 11:34:02 +00:00
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
2022-07-02 06:07:50 +00:00
"github.com/sagernet/sing-box/option"
2022-07-25 00:14:09 +00:00
"github.com/sagernet/sing/common"
2022-07-08 15:03:57 +00:00
"github.com/sagernet/sing/common/auth"
2022-07-30 14:00:04 +00:00
E "github.com/sagernet/sing/common/exceptions"
2022-07-17 07:11:26 +00:00
N "github.com/sagernet/sing/common/network"
2022-07-08 15:03:57 +00:00
"github.com/sagernet/sing/protocol/http"
2022-07-01 11:34:02 +00:00
)
2022-08-29 11:43:13 +00:00
var (
_ adapter.Inbound = (*HTTP)(nil)
_ adapter.InjectableInbound = (*HTTP)(nil)
)
2022-07-01 11:34:02 +00:00
type HTTP struct {
myInboundAdapter
authenticator auth.Authenticator
2022-09-09 10:45:10 +00:00
tlsConfig tls.ServerConfig
2022-07-01 11:34:02 +00:00
}
2022-07-25 00:14:09 +00:00
func NewHTTP(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.HTTPMixedInboundOptions) (*HTTP, error) {
2022-07-01 11:34:02 +00:00
inbound := &HTTP{
2022-07-25 00:14:09 +00:00
myInboundAdapter: myInboundAdapter{
protocol: C.TypeHTTP,
2022-07-29 16:29:22 +00:00
network: []string{N.NetworkTCP},
ctx: ctx,
router: router,
logger: logger,
tag: tag,
listenOptions: options.ListenOptions,
setSystemProxy: options.SetSystemProxy,
2022-07-01 11:34:02 +00:00
},
2022-07-25 00:14:09 +00:00
authenticator: auth.NewAuthenticator(options.Users),
}
if options.TLS != nil {
2022-09-09 10:45:10 +00:00
tlsConfig, err := tls.NewServer(ctx, logger, common.PtrValueOrDefault(options.TLS))
2022-07-25 00:14:09 +00:00
if err != nil {
return nil, err
}
inbound.tlsConfig = tlsConfig
2022-07-01 11:34:02 +00:00
}
inbound.connHandler = inbound
2022-07-25 00:14:09 +00:00
return inbound, nil
2022-07-01 11:34:02 +00:00
}
2022-07-30 14:00:04 +00: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 10:45:10 +00:00
h.tlsConfig,
2022-07-30 14:00:04 +00:00
)
}
2022-07-01 11:34:02 +00:00
func (h *HTTP) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
2022-09-30 03:27:18 +00:00
var err error
2022-07-25 00:14:09 +00:00
if h.tlsConfig != nil {
2022-09-30 03:27:18 +00:00
conn, err = tls.ServerHandshake(ctx, conn, h.tlsConfig)
if err != nil {
return err
}
2022-07-25 00:14:09 +00:00
}
2022-08-23 11:44:40 +00:00
return http.HandleConnection(ctx, conn, std_bufio.NewReader(conn), h.authenticator, h.upstreamUserHandler(metadata), adapter.UpstreamMetadata(metadata))
2022-07-17 07:11:26 +00:00
}
2022-08-29 11:43:13 +00:00
func (h *HTTP) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
return os.ErrInvalid
}
2022-07-17 07:11:26 +00:00
func (a *myInboundAdapter) upstreamUserHandler(metadata adapter.InboundContext) adapter.UpstreamHandlerAdapter {
return adapter.NewUpstreamHandler(metadata, a.newUserConnection, a.streamUserPacketConnection, a)
}
func (a *myInboundAdapter) newUserConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
user, loaded := auth.UserFromContext[string](ctx)
if !loaded {
a.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
return a.router.RouteConnection(ctx, conn, metadata)
}
metadata.User = user
a.logger.InfoContext(ctx, "[", user, "] inbound connection to ", metadata.Destination)
return a.router.RouteConnection(ctx, conn, metadata)
}
func (a *myInboundAdapter) streamUserPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
a.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
return a.router.RoutePacketConnection(ctx, conn, metadata)
2022-07-01 11:34:02 +00:00
}