ip_address.go 501 B

1234567891011121314151617181920212223242526272829
  1. package util
  2. import (
  3. "net"
  4. "strings"
  5. )
  6. // ParseIPAddress parses an IP address and removes port and/or IPV6 format
  7. func ParseIPAddress(input string) string {
  8. s := input
  9. lastIndex := strings.LastIndex(input, ":")
  10. if lastIndex != -1 {
  11. if lastIndex > 0 && input[lastIndex-1:lastIndex] != ":" {
  12. s = input[:lastIndex]
  13. }
  14. }
  15. s = strings.Replace(s, "[", "", -1)
  16. s = strings.Replace(s, "]", "", -1)
  17. ip := net.ParseIP(s)
  18. if ip.IsLoopback() {
  19. return "127.0.0.1"
  20. }
  21. return ip.String()
  22. }