Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

hostnames.go 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // written by Daniel Oaks <daniel@danieloaks.net>
  2. // released under the ISC license
  3. package ircutils
  4. import "strings"
  5. var allowedHostnameChars = "abcdefghijklmnopqrstuvwxyz1234567890-."
  6. // HostnameIsValid provides a way for servers to check whether a looked-up client
  7. // hostname is valid (see InspIRCd #1033 for why this is required).
  8. //
  9. // This function shouldn't be called by clients since they don't need to validate
  10. // hostnames for IRC use, just by servers that need to confirm hostnames of incoming
  11. // clients.
  12. //
  13. // In addition to this function, servers should impose their own limits on max
  14. // hostname length -- this function limits it to 200 but most servers will probably
  15. // want to make it smaller than that.
  16. func HostnameIsValid(hostname string) bool {
  17. // IRC hostnames specifically require a period, rough limit of 200 chars
  18. if !strings.Contains(hostname, ".") || len(hostname) < 1 || len(hostname) > 200 {
  19. return false
  20. }
  21. // ensure each part of hostname is valid
  22. for _, part := range strings.Split(hostname, ".") {
  23. if len(part) < 1 || len(part) > 63 || strings.HasPrefix(part, "-") || strings.HasSuffix(part, "-") {
  24. return false
  25. }
  26. }
  27. // ensure all chars of hostname are valid
  28. for _, char := range strings.Split(strings.ToLower(hostname), "") {
  29. if !strings.Contains(allowedHostnameChars, char) {
  30. return false
  31. }
  32. }
  33. return true
  34. }