tgeoip.go - geoip - short golang utility to fetch geoip data from an API
 (HTM) git clone https://git.parazyd.org/geoip
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
       tgeoip.go (1239B)
       ---
            1 package main
            2 
            3 /* See LICENSE file for copyright and license details */
            4 
            5 import (
            6         "encoding/json"
            7         "encoding/xml"
            8         "fmt"
            9         "io/ioutil"
           10         "log"
           11         "net/http"
           12         "os"
           13         "strings"
           14 )
           15 
           16 type Result struct {
           17         //XMLName     xml.Name `xml:"result"`
           18         IP          string `xml:"ip"`
           19         Host        string `xml:"host"`
           20         ISP         string `xml:"isp"`
           21         City        string `xml:"city"`
           22         CountryCode string `xml:"countrycode"`
           23         CountryName string `xml:"countryname"`
           24         Latitude    string `xml:"latitude"`
           25         Longitude   string `xml:"longitude"`
           26 }
           27 
           28 func main() {
           29         if len(os.Args) != 2 {
           30                 fmt.Fprintf(os.Stderr, "usage: geoip 1.1.1.1\n")
           31                 os.Exit(1)
           32         }
           33 
           34         resp, err := http.Get("http://api.geoiplookup.net/?query=" + os.Args[1])
           35         if err != nil {
           36                 log.Fatal(err)
           37         }
           38         defer resp.Body.Close()
           39 
           40         body, err := ioutil.ReadAll(resp.Body)
           41         if err != nil {
           42                 log.Fatal(err)
           43         }
           44 
           45         sbody := string(body)
           46         sbody = strings.Replace(sbody, "iso-8859-1", "UTF-8", 1)
           47         sbody = strings.Replace(sbody, "<ip>\n<results>", "", 1)
           48         sbody = strings.Replace(sbody, "</results>\n</ip>", "", 1)
           49 
           50         var data Result
           51         if err := xml.Unmarshal([]byte(sbody), &data); err != nil {
           52                 log.Fatal(err)
           53         }
           54 
           55         enc := json.NewEncoder(os.Stdout)
           56         if err := enc.Encode(data); err != nil {
           57                 log.Fatal(err)
           58         }
           59 }