2022-06-30 13:27:56 +00:00
|
|
|
package adapter
|
|
|
|
|
2022-07-01 11:34:02 +00:00
|
|
|
import (
|
2022-07-07 13:47:21 +00:00
|
|
|
"context"
|
2022-07-07 15:36:32 +00:00
|
|
|
"net/netip"
|
2024-11-12 11:37:10 +00:00
|
|
|
"time"
|
2022-07-07 13:47:21 +00:00
|
|
|
|
2022-07-23 11:01:41 +00:00
|
|
|
"github.com/sagernet/sing-box/common/process"
|
2024-11-12 11:37:10 +00:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
2024-11-01 16:39:02 +00:00
|
|
|
"github.com/sagernet/sing-box/log"
|
2022-10-07 12:30:27 +00:00
|
|
|
"github.com/sagernet/sing-box/option"
|
2022-07-08 15:03:57 +00:00
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
2022-07-01 11:34:02 +00:00
|
|
|
)
|
|
|
|
|
2022-06-30 13:27:56 +00:00
|
|
|
type Inbound interface {
|
2024-11-21 10:10:41 +00:00
|
|
|
Lifecycle
|
2022-06-30 13:27:56 +00:00
|
|
|
Type() string
|
|
|
|
Tag() string
|
|
|
|
}
|
2022-07-01 11:34:02 +00:00
|
|
|
|
2024-10-21 15:38:34 +00:00
|
|
|
type TCPInjectableInbound interface {
|
2022-08-29 11:43:13 +00:00
|
|
|
Inbound
|
2024-10-21 15:38:34 +00:00
|
|
|
ConnectionHandlerEx
|
|
|
|
}
|
|
|
|
|
|
|
|
type UDPInjectableInbound interface {
|
|
|
|
Inbound
|
|
|
|
PacketConnectionHandlerEx
|
2022-08-29 11:43:13 +00:00
|
|
|
}
|
|
|
|
|
2024-11-01 16:39:02 +00:00
|
|
|
type InboundRegistry interface {
|
|
|
|
option.InboundOptionsRegistry
|
2024-11-09 13:16:11 +00:00
|
|
|
Create(ctx context.Context, router Router, logger log.ContextLogger, tag string, inboundType string, options any) (Inbound, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type InboundManager interface {
|
2024-11-10 08:46:59 +00:00
|
|
|
Lifecycle
|
2024-11-09 13:16:11 +00:00
|
|
|
Inbounds() []Inbound
|
|
|
|
Get(tag string) (Inbound, bool)
|
|
|
|
Remove(tag string) error
|
|
|
|
Create(ctx context.Context, router Router, logger log.ContextLogger, tag string, inboundType string, options any) error
|
2024-11-01 16:39:02 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 11:34:02 +00:00
|
|
|
type InboundContext struct {
|
|
|
|
Inbound string
|
2022-07-19 14:16:49 +00:00
|
|
|
InboundType string
|
2023-06-07 12:28:21 +00:00
|
|
|
IPVersion uint8
|
2022-07-01 11:34:02 +00:00
|
|
|
Network string
|
2022-07-02 06:07:50 +00:00
|
|
|
Source M.Socksaddr
|
|
|
|
Destination M.Socksaddr
|
2022-07-17 07:11:26 +00:00
|
|
|
User string
|
2022-07-07 13:47:21 +00:00
|
|
|
Outbound string
|
2022-07-02 14:55:10 +00:00
|
|
|
|
2024-07-07 07:45:50 +00:00
|
|
|
// sniffer
|
|
|
|
|
|
|
|
Protocol string
|
|
|
|
Domain string
|
|
|
|
Client string
|
|
|
|
SniffContext any
|
|
|
|
|
2022-07-02 14:55:10 +00:00
|
|
|
// cache
|
|
|
|
|
2024-11-01 16:39:02 +00:00
|
|
|
// Deprecated: implement in rule action
|
2024-11-14 10:31:37 +00:00
|
|
|
InboundDetour string
|
|
|
|
LastInbound string
|
|
|
|
OriginDestination M.Socksaddr
|
|
|
|
RouteOriginalDestination M.Socksaddr
|
2024-11-23 14:34:02 +00:00
|
|
|
// Deprecated: to be removed
|
|
|
|
//nolint:staticcheck
|
2024-10-21 15:38:34 +00:00
|
|
|
InboundOptions option.InboundOptions
|
|
|
|
UDPDisableDomainUnmapping bool
|
2024-11-06 09:30:40 +00:00
|
|
|
UDPConnect bool
|
2024-11-24 06:45:40 +00:00
|
|
|
UDPTimeout time.Duration
|
|
|
|
|
|
|
|
NetworkStrategy C.NetworkStrategy
|
|
|
|
NetworkType []C.InterfaceType
|
|
|
|
FallbackNetworkType []C.InterfaceType
|
|
|
|
FallbackDelay time.Duration
|
2024-11-06 09:30:40 +00:00
|
|
|
|
|
|
|
DNSServer string
|
2024-10-22 14:01:28 +00:00
|
|
|
|
|
|
|
DestinationAddresses []netip.Addr
|
|
|
|
SourceGeoIPCode string
|
|
|
|
GeoIPCode string
|
|
|
|
ProcessInfo *process.Info
|
|
|
|
QueryType uint16
|
|
|
|
FakeIP bool
|
2023-02-08 08:18:40 +00:00
|
|
|
|
2023-12-01 05:24:12 +00:00
|
|
|
// rule cache
|
2023-02-08 08:18:40 +00:00
|
|
|
|
2024-06-24 01:41:00 +00:00
|
|
|
IPCIDRMatchSource bool
|
|
|
|
IPCIDRAcceptEmpty bool
|
|
|
|
|
2024-02-03 09:45:27 +00:00
|
|
|
SourceAddressMatch bool
|
|
|
|
SourcePortMatch bool
|
|
|
|
DestinationAddressMatch bool
|
|
|
|
DestinationPortMatch bool
|
|
|
|
DidMatch bool
|
|
|
|
IgnoreDestinationIPCIDRMatch bool
|
2023-12-01 05:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *InboundContext) ResetRuleCache() {
|
|
|
|
c.IPCIDRMatchSource = false
|
2024-06-24 01:41:00 +00:00
|
|
|
c.IPCIDRAcceptEmpty = false
|
2023-12-01 05:24:12 +00:00
|
|
|
c.SourceAddressMatch = false
|
|
|
|
c.SourcePortMatch = false
|
|
|
|
c.DestinationAddressMatch = false
|
|
|
|
c.DestinationPortMatch = false
|
2024-02-03 09:45:27 +00:00
|
|
|
c.DidMatch = false
|
2022-07-01 11:34:02 +00:00
|
|
|
}
|
2022-07-07 13:47:21 +00:00
|
|
|
|
|
|
|
type inboundContextKey struct{}
|
|
|
|
|
|
|
|
func WithContext(ctx context.Context, inboundContext *InboundContext) context.Context {
|
|
|
|
return context.WithValue(ctx, (*inboundContextKey)(nil), inboundContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ContextFrom(ctx context.Context) *InboundContext {
|
|
|
|
metadata := ctx.Value((*inboundContextKey)(nil))
|
|
|
|
if metadata == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return metadata.(*InboundContext)
|
|
|
|
}
|
|
|
|
|
2023-10-25 04:00:00 +00:00
|
|
|
func ExtendContext(ctx context.Context) (context.Context, *InboundContext) {
|
|
|
|
var newMetadata InboundContext
|
|
|
|
if metadata := ContextFrom(ctx); metadata != nil {
|
|
|
|
newMetadata = *metadata
|
|
|
|
}
|
|
|
|
return WithContext(ctx, &newMetadata), &newMetadata
|
|
|
|
}
|
2024-04-29 03:55:25 +00:00
|
|
|
|
|
|
|
func OverrideContext(ctx context.Context) context.Context {
|
|
|
|
if metadata := ContextFrom(ctx); metadata != nil {
|
|
|
|
var newMetadata InboundContext
|
|
|
|
newMetadata = *metadata
|
|
|
|
return WithContext(ctx, &newMetadata)
|
|
|
|
}
|
|
|
|
return ctx
|
|
|
|
}
|