merge user info inbound metadata

pull/1314/head
Darien Raymond 6 years ago
parent e35e3e6e53
commit 595f3d685e
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

@ -133,7 +133,12 @@ func (d *DefaultDispatcher) getLink(ctx context.Context) (*vio.Link, *vio.Link)
Writer: downlinkWriter,
}
user := protocol.UserFromContext(ctx)
sessionInbound := session.InboundFromContext(ctx)
var user *protocol.MemoryUser
if sessionInbound != nil {
user = sessionInbound.User
}
if user != nil && len(user.Email) > 0 {
p := d.policy.ForLevel(user.Level)
if p.Stats.UserUplink {

@ -8,7 +8,6 @@ import (
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/strmatcher"
"v2ray.com/core/proxy"
)
@ -282,7 +281,12 @@ func NewUserMatcher(users []string) *UserMatcher {
}
func (v *UserMatcher) Apply(ctx context.Context) bool {
user := protocol.UserFromContext(ctx)
inbound := session.InboundFromContext(ctx)
if inbound == nil {
return false
}
user := inbound.User
if user == nil {
return false
}

@ -27,6 +27,10 @@ func withOutbound(outbound *session.Outbound) context.Context {
return session.ContextWithOutbound(context.Background(), outbound)
}
func withInbound(inbound *session.Inbound) context.Context {
return session.ContextWithInbound(context.Background(), inbound)
}
func TestRoutingRule(t *testing.T) {
assert := With(t)
@ -131,11 +135,11 @@ func TestRoutingRule(t *testing.T) {
},
test: []ruleTest{
{
input: protocol.ContextWithUser(context.Background(), &protocol.MemoryUser{Email: "admin@v2ray.com"}),
input: withInbound(&session.Inbound{User: &protocol.MemoryUser{Email: "admin@v2ray.com"}}),
output: true,
},
{
input: protocol.ContextWithUser(context.Background(), &protocol.MemoryUser{Email: "love@v2ray.com"}),
input: withInbound(&session.Inbound{User: &protocol.MemoryUser{Email: "love@v2ray.com"}}),
output: false,
},
{

@ -7,24 +7,9 @@ import (
type key int
const (
userKey key = iota
requestKey
requestKey key = iota
)
// ContextWithUser returns a context combined with a User.
func ContextWithUser(ctx context.Context, user *MemoryUser) context.Context {
return context.WithValue(ctx, userKey, user)
}
// UserFromContext extracts a User from the given context, if any.
func UserFromContext(ctx context.Context) *MemoryUser {
v := ctx.Value(userKey)
if v == nil {
return nil
}
return v.(*MemoryUser)
}
func ContextWithRequestHeader(ctx context.Context, request *RequestHeader) context.Context {
return context.WithValue(ctx, requestKey, request)
}

@ -30,7 +30,9 @@ func (u *User) ToMemoryUser() (*MemoryUser, error) {
}, nil
}
// MemoryUser is a parsed form of User, to reduce number of parsing of Account proto.
type MemoryUser struct {
// Account is the parsed account of the protocol.
Account Account
Email string
Level uint32

@ -7,6 +7,7 @@ import (
"v2ray.com/core/common/errors"
"v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
)
// ID of a session.
@ -34,6 +35,8 @@ type Inbound struct {
Source net.Destination
Gateway net.Destination
Tag string
// User is the user that authencates for the inbound. May be nil if the protocol allows anounymous traffic.
User *protocol.MemoryUser
}
type Outbound struct {

@ -89,6 +89,11 @@ func (s *Server) handlerUDPPayload(ctx context.Context, conn internet.Connection
})
account := s.user.Account.(*MemoryAccount)
inbound := session.InboundFromContext(ctx)
if inbound == nil {
panic("no inbound metadata")
}
inbound.User = s.user
reader := buf.NewReader(conn)
for {
@ -126,7 +131,7 @@ func (s *Server) handlerUDPPayload(ctx context.Context, conn internet.Connection
}
dest := request.Destination()
if inbound := session.InboundFromContext(ctx); inbound != nil && inbound.Source.IsValid() {
if inbound.Source.IsValid() {
log.Record(&log.AccessMessage{
From: inbound.Source,
To: dest,
@ -136,7 +141,6 @@ func (s *Server) handlerUDPPayload(ctx context.Context, conn internet.Connection
}
newError("tunnelling request to ", dest).WriteToLog(session.ExportIDToError(ctx))
ctx = protocol.ContextWithUser(ctx, request.User)
ctx = protocol.ContextWithRequestHeader(ctx, request)
udpServer.Dispatch(ctx, dest, data)
}
@ -162,6 +166,12 @@ func (s *Server) handleConnection(ctx context.Context, conn internet.Connection,
}
conn.SetReadDeadline(time.Time{})
inbound := session.InboundFromContext(ctx)
if inbound == nil {
panic("no inbound metadata")
}
inbound.User = s.user
dest := request.Destination()
log.Record(&log.AccessMessage{
From: conn.RemoteAddr(),
@ -171,8 +181,6 @@ func (s *Server) handleConnection(ctx context.Context, conn internet.Connection,
})
newError("tunnelling request to ", dest).WriteToLog(session.ExportIDToError(ctx))
ctx = protocol.ContextWithUser(ctx, request.User)
ctx, cancel := context.WithCancel(ctx)
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)

@ -264,8 +264,13 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection i
newError("unable to set back read deadline").Base(err).WriteToLog(session.ExportIDToError(ctx))
}
inbound := session.InboundFromContext(ctx)
if inbound == nil {
panic("no inbound metadata")
}
inbound.User = request.User
sessionPolicy = h.policyManager.ForLevel(request.User.Level)
ctx = protocol.ContextWithUser(ctx, request.User)
ctx, cancel := context.WithCancel(ctx)
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)

Loading…
Cancel
Save