IsValidDomain validates if input string is a valid domain name.
(host string)
| 34 | |
| 35 | // IsValidDomain validates if input string is a valid domain name. |
| 36 | func IsValidDomain(host string) bool { |
| 37 | // See RFC 1035, RFC 3696. |
| 38 | host = strings.TrimSpace(host) |
| 39 | if len(host) == 0 || len(host) > 255 { |
| 40 | return false |
| 41 | } |
| 42 | // host cannot start or end with "-" |
| 43 | if host[len(host)-1:] == "-" || host[:1] == "-" { |
| 44 | return false |
| 45 | } |
| 46 | // host cannot start or end with "_" |
| 47 | if host[len(host)-1:] == "_" || host[:1] == "_" { |
| 48 | return false |
| 49 | } |
| 50 | // host cannot start with a "." |
| 51 | if host[:1] == "." { |
| 52 | return false |
| 53 | } |
| 54 | // All non alphanumeric characters are invalid. |
| 55 | if strings.ContainsAny(host, "`~!@#$%^&*()+={}[]|\\\"';:><?/") { |
| 56 | return false |
| 57 | } |
| 58 | // No need to regexp match, since the list is non-exhaustive. |
| 59 | // We let it valid and fail later. |
| 60 | return true |
| 61 | } |
| 62 | |
| 63 | // IsValidIP parses input string for ip address validity. |
| 64 | func IsValidIP(ip string) bool { |
no outgoing calls