2024-10-21 23:38:34 +08:00
|
|
|
package adapter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Deprecated
|
|
|
|
ConnectionHandlerFunc = func(ctx context.Context, conn net.Conn, metadata InboundContext) error
|
|
|
|
// Deprecated
|
|
|
|
PacketConnectionHandlerFunc = func(ctx context.Context, conn N.PacketConn, metadata InboundContext) error
|
|
|
|
)
|
|
|
|
|
|
|
|
// Deprecated
|
2024-11-23 22:34:02 +08:00
|
|
|
//
|
|
|
|
//nolint:staticcheck
|
2024-10-21 23:38:34 +08:00
|
|
|
func NewUpstreamHandler(
|
|
|
|
metadata InboundContext,
|
|
|
|
connectionHandler ConnectionHandlerFunc,
|
|
|
|
packetHandler PacketConnectionHandlerFunc,
|
|
|
|
errorHandler E.Handler,
|
|
|
|
) UpstreamHandlerAdapter {
|
|
|
|
return &myUpstreamHandlerWrapper{
|
|
|
|
metadata: metadata,
|
|
|
|
connectionHandler: connectionHandler,
|
|
|
|
packetHandler: packetHandler,
|
|
|
|
errorHandler: errorHandler,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ UpstreamHandlerAdapter = (*myUpstreamHandlerWrapper)(nil)
|
|
|
|
|
2024-11-23 22:34:02 +08:00
|
|
|
// Deprecated: use myUpstreamHandlerWrapperEx instead.
|
|
|
|
//
|
|
|
|
//nolint:staticcheck
|
2024-10-21 23:38:34 +08:00
|
|
|
type myUpstreamHandlerWrapper struct {
|
|
|
|
metadata InboundContext
|
|
|
|
connectionHandler ConnectionHandlerFunc
|
|
|
|
packetHandler PacketConnectionHandlerFunc
|
|
|
|
errorHandler E.Handler
|
|
|
|
}
|
|
|
|
|
2024-11-23 22:34:02 +08:00
|
|
|
// Deprecated: use myUpstreamHandlerWrapperEx instead.
|
2024-10-21 23:38:34 +08:00
|
|
|
func (w *myUpstreamHandlerWrapper) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
|
|
|
myMetadata := w.metadata
|
|
|
|
if metadata.Source.IsValid() {
|
|
|
|
myMetadata.Source = metadata.Source
|
|
|
|
}
|
|
|
|
if metadata.Destination.IsValid() {
|
|
|
|
myMetadata.Destination = metadata.Destination
|
|
|
|
}
|
|
|
|
return w.connectionHandler(ctx, conn, myMetadata)
|
|
|
|
}
|
|
|
|
|
2024-11-23 22:34:02 +08:00
|
|
|
// Deprecated: use myUpstreamHandlerWrapperEx instead.
|
2024-10-21 23:38:34 +08:00
|
|
|
func (w *myUpstreamHandlerWrapper) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error {
|
|
|
|
myMetadata := w.metadata
|
|
|
|
if metadata.Source.IsValid() {
|
|
|
|
myMetadata.Source = metadata.Source
|
|
|
|
}
|
|
|
|
if metadata.Destination.IsValid() {
|
|
|
|
myMetadata.Destination = metadata.Destination
|
|
|
|
}
|
|
|
|
return w.packetHandler(ctx, conn, myMetadata)
|
|
|
|
}
|
|
|
|
|
2024-11-23 22:34:02 +08:00
|
|
|
// Deprecated: use myUpstreamHandlerWrapperEx instead.
|
2024-10-21 23:38:34 +08:00
|
|
|
func (w *myUpstreamHandlerWrapper) NewError(ctx context.Context, err error) {
|
|
|
|
w.errorHandler.NewError(ctx, err)
|
|
|
|
}
|
|
|
|
|
2024-11-23 22:34:02 +08:00
|
|
|
// Deprecated: removed
|
2024-10-21 23:38:34 +08:00
|
|
|
func UpstreamMetadata(metadata InboundContext) M.Metadata {
|
|
|
|
return M.Metadata{
|
|
|
|
Source: metadata.Source,
|
|
|
|
Destination: metadata.Destination,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-23 22:34:02 +08:00
|
|
|
// Deprecated: Use NewUpstreamContextHandlerEx instead.
|
2024-10-21 23:38:34 +08:00
|
|
|
type myUpstreamContextHandlerWrapper struct {
|
|
|
|
connectionHandler ConnectionHandlerFunc
|
|
|
|
packetHandler PacketConnectionHandlerFunc
|
|
|
|
errorHandler E.Handler
|
|
|
|
}
|
|
|
|
|
2024-11-23 22:34:02 +08:00
|
|
|
// Deprecated: Use NewUpstreamContextHandlerEx instead.
|
2024-10-21 23:38:34 +08:00
|
|
|
func NewUpstreamContextHandler(
|
|
|
|
connectionHandler ConnectionHandlerFunc,
|
|
|
|
packetHandler PacketConnectionHandlerFunc,
|
|
|
|
errorHandler E.Handler,
|
|
|
|
) UpstreamHandlerAdapter {
|
|
|
|
return &myUpstreamContextHandlerWrapper{
|
|
|
|
connectionHandler: connectionHandler,
|
|
|
|
packetHandler: packetHandler,
|
|
|
|
errorHandler: errorHandler,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-23 22:34:02 +08:00
|
|
|
// Deprecated: Use NewUpstreamContextHandlerEx instead.
|
2024-10-21 23:38:34 +08:00
|
|
|
func (w *myUpstreamContextHandlerWrapper) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
|
|
|
myMetadata := ContextFrom(ctx)
|
|
|
|
if metadata.Source.IsValid() {
|
|
|
|
myMetadata.Source = metadata.Source
|
|
|
|
}
|
|
|
|
if metadata.Destination.IsValid() {
|
|
|
|
myMetadata.Destination = metadata.Destination
|
|
|
|
}
|
|
|
|
return w.connectionHandler(ctx, conn, *myMetadata)
|
|
|
|
}
|
|
|
|
|
2024-11-23 22:34:02 +08:00
|
|
|
// Deprecated: Use NewUpstreamContextHandlerEx instead.
|
2024-10-21 23:38:34 +08:00
|
|
|
func (w *myUpstreamContextHandlerWrapper) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error {
|
|
|
|
myMetadata := ContextFrom(ctx)
|
|
|
|
if metadata.Source.IsValid() {
|
|
|
|
myMetadata.Source = metadata.Source
|
|
|
|
}
|
|
|
|
if metadata.Destination.IsValid() {
|
|
|
|
myMetadata.Destination = metadata.Destination
|
|
|
|
}
|
|
|
|
return w.packetHandler(ctx, conn, *myMetadata)
|
|
|
|
}
|
|
|
|
|
2024-11-23 22:34:02 +08:00
|
|
|
// Deprecated: Use NewUpstreamContextHandlerEx instead.
|
2024-10-21 23:38:34 +08:00
|
|
|
func (w *myUpstreamContextHandlerWrapper) NewError(ctx context.Context, err error) {
|
|
|
|
w.errorHandler.NewError(ctx, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deprecated: Use ConnectionRouterEx instead.
|
|
|
|
func NewRouteHandler(
|
|
|
|
metadata InboundContext,
|
|
|
|
router ConnectionRouter,
|
|
|
|
logger logger.ContextLogger,
|
|
|
|
) UpstreamHandlerAdapter {
|
|
|
|
return &routeHandlerWrapper{
|
|
|
|
metadata: metadata,
|
|
|
|
router: router,
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deprecated: Use ConnectionRouterEx instead.
|
|
|
|
func NewRouteContextHandler(
|
|
|
|
router ConnectionRouter,
|
|
|
|
logger logger.ContextLogger,
|
|
|
|
) UpstreamHandlerAdapter {
|
|
|
|
return &routeContextHandlerWrapper{
|
|
|
|
router: router,
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ UpstreamHandlerAdapter = (*routeHandlerWrapper)(nil)
|
|
|
|
|
|
|
|
// Deprecated: Use ConnectionRouterEx instead.
|
2024-11-23 22:34:02 +08:00
|
|
|
//
|
|
|
|
//nolint:staticcheck
|
2024-10-21 23:38:34 +08:00
|
|
|
type routeHandlerWrapper struct {
|
|
|
|
metadata InboundContext
|
|
|
|
router ConnectionRouter
|
|
|
|
logger logger.ContextLogger
|
|
|
|
}
|
|
|
|
|
2024-11-23 22:34:02 +08:00
|
|
|
// Deprecated: Use ConnectionRouterEx instead.
|
2024-10-21 23:38:34 +08:00
|
|
|
func (w *routeHandlerWrapper) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
|
|
|
myMetadata := w.metadata
|
|
|
|
if metadata.Source.IsValid() {
|
|
|
|
myMetadata.Source = metadata.Source
|
|
|
|
}
|
|
|
|
if metadata.Destination.IsValid() {
|
|
|
|
myMetadata.Destination = metadata.Destination
|
|
|
|
}
|
|
|
|
return w.router.RouteConnection(ctx, conn, myMetadata)
|
|
|
|
}
|
|
|
|
|
2024-11-23 22:34:02 +08:00
|
|
|
// Deprecated: Use ConnectionRouterEx instead.
|
2024-10-21 23:38:34 +08:00
|
|
|
func (w *routeHandlerWrapper) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error {
|
|
|
|
myMetadata := w.metadata
|
|
|
|
if metadata.Source.IsValid() {
|
|
|
|
myMetadata.Source = metadata.Source
|
|
|
|
}
|
|
|
|
if metadata.Destination.IsValid() {
|
|
|
|
myMetadata.Destination = metadata.Destination
|
|
|
|
}
|
|
|
|
return w.router.RoutePacketConnection(ctx, conn, myMetadata)
|
|
|
|
}
|
|
|
|
|
2024-11-23 22:34:02 +08:00
|
|
|
// Deprecated: Use ConnectionRouterEx instead.
|
2024-10-21 23:38:34 +08:00
|
|
|
func (w *routeHandlerWrapper) NewError(ctx context.Context, err error) {
|
|
|
|
w.logger.ErrorContext(ctx, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ UpstreamHandlerAdapter = (*routeContextHandlerWrapper)(nil)
|
|
|
|
|
|
|
|
// Deprecated: Use ConnectionRouterEx instead.
|
|
|
|
type routeContextHandlerWrapper struct {
|
|
|
|
router ConnectionRouter
|
|
|
|
logger logger.ContextLogger
|
|
|
|
}
|
|
|
|
|
2024-11-23 22:34:02 +08:00
|
|
|
// Deprecated: Use ConnectionRouterEx instead.
|
2024-10-21 23:38:34 +08:00
|
|
|
func (w *routeContextHandlerWrapper) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
|
|
|
myMetadata := ContextFrom(ctx)
|
|
|
|
if metadata.Source.IsValid() {
|
|
|
|
myMetadata.Source = metadata.Source
|
|
|
|
}
|
|
|
|
if metadata.Destination.IsValid() {
|
|
|
|
myMetadata.Destination = metadata.Destination
|
|
|
|
}
|
|
|
|
return w.router.RouteConnection(ctx, conn, *myMetadata)
|
|
|
|
}
|
|
|
|
|
2024-11-23 22:34:02 +08:00
|
|
|
// Deprecated: Use ConnectionRouterEx instead.
|
2024-10-21 23:38:34 +08:00
|
|
|
func (w *routeContextHandlerWrapper) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error {
|
|
|
|
myMetadata := ContextFrom(ctx)
|
|
|
|
if metadata.Source.IsValid() {
|
|
|
|
myMetadata.Source = metadata.Source
|
|
|
|
}
|
|
|
|
if metadata.Destination.IsValid() {
|
|
|
|
myMetadata.Destination = metadata.Destination
|
|
|
|
}
|
|
|
|
return w.router.RoutePacketConnection(ctx, conn, *myMetadata)
|
|
|
|
}
|
|
|
|
|
2024-11-23 22:34:02 +08:00
|
|
|
// Deprecated: Use ConnectionRouterEx instead.
|
2024-10-21 23:38:34 +08:00
|
|
|
func (w *routeContextHandlerWrapper) NewError(ctx context.Context, err error) {
|
|
|
|
w.logger.ErrorContext(ctx, err)
|
|
|
|
}
|