|
| 1 | +package promexporter |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io/ioutil" |
| 6 | + "log" |
| 7 | + "net" |
| 8 | + "net/http" |
| 9 | + |
| 10 | + "github.com/oschwald/geoip2-golang" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + geodbType string |
| 15 | + geodbPath string |
| 16 | + geoLang string |
| 17 | + geoURL string |
| 18 | + geodbIs = false |
| 19 | +) |
| 20 | + |
| 21 | +type geoInfo struct { |
| 22 | + countyISOCode string |
| 23 | + countryName string |
| 24 | + cityName string |
| 25 | +} |
| 26 | + |
| 27 | +// SetGeodbPath sets geoIP database parameters from command line argument geo.type, geo.db and geo.lang or geo.url |
| 28 | +func SetGeodbPath(geoType, filePath, outputLang, url string) { |
| 29 | + geodbType = geoType |
| 30 | + geodbPath = filePath |
| 31 | + geoLang = outputLang |
| 32 | + geoURL = url |
| 33 | + checkGeoDBFlags() |
| 34 | +} |
| 35 | + |
| 36 | +func checkGeoDBFlags() { |
| 37 | + notSetLogMsg := "GeoIP database is not set and not use" |
| 38 | + switch geodbType { |
| 39 | + case "": |
| 40 | + log.Println(notSetLogMsg) |
| 41 | + case "db": |
| 42 | + if geodbPath == "" { |
| 43 | + log.Println("Error geo.db flag is not set", geodbPath) |
| 44 | + log.Println(notSetLogMsg) |
| 45 | + } else { |
| 46 | + geodbIs = true |
| 47 | + log.Println("Use GeoIp database file", geodbPath) |
| 48 | + } |
| 49 | + case "url": |
| 50 | + geodbIs = true |
| 51 | + log.Println("Use GeoIp database url", geoURL) |
| 52 | + default: |
| 53 | + log.Println("Error geo.type flag value is incorect", geodbType) |
| 54 | + log.Println(notSetLogMsg) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func getIPDetailsFromLocalDB(returnValues *geoInfo, ipAddres string) { |
| 59 | + geodb, err := geoip2.Open(geodbPath) |
| 60 | + if err != nil { |
| 61 | + log.Println("Error opening GeoIp database file", err) |
| 62 | + return |
| 63 | + } |
| 64 | + defer geodb.Close() |
| 65 | + ip := net.ParseIP(ipAddres) |
| 66 | + if ip == nil { |
| 67 | + log.Println("Error parsing ip address", ipAddres) |
| 68 | + return |
| 69 | + } |
| 70 | + record, err := geodb.City(ip) |
| 71 | + if err != nil { |
| 72 | + log.Println("Error getting location details", err) |
| 73 | + return |
| 74 | + } |
| 75 | + returnValues.countyISOCode = record.Country.IsoCode |
| 76 | + returnValues.countryName = record.Country.Names[geoLang] |
| 77 | + returnValues.cityName = record.City.Names[geoLang] |
| 78 | +} |
| 79 | + |
| 80 | +func getIPDetailsFromURL(returnValues *geoInfo, ipAddres string) { |
| 81 | + response, err := http.Get(geoURL + ipAddres) |
| 82 | + if err != nil { |
| 83 | + log.Println("Error getting GeoIp URL", err) |
| 84 | + return |
| 85 | + } |
| 86 | + defer response.Body.Close() |
| 87 | + body, err := ioutil.ReadAll(response.Body) |
| 88 | + if err != nil { |
| 89 | + log.Println("Error getting body from GeoIp URL", err) |
| 90 | + return |
| 91 | + } |
| 92 | + var parseData map[string]interface{} |
| 93 | + err = json.Unmarshal([]byte(body), &parseData) |
| 94 | + if err != nil { |
| 95 | + log.Println("Error parsing json-encoded body from GeoIp URL", err) |
| 96 | + return |
| 97 | + } |
| 98 | + returnValues.countyISOCode = getMap(parseData, "country_code") |
| 99 | + returnValues.countryName = getMap(parseData, "country_name") |
| 100 | + returnValues.cityName = getMap(parseData, "city") |
| 101 | +} |
| 102 | + |
| 103 | +func getMap(data map[string]interface{}, key string) string { |
| 104 | + str, ok := data[key].(string) |
| 105 | + if !ok { |
| 106 | + log.Printf("Error for key %s value %s is not a string", key, str) |
| 107 | + } |
| 108 | + return str |
| 109 | +} |
0 commit comments