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"
|
|
|
|
|
|
|
|
"v2ray.com/core/common/serial"
|
|
|
|
)
|
|
|
|
|
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{}
|
2019-06-18 05:47:46 -04:00
|
|
|
Email string
|
2019-10-27 11:13:00 -04:00
|
|
|
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))
|
|
|
|
builder.WriteByte(' ')
|
2019-10-27 11:13:00 -04:00
|
|
|
if len(m.Detour) > 0 {
|
|
|
|
builder.WriteByte('[')
|
|
|
|
builder.WriteString(m.Detour)
|
|
|
|
builder.WriteString("] ")
|
|
|
|
}
|
2018-10-01 06:54:06 -04:00
|
|
|
builder.WriteString(serial.ToString(m.Reason))
|
2019-06-18 05:47:46 -04:00
|
|
|
|
|
|
|
if len(m.Email) > 0 {
|
|
|
|
builder.WriteString("email:")
|
|
|
|
builder.WriteString(m.Email)
|
|
|
|
builder.WriteByte(' ')
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|