This repository has been archived by the owner on Jun 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathgeoip.go
145 lines (131 loc) · 3.71 KB
/
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
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
package main
import (
"encoding/json"
"flag"
"github.com/klauspost/geoip-service/geoip2"
"github.com/pmylund/go-cache"
"log"
"net"
"net/http"
"runtime"
"strings"
"time"
)
//go:generate ffjson --nodecoder $GOFILE
// ffjson: nodecoder
type ResponseCity struct {
Data *geoip2.City `json:",omitempty"`
Error string `json:",omitempty"`
}
// ffjson: nodecoder
type ResponseCountry struct {
Data *geoip2.Country `json:",omitempty"`
Error string `json:",omitempty"`
}
func main() {
var dbName = flag.String("db", "GeoLite2-City.mmdb", "File name of MaxMind GeoIP2 and GeoLite2 database")
var lookup = flag.String("lookup", "city", "Specify which value to look up. Can be 'city' or 'country' depending on which database you load.")
var listen = flag.String("listen", ":5000", "Listen address and port, for instance 127.0.0.1:5000")
var threads = flag.Int("threads", runtime.NumCPU(), "Number of threads to use. Defaults to number of detected cores")
var pretty = flag.Bool("pretty", false, "Should output be formatted with newlines and intentation")
var cacheSecs = flag.Int("cache", 0, "How many seconds should requests be cached. Set to 0 to disable")
var originPolicy = flag.String("origin", "*", `Value sent in the 'Access-Control-Allow-Origin' header. Set to "" to disable.`)
serverStart := time.Now().Format(http.TimeFormat)
flag.Parse()
runtime.GOMAXPROCS(*threads)
var memCache *cache.Cache
if *cacheSecs > 0 {
memCache = cache.New(time.Duration(*cacheSecs)*time.Second, 1*time.Second)
}
lookupCity := true
if *lookup == "country" {
lookupCity = false
} else if *lookup != "city" {
log.Fatalf("lookup parameter should be either 'city', or 'country', it is '%s'", *lookup)
}
db, err := geoip2.Open(*dbName)
if err != nil {
log.Fatal(err)
}
defer db.Close()
log.Println("Loaded database " + *dbName)
// We dereference this to avoid a pretty big penalty under heavy load.
prettyL := *pretty
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
var ipText string
// Prepare the response and queue sending the result.
var cached []byte
var returnError string
var result interface{} = nil
defer func() {
var j []byte
var err error
if cached != nil {
j = cached
} else {
city, ok := result.(*geoip2.City)
if ok {
res := ResponseCity{Data: city, Error: returnError}
if prettyL {
j, err = json.MarshalIndent(res, "", " ")
} else {
j, err = res.MarshalJSON()
}
} else {
country, _ := result.(*geoip2.Country)
res := ResponseCountry{Data: country, Error: returnError}
if prettyL {
j, err = json.MarshalIndent(res, "", " ")
} else {
j, err = res.MarshalJSON()
}
}
if err != nil {
log.Fatal(err)
}
}
if memCache != nil && cached == nil {
memCache.Set(ipText, j, 0)
}
w.Write(j)
}()
// Set headers
if *originPolicy != "" {
w.Header().Set("Access-Control-Allow-Origin", *originPolicy)
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Last-Modified", serverStart)
ipText = req.URL.Query().Get("ip")
if ipText == "" {
ipText = strings.Trim(req.URL.Path, "/")
}
ip := net.ParseIP(ipText)
if ip == nil {
returnError = "unable to decode ip"
w.WriteHeader(http.StatusBadRequest)
return
}
if memCache != nil {
v, found := memCache.Get(ipText)
if found {
cached = v.([]byte)
return
}
}
if lookupCity {
result, err = db.City(ip)
if err != nil {
returnError = err.Error()
return
}
} else {
result, err = db.Country(ip)
if err != nil {
returnError = err.Error()
return
}
}
})
log.Println("Listening on " + *listen)
log.Fatal(http.ListenAndServe(*listen, nil))
}