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"
|
2022-08-29 19:43:13 +08:00
|
|
|
"net"
|
2022-07-07 23:36:32 +08:00
|
|
|
"net/netip"
|
2022-07-07 21:47:21 +08:00
|
|
|
|
2022-07-23 19:01:41 +08:00
|
|
|
"github.com/sagernet/sing-box/common/process"
|
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"
|
2022-08-29 19:43:13 +08:00
|
|
|
N "github.com/sagernet/sing/common/network"
|
2022-07-01 19:34:02 +08:00
|
|
|
)
|
|
|
|
|
2022-06-30 21:27:56 +08:00
|
|
|
type Inbound interface {
|
|
|
|
Service
|
|
|
|
Type() string
|
|
|
|
Tag() string
|
|
|
|
}
|
2022-07-01 19:34:02 +08:00
|
|
|
|
2022-08-29 19:43:13 +08:00
|
|
|
type InjectableInbound interface {
|
|
|
|
Inbound
|
|
|
|
Network() []string
|
|
|
|
NewConnection(ctx context.Context, conn net.Conn, metadata InboundContext) error
|
|
|
|
NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext) error
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
Protocol string
|
|
|
|
Domain string
|
|
|
|
Client string
|
|
|
|
SniffContext any
|
|
|
|
|
2022-07-02 22:55:10 +08:00
|
|
|
// cache
|
|
|
|
|
2022-10-07 20:30:27 +08:00
|
|
|
InboundDetour string
|
|
|
|
LastInbound string
|
|
|
|
OriginDestination M.Socksaddr
|
|
|
|
InboundOptions option.InboundOptions
|
|
|
|
DestinationAddresses []netip.Addr
|
|
|
|
SourceGeoIPCode string
|
|
|
|
GeoIPCode string
|
|
|
|
ProcessInfo *process.Info
|
2023-12-01 13:24:12 +08:00
|
|
|
QueryType uint16
|
2023-06-10 16:26:40 +08:00
|
|
|
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
|
|
|
|
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
|
2023-12-01 13:24:12 +08:00
|
|
|
c.SourceAddressMatch = false
|
|
|
|
c.SourcePortMatch = false
|
|
|
|
c.DestinationAddressMatch = false
|
|
|
|
c.DestinationPortMatch = false
|
2024-02-03 17:45:27 +08:00
|
|
|
c.DidMatch = false
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func AppendContext(ctx context.Context) (context.Context, *InboundContext) {
|
|
|
|
metadata := ContextFrom(ctx)
|
|
|
|
if metadata != nil {
|
|
|
|
return ctx, metadata
|
|
|
|
}
|
|
|
|
metadata = new(InboundContext)
|
2022-07-07 23:36:32 +08:00
|
|
|
return WithContext(ctx, metadata), metadata
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
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 {
|
|
|
|
var newMetadata InboundContext
|
|
|
|
newMetadata = *metadata
|
|
|
|
return WithContext(ctx, &newMetadata)
|
|
|
|
}
|
|
|
|
return ctx
|
|
|
|
}
|