Files
sing-box-extended-mirror/adapter/upstream.go
T

169 lines
4.9 KiB
Go
Raw Normal View History

2022-07-05 13:23:47 +08:00
package adapter
import (
"context"
"net"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
type (
2026-04-24 08:54:40 +08:00
ConnectionHandlerFunc = func(ctx context.Context, conn net.Conn, metadata InboundContext, onClose N.CloseHandlerFunc)
PacketConnectionHandlerFunc = func(ctx context.Context, conn N.PacketConn, metadata InboundContext, onClose N.CloseHandlerFunc)
2022-07-05 13:23:47 +08:00
)
2026-04-24 08:54:40 +08:00
func NewUpstreamHandler(
2022-07-05 13:23:47 +08:00
metadata InboundContext,
2026-04-24 08:54:40 +08:00
connectionHandler ConnectionHandlerFunc,
packetHandler PacketConnectionHandlerFunc,
) UpstreamHandlerAdapter {
return &myUpstreamHandlerWrapper{
2022-07-05 13:23:47 +08:00
metadata: metadata,
connectionHandler: connectionHandler,
packetHandler: packetHandler,
}
}
2026-04-24 08:54:40 +08:00
var _ UpstreamHandlerAdapter = (*myUpstreamHandlerWrapper)(nil)
2022-07-05 13:23:47 +08:00
2026-04-24 08:54:40 +08:00
type myUpstreamHandlerWrapper struct {
2022-07-05 13:23:47 +08:00
metadata InboundContext
2026-04-24 08:54:40 +08:00
connectionHandler ConnectionHandlerFunc
packetHandler PacketConnectionHandlerFunc
2022-07-05 13:23:47 +08:00
}
2026-04-24 08:54:40 +08:00
func (w *myUpstreamHandlerWrapper) NewConnectionEx(ctx context.Context, conn net.Conn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
myMetadata := w.metadata
2024-10-21 23:38:34 +08:00
if source.IsValid() {
myMetadata.Source = source
}
2024-10-21 23:38:34 +08:00
if destination.IsValid() {
myMetadata.Destination = destination
}
2024-10-21 23:38:34 +08:00
w.connectionHandler(ctx, conn, myMetadata, onClose)
2022-07-05 13:23:47 +08:00
}
2026-04-24 08:54:40 +08:00
func (w *myUpstreamHandlerWrapper) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
myMetadata := w.metadata
2024-10-21 23:38:34 +08:00
if source.IsValid() {
myMetadata.Source = source
}
2024-10-21 23:38:34 +08:00
if destination.IsValid() {
myMetadata.Destination = destination
}
2024-10-21 23:38:34 +08:00
w.packetHandler(ctx, conn, myMetadata, onClose)
2022-07-05 13:23:47 +08:00
}
2026-04-24 08:54:40 +08:00
var _ UpstreamHandlerAdapter = (*myUpstreamContextHandlerWrapper)(nil)
2022-07-05 13:23:47 +08:00
2026-04-24 08:54:40 +08:00
type myUpstreamContextHandlerWrapper struct {
connectionHandler ConnectionHandlerFunc
packetHandler PacketConnectionHandlerFunc
2022-07-05 13:23:47 +08:00
}
2026-04-24 08:54:40 +08:00
func NewUpstreamContextHandler(
connectionHandler ConnectionHandlerFunc,
packetHandler PacketConnectionHandlerFunc,
) UpstreamHandlerAdapter {
return &myUpstreamContextHandlerWrapper{
2022-07-05 13:23:47 +08:00
connectionHandler: connectionHandler,
packetHandler: packetHandler,
}
}
2026-04-24 08:54:40 +08:00
func (w *myUpstreamContextHandlerWrapper) NewConnectionEx(ctx context.Context, conn net.Conn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
2025-11-16 08:34:28 +08:00
_, myMetadata := ExtendContext(ctx)
2024-10-21 23:38:34 +08:00
if source.IsValid() {
myMetadata.Source = source
}
2024-10-21 23:38:34 +08:00
if destination.IsValid() {
myMetadata.Destination = destination
}
2024-10-21 23:38:34 +08:00
w.connectionHandler(ctx, conn, *myMetadata, onClose)
2022-07-05 13:23:47 +08:00
}
2026-04-24 08:54:40 +08:00
func (w *myUpstreamContextHandlerWrapper) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
2025-11-16 08:34:28 +08:00
_, myMetadata := ExtendContext(ctx)
2024-10-21 23:38:34 +08:00
if source.IsValid() {
myMetadata.Source = source
}
if destination.IsValid() {
myMetadata.Destination = destination
}
w.packetHandler(ctx, conn, *myMetadata, onClose)
}
2026-04-24 08:54:40 +08:00
func NewRouteHandler(
2024-10-21 23:38:34 +08:00
metadata InboundContext,
router ConnectionRouterEx,
2026-04-24 08:54:40 +08:00
) UpstreamHandlerAdapter {
return &routeHandlerWrapper{
2024-10-21 23:38:34 +08:00
metadata: metadata,
router: router,
}
}
2026-04-24 08:54:40 +08:00
var _ UpstreamHandlerAdapter = (*routeHandlerWrapper)(nil)
2024-10-21 23:38:34 +08:00
2026-04-24 08:54:40 +08:00
type routeHandlerWrapper struct {
2024-10-21 23:38:34 +08:00
metadata InboundContext
router ConnectionRouterEx
}
2026-04-24 08:54:40 +08:00
func (r *routeHandlerWrapper) NewConnectionEx(ctx context.Context, conn net.Conn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
2024-10-21 23:38:34 +08:00
if source.IsValid() {
r.metadata.Source = source
}
2024-10-21 23:38:34 +08:00
if destination.IsValid() {
r.metadata.Destination = destination
}
2024-10-21 23:38:34 +08:00
r.router.RouteConnectionEx(ctx, conn, r.metadata, onClose)
2022-07-05 13:23:47 +08:00
}
2026-04-24 08:54:40 +08:00
func (r *routeHandlerWrapper) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
2024-10-21 23:38:34 +08:00
if source.IsValid() {
r.metadata.Source = source
}
if destination.IsValid() {
r.metadata.Destination = destination
}
r.router.RoutePacketConnectionEx(ctx, conn, r.metadata, onClose)
}
2026-04-24 08:54:40 +08:00
func NewRouteContextHandler(
2024-10-21 23:38:34 +08:00
router ConnectionRouterEx,
2026-04-24 08:54:40 +08:00
) UpstreamHandlerAdapter {
return &routeContextHandlerWrapper{
2024-10-21 23:38:34 +08:00
router: router,
}
}
2026-04-24 08:54:40 +08:00
var _ UpstreamHandlerAdapter = (*routeContextHandlerWrapper)(nil)
2024-10-21 23:38:34 +08:00
2026-04-24 08:54:40 +08:00
type routeContextHandlerWrapper struct {
2024-10-21 23:38:34 +08:00
router ConnectionRouterEx
}
2026-04-24 08:54:40 +08:00
func (r *routeContextHandlerWrapper) NewConnectionEx(ctx context.Context, conn net.Conn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
2025-11-16 08:34:28 +08:00
_, metadata := ExtendContext(ctx)
2024-10-21 23:38:34 +08:00
if source.IsValid() {
metadata.Source = source
}
if destination.IsValid() {
metadata.Destination = destination
}
r.router.RouteConnectionEx(ctx, conn, *metadata, onClose)
}
2026-04-24 08:54:40 +08:00
func (r *routeContextHandlerWrapper) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
2025-11-16 08:34:28 +08:00
_, metadata := ExtendContext(ctx)
2024-10-21 23:38:34 +08:00
if source.IsValid() {
metadata.Source = source
}
if destination.IsValid() {
metadata.Destination = destination
}
r.router.RoutePacketConnectionEx(ctx, conn, *metadata, onClose)
2022-07-05 13:23:47 +08:00
}