forked from fiorix/freegeoip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreegeoip.go
487 lines (466 loc) · 12.1 KB
/
freegeoip.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
// Copyright 2013 Alexandre Fiori
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Web server of freegeoip.net
package main
import (
"bytes"
"database/sql"
"encoding/binary"
"encoding/json"
"encoding/xml"
"expvar"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"runtime"
"strconv"
"strings"
"sync"
"time"
_ "code.google.com/p/gosqlite/sqlite3"
"github.com/fiorix/go-redis/redis"
"github.com/fiorix/go-web/httpxtra"
)
type Settings struct {
Debug bool `xml:"debug,attr"`
DebugSrv string `xml:"debugsrv,attr"`
XMLName xml.Name `xml:"Server"`
Listen []*struct {
Log bool `xml:"log,attr"`
XHeaders bool `xml:"xheaders,attr"`
Addr string `xml:"addr,attr"`
CertFile string
KeyFile string
}
DocumentRoot string
IPDB struct {
File string `xml:",attr"`
CacheSize string `xml:",attr"`
}
Limit struct {
MaxRequests int `xml:",attr"`
Expire int `xml:",attr"`
}
Redis []string `xml:"Redis>Addr"`
}
var (
conf *Settings
protoCount = expvar.NewMap("Protocol") // HTTP or HTTPS
outputCount = expvar.NewMap("Output") // json, xml, csv or other
statusCount = expvar.NewMap("Status") // 200, 403, 404, etc
)
func main() {
cf := flag.String("config", "freegeoip.conf", "set config file")
flag.Parse()
if buf, err := ioutil.ReadFile(*cf); err != nil {
log.Fatal(err)
} else {
conf = &Settings{}
if err := xml.Unmarshal(buf, conf); err != nil {
log.Fatal(err)
}
}
runtime.GOMAXPROCS(runtime.NumCPU())
log.Printf("FreeGeoIP server starting. debug=%t", conf.Debug)
if conf.Debug && len(conf.DebugSrv) > 0 {
go func() {
// server for expvar's /debug/vars only
log.Printf("DEBUG server on %s", conf.DebugSrv)
log.Fatal(http.ListenAndServe(conf.DebugSrv, nil))
}()
}
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.Dir(conf.DocumentRoot)))
h := LookupHandler()
mux.HandleFunc("/csv/", h)
mux.HandleFunc("/xml/", h)
mux.HandleFunc("/json/", h)
wg := new(sync.WaitGroup)
for _, l := range conf.Listen {
if l.Addr == "" {
continue
}
wg.Add(1)
h := httpxtra.Handler{Handler: mux, XHeaders: l.XHeaders}
if l.Log {
h.Logger = logger
}
s := http.Server{
Addr: l.Addr,
Handler: h,
ReadTimeout: 15 * time.Second,
WriteTimeout: 15 * time.Second,
}
if l.KeyFile == "" {
log.Printf("Listening HTTP on %s "+
"log=%t xheaders=%t",
l.Addr, l.Log, l.XHeaders)
go func() {
log.Fatal(httpxtra.ListenAndServe(s))
}()
} else {
log.Printf("Listening HTTPS on %s "+
"log=%t xheaders=%t cert=%s key=%s",
l.Addr, l.Log, l.XHeaders,
l.CertFile, l.KeyFile)
go func() {
log.Fatal(s.ListenAndServeTLS(
l.CertFile,
l.KeyFile,
))
}()
}
}
wg.Wait()
}
func logger(r *http.Request, created time.Time, status, bytes int) {
//fmt.Println(httpxtra.ApacheCommonLog(r, created, status, bytes))
var (
s, ip string
err error
)
if r.TLS == nil {
s = "HTTP"
} else {
s = "HTTPS"
}
if ip, _, err = net.SplitHostPort(r.RemoteAddr); err != nil {
ip = r.RemoteAddr
}
log.Printf("%s %d %s %q (%s) :: %s",
s,
status,
r.Method,
r.URL.Path,
ip,
time.Since(created),
)
if conf.Debug {
protoCount.Add(s, 1)
statusCount.Add(fmt.Sprintf("%d", status), 1)
switch strings.SplitN(r.URL.Path, "/", 2)[1] {
case "json/":
outputCount.Add("json", 1)
case "xml/":
outputCount.Add("xml", 1)
case "csv/":
outputCount.Add("csv", 1)
default:
outputCount.Add("other", 1)
}
}
}
// LookupHandler handles GET on /csv, /xml and /json.
func LookupHandler() http.HandlerFunc {
db, err := sql.Open("sqlite3", conf.IPDB.File)
if err != nil {
log.Fatal(err)
}
_, err = db.Exec("PRAGMA cache_size=" + conf.IPDB.CacheSize)
if err != nil {
log.Fatal(err)
}
stmt, err := db.Prepare(query)
if err != nil {
log.Fatal(err)
}
//defer stmt.Close()
var quota Quota
if len(conf.Redis) == 0 {
quota = new(MapQuota)
quota.Setup()
log.Printf("Using internal map to manage quota.")
} else {
quota = new(RedisQuota)
quota.Setup(conf.Redis...)
log.Printf("Using redis to manage quota: %s", conf.Redis)
}
return func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
w.Header().Set("Access-Control-Allow-Origin", "*")
case "OPTIONS":
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Access-Control-Allow-Methods", "GET")
w.Header().Set("Access-Control-Allow-Headers", "X-Requested-With")
w.WriteHeader(200)
return
default:
w.Header().Set("Allow", "GET, OPTIONS")
http.Error(w, http.StatusText(405), 405)
return
}
// GET continues...
var srcIP net.IP
if ip, _, err := net.SplitHostPort(r.RemoteAddr); err != nil {
srcIP = net.ParseIP(r.RemoteAddr) // Use X-Real-IP
} else {
srcIP = net.ParseIP(ip)
}
if srcIP == nil {
http.Error(w, http.StatusText(400), 400)
return
}
nsrcIP, err := ip2int(srcIP)
if err != nil {
if conf.Debug {
log.Println(err)
}
http.Error(w, http.StatusText(400), 400)
return
}
// Check quota.
if conf.Limit.MaxRequests > 0 {
var ok bool
if ok, err = quota.Ok(nsrcIP); err != nil {
if conf.Debug {
log.Println(err) // redis error
}
http.Error(w, http.StatusText(503), 503)
return
} else if !ok {
// Over quota, soz :(
http.Error(w, http.StatusText(403), 403)
return
}
}
var (
queryIP net.IP
nqueryIP uint32
)
// Parse URL (e.g. /csv/ip, /xml/)
a := strings.SplitN(r.URL.Path, "/", 3)
if len(a) == 3 && a[2] != "" {
addrs, err := net.LookupHost(a[2])
if err != nil {
// DNS lookup failed, assume host not found.
http.Error(w, http.StatusText(404), 404)
return
}
if queryIP = net.ParseIP(addrs[0]); queryIP == nil {
http.Error(w, http.StatusText(400), 400)
return
}
nqueryIP, err = ip2int(net.ParseIP(addrs[0]))
if err != nil {
if conf.Debug {
log.Println(err)
}
http.Error(w, http.StatusText(400), 400)
return
}
} else {
queryIP = srcIP
nqueryIP = nsrcIP
}
// Query the db.
geoip, err := lookup(stmt, queryIP, nqueryIP)
if err != nil {
http.NotFound(w, r)
return
}
switch a[1][0] {
case 'j': // json
resp, err := json.Marshal(geoip)
if err != nil {
if conf.Debug {
log.Println("JSON error:", err.Error())
}
http.Error(w, http.StatusText(500), 500)
return
}
if cb := r.FormValue("callback"); cb != "" {
w.Header().Set("Content-Type", "text/javascript")
fmt.Fprintf(w, "%s(%s);", cb, resp)
} else {
w.Header().Set("Content-Type", "application/json")
w.Write(resp)
}
case 'x': // xml
w.Header().Set("Content-Type", "application/xml")
resp, err := xml.MarshalIndent(geoip, "", " ")
if err != nil {
if conf.Debug {
log.Println("XML error:", err.Error())
}
http.Error(w, http.StatusText(500), 500)
return
}
fmt.Fprintf(w, xml.Header+"%s\n", resp)
case 'c': // csv
w.Header().Set("Content-Type", "application/csv")
fmt.Fprintf(w, `"%s","%s","%s","%s","%s","%s",`+
`"%s","%0.4f","%0.4f","%s","%s"`+"\r\n",
geoip.Ip,
geoip.CountryCode, geoip.CountryName,
geoip.RegionCode, geoip.RegionName,
geoip.CityName, geoip.ZipCode,
geoip.Latitude, geoip.Longitude,
geoip.MetroCode, geoip.AreaCode)
}
}
}
func ip2int(ip net.IP) (uint32, error) {
var n uint32
ipv4 := ip.To4()
if ipv4 == nil {
return 0, fmt.Errorf("IP %s is not IPv4", ip.String())
}
if err := binary.Read(
bytes.NewBuffer(ipv4),
binary.BigEndian,
&n,
); err != nil {
return 0, fmt.Errorf("IP conversion failed: %s", err.Error())
}
return n, nil
}
func lookup(stmt *sql.Stmt, IP net.IP, nIP uint32) (*GeoIP, error) {
var reserved bool
for _, net := range reservedIPs {
if net.Contains(IP) {
reserved = true
break
}
}
geoip := GeoIP{Ip: IP.String()}
if reserved {
geoip.CountryCode = "RD"
geoip.CountryName = "Reserved"
} else {
if err := stmt.QueryRow(nIP).Scan(
&geoip.CountryCode,
&geoip.CountryName,
&geoip.RegionCode,
&geoip.RegionName,
&geoip.CityName,
&geoip.ZipCode,
&geoip.Latitude,
&geoip.Longitude,
&geoip.MetroCode,
&geoip.AreaCode,
); err != nil {
return nil, err
}
}
return &geoip, nil
}
type GeoIP struct {
XMLName xml.Name `json:"-" xml:"Response"`
Ip string `json:"ip"`
CountryCode string `json:"country_code"`
CountryName string `json:"country_name"`
RegionCode string `json:"region_code"`
RegionName string `json:"region_name"`
CityName string `json:"city" xml:"City"`
ZipCode string `json:"zipcode"`
Latitude float32 `json:"latitude"`
Longitude float32 `json:"longitude"`
MetroCode string `json:"metro_code"`
AreaCode string `json:"areacode"`
}
// http://en.wikipedia.org/wiki/Reserved_IP_addresses
var reservedIPs = []net.IPNet{
{net.IPv4(0, 0, 0, 0), net.IPv4Mask(255, 0, 0, 0)},
{net.IPv4(10, 0, 0, 0), net.IPv4Mask(255, 0, 0, 0)},
{net.IPv4(100, 64, 0, 0), net.IPv4Mask(255, 192, 0, 0)},
{net.IPv4(127, 0, 0, 0), net.IPv4Mask(255, 0, 0, 0)},
{net.IPv4(169, 254, 0, 0), net.IPv4Mask(255, 255, 0, 0)},
{net.IPv4(172, 16, 0, 0), net.IPv4Mask(255, 240, 0, 0)},
{net.IPv4(192, 0, 0, 0), net.IPv4Mask(255, 255, 255, 248)},
{net.IPv4(192, 0, 2, 0), net.IPv4Mask(255, 255, 255, 0)},
{net.IPv4(192, 88, 99, 0), net.IPv4Mask(255, 255, 255, 0)},
{net.IPv4(192, 168, 0, 0), net.IPv4Mask(255, 255, 0, 0)},
{net.IPv4(198, 18, 0, 0), net.IPv4Mask(255, 254, 0, 0)},
{net.IPv4(198, 51, 100, 0), net.IPv4Mask(255, 255, 255, 0)},
{net.IPv4(203, 0, 113, 0), net.IPv4Mask(255, 255, 255, 0)},
{net.IPv4(224, 0, 0, 0), net.IPv4Mask(240, 0, 0, 0)},
{net.IPv4(240, 0, 0, 0), net.IPv4Mask(240, 0, 0, 0)},
{net.IPv4(255, 255, 255, 255), net.IPv4Mask(255, 255, 255, 255)},
}
// SQLite query.
const query = `SELECT
city_location.country_code,
country_blocks.country_name,
city_location.region_code,
region_names.region_name,
city_location.city_name,
city_location.postal_code,
city_location.latitude,
city_location.longitude,
city_location.metro_code,
city_location.area_code
FROM city_blocks
NATURAL JOIN city_location
INNER JOIN country_blocks ON
city_location.country_code = country_blocks.country_code
LEFT OUTER JOIN region_names ON
city_location.country_code = region_names.country_code
AND
city_location.region_code = region_names.region_code
WHERE city_blocks.ip_start <= ?
ORDER BY city_blocks.ip_start DESC LIMIT 1`
// Quota interface for limiting access to the API.
type Quota interface {
Setup(args ...string) // Initialize quota backend
Ok(ipkey uint32) (bool, error) // Returns true if under quota
}
// MapQuota implements the Quota interface using a map as the backend.
type MapQuota struct {
mu sync.Mutex
m map[uint32]int
}
func (q *MapQuota) Setup(args ...string) {
q.m = make(map[uint32]int)
}
func (q *MapQuota) Ok(ipkey uint32) (bool, error) {
q.mu.Lock()
defer q.mu.Unlock()
if n, ok := q.m[ipkey]; ok {
if n < conf.Limit.MaxRequests {
q.m[ipkey]++
return true, nil
}
return false, nil
}
q.m[ipkey] = 1
go func() {
time.Sleep(time.Duration(conf.Limit.Expire) * time.Second)
q.mu.Lock()
defer q.mu.Unlock()
delete(q.m, ipkey)
}()
return true, nil
}
// RedisQuota implements the Quota interface using Redis as the backend.
type RedisQuota struct {
c *redis.Client
}
func (q *RedisQuota) Setup(args ...string) {
q.c = redis.New(args...)
q.c.Timeout = time.Duration(800) * time.Millisecond
}
func (q *RedisQuota) Ok(ipkey uint32) (bool, error) {
k := fmt.Sprintf("%d", ipkey) // "numeric" key
if ns, err := q.c.Get(k); err != nil {
return false, fmt.Errorf("redis get: %s", err.Error())
} else if ns == "" {
if err = q.c.SetEx(k, conf.Limit.Expire, "1"); err != nil {
return false, fmt.Errorf("redis setex: %s", err.Error())
}
} else if n, _ := strconv.Atoi(ns); n < conf.Limit.MaxRequests {
if n, err = q.c.Incr(k); err != nil {
return false, fmt.Errorf("redis incr: %s", err.Error())
} else if n == 1 {
q.c.Expire(k, conf.Limit.Expire)
}
} else {
return false, nil
}
return true, nil
}