1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-16 09:26:21 -05:00
v2fly/common/log/access.go

69 lines
1.3 KiB
Go
Raw Normal View History

2017-12-10 16:48:28 -05:00
package log
2017-12-19 15:28:12 -05:00
import (
2019-06-12 01:04:34 -04:00
"context"
2017-12-19 15:28:12 -05:00
"strings"
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v4/common/serial"
2017-12-19 15:28:12 -05:00
)
2019-06-12 01:04:34 -04:00
type logKey int
const (
accessMessageKey logKey = iota
)
2017-12-10 16:48:28 -05:00
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
2017-12-10 16:48:28 -05:00
}
2017-12-19 15:28:12 -05:00
func (m *AccessMessage) String() string {
2018-10-01 06:54:06 -04: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))
2019-10-27 11:13:00 -04:00
if len(m.Detour) > 0 {
builder.WriteString(" [")
2019-10-27 11:13:00 -04:00
builder.WriteString(m.Detour)
builder.WriteByte(']')
}
if reason := serial.ToString(m.Reason); len(reason) > 0 {
builder.WriteString(" ")
builder.WriteString(reason)
2019-10-27 11:13:00 -04:00
}
if len(m.Email) > 0 {
builder.WriteString(" email: ")
builder.WriteString(m.Email)
}
2018-10-01 06:54:06 -04:00
return builder.String()
2017-12-19 15:28:12 -05:00
}
2019-06-12 01:04:34 -04: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
}
2019-10-30 08:15:31 -04:00
return nil
2019-06-12 01:04:34 -04:00
}