mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-22 08:31:30 +00:00
60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package adapter
|
|
|
|
import (
|
|
"context"
|
|
"net/netip"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
)
|
|
|
|
type Inbound interface {
|
|
Service
|
|
Type() string
|
|
Tag() string
|
|
}
|
|
|
|
type InboundContext struct {
|
|
Inbound string
|
|
Network string
|
|
Source M.Socksaddr
|
|
Destination M.Socksaddr
|
|
Domain string
|
|
Protocol string
|
|
Outbound string
|
|
|
|
// cache
|
|
|
|
DomainStrategy C.DomainStrategy
|
|
SniffEnabled bool
|
|
SniffOverrideDestination bool
|
|
DestinationAddresses []netip.Addr
|
|
|
|
SourceGeoIPCode string
|
|
GeoIPCode string
|
|
}
|
|
|
|
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)
|
|
return WithContext(ctx, metadata), metadata
|
|
}
|