1package ipinfo 2 3import ( 4 "net" 5) 6 7// Core represents data from the Core API. 8type Core struct { 9 IP net.IP `json:"ip" csv:"ip"` 10 Hostname string `json:"hostname,omitempty" csv:"hostname"` 11 Bogon bool `json:"bogon,omitempty" csv:"bogon"` 12 Anycast bool `json:"anycast,omitempty" csv:"anycast"` 13 City string `json:"city,omitempty" csv:"city"` 14 Region string `json:"region,omitempty" csv:"region"` 15 Country string `json:"country,omitempty" csv:"country"` 16 CountryName string `json:"country_name,omitempty" csv:"country_name"` 17 Location string `json:"loc,omitempty" csv:"loc"` 18 Org string `json:"org,omitempty" csv:"org"` 19 Postal string `json:"postal,omitempty" csv:"postal"` 20 Timezone string `json:"timezone,omitempty" csv:"timezone"` 21 ASN *CoreASN `json:"asn,omitempty" csv:"asn_,inline"` 22 Company *CoreCompany `json:"company,omitempty" csv:"company_,inline"` 23 Carrier *CoreCarrier `json:"carrier,omitempty" csv:"carrier_,inline"` 24 Privacy *CorePrivacy `json:"privacy,omitempty" csv:"privacy_,inline"` 25 Abuse *CoreAbuse `json:"abuse,omitempty" csv:"abuse_,inline"` 26 Domains *CoreDomains `json:"domains,omitempty" csv:"domains_,inline"` 27} 28 29// CoreASN represents ASN data for the Core API. 30type CoreASN struct { 31 ASN string `json:"asn" csv:"id"` 32 Name string `json:"name" csv:"asn"` 33 Domain string `json:"domain" csv:"domain"` 34 Route string `json:"route" csv:"route"` 35 Type string `json:"type" csv:"type"` 36} 37 38// CoreCompany represents company data for the Core API. 39type CoreCompany struct { 40 Name string `json:"name" csv:"name"` 41 Domain string `json:"domain" csv:"domain"` 42 Type string `json:"type" csv:"type"` 43} 44 45// CoreCarrier represents carrier data for the Core API. 46type CoreCarrier struct { 47 Name string `json:"name" csv:"name"` 48 MCC string `json:"mcc" csv:"mcc"` 49 MNC string `json:"mnc" csv:"mnc"` 50} 51 52// CorePrivacy represents privacy data for the Core API. 53type CorePrivacy struct { 54 VPN bool `json:"vpn" csv:"vpn"` 55 Proxy bool `json:"proxy" csv:"proxy"` 56 Tor bool `json:"tor" csv:"tor"` 57 Relay bool `json:"relay" csv:"relay"` 58 Hosting bool `json:"hosting" csv:"hosting"` 59 Service string `json:"service" csv:"service"` 60} 61 62// CoreAbuse represents abuse data for the Core API. 63type CoreAbuse struct { 64 Address string `json:"address" csv:"address"` 65 Country string `json:"country" csv:"country"` 66 CountryName string `json:"country_name" csv:"country_name"` 67 Email string `json:"email" csv:"email"` 68 Name string `json:"name" csv:"name"` 69 Network string `json:"network" csv:"network"` 70 Phone string `json:"phone" csv:"phone"` 71} 72 73// CoreDomains represents domains data for the Core API. 74type CoreDomains struct { 75 IP string `json:"ip" csv:"-"` 76 Total uint64 `json:"total" csv:"total"` 77 Domains []string `json:"domains" csv:"-"` 78} 79 80func (v *Core) setCountryName() { 81 if v.Country != "" { 82 v.CountryName = countriesMap[v.Country] 83 } 84 if v.Abuse != nil && v.Abuse.Country != "" { 85 v.Abuse.CountryName = countriesMap[v.Abuse.Country] 86 } 87} 88 89/* CORE */ 90 91// GetIPInfo returns the details for the specified IP. 92func GetIPInfo(ip net.IP) (*Core, error) { 93 return DefaultClient.GetIPInfo(ip) 94} 95 96// GetIPInfo returns the details for the specified IP. 97func (c *Client) GetIPInfo(ip net.IP) (*Core, error) { 98 relURL := "" 99 if ip != nil { 100 relURL = ip.String() 101 } 102 103 // perform cache lookup. 104 if c.Cache != nil { 105 if res, err := c.Cache.Get(cacheKey(relURL)); err == nil { 106 return res.(*Core), nil 107 } 108 } 109 110 // prepare req 111 req, err := c.newRequest(nil, "GET", relURL, nil) 112 if err != nil { 113 return nil, err 114 } 115 116 // do req 117 v := new(Core) 118 if _, err := c.do(req, v); err != nil { 119 return nil, err 120 } 121 122 // format 123 v.setCountryName() 124 125 // cache req result 126 if c.Cache != nil { 127 if err := c.Cache.Set(cacheKey(relURL), v); err != nil { 128 // NOTE: still return the value even if the cache fails. 129 return v, err 130 } 131 } 132 133 return v, nil 134} 135 136/* IP ADDRESS */ 137 138// GetIPAddr returns the IP address that IPinfo sees when you make a request. 139func GetIPAddr() (string, error) { 140 return DefaultClient.GetIPAddr() 141} 142 143// GetIPAddr returns the IP address that IPinfo sees when you make a request. 144func (c *Client) GetIPAddr() (string, error) { 145 core, err := c.GetIPInfo(nil) 146 if err != nil { 147 return "", err 148 } 149 return core.IP.String(), nil 150} 151 152/* HOSTNAME */ 153 154// GetIPHostname returns the hostname of the domain on the specified IP. 155func GetIPHostname(ip net.IP) (string, error) { 156 return DefaultClient.GetIPHostname(ip) 157} 158 159// GetIPHostname returns the hostname of the domain on the specified IP. 160func (c *Client) GetIPHostname(ip net.IP) (string, error) { 161 core, err := c.GetIPInfo(ip) 162 if err != nil { 163 return "", err 164 } 165 return core.Hostname, nil 166} 167 168/* BOGON */ 169 170// GetIPBogon returns whether an IP is a bogon IP. 171func GetIPBogon(ip net.IP) (bool, error) { 172 return DefaultClient.GetIPBogon(ip) 173} 174 175// GetIPBogon returns whether an IP is a bogon IP. 176func (c *Client) GetIPBogon(ip net.IP) (bool, error) { 177 core, err := c.GetIPInfo(ip) 178 if err != nil { 179 return false, err 180 } 181 return core.Bogon, nil 182} 183 184/* ANYCAST */ 185 186// GetIPAnycast returns whether an IP is an anycast IP. 187func GetIPAnycast(ip net.IP) (bool, error) { 188 return DefaultClient.GetIPAnycast(ip) 189} 190 191// GetIPAnycast returns whether an IP is an anycast IP. 192func (c *Client) GetIPAnycast(ip net.IP) (bool, error) { 193 core, err := c.GetIPInfo(ip) 194 if err != nil { 195 return false, err 196 } 197 return core.Anycast, nil 198} 199 200/* CITY */ 201 202// GetIPCity returns the city for the specified IP. 203func GetIPCity(ip net.IP) (string, error) { 204 return DefaultClient.GetIPCity(ip) 205} 206 207// GetIPCity returns the city for the specified IP. 208func (c *Client) GetIPCity(ip net.IP) (string, error) { 209 core, err := c.GetIPInfo(ip) 210 if err != nil { 211 return "", err 212 } 213 return core.City, nil 214} 215 216/* REGION */ 217 218// GetIPRegion returns the region for the specified IP. 219func GetIPRegion(ip net.IP) (string, error) { 220 return DefaultClient.GetIPRegion(ip) 221} 222 223// GetIPRegion returns the region for the specified IP. 224func (c *Client) GetIPRegion(ip net.IP) (string, error) { 225 core, err := c.GetIPInfo(ip) 226 if err != nil { 227 return "", err 228 } 229 return core.Region, nil 230} 231 232/* COUNTRY */ 233 234// GetIPCountry returns the country for the specified IP. 235func GetIPCountry(ip net.IP) (string, error) { 236 return DefaultClient.GetIPCountry(ip) 237} 238 239// GetIPCountry returns the country for the specified IP. 240func (c *Client) GetIPCountry(ip net.IP) (string, error) { 241 core, err := c.GetIPInfo(ip) 242 if err != nil { 243 return "", err 244 } 245 return core.Country, nil 246} 247 248/* COUNTRY NAME */ 249 250// GetIPCountryName returns the full country name for the specified IP. 251func GetIPCountryName(ip net.IP) (string, error) { 252 return DefaultClient.GetIPCountryName(ip) 253} 254 255// GetIPCountryName returns the full country name for the specified IP. 256func (c *Client) GetIPCountryName(ip net.IP) (string, error) { 257 core, err := c.GetIPInfo(ip) 258 if err != nil { 259 return "", err 260 } 261 return core.CountryName, nil 262} 263 264/* LOCATION */ 265 266// GetIPLocation returns the location for the specified IP. 267func GetIPLocation(ip net.IP) (string, error) { 268 return DefaultClient.GetIPLocation(ip) 269} 270 271// GetIPLocation returns the location for the specified IP. 272func (c *Client) GetIPLocation(ip net.IP) (string, error) { 273 core, err := c.GetIPInfo(ip) 274 if err != nil { 275 return "", err 276 } 277 return core.Location, nil 278} 279 280/* ORG */ 281 282// GetIPOrg returns the organization for the specified IP. 283func GetIPOrg(ip net.IP) (string, error) { 284 return DefaultClient.GetIPOrg(ip) 285} 286 287// GetIPOrg returns the organization for the specified IP. 288func (c *Client) GetIPOrg(ip net.IP) (string, error) { 289 core, err := c.GetIPInfo(ip) 290 if err != nil { 291 return "", err 292 } 293 return core.Org, nil 294} 295 296/* POSTAL */ 297 298// GetIPPostal returns the postal for the specified IP. 299func GetIPPostal(ip net.IP) (string, error) { 300 return DefaultClient.GetIPPostal(ip) 301} 302 303// GetIPPostal returns the postal for the specified IP. 304func (c *Client) GetIPPostal(ip net.IP) (string, error) { 305 core, err := c.GetIPInfo(ip) 306 if err != nil { 307 return "", err 308 } 309 return core.Postal, nil 310} 311 312/* TIMEZONE */ 313 314// GetIPTimezone returns the timezone for the specified IP. 315func GetIPTimezone(ip net.IP) (string, error) { 316 return DefaultClient.GetIPTimezone(ip) 317} 318 319// GetIPTimezone returns the timezone for the specified IP. 320func (c *Client) GetIPTimezone(ip net.IP) (string, error) { 321 core, err := c.GetIPInfo(ip) 322 if err != nil { 323 return "", err 324 } 325 return core.Timezone, nil 326} 327 328/* ASN */ 329 330// GetIPASN returns the ASN details for the specified IP. 331func GetIPASN(ip net.IP) (*CoreASN, error) { 332 return DefaultClient.GetIPASN(ip) 333} 334 335// GetIPASN returns the ASN details for the specified IP. 336func (c *Client) GetIPASN(ip net.IP) (*CoreASN, error) { 337 core, err := c.GetIPInfo(ip) 338 if err != nil { 339 return nil, err 340 } 341 return core.ASN, nil 342} 343 344/* COMPANY */ 345 346// GetIPCompany returns the company details for the specified IP. 347func GetIPCompany(ip net.IP) (*CoreCompany, error) { 348 return DefaultClient.GetIPCompany(ip) 349} 350 351// GetIPCompany returns the company details for the specified IP. 352func (c *Client) GetIPCompany(ip net.IP) (*CoreCompany, error) { 353 core, err := c.GetIPInfo(ip) 354 if err != nil { 355 return nil, err 356 } 357 return core.Company, nil 358} 359 360/* CARRIER */ 361 362// GetIPCarrier returns the carrier details for the specified IP. 363func GetIPCarrier(ip net.IP) (*CoreCarrier, error) { 364 return DefaultClient.GetIPCarrier(ip) 365} 366 367// GetIPCarrier returns the carrier details for the specified IP. 368func (c *Client) GetIPCarrier(ip net.IP) (*CoreCarrier, error) { 369 core, err := c.GetIPInfo(ip) 370 if err != nil { 371 return nil, err 372 } 373 return core.Carrier, nil 374} 375 376/* PRIVACY */ 377 378// GetIPPrivacy returns the privacy details for the specified IP. 379func GetIPPrivacy(ip net.IP) (*CorePrivacy, error) { 380 return DefaultClient.GetIPPrivacy(ip) 381} 382 383// GetIPPrivacy returns the privacy details for the specified IP. 384func (c *Client) GetIPPrivacy(ip net.IP) (*CorePrivacy, error) { 385 core, err := c.GetIPInfo(ip) 386 if err != nil { 387 return nil, err 388 } 389 return core.Privacy, nil 390} 391 392/* ABUSE */ 393 394// GetIPAbuse returns the abuse details for the specified IP. 395func GetIPAbuse(ip net.IP) (*CoreAbuse, error) { 396 return DefaultClient.GetIPAbuse(ip) 397} 398 399// GetIPAbuse returns the abuse details for the specified IP. 400func (c *Client) GetIPAbuse(ip net.IP) (*CoreAbuse, error) { 401 core, err := c.GetIPInfo(ip) 402 if err != nil { 403 return nil, err 404 } 405 return core.Abuse, nil 406} 407 408/* DOMAINS */ 409 410// GetIPDomains returns the domains details for the specified IP. 411func GetIPDomains(ip net.IP) (*CoreDomains, error) { 412 return DefaultClient.GetIPDomains(ip) 413} 414 415// GetIPDomains returns the domains details for the specified IP. 416func (c *Client) GetIPDomains(ip net.IP) (*CoreDomains, error) { 417 core, err := c.GetIPInfo(ip) 418 if err != nil { 419 return nil, err 420 } 421 return core.Domains, nil 422} 423