access.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package log
  2. import (
  3. "github.com/v2ray/v2ray-core/common/serial"
  4. )
  5. // AccessStatus is the status of an access request from clients.
  6. type AccessStatus string
  7. const (
  8. AccessAccepted = AccessStatus("accepted")
  9. AccessRejected = AccessStatus("rejected")
  10. )
  11. var (
  12. accessLoggerInstance logWriter = &noOpLogWriter{}
  13. )
  14. type accessLog struct {
  15. From serial.String
  16. To serial.String
  17. Status AccessStatus
  18. Reason serial.String
  19. }
  20. func (this *accessLog) String() string {
  21. return this.From.String() + " " + string(this.Status) + " " + this.To.String() + " " + this.Reason.String()
  22. }
  23. // InitAccessLogger initializes the access logger to write into the give file.
  24. func InitAccessLogger(file string) error {
  25. logger, err := newFileLogWriter(file)
  26. if err != nil {
  27. Error("Failed to create access logger on file (", file, "): ", file, err)
  28. return err
  29. }
  30. accessLoggerInstance = logger
  31. return nil
  32. }
  33. // Access writes an access log.
  34. func Access(from, to serial.String, status AccessStatus, reason serial.String) {
  35. accessLoggerInstance.Log(&accessLog{
  36. From: from,
  37. To: to,
  38. Status: status,
  39. Reason: reason,
  40. })
  41. }