2022-07-05 05:23:47 +00:00
|
|
|
package adapter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2024-10-21 15:38:34 +00:00
|
|
|
ConnectionHandlerFuncEx = func(ctx context.Context, conn net.Conn, metadata InboundContext, onClose N.CloseHandlerFunc)
|
|
|
|
PacketConnectionHandlerFuncEx = func(ctx context.Context, conn N.PacketConn, metadata InboundContext, onClose N.CloseHandlerFunc)
|
2022-07-05 05:23:47 +00:00
|
|
|
)
|
|
|
|
|
2024-10-21 15:38:34 +00:00
|
|
|
func NewUpstreamHandlerEx(
|
2022-07-05 05:23:47 +00:00
|
|
|
metadata InboundContext,
|
2024-10-21 15:38:34 +00:00
|
|
|
connectionHandler ConnectionHandlerFuncEx,
|
|
|
|
packetHandler PacketConnectionHandlerFuncEx,
|
|
|
|
) UpstreamHandlerAdapterEx {
|
|
|
|
return &myUpstreamHandlerWrapperEx{
|
2022-07-05 05:23:47 +00:00
|
|
|
metadata: metadata,
|
|
|
|
connectionHandler: connectionHandler,
|
|
|
|
packetHandler: packetHandler,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-21 15:38:34 +00:00
|
|
|
var _ UpstreamHandlerAdapterEx = (*myUpstreamHandlerWrapperEx)(nil)
|
2022-07-05 05:23:47 +00:00
|
|
|
|
2024-10-21 15:38:34 +00:00
|
|
|
type myUpstreamHandlerWrapperEx struct {
|
2022-07-05 05:23:47 +00:00
|
|
|
metadata InboundContext
|
2024-10-21 15:38:34 +00:00
|
|
|
connectionHandler ConnectionHandlerFuncEx
|
|
|
|
packetHandler PacketConnectionHandlerFuncEx
|
2022-07-05 05:23:47 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 15:38:34 +00:00
|
|
|
func (w *myUpstreamHandlerWrapperEx) NewConnectionEx(ctx context.Context, conn net.Conn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
|
2022-09-23 10:55:28 +00:00
|
|
|
myMetadata := w.metadata
|
2024-10-21 15:38:34 +00:00
|
|
|
if source.IsValid() {
|
|
|
|
myMetadata.Source = source
|
2022-09-23 10:55:28 +00:00
|
|
|
}
|
2024-10-21 15:38:34 +00:00
|
|
|
if destination.IsValid() {
|
|
|
|
myMetadata.Destination = destination
|
2022-09-23 10:55:28 +00:00
|
|
|
}
|
2024-10-21 15:38:34 +00:00
|
|
|
w.connectionHandler(ctx, conn, myMetadata, onClose)
|
2022-07-05 05:23:47 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 15:38:34 +00:00
|
|
|
func (w *myUpstreamHandlerWrapperEx) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
|
2022-09-23 10:55:28 +00:00
|
|
|
myMetadata := w.metadata
|
2024-10-21 15:38:34 +00:00
|
|
|
if source.IsValid() {
|
|
|
|
myMetadata.Source = source
|
2022-09-23 10:55:28 +00:00
|
|
|
}
|
2024-10-21 15:38:34 +00:00
|
|
|
if destination.IsValid() {
|
|
|
|
myMetadata.Destination = destination
|
2022-09-23 10:55:28 +00:00
|
|
|
}
|
2024-10-21 15:38:34 +00:00
|
|
|
w.packetHandler(ctx, conn, myMetadata, onClose)
|
2022-07-05 05:23:47 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 15:38:34 +00:00
|
|
|
var _ UpstreamHandlerAdapterEx = (*myUpstreamContextHandlerWrapperEx)(nil)
|
2022-07-05 05:23:47 +00:00
|
|
|
|
2024-10-21 15:38:34 +00:00
|
|
|
type myUpstreamContextHandlerWrapperEx struct {
|
|
|
|
connectionHandler ConnectionHandlerFuncEx
|
|
|
|
packetHandler PacketConnectionHandlerFuncEx
|
2022-07-05 05:23:47 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 15:38:34 +00:00
|
|
|
func NewUpstreamContextHandlerEx(
|
|
|
|
connectionHandler ConnectionHandlerFuncEx,
|
|
|
|
packetHandler PacketConnectionHandlerFuncEx,
|
|
|
|
) UpstreamHandlerAdapterEx {
|
|
|
|
return &myUpstreamContextHandlerWrapperEx{
|
2022-07-05 05:23:47 +00:00
|
|
|
connectionHandler: connectionHandler,
|
|
|
|
packetHandler: packetHandler,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-21 15:38:34 +00:00
|
|
|
func (w *myUpstreamContextHandlerWrapperEx) NewConnectionEx(ctx context.Context, conn net.Conn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
|
2022-07-10 01:15:01 +00:00
|
|
|
myMetadata := ContextFrom(ctx)
|
2024-10-21 15:38:34 +00:00
|
|
|
if source.IsValid() {
|
|
|
|
myMetadata.Source = source
|
2022-09-23 10:55:28 +00:00
|
|
|
}
|
2024-10-21 15:38:34 +00:00
|
|
|
if destination.IsValid() {
|
|
|
|
myMetadata.Destination = destination
|
2022-09-23 10:55:28 +00:00
|
|
|
}
|
2024-10-21 15:38:34 +00:00
|
|
|
w.connectionHandler(ctx, conn, *myMetadata, onClose)
|
2022-07-05 05:23:47 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 15:38:34 +00:00
|
|
|
func (w *myUpstreamContextHandlerWrapperEx) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
|
2022-07-10 01:15:01 +00:00
|
|
|
myMetadata := ContextFrom(ctx)
|
2024-10-21 15:38:34 +00:00
|
|
|
if source.IsValid() {
|
|
|
|
myMetadata.Source = source
|
|
|
|
}
|
|
|
|
if destination.IsValid() {
|
|
|
|
myMetadata.Destination = destination
|
|
|
|
}
|
|
|
|
w.packetHandler(ctx, conn, *myMetadata, onClose)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRouteHandlerEx(
|
|
|
|
metadata InboundContext,
|
|
|
|
router ConnectionRouterEx,
|
|
|
|
) UpstreamHandlerAdapterEx {
|
|
|
|
return &routeHandlerWrapperEx{
|
|
|
|
metadata: metadata,
|
|
|
|
router: router,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ UpstreamHandlerAdapterEx = (*routeHandlerWrapperEx)(nil)
|
|
|
|
|
|
|
|
type routeHandlerWrapperEx struct {
|
|
|
|
metadata InboundContext
|
|
|
|
router ConnectionRouterEx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *routeHandlerWrapperEx) NewConnectionEx(ctx context.Context, conn net.Conn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
|
|
|
|
if source.IsValid() {
|
|
|
|
r.metadata.Source = source
|
2022-09-23 10:55:28 +00:00
|
|
|
}
|
2024-10-21 15:38:34 +00:00
|
|
|
if destination.IsValid() {
|
|
|
|
r.metadata.Destination = destination
|
2022-09-23 10:55:28 +00:00
|
|
|
}
|
2024-10-21 15:38:34 +00:00
|
|
|
r.router.RouteConnectionEx(ctx, conn, r.metadata, onClose)
|
2022-07-05 05:23:47 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 15:38:34 +00:00
|
|
|
func (r *routeHandlerWrapperEx) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
|
|
|
|
if source.IsValid() {
|
|
|
|
r.metadata.Source = source
|
|
|
|
}
|
|
|
|
if destination.IsValid() {
|
|
|
|
r.metadata.Destination = destination
|
|
|
|
}
|
|
|
|
r.router.RoutePacketConnectionEx(ctx, conn, r.metadata, onClose)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRouteContextHandlerEx(
|
|
|
|
router ConnectionRouterEx,
|
|
|
|
) UpstreamHandlerAdapterEx {
|
|
|
|
return &routeContextHandlerWrapperEx{
|
|
|
|
router: router,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ UpstreamHandlerAdapterEx = (*routeContextHandlerWrapperEx)(nil)
|
|
|
|
|
|
|
|
type routeContextHandlerWrapperEx struct {
|
|
|
|
router ConnectionRouterEx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *routeContextHandlerWrapperEx) NewConnectionEx(ctx context.Context, conn net.Conn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
|
|
|
|
metadata := ContextFrom(ctx)
|
|
|
|
if source.IsValid() {
|
|
|
|
metadata.Source = source
|
|
|
|
}
|
|
|
|
if destination.IsValid() {
|
|
|
|
metadata.Destination = destination
|
|
|
|
}
|
|
|
|
r.router.RouteConnectionEx(ctx, conn, *metadata, onClose)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *routeContextHandlerWrapperEx) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
|
|
|
|
metadata := ContextFrom(ctx)
|
|
|
|
if source.IsValid() {
|
|
|
|
metadata.Source = source
|
|
|
|
}
|
|
|
|
if destination.IsValid() {
|
|
|
|
metadata.Destination = destination
|
|
|
|
}
|
|
|
|
r.router.RoutePacketConnectionEx(ctx, conn, *metadata, onClose)
|
2022-07-05 05:23:47 +00:00
|
|
|
}
|