github.com/nxtrace/NTrace-core@v1.3.1-0.20240513132635-39169291e8c9/ipgeo/ipinfo.go (about)

     1  package ipgeo
     2  
     3  import (
     4  	"github.com/nxtrace/NTrace-core/util"
     5  	"io"
     6  	"net/http"
     7  	"strconv"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/tidwall/gjson"
    12  )
    13  
    14  func IPInfo(ip string, timeout time.Duration, _ string, _ bool) (*IPGeoData, error) {
    15  	url := "http://ipinfo.io/" + ip + "?token=" + token.ipinfo
    16  	client := &http.Client{
    17  		// 2 秒超时
    18  		Timeout: timeout,
    19  	}
    20  	resp, err := client.Get(url)
    21  	//resp, err := http.Get("https://ipinfo.io/" + ip + "?token=" + token.ipinfo)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	defer resp.Body.Close()
    26  	body, err := io.ReadAll(resp.Body)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	res := gjson.ParseBytes(body)
    32  
    33  	// ISO-3166 转换
    34  	var countryMap = map[string]string{
    35  		"AF": "Afghanistan",
    36  		"AX": "Åland Islands",
    37  		"AL": "Albania",
    38  		"DZ": "Algeria",
    39  		"AS": "American Samoa",
    40  		"AD": "Andorra",
    41  		"AO": "Angola",
    42  		"AI": "Anguilla",
    43  		"AQ": "Antarctica",
    44  		"AG": "Antigua and Barbuda",
    45  		"AR": "Argentina",
    46  		"AM": "Armenia",
    47  		"AW": "Aruba",
    48  		"AU": "Australia",
    49  		"AT": "Austria",
    50  		"AZ": "Azerbaijan",
    51  		"BH": "Bahrain",
    52  		"BS": "Bahamas",
    53  		"BD": "Bangladesh",
    54  		"BB": "Barbados",
    55  		"BY": "Belarus",
    56  		"BE": "Belgium",
    57  		"BZ": "Belize",
    58  		"BJ": "Benin",
    59  		"BM": "Bermuda",
    60  		"BT": "Bhutan",
    61  		"BO": "Bolivia",
    62  		"BQ": "Bonaire",
    63  		"BA": "Bosnia and Herzegovina",
    64  		"BW": "Botswana",
    65  		"BV": "Bouvet Island",
    66  		"BR": "Brazil",
    67  		"IO": "British Indian Ocean Territory",
    68  		"BN": "Brunei Darussalam",
    69  		"BG": "Bulgaria",
    70  		"BF": "Burkina Faso",
    71  		"BI": "Burundi",
    72  		"KH": "Cambodia",
    73  		"CM": "Cameroon",
    74  		"CA": "Canada",
    75  		"CV": "Cape Verde",
    76  		"KY": "Cayman Islands",
    77  		"CF": "Central African Republic",
    78  		"TD": "Chad",
    79  		"CL": "Chile",
    80  		"CN": "China",
    81  		"CX": "Christmas Island",
    82  		"CC": "Cocos (Keeling) Islands",
    83  		"CO": "Colombia",
    84  		"KM": "Comoros",
    85  		"CG": "Congo",
    86  		"CD": "Congo",
    87  		"CK": "Cook Islands",
    88  		"CR": "Costa Rica",
    89  		"CI": "Côte d'Ivoire",
    90  		"HR": "Croatia",
    91  		"CU": "Cuba",
    92  		"CW": "Curaçao",
    93  		"CY": "Cyprus",
    94  		"CZ": "Czech Republic",
    95  		"DK": "Denmark",
    96  		"DJ": "Djibouti",
    97  		"DM": "Dominica",
    98  		"DO": "Dominican Republic",
    99  		"EC": "Ecuador",
   100  		"EG": "Egypt",
   101  		"SV": "El Salvador",
   102  		"GQ": "Equatorial Guinea",
   103  		"ER": "Eritrea",
   104  		"EE": "Estonia",
   105  		"ET": "Ethiopia",
   106  		"FK": "Falkland Islands (Malvinas)",
   107  		"FO": "Faroe Islands",
   108  		"FJ": "Fiji",
   109  		"FI": "Finland",
   110  		"FR": "France",
   111  		"GF": "French Guiana",
   112  		"PF": "French Polynesia",
   113  		"TF": "French Southern Territories",
   114  		"GA": "Gabon",
   115  		"GM": "Gambia",
   116  		"GE": "Georgia",
   117  		"DE": "Germany",
   118  		"GH": "Ghana",
   119  		"GI": "Gibraltar",
   120  		"GR": "Greece",
   121  		"GL": "Greenland",
   122  		"GD": "Grenada",
   123  		"GP": "Guadeloupe",
   124  		"GU": "Guam",
   125  		"GT": "Guatemala",
   126  		"GG": "Guernsey",
   127  		"GN": "Guinea",
   128  		"GW": "Guinea-Bissau",
   129  		"GY": "Guyana",
   130  		"HT": "Haiti",
   131  		"HM": "Heard Island and McDonald Islands",
   132  		"VA": "Holy See (Vatican City State)",
   133  		"HN": "Honduras",
   134  		"HK": "Hong Kong",
   135  		"HU": "Hungary",
   136  		"IS": "Iceland",
   137  		"IN": "India",
   138  		"ID": "Indonesia",
   139  		"IR": "Iran",
   140  		"IQ": "Iraq",
   141  		"IE": "Ireland",
   142  		"IM": "Isle of Man",
   143  		"IL": "Israel",
   144  		"IT": "Italy",
   145  		"JM": "Jamaica",
   146  		"JP": "Japan",
   147  		"JE": "Jersey",
   148  		"JO": "Jordan",
   149  		"KZ": "Kazakhstan",
   150  		"KE": "Kenya",
   151  		"KI": "Kiribati",
   152  		"KP": "Korea",
   153  		"KR": "Korea",
   154  		"KW": "Kuwait",
   155  		"KG": "Kyrgyzstan",
   156  		"LA": "Lao People's Democratic Republic",
   157  		"LV": "Latvia",
   158  		"LB": "Lebanon",
   159  		"LS": "Lesotho",
   160  		"LR": "Liberia",
   161  		"LY": "Libya",
   162  		"LI": "Liechtenstein",
   163  		"LT": "Lithuania",
   164  		"LU": "Luxembourg",
   165  		"MO": "Macao",
   166  		"MK": "Macedonia",
   167  		"MG": "Madagascar",
   168  		"MW": "Malawi",
   169  		"MY": "Malaysia",
   170  		"MV": "Maldives",
   171  		"ML": "Mali",
   172  		"MT": "Malta",
   173  		"MH": "Marshall Islands",
   174  		"MQ": "Martinique",
   175  		"MR": "Mauritania",
   176  		"MU": "Mauritius",
   177  		"YT": "Mayotte",
   178  		"MX": "Mexico",
   179  		"FM": "Micronesia",
   180  		"MD": "Moldova",
   181  		"MC": "Monaco",
   182  		"MN": "Mongolia",
   183  		"ME": "Montenegro",
   184  		"MS": "Montserrat",
   185  		"MA": "Morocco",
   186  		"MZ": "Mozambique",
   187  		"MM": "Myanmar",
   188  		"NA": "Namibia",
   189  		"NR": "Nauru",
   190  		"NP": "Nepal",
   191  		"NL": "Netherlands",
   192  		"NC": "New Caledonia",
   193  		"NZ": "New Zealand",
   194  		"NI": "Nicaragua",
   195  		"NE": "Niger",
   196  		"NG": "Nigeria",
   197  		"NU": "Niue",
   198  		"NF": "Norfolk Island",
   199  		"MP": "Northern Mariana Islands",
   200  		"NO": "Norway",
   201  		"OM": "Oman",
   202  		"PK": "Pakistan",
   203  		"PW": "Palau",
   204  		"PS": "Palestine",
   205  		"PA": "Panama",
   206  		"PG": "Papua New Guinea",
   207  		"PY": "Paraguay",
   208  		"PE": "Peru",
   209  		"PH": "Philippines",
   210  		"PN": "Pitcairn",
   211  		"PL": "Poland",
   212  		"PT": "Portugal",
   213  		"PR": "Puerto Rico",
   214  		"QA": "Qatar",
   215  		"RE": "Réunion",
   216  		"RO": "Romania",
   217  		"RU": "Russian Federation",
   218  		"RW": "Rwanda",
   219  		"BL": "Saint Barthélemy",
   220  		"SH": "Saint Helena",
   221  		"KN": "Saint Kitts and Nevis",
   222  		"LC": "Saint Lucia",
   223  		"MF": "Saint Martin (French part)",
   224  		"PM": "Saint Pierre and Miquelon",
   225  		"VC": "Saint Vincent and the Grenadines",
   226  		"WS": "Samoa",
   227  		"SM": "San Marino",
   228  		"ST": "Sao Tome and Principe",
   229  		"SA": "Saudi Arabia",
   230  		"SN": "Senegal",
   231  		"RS": "Serbia",
   232  		"SC": "Seychelles",
   233  		"SL": "Sierra Leone",
   234  		"SG": "Singapore",
   235  		"SX": "Sint Maarten (Dutch part)",
   236  		"SK": "Slovakia",
   237  		"SI": "Slovenia",
   238  		"SB": "Solomon Islands",
   239  		"SO": "Somalia",
   240  		"ZA": "South Africa",
   241  		"GS": "South Georgia and the South Sandwich Islands",
   242  		"SS": "South Sudan",
   243  		"ES": "Spain",
   244  		"LK": "Sri Lanka",
   245  		"SD": "Sudan",
   246  		"SR": "Suriname",
   247  		"SJ": "Svalbard and Jan Mayen",
   248  		"SZ": "Swaziland",
   249  		"SE": "Sweden",
   250  		"CH": "Switzerland",
   251  		"SY": "Syrian Arab Republic",
   252  		"TW": "Taiwan",
   253  		"TJ": "Tajikistan",
   254  		"TZ": "Tanzania",
   255  		"TH": "Thailand",
   256  		"TL": "Timor-Leste",
   257  		"TG": "Togo",
   258  		"TK": "Tokelau",
   259  		"TO": "Tonga",
   260  		"TT": "Trinidad and Tobago",
   261  		"TN": "Tunisia",
   262  		"TR": "Turkey",
   263  		"TM": "Turkmenistan",
   264  		"TC": "Turks and Caicos Islands",
   265  		"TV": "Tuvalu",
   266  		"UG": "Uganda",
   267  		"UA": "Ukraine",
   268  		"AE": "United Arab Emirates",
   269  		"GB": "United Kingdom",
   270  		"US": "United States of America",
   271  		"UM": "United States Minor Outlying Islands",
   272  		"UY": "Uruguay",
   273  		"UZ": "Uzbekistan",
   274  		"VU": "Vanuatu",
   275  		"VE": "Venezuela",
   276  		"VN": "Viet Nam",
   277  		"VG": "Virgin Islands",
   278  		"VI": "Virgin Islands",
   279  		"WF": "Wallis and Futuna",
   280  		"EH": "Western Sahara",
   281  		"YE": "Yemen",
   282  		"ZM": "Zambia",
   283  		"ZW": "Zimbabwe",
   284  	}
   285  	var country = res.Get("country").String()
   286  	var prov = res.Get("region").String()
   287  	var city = res.Get("city").String()
   288  	var district = ""
   289  	if util.StringInSlice(country, []string{"TW", "MO", "HK"}) {
   290  		district = prov + " " + city
   291  		city = countryMap[country]
   292  		prov = ""
   293  		country = "CN"
   294  	}
   295  	country = countryMap[country]
   296  
   297  	var anycast = false
   298  	if res.Get("anycast").String() == "true" {
   299  		country = "ANYCAST"
   300  		prov = "ANYCAST"
   301  		city = ""
   302  		anycast = true
   303  	}
   304  
   305  	i := strings.Index(res.Get("org").String(), " ")
   306  	var owner string
   307  	if i == -1 {
   308  		owner = ""
   309  	} else {
   310  		owner = res.Get("org").String()[i:]
   311  	}
   312  
   313  	var asnumber = ""
   314  	// 有时候不返回asn或其本身没有asn
   315  	if strings.HasPrefix(res.Get("org").String(), "AS") {
   316  		asnumber = strings.Fields(strings.TrimPrefix(res.Get("org").String(), "AS"))[0]
   317  	}
   318  
   319  	//"loc": "34.0522,-118.2437",
   320  	var lat, lng float64
   321  	if res.Get("loc").String() != "" {
   322  		lat, _ = strconv.ParseFloat(strings.Split(res.Get("loc").String(), ",")[0], 32)
   323  		lng, _ = strconv.ParseFloat(strings.Split(res.Get("loc").String(), ",")[1], 32)
   324  	}
   325  	if anycast {
   326  		lat, lng = 0, 0
   327  	}
   328  
   329  	return &IPGeoData{
   330  		Asnumber: asnumber,
   331  		Country:  country,
   332  		City:     city,
   333  		Prov:     prov,
   334  		District: district,
   335  		Owner:    owner,
   336  		Lat:      lat,
   337  		Lng:      lng,
   338  	}, nil
   339  }