validation.go 897 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package pia
  2. import (
  3. "encoding/base64"
  4. "net"
  5. "strings"
  6. "unicode"
  7. )
  8. func validSecret(value []byte, min, max int) bool {
  9. if len(value) < min || len(value) > max {
  10. return false
  11. }
  12. for _, b := range value {
  13. if b == 0 || b == '\r' || b == '\n' {
  14. return false
  15. }
  16. }
  17. return true
  18. }
  19. func validHostname(host string) bool {
  20. if host == "" || len(host) > 253 || net.ParseIP(host) != nil || strings.HasSuffix(host, ".") {
  21. return false
  22. }
  23. for _, label := range strings.Split(host, ".") {
  24. if label == "" || len(label) > 63 || label[0] == '-' || label[len(label)-1] == '-' {
  25. return false
  26. }
  27. for _, r := range label {
  28. if r > unicode.MaxASCII || (!unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '-') {
  29. return false
  30. }
  31. }
  32. }
  33. return true
  34. }
  35. func validWGKey(key string) bool {
  36. decoded, err := base64.StdEncoding.DecodeString(key)
  37. return err == nil && len(decoded) == 32
  38. }