Xray-core/common/log/access.go

71 lines
1.4 KiB
Go
Raw Permalink Normal View History

2020-11-25 11:01:53 +00:00
package log
import (
"context"
"strings"
2020-12-04 01:36:16 +00:00
"github.com/xtls/xray-core/common/serial"
2020-11-25 11:01:53 +00:00
)
type logKey int
const (
accessMessageKey logKey = iota
)
type AccessStatus string
const (
AccessAccepted = AccessStatus("accepted")
AccessRejected = AccessStatus("rejected")
)
type AccessMessage struct {
From interface{}
To interface{}
Status AccessStatus
Reason interface{}
Email string
Detour string
}
func (m *AccessMessage) String() string {
builder := strings.Builder{}
builder.WriteString("from")
builder.WriteByte(' ')
2020-11-25 11:01:53 +00:00
builder.WriteString(serial.ToString(m.From))
builder.WriteByte(' ')
builder.WriteString(string(m.Status))
builder.WriteByte(' ')
builder.WriteString(serial.ToString(m.To))
2020-12-24 19:45:35 +00:00
2020-11-25 11:01:53 +00:00
if len(m.Detour) > 0 {
2020-12-24 19:45:35 +00:00
builder.WriteString(" [")
2020-11-25 11:01:53 +00:00
builder.WriteString(m.Detour)
2020-12-24 19:45:35 +00:00
builder.WriteByte(']')
}
if reason := serial.ToString(m.Reason); len(reason) > 0 {
builder.WriteString(" ")
builder.WriteString(reason)
2020-11-25 11:01:53 +00:00
}
if len(m.Email) > 0 {
2020-12-24 19:45:35 +00:00
builder.WriteString(" email: ")
2020-11-25 11:01:53 +00:00
builder.WriteString(m.Email)
}
2020-12-24 19:45:35 +00:00
2020-11-25 11:01:53 +00:00
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
}