forked from abh/geodns
-
Notifications
You must be signed in to change notification settings - Fork 1
/
geoip-demo.go
65 lines (50 loc) · 1.34 KB
/
geoip-demo.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
65
package main
import (
"fmt"
"github.com/abh/geodns/Godeps/_workspace/src/github.com/abh/geoip"
)
func main() {
file6 := "../db/GeoIPv6.dat"
gi6, err := geoip.Open(file6)
if err != nil {
fmt.Printf("Could not open GeoIPv6 database: %s\n", err)
}
gi, err := geoip.Open()
if err != nil {
fmt.Printf("Could not open GeoIP database: %s\n", err)
}
giasn, err := geoip.Open("../db/GeoIPASNum.dat")
if err != nil {
fmt.Printf("Could not open GeoIPASN database: %s\n", err)
}
giasn6, err := geoip.Open("../db/GeoIPASNumv6.dat")
if err != nil {
fmt.Printf("Could not open GeoIPASN database: %s\n", err)
}
if giasn != nil {
ip := "207.171.7.51"
asn, netmask := giasn.GetName(ip)
fmt.Printf("%s: %s (netmask /%d)\n", ip, asn, netmask)
}
if gi != nil {
test4(*gi, "207.171.7.51")
test4(*gi, "127.0.0.1")
}
if gi6 != nil {
ip := "2607:f238:2::5"
country, netmask := gi6.GetCountry_v6(ip)
var asn string
var asn_netmask int
if giasn6 != nil {
asn, asn_netmask = giasn6.GetNameV6(ip)
}
fmt.Printf("%s: %s/%d %s/%d\n", ip, country, netmask, asn, asn_netmask)
}
}
func test4(g geoip.GeoIP, ip string) {
test(func(s string) (string, int) { return g.GetCountry(s) }, ip)
}
func test(f func(string) (string, int), ip string) {
country, netmask := f(ip)
fmt.Printf("ip: %s is [%s] (netmask %d)\n", ip, country, netmask)
}