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

196 lines
4.7 KiB
Go
Raw Normal View History

2022-06-30 21:27:56 +08:00
package adapter
2022-07-01 19:34:02 +08:00
import (
2022-07-07 21:47:21 +08:00
"context"
2026-03-03 20:59:13 +08:00
"net"
2022-07-07 23:36:32 +08:00
"net/netip"
2024-11-12 19:37:10 +08:00
"time"
2022-07-07 21:47:21 +08:00
2024-11-12 19:37:10 +08:00
C "github.com/sagernet/sing-box/constant"
2024-11-02 00:39:02 +08:00
"github.com/sagernet/sing-box/log"
2022-10-07 20:30:27 +08:00
"github.com/sagernet/sing-box/option"
2022-07-08 23:03:57 +08:00
M "github.com/sagernet/sing/common/metadata"
"github.com/miekg/dns"
2022-07-01 19:34:02 +08:00
)
2022-06-30 21:27:56 +08:00
type Inbound interface {
2024-11-21 18:10:41 +08:00
Lifecycle
2022-06-30 21:27:56 +08:00
Type() string
Tag() string
}
2022-07-01 19:34:02 +08:00
2024-10-21 23:38:34 +08:00
type TCPInjectableInbound interface {
2022-08-29 19:43:13 +08:00
Inbound
2024-10-21 23:38:34 +08:00
ConnectionHandlerEx
}
type UDPInjectableInbound interface {
Inbound
PacketConnectionHandlerEx
2022-08-29 19:43:13 +08:00
}
2024-11-02 00:39:02 +08:00
type InboundRegistry interface {
option.InboundOptionsRegistry
2024-11-09 21:16:11 +08:00
Create(ctx context.Context, router Router, logger log.ContextLogger, tag string, inboundType string, options any) (Inbound, error)
}
type InboundManager interface {
2024-11-10 16:46:59 +08:00
Lifecycle
2024-11-09 21:16:11 +08: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-02 00:39:02 +08:00
}
2022-07-01 19:34:02 +08:00
type InboundContext struct {
Inbound string
2022-07-19 22:16:49 +08:00
InboundType string
2023-06-07 20:28:21 +08:00
IPVersion uint8
2022-07-01 19:34:02 +08:00
Network string
2022-07-02 14:07:50 +08:00
Source M.Socksaddr
Destination M.Socksaddr
2022-07-17 15:11:26 +08:00
User string
2022-07-07 21:47:21 +08:00
Outbound string
2022-07-02 22:55:10 +08:00
2024-07-07 15:45:50 +08:00
// sniffer
2025-06-21 12:57:09 +08:00
Protocol string
Domain string
Client string
SniffContext any
2025-09-02 18:09:48 +08:00
SnifferNames []string
2025-06-21 12:57:09 +08:00
SniffError error
2024-07-07 15:45:50 +08:00
2022-07-02 22:55:10 +08:00
// cache
2024-11-02 00:39:02 +08:00
// Deprecated: implement in rule action
2026-02-28 17:55:56 +08:00
InboundDetour string
LastInbound string
OriginDestination M.Socksaddr
RouteOriginalDestination M.Socksaddr
2024-10-21 23:38:34 +08:00
UDPDisableDomainUnmapping bool
2024-11-06 17:30:40 +08:00
UDPConnect bool
2024-11-24 14:45:40 +08:00
UDPTimeout time.Duration
2025-01-26 09:01:00 +08:00
TLSFragment bool
TLSFragmentFallbackDelay time.Duration
2025-05-12 12:03:34 +08:00
TLSRecordFragment bool
2024-11-24 14:45:40 +08:00
2024-12-15 00:45:41 +08:00
NetworkStrategy *C.NetworkStrategy
2024-11-24 14:45:40 +08:00
NetworkType []C.InterfaceType
FallbackNetworkType []C.InterfaceType
FallbackDelay time.Duration
2024-11-06 17:30:40 +08:00
DestinationAddresses []netip.Addr
DNSResponse *dns.Msg
DestinationAddressMatchFromResponse bool
SourceGeoIPCode string
GeoIPCode string
ProcessInfo *ConnectionOwner
SourceMACAddress net.HardwareAddr
SourceHostname string
QueryType uint16
FakeIP bool
2023-02-08 16:18:40 +08:00
2023-12-01 13:24:12 +08:00
// rule cache
2023-02-08 16:18:40 +08:00
2024-06-24 09:41:00 +08:00
IPCIDRMatchSource bool
IPCIDRAcceptEmpty bool
2024-02-03 17:45:27 +08:00
SourceAddressMatch bool
SourcePortMatch bool
DestinationAddressMatch bool
DestinationPortMatch bool
DidMatch bool
2024-02-03 17:45:27 +08:00
IgnoreDestinationIPCIDRMatch bool
2023-12-01 13:24:12 +08:00
}
func (c *InboundContext) ResetRuleCache() {
c.IPCIDRMatchSource = false
2024-06-24 09:41:00 +08:00
c.IPCIDRAcceptEmpty = false
2026-03-23 12:26:19 +08:00
c.ResetRuleMatchCache()
}
func (c *InboundContext) ResetRuleMatchCache() {
2023-12-01 13:24:12 +08:00
c.SourceAddressMatch = false
c.SourcePortMatch = false
c.DestinationAddressMatch = false
c.DestinationPortMatch = false
c.DidMatch = false
}
func (c *InboundContext) DNSResponseAddressesForMatch() []netip.Addr {
return DNSResponseAddresses(c.DNSResponse)
}
func DNSResponseAddresses(response *dns.Msg) []netip.Addr {
if response == nil || response.Rcode != dns.RcodeSuccess {
return nil
}
addresses := make([]netip.Addr, 0, len(response.Answer))
for _, rawRecord := range response.Answer {
switch record := rawRecord.(type) {
case *dns.A:
addr := M.AddrFromIP(record.A)
if addr.IsValid() {
addresses = append(addresses, addr)
}
case *dns.AAAA:
addr := M.AddrFromIP(record.AAAA)
if addr.IsValid() {
addresses = append(addresses, addr)
}
case *dns.HTTPS:
for _, value := range record.SVCB.Value {
switch hint := value.(type) {
case *dns.SVCBIPv4Hint:
for _, ip := range hint.Hint {
addr := M.AddrFromIP(ip).Unmap()
if addr.IsValid() {
addresses = append(addresses, addr)
}
}
case *dns.SVCBIPv6Hint:
for _, ip := range hint.Hint {
addr := M.AddrFromIP(ip)
if addr.IsValid() {
addresses = append(addresses, addr)
}
}
}
}
}
}
return addresses
2022-07-01 19:34:02 +08:00
}
2022-07-07 21:47:21 +08: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 12:00:00 +08: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 11:55:25 +08:00
func OverrideContext(ctx context.Context) context.Context {
if metadata := ContextFrom(ctx); metadata != nil {
2025-08-13 22:42:54 +08:00
newMetadata := *metadata
2024-04-29 11:55:25 +08:00
return WithContext(ctx, &newMetadata)
}
return ctx
}