You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.4 KiB

4 years ago
package log
import (
"context"
"strings"
4 years ago
"github.com/xtls/xray-core/common/serial"
4 years ago
)
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(' ')
4 years ago
builder.WriteString(serial.ToString(m.From))
builder.WriteByte(' ')
builder.WriteString(string(m.Status))
builder.WriteByte(' ')
builder.WriteString(serial.ToString(m.To))
4 years ago
if len(m.Detour) > 0 {
builder.WriteString(" [")
4 years ago
builder.WriteString(m.Detour)
builder.WriteByte(']')
}
if reason := serial.ToString(m.Reason); len(reason) > 0 {
builder.WriteString(" ")
builder.WriteString(reason)
4 years ago
}
if len(m.Email) > 0 {
builder.WriteString(" email: ")
4 years ago
builder.WriteString(m.Email)
}
4 years ago
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
}