1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
/**
* libgeo.go
*
* Copyright (c) 2010, Nikola Ranchev
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package libgeo
// Dependencies
import (
"errors"
"os"
)
// Globals (const arrays that will be initialized inside init())
var (
countryCode = []string{
"--", "AP", "EU", "AD", "AE", "AF", "AG", "AI", "AL", "AM", "AN", "AO", "AQ", "AR",
"AS", "AT", "AU", "AW", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ",
"BM", "BN", "BO", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CC", "CD", "CF",
"CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU", "CV", "CX", "CY", "CZ",
"DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI",
"FJ", "FK", "FM", "FO", "FR", "FX", "GA", "GB", "GD", "GE", "GF", "GH", "GI", "GL",
"GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", "GW", "GY", "HK", "HM", "HN", "HR",
"HT", "HU", "ID", "IE", "IL", "IN", "IO", "IQ", "IR", "IS", "IT", "JM", "JO", "JP",
"KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA", "LB", "LC",
"LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "MG", "MH", "MK",
"ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY",
"MZ", "NA", "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM",
"PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW", "PY",
"QA", "RE", "RO", "RU", "RW", "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ",
"SK", "SL", "SM", "SN", "SO", "SR", "ST", "SV", "SY", "SZ", "TC", "TD", "TF", "TG",
"TH", "TJ", "TK", "TM", "TN", "TO", "TL", "TR", "TT", "TV", "TW", "TZ", "UA", "UG",
"UM", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF", "WS", "YE",
"YT", "RS", "ZA", "ZM", "ME", "ZW", "A1", "A2", "O1", "AX", "GG", "IM", "JE", "BL",
"MF"}
countryName = []string{
"N/A", "Asia/Pacific Region", "Europe", "Andorra", "United Arab Emirates",
"Afghanistan", "Antigua and Barbuda", "Anguilla", "Albania", "Armenia",
"Netherlands Antilles", "Angola", "Antarctica", "Argentina", "American Samoa",
"Austria", "Australia", "Aruba", "Azerbaijan", "Bosnia and Herzegovina",
"Barbados", "Bangladesh", "Belgium", "Burkina Faso", "Bulgaria", "Bahrain",
"Burundi", "Benin", "Bermuda", "Brunei Darussalam", "Bolivia", "Brazil", "Bahamas",
"Bhutan", "Bouvet Island", "Botswana", "Belarus", "Belize", "Canada",
"Cocos (Keeling) Islands", "Congo, The Democratic Republic of the",
"Central African Republic", "Congo", "Switzerland", "Cote D'Ivoire",
"Cook Islands", "Chile", "Cameroon", "China", "Colombia", "Costa Rica", "Cuba",
"Cape Verde", "Christmas Island", "Cyprus", "Czech Republic", "Germany",
"Djibouti", "Denmark", "Dominica", "Dominican Republic", "Algeria", "Ecuador",
"Estonia", "Egypt", "Western Sahara", "Eritrea", "Spain", "Ethiopia", "Finland",
"Fiji", "Falkland Islands (Malvinas)", "Micronesia, Federated States of",
"Faroe Islands", "France", "France, Metropolitan", "Gabon", "United Kingdom",
"Grenada", "Georgia", "French Guiana", "Ghana", "Gibraltar", "Greenland", "Gambia",
"Guinea", "Guadeloupe", "Equatorial Guinea", "Greece",
"South Georgia and the South Sandwich Islands", "Guatemala", "Guam",
"Guinea-Bissau", "Guyana", "Hong Kong", "Heard Island and McDonald Islands",
"Honduras", "Croatia", "Haiti", "Hungary", "Indonesia", "Ireland", "Israel", "India",
"British Indian Ocean Territory", "Iraq", "Iran, Islamic Republic of",
"Iceland", "Italy", "Jamaica", "Jordan", "Japan", "Kenya", "Kyrgyzstan", "Cambodia",
"Kiribati", "Comoros", "Saint Kitts and Nevis",
"Korea, Democratic People's Republic of", "Korea, Republic of", "Kuwait",
"Cayman Islands", "Kazakhstan", "Lao People's Democratic Republic", "Lebanon",
"Saint Lucia", "Liechtenstein", "Sri Lanka", "Liberia", "Lesotho", "Lithuania",
"Luxembourg", "Latvia", "Libyan Arab Jamahiriya", "Morocco", "Monaco",
"Moldova, Republic of", "Madagascar", "Marshall Islands",
"Macedonia", "Mali", "Myanmar", "Mongolia",
"Macau", "Northern Mariana Islands", "Martinique", "Mauritania", "Montserrat",
"Malta", "Mauritius", "Maldives", "Malawi", "Mexico", "Malaysia", "Mozambique",
"Namibia", "New Caledonia", "Niger", "Norfolk Island", "Nigeria", "Nicaragua",
"Netherlands", "Norway", "Nepal", "Nauru", "Niue", "New Zealand", "Oman", "Panama",
"Peru", "French Polynesia", "Papua New Guinea", "Philippines", "Pakistan",
"Poland", "Saint Pierre and Miquelon", "Pitcairn Islands", "Puerto Rico",
"Palestinian Territory", "Portugal", "Palau", "Paraguay", "Qatar",
"Reunion", "Romania", "Russian Federation", "Rwanda", "Saudi Arabia",
"Solomon Islands", "Seychelles", "Sudan", "Sweden", "Singapore", "Saint Helena",
"Slovenia", "Svalbard and Jan Mayen", "Slovakia", "Sierra Leone", "San Marino",
"Senegal", "Somalia", "Suriname", "Sao Tome and Principe", "El Salvador",
"Syrian Arab Republic", "Swaziland", "Turks and Caicos Islands", "Chad",
"French Southern Territories", "Togo", "Thailand", "Tajikistan", "Tokelau",
"Turkmenistan", "Tunisia", "Tonga", "Timor-Leste", "Turkey", "Trinidad and Tobago",
"Tuvalu", "Taiwan", "Tanzania, United Republic of", "Ukraine", "Uganda",
"United States Minor Outlying Islands", "United States", "Uruguay", "Uzbekistan",
"Holy See (Vatican City State)", "Saint Vincent and the Grenadines",
"Venezuela", "Virgin Islands, British", "Virgin Islands, U.S.", "Vietnam",
"Vanuatu", "Wallis and Futuna", "Samoa", "Yemen", "Mayotte", "Serbia",
"South Africa", "Zambia", "Montenegro", "Zimbabwe", "Anonymous Proxy",
"Satellite Provider", "Other", "Aland Islands", "Guernsey", "Isle of Man", "Jersey",
"Saint Barthelemy", "Saint Martin"}
)
// Constants
const (
maxRecordLength = 4
standardRecordLength = 3
countryBegin = 16776960
structureInfoMaxSize = 20
fullRecordLength = 60
segmentRecordLength = 3
// DB Types
dbCountryEdition = byte(1)
dbCityEditionRev0 = byte(6)
dbCityEditionRev1 = byte(2)
)
// These are some structs
type GeoIP struct {
databaseSegment int // No need to make an array of size 1
recordLength int // Set to one of the constants above
dbType byte // Store the database type
data []byte // All of the data from the DB file
}
type Location struct {
CountryCode string // If country ed. only country info is filled
CountryName string // If country ed. only country info is filled
Region string
City string
PostalCode string
Latitude float32
Longitude float32
}
// Load the database file in memory, detect the db format and setup the GeoIP struct
func Load(filename string) (gi *GeoIP, err error) {
// Try to open the requested file
dbInfo, err := os.Lstat(filename)
if err != nil {
return
}
dbFile, err := os.Open(filename)
if err != nil {
return
}
// Copy the db into memory
gi = new(GeoIP)
gi.data = make([]byte, dbInfo.Size())
dbFile.Read(gi.data)
dbFile.Close()
// Check the database type
gi.dbType = dbCountryEdition // Default the database to country edition
gi.databaseSegment = countryBegin // Default to country DB
gi.recordLength = standardRecordLength // Default to country DB
// Search for the DB type headers
delim := make([]byte, 3)
for i := 0; i < structureInfoMaxSize; i++ {
delim = gi.data[len(gi.data)-i-3-1 : len(gi.data)-i-1]
if int8(delim[0]) == -1 && int8(delim[1]) == -1 && int8(delim[2]) == -1 {
gi.dbType = gi.data[len(gi.data)-i-1]
// If we detect city edition set the correct segment offset
if gi.dbType == dbCityEditionRev0 || gi.dbType == dbCityEditionRev1 {
buf := make([]byte, segmentRecordLength)
buf = gi.data[len(gi.data)-i-1+1 : len(gi.data)-i-1+4]
gi.databaseSegment = 0
for j := 0; j < segmentRecordLength; j++ {
gi.databaseSegment += (int(buf[j]) << uint8(j*8))
}
}
break
}
}
// Support older formats
if gi.dbType >= 106 {
gi.dbType -= 105
}
// Reject unsupported formats
if gi.dbType != dbCountryEdition && gi.dbType != dbCityEditionRev0 && gi.dbType != dbCityEditionRev1 {
err = errors.New("Unsupported database format")
return
}
return
}
// Lookup by IP address and return location
func (gi *GeoIP) GetLocationByIP(ip string) *Location {
return gi.GetLocationByIPNum(AddrToNum(ip))
}
// Lookup by IP number and return location
func (gi *GeoIP) GetLocationByIPNum(ipNum uint32) *Location {
// Perform the lookup on the database to see if the record is found
offset := gi.lookupByIPNum(ipNum)
// Check if the country was found
if gi.dbType == dbCountryEdition && offset-countryBegin == 0 ||
gi.dbType != dbCountryEdition && gi.databaseSegment == offset {
return nil
}
// Create a generic location structure
location := new(Location)
// If the database is country
if gi.dbType == dbCountryEdition {
location.CountryCode = countryCode[offset-countryBegin]
location.CountryName = countryName[offset-countryBegin]
return location
}
// Find the max record length
recPointer := offset + (2*gi.recordLength-1)*gi.databaseSegment
recordEnd := recPointer + fullRecordLength
if len(gi.data)-recPointer < fullRecordLength {
recordEnd = len(gi.data)
}
// Read the country code/name first
location.CountryCode = countryCode[gi.data[recPointer]]
location.CountryName = countryName[gi.data[recPointer]]
readLen := 1
recPointer += 1
// Get the region
for readLen = 0; gi.data[recPointer+readLen] != '\000' &&
recPointer+readLen < recordEnd; readLen++ {
}
if readLen != 0 {
location.Region = string(gi.data[recPointer : recPointer+readLen])
}
recPointer += readLen + 1
// Get the city
for readLen = 0; gi.data[recPointer+readLen] != '\000' &&
recPointer+readLen < recordEnd; readLen++ {
}
if readLen != 0 {
location.City = string(gi.data[recPointer : recPointer+readLen])
}
recPointer += readLen + 1
// Get the postal code
for readLen = 0; gi.data[recPointer+readLen] != '\000' &&
recPointer+readLen < recordEnd; readLen++ {
}
if readLen != 0 {
location.PostalCode = string(gi.data[recPointer : recPointer+readLen])
}
recPointer += readLen + 1
// Get the latitude
coordinate := float32(0)
for j := 0; j < 3; j++ {
coordinate += float32(int32(gi.data[recPointer+j]) << uint8(j*8))
}
location.Latitude = float32(coordinate/10000 - 180)
recPointer += 3
// Get the longitude
coordinate = 0
for j := 0; j < 3; j++ {
coordinate += float32(int32(gi.data[recPointer+j]) << uint8(j*8))
}
location.Longitude = float32(coordinate/10000 - 180)
return location
}
// Read the database and return record position
func (gi *GeoIP) lookupByIPNum(ip uint32) int {
buf := make([]byte, 2*maxRecordLength)
x := make([]int, 2)
offset := 0
for depth := 31; depth >= 0; depth-- {
for i := 0; i < 2*maxRecordLength; i++ {
buf[i] = gi.data[(2*gi.recordLength*offset)+i]
}
for i := 0; i < 2; i++ {
x[i] = 0
for j := 0; j < gi.recordLength; j++ {
var y int = int(buf[i*gi.recordLength+j])
if y < 0 {
y += 256
}
x[i] += (y << uint(j*8))
}
}
if (ip & (1 << uint(depth))) > 0 {
if x[1] >= gi.databaseSegment {
return x[1]
}
offset = x[1]
} else {
if x[0] >= gi.databaseSegment {
return x[0]
}
offset = x[0]
}
}
return 0
}
// Convert ip address to an int representation
func AddrToNum(ip string) uint32 {
octet := uint32(0)
ipnum := uint32(0)
i := 3
for j := 0; j < len(ip); j++ {
c := byte(ip[j])
if c == '.' {
if octet > 255 {
return 0
}
ipnum <<= 8
ipnum += octet
i--
octet = 0
} else {
t := octet
octet <<= 3
octet += t
octet += t
c -= '0'
if c > 9 {
return 0
}
octet += uint32(c)
}
}
if (octet > 255) || (i != 0) {
return 0
}
ipnum <<= 8
return uint32(ipnum + octet)
}
|