github.com/v2fly/v2ray-core/v4@v4.45.2/features/routing/session/context.go (about) 1 package session 2 3 import ( 4 "context" 5 6 "github.com/v2fly/v2ray-core/v4/common/net" 7 "github.com/v2fly/v2ray-core/v4/common/session" 8 "github.com/v2fly/v2ray-core/v4/features/routing" 9 ) 10 11 // Context is an implementation of routing.Context, which is a wrapper of context.context with session info. 12 type Context struct { 13 Inbound *session.Inbound 14 Outbound *session.Outbound 15 Content *session.Content 16 } 17 18 // GetInboundTag implements routing.Context. 19 func (ctx *Context) GetInboundTag() string { 20 if ctx.Inbound == nil { 21 return "" 22 } 23 return ctx.Inbound.Tag 24 } 25 26 // GetSourceIPs implements routing.Context. 27 func (ctx *Context) GetSourceIPs() []net.IP { 28 if ctx.Inbound == nil || !ctx.Inbound.Source.IsValid() { 29 return nil 30 } 31 dest := ctx.Inbound.Source 32 if dest.Address.Family().IsDomain() { 33 return nil 34 } 35 36 return []net.IP{dest.Address.IP()} 37 } 38 39 // GetSourcePort implements routing.Context. 40 func (ctx *Context) GetSourcePort() net.Port { 41 if ctx.Inbound == nil || !ctx.Inbound.Source.IsValid() { 42 return 0 43 } 44 return ctx.Inbound.Source.Port 45 } 46 47 // GetTargetIPs implements routing.Context. 48 func (ctx *Context) GetTargetIPs() []net.IP { 49 if ctx.Outbound == nil || !ctx.Outbound.Target.IsValid() { 50 return nil 51 } 52 53 if ctx.Outbound.Target.Address.Family().IsIP() { 54 return []net.IP{ctx.Outbound.Target.Address.IP()} 55 } 56 57 return nil 58 } 59 60 // GetTargetPort implements routing.Context. 61 func (ctx *Context) GetTargetPort() net.Port { 62 if ctx.Outbound == nil || !ctx.Outbound.Target.IsValid() { 63 return 0 64 } 65 return ctx.Outbound.Target.Port 66 } 67 68 // GetTargetDomain implements routing.Context. 69 func (ctx *Context) GetTargetDomain() string { 70 if ctx.Outbound == nil || !ctx.Outbound.Target.IsValid() { 71 return "" 72 } 73 dest := ctx.Outbound.Target 74 if !dest.Address.Family().IsDomain() { 75 return "" 76 } 77 return dest.Address.Domain() 78 } 79 80 // GetNetwork implements routing.Context. 81 func (ctx *Context) GetNetwork() net.Network { 82 if ctx.Outbound == nil { 83 return net.Network_Unknown 84 } 85 return ctx.Outbound.Target.Network 86 } 87 88 // GetProtocol implements routing.Context. 89 func (ctx *Context) GetProtocol() string { 90 if ctx.Content == nil { 91 return "" 92 } 93 return ctx.Content.Protocol 94 } 95 96 // GetUser implements routing.Context. 97 func (ctx *Context) GetUser() string { 98 if ctx.Inbound == nil || ctx.Inbound.User == nil { 99 return "" 100 } 101 return ctx.Inbound.User.Email 102 } 103 104 // GetAttributes implements routing.Context. 105 func (ctx *Context) GetAttributes() map[string]string { 106 if ctx.Content == nil { 107 return nil 108 } 109 return ctx.Content.Attributes 110 } 111 112 // GetSkipDNSResolve implements routing.Context. 113 func (ctx *Context) GetSkipDNSResolve() bool { 114 if ctx.Content == nil { 115 return false 116 } 117 return ctx.Content.SkipDNSResolve 118 } 119 120 // AsRoutingContext creates a context from context.context with session info. 121 func AsRoutingContext(ctx context.Context) routing.Context { 122 return &Context{ 123 Inbound: session.InboundFromContext(ctx), 124 Outbound: session.OutboundFromContext(ctx), 125 Content: session.ContentFromContext(ctx), 126 } 127 }