common.go 407 B

123456789101112131415161718192021
  1. package vpn
  2. import "fmt"
  3. const (
  4. PapSecretsPath = "/etc/ppp/pap-secrets"
  5. ChapSecretsPath = "/etc/ppp/chap-secrets"
  6. )
  7. func formatTrafficBytes(b uint64) string {
  8. const unit = 1024
  9. if b < unit {
  10. return fmt.Sprintf("%d B", b)
  11. }
  12. div, exp := uint64(unit), 0
  13. for n := b / unit; n >= unit; n /= unit {
  14. div *= unit
  15. exp++
  16. }
  17. return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
  18. }