access.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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) Release() {
  21. this.From = nil
  22. this.To = nil
  23. this.Reason = nil
  24. }
  25. func (this *accessLog) String() string {
  26. return this.From.String() + " " + string(this.Status) + " " + this.To.String() + " " + this.Reason.String()
  27. }
  28. // InitAccessLogger initializes the access logger to write into the give file.
  29. func InitAccessLogger(file string) error {
  30. logger, err := newFileLogWriter(file)
  31. if err != nil {
  32. Error("Failed to create access logger on file (", file, "): ", file, err)
  33. return err
  34. }
  35. accessLoggerInstance = logger
  36. return nil
  37. }
  38. // Access writes an access log.
  39. func Access(from, to serial.String, status AccessStatus, reason serial.String) {
  40. accessLoggerInstance.Log(&accessLog{
  41. From: from,
  42. To: to,
  43. Status: status,
  44. Reason: reason,
  45. })
  46. }