sing-box/adapter/inbound.go

59 lines
1.2 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"
2022-07-07 23:36:32 +08:00
"net/netip"
2022-07-07 21:47:21 +08:00
2022-07-07 23:36:32 +08:00
C "github.com/sagernet/sing-box/constant"
2022-07-08 23:03:57 +08:00
M "github.com/sagernet/sing/common/metadata"
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
type InboundContext struct {
Inbound string
Network string
2022-07-02 14:07:50 +08:00
Source M.Socksaddr
Destination M.Socksaddr
2022-07-01 19:34:02 +08:00
Domain string
2022-07-02 14:07:50 +08:00
Protocol string
2022-07-07 21:47:21 +08:00
Outbound string
2022-07-02 22:55:10 +08:00
// cache
2022-07-07 23:36:32 +08:00
DomainStrategy C.DomainStrategy
2022-07-06 12:39:44 +08:00
SniffEnabled bool
SniffOverrideDestination bool
2022-07-07 23:36:32 +08:00
DestinationAddresses []netip.Addr
2022-07-06 12:39:44 +08:00
2022-07-02 22:55:10 +08:00
SourceGeoIPCode string
GeoIPCode string
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
}