mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-29 16:58:34 +00:00
v1.0.0
This commit is contained in:
parent
47d23e9972
commit
c7f7c08ead
711 changed files with 82154 additions and 2 deletions
119
features/routing/session/context.go
Normal file
119
features/routing/session/context.go
Normal file
|
@ -0,0 +1,119 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/xtls/xray-core/v1/common/net"
|
||||
"github.com/xtls/xray-core/v1/common/session"
|
||||
"github.com/xtls/xray-core/v1/features/routing"
|
||||
)
|
||||
|
||||
// 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 ""
|
||||
}
|
||||
dest := ctx.Outbound.Target
|
||||
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
|
||||
}
|
||||
|
||||
// AsRoutingContext creates a context from context.context with session info.
|
||||
func AsRoutingContext(ctx context.Context) routing.Context {
|
||||
return &Context{
|
||||
Inbound: session.InboundFromContext(ctx),
|
||||
Outbound: session.OutboundFromContext(ctx),
|
||||
Content: session.ContentFromContext(ctx),
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue