func IpDetail(c *gin.Context) { IpAddr := c.Params.ByName("addr") r, err := regexp.Compile("(^10\\.)|(^172\\.1[6-9]\\.)|(^172\\.2[0-9]\\.)|(^172\\.3[0-9]\\.)|(^192\\.168\\.)") if err != nil { return } if govalidator.IsIPv4(IpAddr) == false { content := gin.H{"Error": "Data invalid"} c.JSON(200, content) return } if govalidator.IsIPv4(IpAddr) == true { if r.MatchString(IpAddr) == true { content := gin.H{"Country": "Private Addr"} c.JSON(200, content) } else if IpAddr == "127.0.0.1" { content := gin.H{"Country": "Localhost"} c.JSON(200, content) } else { geoip := getIp(IpAddr) content := gin.H{"Country": geoip.Country} c.JSON(200, content) } } }
func verifyFormat(val string, format string) error { switch format { case "date-time": _, err := time.Parse(time.RFC3339, val) return err case "email": if !govalidator.IsEmail(val) { return fmt.Errorf("%q is not a valid email", val) } case "hostname": if !hostnameRe.MatchString(val) || len(val) > 255 { return fmt.Errorf("%q is not a valid hostname", val) } case "ipv4": if !govalidator.IsIPv4(val) { return fmt.Errorf("%q is not a valid IPv4 address", val) } case "ipv6": if !govalidator.IsIPv6(val) { return fmt.Errorf("%q is not a valid IPv6 address", val) } case "uri": u, err := url.Parse(val) if err != nil { return err } // XXX: \noideadog{this makes all the tests pass, not sure how it really should be validated} if u.Host == "" { return fmt.Errorf("%q is not absolute", val) } } return nil }
func main() { ip := "10.20.30.101" if govalidator.IsIPv4(ip) == true { fmt.Println("IpAddr:", ip) } else { fmt.Println("Data invalid") } }
func main() { IpAddr := "10.20.30.101" r, err := regexp.Compile("(^10\\.)|(^172\\.1[6-9]\\.)|(^172\\.2[0-9]\\.)|(^172\\.3[0-9]\\.)|(^192\\.168\\.)") if err != nil { fmt.Println("There is a problem with your regexp!!\n") fmt.Println(err) return } if govalidator.IsIPv4(IpAddr) == false { fmt.Println("Data invaild") return } if r.MatchString(IpAddr) == true { fmt.Println("This is private IP") } else { fmt.Println("This is public IP") } }
func (i IsIPv4Checker) IsFormat(data string) bool { return govalidator.IsIPv4(data) }
func (s *service) IsIPv4(str string) bool { return gov.IsIPv4(str) }