github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/common/log/access.go (about)

     1  package log
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	"v2ray.com/core/common/serial"
     8  )
     9  
    10  type logKey int
    11  
    12  const (
    13  	accessMessageKey logKey = iota
    14  )
    15  
    16  type AccessStatus string
    17  
    18  const (
    19  	AccessAccepted = AccessStatus("accepted")
    20  	AccessRejected = AccessStatus("rejected")
    21  )
    22  
    23  type AccessMessage struct {
    24  	From   interface{}
    25  	To     interface{}
    26  	Status AccessStatus
    27  	Reason interface{}
    28  	Email  string
    29  	Detour string
    30  }
    31  
    32  func (m *AccessMessage) String() string {
    33  	builder := strings.Builder{}
    34  	builder.WriteString(serial.ToString(m.From))
    35  	builder.WriteByte(' ')
    36  	builder.WriteString(string(m.Status))
    37  	builder.WriteByte(' ')
    38  	builder.WriteString(serial.ToString(m.To))
    39  	builder.WriteByte(' ')
    40  	if len(m.Detour) > 0 {
    41  		builder.WriteByte('[')
    42  		builder.WriteString(m.Detour)
    43  		builder.WriteString("] ")
    44  	}
    45  	builder.WriteString(serial.ToString(m.Reason))
    46  
    47  	if len(m.Email) > 0 {
    48  		builder.WriteString("email:")
    49  		builder.WriteString(m.Email)
    50  		builder.WriteByte(' ')
    51  	}
    52  	return builder.String()
    53  }
    54  
    55  func ContextWithAccessMessage(ctx context.Context, accessMessage *AccessMessage) context.Context {
    56  	return context.WithValue(ctx, accessMessageKey, accessMessage)
    57  }
    58  
    59  func AccessMessageFromContext(ctx context.Context) *AccessMessage {
    60  	if accessMessage, ok := ctx.Value(accessMessageKey).(*AccessMessage); ok {
    61  		return accessMessage
    62  	}
    63  	return nil
    64  }