2020-11-25 13:01:53 +02:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-12-04 03:36:16 +02:00
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/common/session"
|
|
|
|
"github.com/xtls/xray-core/features/routing"
|
2020-11-25 13:01:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Context is an implementation of routing.Context, which is a wrapper of context.context with session info.
|
|
|
|
type Context struct {
|
|
|
|
Inbound *session.Inbound
|
|
|
|
Outbound *session.Outbound
|
|
|
|
Content *session.Content
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetInboundTag implements routing.Context.
|
|
|
|
func (ctx *Context) GetInboundTag() string {
|
|
|
|
if ctx.Inbound == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return ctx.Inbound.Tag
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSourceIPs implements routing.Context.
|
|
|
|
func (ctx *Context) GetSourceIPs() []net.IP {
|
|
|
|
if ctx.Inbound == nil || !ctx.Inbound.Source.IsValid() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
dest := ctx.Inbound.Source
|
|
|
|
if dest.Address.Family().IsDomain() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return []net.IP{dest.Address.IP()}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSourcePort implements routing.Context.
|
|
|
|
func (ctx *Context) GetSourcePort() net.Port {
|
|
|
|
if ctx.Inbound == nil || !ctx.Inbound.Source.IsValid() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return ctx.Inbound.Source.Port
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTargetIPs implements routing.Context.
|
|
|
|
func (ctx *Context) GetTargetIPs() []net.IP {
|
|
|
|
if ctx.Outbound == nil || !ctx.Outbound.Target.IsValid() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Outbound.Target.Address.Family().IsIP() {
|
|
|
|
return []net.IP{ctx.Outbound.Target.Address.IP()}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTargetPort implements routing.Context.
|
|
|
|
func (ctx *Context) GetTargetPort() net.Port {
|
|
|
|
if ctx.Outbound == nil || !ctx.Outbound.Target.IsValid() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return ctx.Outbound.Target.Port
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTargetDomain implements routing.Context.
|
|
|
|
func (ctx *Context) GetTargetDomain() string {
|
|
|
|
if ctx.Outbound == nil || !ctx.Outbound.Target.IsValid() {
|
|
|
|
return ""
|
|
|
|
}
|
2021-09-16 10:05:48 +03:00
|
|
|
dest := ctx.Outbound.RouteTarget
|
|
|
|
if dest.IsValid() && dest.Address.Family().IsDomain() {
|
|
|
|
return dest.Address.Domain()
|
|
|
|
}
|
|
|
|
|
|
|
|
dest = ctx.Outbound.Target
|
2020-11-25 13:01:53 +02:00
|
|
|
if !dest.Address.Family().IsDomain() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return dest.Address.Domain()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNetwork implements routing.Context.
|
|
|
|
func (ctx *Context) GetNetwork() net.Network {
|
|
|
|
if ctx.Outbound == nil {
|
|
|
|
return net.Network_Unknown
|
|
|
|
}
|
|
|
|
return ctx.Outbound.Target.Network
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetProtocol implements routing.Context.
|
|
|
|
func (ctx *Context) GetProtocol() string {
|
|
|
|
if ctx.Content == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return ctx.Content.Protocol
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUser implements routing.Context.
|
|
|
|
func (ctx *Context) GetUser() string {
|
|
|
|
if ctx.Inbound == nil || ctx.Inbound.User == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return ctx.Inbound.User.Email
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAttributes implements routing.Context.
|
|
|
|
func (ctx *Context) GetAttributes() map[string]string {
|
|
|
|
if ctx.Content == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ctx.Content.Attributes
|
|
|
|
}
|
|
|
|
|
2021-10-16 16:02:51 +03:00
|
|
|
// GetSkipDNSResolve implements routing.Context.
|
|
|
|
func (ctx *Context) GetSkipDNSResolve() bool {
|
|
|
|
if ctx.Content == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return ctx.Content.SkipDNSResolve
|
|
|
|
}
|
|
|
|
|
2020-11-25 13:01:53 +02:00
|
|
|
// AsRoutingContext creates a context from context.context with session info.
|
|
|
|
func AsRoutingContext(ctx context.Context) routing.Context {
|
2024-05-14 04:52:24 +03:00
|
|
|
outbounds := session.OutboundsFromContext(ctx)
|
|
|
|
ob := outbounds[len(outbounds) - 1]
|
2020-11-25 13:01:53 +02:00
|
|
|
return &Context{
|
|
|
|
Inbound: session.InboundFromContext(ctx),
|
2024-05-14 04:52:24 +03:00
|
|
|
Outbound: ob,
|
2020-11-25 13:01:53 +02:00
|
|
|
Content: session.ContentFromContext(ctx),
|
|
|
|
}
|
|
|
|
}
|