diff --git a/coordinates/country.go b/coordinates/country.go new file mode 100644 index 0000000..e09ca2a --- /dev/null +++ b/coordinates/country.go @@ -0,0 +1,264 @@ +// Copyright (C) 2023 Shizun Ge +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// + +package coordinates + +// Map country's ISO to their capital's latitude and longitude. +// Country's ISO see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 +type Location struct { + Latitude float64 + Longitude float64 +} + +var Country = map[string]Location{ + "AD": {42.5, 1.5}, + "AE": {24.4511, 54.3969}, + "AF": {34.5328, 69.1658}, + "AG": {17.1211, -61.8447}, + "AI": {18.2167, -63.05}, + "AL": {41.33, 19.82}, + "AM": {40.1814, 44.5144}, + "AO": {-8.8383, 13.2344}, + "AR": {-34.5997, -58.3819}, + "AS": {-14.274, -170.7046}, + "AT": {48.2083, 16.3725}, + "AU": {-35.2931, 149.1269}, + "AW": {12.5186, -70.0358}, + "AZ": {40.3667, 49.8352}, + "BA": {43.8563, 18.4132}, + "BB": {13.0975, -59.6167}, + "BD": {23.7289, 90.3944}, + "BE": {50.8353, 4.3314}, + "BF": {12.3686, -1.5275}, + "BG": {42.6979, 23.3217}, + "BH": {26.225, 50.5775}, + "BI": {-3.3825, 29.3611}, + "BJ": {6.402, 2.518}, + "BL": {17.8958, -62.8508}, + "BM": {32.2942, -64.7839}, + "BN": {4.9167, 114.9167}, + "BO": {-16.4942, -68.1475}, + "BR": {-15.7939, -47.8828}, + "BS": {25.0667, -77.3333}, + "BT": {27.4833, 89.6333}, + "BW": {-24.6569, 25.9086}, + "BY": {53.9022, 27.5618}, + "BZ": {17.25, -88.7675}, + "CA": {45.4247, -75.695}, + "CD": {-4.3317, 15.3139}, + "CF": {4.3732, 18.5628}, + "CG": {-4.2667, 15.2833}, + "CH": {46.948, 7.4474}, + "CI": {5.3364, -4.0267}, + "CK": {-21.207, -159.771}, + "CL": {-33.45, -70.6667}, + "CM": {3.8578, 11.5181}, + "CN": {39.904, 116.4075}, + "CO": {4.6126, -74.0705}, + "CR": {9.9333, -84.0833}, + "CU": {23.1367, -82.3589}, + "CV": {14.9177, -23.5092}, + "CW": {12.108, -68.935}, + "CX": {-10.4167, 105.7167}, + "CY": {35.1725, 33.365}, + "CZ": {50.0833, 14.4167}, + "DE": {52.5167, 13.3833}, + "DJ": {11.595, 43.1481}, + "DK": {55.6805, 12.5615}, + "DM": {15.3, -61.3833}, + "DO": {18.4764, -69.8933}, + "DZ": {36.7764, 3.0586}, + "EC": {-0.22, -78.5125}, + "EE": {59.4372, 24.745}, + "EG": {30.0444, 31.2358}, + "ER": {15.3333, 38.9167}, + "ES": {40.4167, -3.7167}, + "ET": {9.0272, 38.7369}, + "FI": {60.1756, 24.9342}, + "FJ": {-18.1333, 178.4333}, + "FK": {-51.7, -57.85}, + "FM": {6.9178, 158.185}, + "FO": {62, -6.7833}, + "FR": {48.8566, 2.3522}, + "GA": {0.3901, 9.4544}, + "GB": {51.5072, -0.1275}, + "GD": {12.0444, -61.7417}, + "GE": {41.7225, 44.7925}, + "GF": {4.933, -52.33}, + "GH": {5.6037, -0.187}, + "GI": {36.1324, -5.3781}, + "GL": {64.175, -51.7333}, + "GM": {13.4531, -16.5775}, + "GN": {9.538, -13.6773}, + "GP": {16.0104, -61.7055}, + "GQ": {3.7521, 8.7737}, + "GR": {37.9842, 23.7281}, + "GS": {-54.2833, -36.5}, + "GT": {14.6099, -90.5252}, + "GU": {13.4745, 144.7504}, + "GW": {11.8592, -15.5956}, + "GY": {6.7833, -58.1667}, + "HK": {22.3069, 114.1831}, + "HN": {14.0942, -87.2067}, + "HR": {45.8131, 15.9772}, + "HT": {18.5425, -72.3386}, + "HU": {47.4983, 19.0408}, + "ID": {-6.2146, 106.8451}, + "IE": {53.3497, -6.2603}, + "IL": {31.7833, 35.2167}, + "IM": {54.15, -4.4819}, + "IN": {28.6139, 77.209}, + "IQ": {33.35, 44.4167}, + "IR": {35.7, 51.4167}, + "IS": {64.1475, -21.935}, + "IT": {41.8931, 12.4828}, + "JE": {49.1858, -2.11}, + "JM": {17.9714, -76.7931}, + "JO": {31.95, 35.9333}, + "JP": {35.6839, 139.7744}, + "KE": {-1.2864, 36.8172}, + "KG": {42.8667, 74.5667}, + "KH": {11.5696, 104.921}, + "KI": {1.3382, 173.0176}, + "KM": {-11.7036, 43.2536}, + "KN": {17.2983, -62.7342}, + "KP": {39.03, 125.73}, + "KR": {37.56, 126.99}, + "KW": {29.375, 47.98}, + "KY": {19.2866, -81.3744}, + "KZ": {51.1333, 71.4333}, + "LA": {17.9667, 102.6}, + "LB": {33.8869, 35.5131}, + "LC": {14.0167, -60.9833}, + "LI": {47.1397, 9.5219}, + "LK": {6.9, 79.9164}, + "LR": {6.3106, -10.8047}, + "LS": {-29.31, 27.48}, + "LT": {54.6833, 25.2833}, + "LU": {49.6106, 6.1328}, + "LV": {56.9475, 24.1069}, + "LY": {32.8752, 13.1875}, + "MA": {26.0928, -10.6089}, + "MC": {43.7396, 7.4069}, + "MD": {47.0228, 28.8353}, + "ME": {42.4397, 19.2661}, + "MF": {18.0706, -63.0847}, + "MG": {-18.9386, 47.5214}, + "MH": {7.0918, 171.3802}, + "MK": {41.9833, 21.4333}, + "ML": {12.6458, -7.9922}, + "MM": {16.795, 96.16}, + "MN": {47.9214, 106.9055}, + "MP": {15.2137, 145.7546}, + "MQ": {14.6104, -61.08}, + "MR": {18.0858, -15.9785}, + "MS": {16.7928, -62.2106}, + "MT": {35.8978, 14.5125}, + "MU": {-20.1667, 57.5}, + "MV": {4.175, 73.5083}, + "MW": {-13.9833, 33.7833}, + "MX": {19.4333, -99.1333}, + "MY": {3.1478, 101.6953}, + "MZ": {-25.9153, 32.5764}, + "NA": {-22.57, 17.0836}, + "NC": {-22.2625, 166.4443}, + "NE": {13.5086, 2.1111}, + "NF": {-29.0569, 167.9617}, + "NG": {9.0556, 7.4914}, + "NI": {12.15, -86.2667}, + "NL": {52.08, 4.31}, + "NO": {59.9111, 10.7528}, + "NP": {27.7167, 85.3667}, + "NR": {-0.5477, 166.9209}, + "NU": {-19.056, -169.921}, + "NZ": {-41.2889, 174.7772}, + "OM": {23.6139, 58.5922}, + "PA": {9, -79.5}, + "PE": {-12.06, -77.0375}, + "PF": {-17.5334, -149.5667}, + "PG": {-9.4789, 147.1494}, + "PH": {14.6, 120.9833}, + "PK": {33.6989, 73.0369}, + "PL": {52.23, 21.0111}, + "PM": {46.7811, -56.1764}, + "PN": {-25.0667, -130.0833}, + "PR": {18.4037, -66.0636}, + "PT": {38.708, -9.139}, + "PW": {7.5006, 134.6242}, + "PY": {-25.3, -57.6333}, + "QA": {25.3, 51.5333}, + "RE": {-20.8789, 55.4481}, + "RO": {44.4, 26.0833}, + "RS": {44.8167, 20.4667}, + "RU": {55.7558, 37.6178}, + "RW": {-1.9536, 30.0606}, + "SA": {24.65, 46.71}, + "SB": {-9.4333, 159.95}, + "SC": {-4.6236, 55.4544}, + "SD": {15.6031, 32.5265}, + "SE": {59.3294, 18.0686}, + "SG": {1.3, 103.8}, + "SH": {-15.9251, -5.7179}, + "SI": {46.05, 14.5167}, + "SK": {48.1447, 17.1128}, + "SL": {8.4833, -13.2331}, + "SM": {43.932, 12.4484}, + "SN": {14.7319, -17.4572}, + "SO": {2.0408, 45.3425}, + "SR": {5.8667, -55.1667}, + "SS": {4.85, 31.6}, + "ST": {0.3333, 6.7333}, + "SV": {13.6989, -89.1914}, + "SX": {18.0256, -63.0492}, + "SY": {33.5131, 36.2919}, + "SZ": {-26.3208, 31.1617}, + "TC": {21.4664, -71.136}, + "TD": {12.11, 15.05}, + "TG": {6.1319, 1.2228}, + "TH": {13.75, 100.5167}, + "TJ": {38.5731, 68.7864}, + "TL": {-8.5536, 125.5783}, + "TM": {37.95, 58.3833}, + "TN": {36.8008, 10.18}, + "TO": {-21.1347, -175.2083}, + "TR": {39.93, 32.85}, + "TT": {10.6667, -61.5167}, + "TV": {-8.5243, 179.1942}, + "TZ": {-6.8, 39.2833}, + "UA": {50.45, 30.5236}, + "UG": {0.3136, 32.5811}, + "US": {38.9047, -77.0163}, + "UY": {-34.8667, -56.1667}, + "UZ": {41.3, 69.2667}, + "VA": {41.9033, 12.4534}, + "VC": {13.1667, -61.2333}, + "VE": {10.5, -66.9333}, + "VG": {18.4167, -64.6167}, + "VI": {18.3419, -64.9332}, + "VN": {21.0245, 105.8412}, + "VU": {-17.7333, 168.3167}, + "WF": {-13.2825, -176.1736}, + "WS": {-13.8333, -171.8333}, + "XG": {31.5069, 34.456}, + "XK": {42.6633, 21.1622}, + "XR": {78.2167, 15.6333}, + "XW": {31.7764, 35.2269}, + "YE": {15.35, 44.2}, + "YT": {-12.7871, 45.275}, + "ZA": {-25.7464, 28.1881}, + "ZM": {-15.4167, 28.2833}, + "ZW": {-17.8292, 31.0522}, +} diff --git a/geoip.go b/geoip.go index 1e96467..469a54f 100644 --- a/geoip.go +++ b/geoip.go @@ -1,4 +1,4 @@ -// Copyright (C) 2021 Shizun Ge +// Copyright (C) 2021-2023 Shizun Ge // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -24,6 +24,8 @@ import ( "net/http" "strings" + "endlessh-go/coordinates" + "github.com/oschwald/geoip2-golang" "github.com/pierrre/geohash" ) @@ -102,25 +104,38 @@ func geohashAndLocationFromMaxMindDb(address string) (string, string, string, er return "s000", "Unknown", "Unknown", err } defer db.Close() - // If you are using strings that may be invalid, check that ip is not nil - ip := net.ParseIP(address) - cityRecord, err := db.City(ip) countryName := "Unknown" cityName := "Unknown" var latitude, longitude float64 + var iso string + // If you are using strings that may be invalid, check that ip is not nil + ip := net.ParseIP(address) + cityRecord, err := db.City(ip) if err == nil { countryName = cityRecord.Country.Names["en"] cityName = cityRecord.City.Names["en"] latitude = cityRecord.Location.Latitude longitude = cityRecord.Location.Longitude + iso = cityRecord.Country.IsoCode } else { - // In case of using Country DB, city is not available. countryRecord, err := db.Country(ip) if err != nil { return "s000", countryName, cityName, err } countryName = countryRecord.Country.Names["en"] cityName = "Unknown" + iso = countryRecord.Country.IsoCode + } + if latitude == 0 && longitude == 0 { + // In case of using Country DB, city is not available. + loc, ok := coordinates.Country[iso] + if ok { + latitude = loc.Latitude + longitude = loc.Longitude + } else { + // For debugging, adding the iso to the country name. + countryName = countryName + " (" + iso + ")" + } } gh := geohash.EncodeAuto(latitude, longitude) country := composeCountry(countryName) diff --git a/main.go b/main.go index 9cd1627..24c637b 100644 --- a/main.go +++ b/main.go @@ -1,4 +1,4 @@ -// Copyright (C) 2021 Shizun Ge +// Copyright (C) 2021-2023 Shizun Ge // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by