Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (c) 2012-2014 Jeremy Latt
  2. // Copyright (c) 2016 Daniel Oaks <daniel@danieloaks.net>
  3. // released under the MIT license
  4. package utils
  5. import (
  6. "net"
  7. "strings"
  8. )
  9. // IPString returns a simple IP string from the given net.Addr.
  10. func IPString(addr net.Addr) string {
  11. addrStr := addr.String()
  12. ipaddr, _, err := net.SplitHostPort(addrStr)
  13. //TODO(dan): Why is this needed, does this happen?
  14. if err != nil {
  15. return addrStr
  16. }
  17. return ipaddr
  18. }
  19. // AddrLookupHostname returns the hostname (if possible) or address for the given `net.Addr`.
  20. func AddrLookupHostname(addr net.Addr) string {
  21. if AddrIsUnix(addr) {
  22. return "localhost"
  23. }
  24. return LookupHostname(IPString(addr))
  25. }
  26. // AddrIsLocal returns whether the address is from a trusted local connection (loopback or unix).
  27. func AddrIsLocal(addr net.Addr) bool {
  28. if tcpaddr, ok := addr.(*net.TCPAddr); ok {
  29. return tcpaddr.IP.IsLoopback()
  30. }
  31. _, ok := addr.(*net.UnixAddr)
  32. return ok
  33. }
  34. // AddrToIP returns the IP address for a net.Addr, or nil if it's a unix domain socket.
  35. func AddrToIP(addr net.Addr) net.IP {
  36. if tcpaddr, ok := addr.(*net.TCPAddr); ok {
  37. return tcpaddr.IP
  38. }
  39. return nil
  40. }
  41. // AddrIsUnix returns whether the address is a unix domain socket.
  42. func AddrIsUnix(addr net.Addr) bool {
  43. _, ok := addr.(*net.UnixAddr)
  44. return ok
  45. }
  46. // LookupHostname returns the hostname for `addr` if it has one. Otherwise, just returns `addr`.
  47. func LookupHostname(addr string) string {
  48. names, err := net.LookupAddr(addr)
  49. if err == nil && len(names) > 0 {
  50. candidate := strings.TrimSuffix(names[0], ".")
  51. if IsHostname(candidate) {
  52. return candidate
  53. }
  54. }
  55. // return original address if no hostname found
  56. if len(addr) > 0 && addr[0] == ':' {
  57. // fix for IPv6 hostnames (so they don't start with a colon), same as all other IRCds
  58. addr = "0" + addr
  59. }
  60. return addr
  61. }
  62. var allowedHostnameChars = "abcdefghijklmnopqrstuvwxyz1234567890-."
  63. // IsHostname returns whether we consider `name` a valid hostname.
  64. func IsHostname(name string) bool {
  65. // IRC hostnames specifically require a period
  66. if !strings.Contains(name, ".") || len(name) < 1 || len(name) > 253 {
  67. return false
  68. }
  69. // ensure each part of hostname is valid
  70. for _, part := range strings.Split(name, ".") {
  71. if len(part) < 1 || len(part) > 63 || strings.HasPrefix(part, "-") || strings.HasSuffix(part, "-") {
  72. return false
  73. }
  74. }
  75. // ensure all chars of hostname are valid
  76. for _, char := range strings.Split(strings.ToLower(name), "") {
  77. if !strings.Contains(allowedHostnameChars, char) {
  78. return false
  79. }
  80. }
  81. return true
  82. }