Skip to content

Commit

Permalink
Merge pull request #1973 from fcying/access_log_add_detour_tag
Browse files Browse the repository at this point in the history
access log add detour tag
  • Loading branch information
kslr authored Oct 27, 2019
2 parents 85d2c69 + fa926a1 commit e34266b
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 7 deletions.
7 changes: 7 additions & 0 deletions app/dispatcher/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
"v2ray.com/core/common/log"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/session"
"v2ray.com/core/features/outbound"
Expand Down Expand Up @@ -281,5 +282,11 @@ func (d *DefaultDispatcher) routedDispatch(ctx context.Context, link *transport.
return
}

accessMessage := log.AccessMessageFromContext(ctx)
if accessMessage != nil {
accessMessage.Detour = "[" + handler.Tag() + "]"
log.Record(accessMessage)
}

handler.Dispatch(ctx, link)
}
21 changes: 21 additions & 0 deletions common/log/access.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package log

import (
"context"
"strings"

"v2ray.com/core/common/serial"
)

type logKey int

const (
accessMessageKey logKey = iota
)

type AccessStatus string

const (
Expand All @@ -19,6 +26,7 @@ type AccessMessage struct {
Status AccessStatus
Reason interface{}
Email string
Detour interface{}
}

func (m *AccessMessage) String() string {
Expand All @@ -29,6 +37,8 @@ func (m *AccessMessage) String() string {
builder.WriteByte(' ')
builder.WriteString(serial.ToString(m.To))
builder.WriteByte(' ')
builder.WriteString(serial.ToString(m.Detour))
builder.WriteByte(' ')
builder.WriteString(serial.ToString(m.Reason))

if len(m.Email) > 0 {
Expand All @@ -38,3 +48,14 @@ func (m *AccessMessage) String() string {
}
return builder.String()
}

func ContextWithAccessMessage(ctx context.Context, accessMessage *AccessMessage) context.Context {
return context.WithValue(ctx, accessMessageKey, accessMessage)
}

func AccessMessageFromContext(ctx context.Context) *AccessMessage {
if accessMessage, ok := ctx.Value(accessMessageKey).(*AccessMessage); ok {
return accessMessage
}
return nil
}
2 changes: 1 addition & 1 deletion common/mux/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (w *ServerWorker) handleStatusNew(ctx context.Context, meta *FrameMetadata,
msg.From = inbound.Source
msg.Email = inbound.User.Email
}
log.Record(msg)
ctx = log.ContextWithAccessMessage(ctx, msg)
}
link, err := w.dispatcher.Dispatch(ctx, meta.Target)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion proxy/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,11 @@ Start:
if err != nil {
return newError("malformed proxy host: ", host).AtWarning().Base(err)
}
log.Record(&log.AccessMessage{
ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
From: conn.RemoteAddr(),
To: request.URL,
Status: log.AccessAccepted,
Reason: "",
})

if strings.EqualFold(request.Method, "CONNECT") {
Expand Down
4 changes: 2 additions & 2 deletions proxy/shadowsocks/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (s *Server) handlerUDPPayload(ctx context.Context, conn internet.Connection

dest := request.Destination()
if inbound.Source.IsValid() {
log.Record(&log.AccessMessage{
ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
From: inbound.Source,
To: dest,
Status: log.AccessAccepted,
Expand Down Expand Up @@ -176,7 +176,7 @@ func (s *Server) handleConnection(ctx context.Context, conn internet.Connection,
inbound.User = s.user

dest := request.Destination()
log.Record(&log.AccessMessage{
ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
From: conn.RemoteAddr(),
To: dest,
Status: log.AccessAccepted,
Expand Down
4 changes: 2 additions & 2 deletions proxy/socks/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (s *Server) processTCP(ctx context.Context, conn internet.Connection, dispa
dest := request.Destination()
newError("TCP Connect request to ", dest).WriteToLog(session.ExportIDToError(ctx))
if inbound != nil && inbound.Source.IsValid() {
log.Record(&log.AccessMessage{
ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
From: inbound.Source,
To: dest,
Status: log.AccessAccepted,
Expand Down Expand Up @@ -229,7 +229,7 @@ func (s *Server) handleUDPPayload(ctx context.Context, conn internet.Connection,

newError("send packet to ", request.Destination(), " with ", payload.Len(), " bytes").AtDebug().WriteToLog(session.ExportIDToError(ctx))
if inbound := session.InboundFromContext(ctx); inbound != nil && inbound.Source.IsValid() {
log.Record(&log.AccessMessage{
ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
From: inbound.Source,
To: request.Destination(),
Status: log.AccessAccepted,
Expand Down
2 changes: 1 addition & 1 deletion proxy/vmess/inbound/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection i
}

if request.Command != protocol.RequestCommandMux {
log.Record(&log.AccessMessage{
ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
From: connection.RemoteAddr(),
To: request.Destination(),
Status: log.AccessAccepted,
Expand Down

0 comments on commit e34266b

Please sign in to comment.