v2ray-core/common/log/access.go

65 lines
1.3 KiB
Go
Raw Normal View History

2017-12-10 21:48:28 +00:00
package log
2017-12-19 20:28:12 +00:00
import (
2019-06-12 05:04:34 +00:00
"context"
2017-12-19 20:28:12 +00:00
"strings"
"v2ray.com/core/common/serial"
)
2019-06-12 05:04:34 +00:00
type logKey int
const (
accessMessageKey logKey = iota
)
2017-12-10 21:48:28 +00:00
type AccessStatus string
const (
AccessAccepted = AccessStatus("accepted")
AccessRejected = AccessStatus("rejected")
)
type AccessMessage struct {
From interface{}
To interface{}
Status AccessStatus
Reason interface{}
Email string
2019-10-27 15:13:00 +00:00
Detour string
2017-12-10 21:48:28 +00:00
}
2017-12-19 20:28:12 +00:00
func (m *AccessMessage) String() string {
2018-10-01 10:54:06 +00:00
builder := strings.Builder{}
builder.WriteString(serial.ToString(m.From))
builder.WriteByte(' ')
builder.WriteString(string(m.Status))
builder.WriteByte(' ')
builder.WriteString(serial.ToString(m.To))
builder.WriteByte(' ')
2019-10-27 15:13:00 +00:00
if len(m.Detour) > 0 {
builder.WriteByte('[')
builder.WriteString(m.Detour)
builder.WriteString("] ")
}
2018-10-01 10:54:06 +00:00
builder.WriteString(serial.ToString(m.Reason))
if len(m.Email) > 0 {
builder.WriteString("email:")
builder.WriteString(m.Email)
builder.WriteByte(' ')
}
2018-10-01 10:54:06 +00:00
return builder.String()
2017-12-19 20:28:12 +00:00
}
2019-06-12 05:04:34 +00:00
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
}