Hunter0x7c7
2022-08-11 b8230139fb40edea387617b6accd8371e37eda58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package session
 
import (
    "context"
 
    "github.com/v2fly/v2ray-core/v5/common/net"
    "github.com/v2fly/v2ray-core/v5/common/session"
    "github.com/v2fly/v2ray-core/v5/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
}
 
// GetSkipDNSResolve implements routing.Context.
func (ctx *Context) GetSkipDNSResolve() bool {
    if ctx.Content == nil {
        return false
    }
    return ctx.Content.SkipDNSResolve
}
 
// 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),
    }
}