-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws-call-geoip.go
65 lines (59 loc) · 1.49 KB
/
ws-call-geoip.go
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
// Example How To Call Web Service from Go
//
// Copyright (C), Henryk Paluch, BSD license
//
// Note: We are Calling REST like variant of Web Service
// (GET requeust, data are returned as XML without SOAP Envelope)
// See: http://www.webservicex.net/WS/WSDetails.aspx?CATID=12&WSID=64
// for WS specification
//
// Compile and run using:
// go run ws-call-geoip.go
//
package main
import (
"encoding/xml"
"net/http"
"net/url"
"io/ioutil"
"fmt"
)
type GeoIPResponse struct {
XMLName xml.Name `xml:"GeoIP"`
ReturnCode int
IP string
ReturnCodeDetails string
CountryName string
CountryCode string
}
func deSerializeXML(data []byte) (*GeoIPResponse,error) {
v := new(GeoIPResponse)
err := xml.Unmarshal(data, v)
return v,err
}
func HPGetGeoIP( ip string) (*GeoIPResponse,error){
url := "http://www.webservicex.net/geoipservice.asmx/GetGeoIP?IPAddress="+url.QueryEscape(ip)
resp,err := http.Get(url)
if err!=nil {
return nil,err
}
body,err2:= ioutil.ReadAll(resp.Body)
if err2!=nil {
return nil,err2
}
v,err3 := deSerializeXML(body)
if err3!=nil {
return nil,err3
}
return v,nil
}
func main(){
addresses := []string{"8.8.4.4","194.79.52.192","134.76.12.3","213.180.204.46"}
for _,ip := range addresses {
v,err := HPGetGeoIP(ip)
if err!=nil {
panic(fmt.Sprintln("Error on GetGeoIP",err))
}
fmt.Printf("IP %15s (%d,%s) %s %s\n",v.IP,v.ReturnCode,v.ReturnCodeDetails,v.CountryName,v.CountryCode)
}
}