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 {
|
2021-01-30 07:32:34 -05:00
|
|
|
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))
|
2020-12-16 16:03:26 -05:00
|
|
|
|
2019-10-27 11:13:00 -04:00
|
|
|
if len(m.Detour) > 0 {
|
2020-12-16 16:03:26 -05:00
|
|
|
builder.WriteString(" [")
|
2019-10-27 11:13:00 -04:00
|
|
|
builder.WriteString(m.Detour)
|
2020-12-16 16:03:26 -05:00
|
|
|
builder.WriteByte(']')
|
|
|
|
}
|
|
|
|
|
|
|
|
if reason := serial.ToString(m.Reason); len(reason) > 0 {
|
|
|
|
builder.WriteString(" ")
|
|
|
|
builder.WriteString(reason)
|
2019-10-27 11:13:00 -04:00
|
|
|
}
|
2019-06-18 05:47:46 -04:00
|
|
|
|
|
|
|
if len(m.Email) > 0 {
|
2020-12-16 16:03:26 -05:00
|
|
|
builder.WriteString(" email: ")
|
2019-06-18 05:47:46 -04:00
|
|
|
builder.WriteString(m.Email)
|
|
|
|
}
|
2020-12-16 16:03:26 -05:00
|
|
|
|
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
|
|
|
}
|