github.com/xmplusdev/xray-core@v1.8.10/common/log/access.go (about)

     1  package log
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	"github.com/xmplusdev/xray-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  
    40  	if len(m.Detour) > 0 {
    41  		builder.WriteString(" [")
    42  		builder.WriteString(m.Detour)
    43  		builder.WriteByte(']')
    44  	}
    45  
    46  	if reason := serial.ToString(m.Reason); len(reason) > 0 {
    47  		builder.WriteString(" ")
    48  		builder.WriteString(reason)
    49  	}
    50  
    51  	if len(m.Email) > 0 {
    52  		builder.WriteString(" email: ")
    53  		builder.WriteString(m.Email)
    54  	}
    55  
    56  	return builder.String()
    57  }
    58  
    59  func ContextWithAccessMessage(ctx context.Context, accessMessage *AccessMessage) context.Context {
    60  	return context.WithValue(ctx, accessMessageKey, accessMessage)
    61  }
    62  
    63  func AccessMessageFromContext(ctx context.Context) *AccessMessage {
    64  	if accessMessage, ok := ctx.Value(accessMessageKey).(*AccessMessage); ok {
    65  		return accessMessage
    66  	}
    67  	return nil
    68  }