forked from fiorix/freegeoip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
freegeoip.go
266 lines (255 loc) · 7.23 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
// Copyright 2013 Alexandre Fiori
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package main
import (
"bytes"
"database/sql"
"encoding/binary"
"encoding/json"
"encoding/xml"
"fmt"
"log"
"net"
"net/http"
"strconv"
"strings"
"time"
"github.com/fiorix/go-redis/redis"
"github.com/fiorix/go-web/httpxtra"
_ "github.com/mattn/go-sqlite3"
)
const (
// API limits
maxrequests = 10000
expire = 3600
// Server settings
debug = true
addr = ":8080" // ip:port or /path/to/unix.sock
redisaddr = "127.0.0.1:6379" // redis server
staticpath = "./static" // public website
ipdb = "./db/ipdb.sqlite" // geoip database
)
func main() {
http.Handle("/", http.FileServer(http.Dir(staticpath)))
h := GeoipHandler()
http.HandleFunc("/csv/", h)
http.HandleFunc("/xml/", h)
http.HandleFunc("/json/", h)
server := http.Server{
Addr: addr,
Handler: httpxtra.Handler{Logger: logger},
ReadTimeout: 15 * time.Second,
WriteTimeout: 15 * time.Second,
}
log.Println("FreeGeoIP server starting")
if e := httpxtra.ListenAndServe(server); e != nil {
log.Println(e.Error())
}
}
func logger(r *http.Request, created time.Time, status, bytes int) {
//fmt.Println(httpxtra.ApacheCommonLog(r, created, status, bytes))
log.Printf("HTTP %d %s %s (%s) :: %s",
status,
r.Method,
r.URL.Path,
r.RemoteAddr,
time.Since(created))
}
// GeoipHandler handles GET on /csv, /xml and /json.
func GeoipHandler() http.HandlerFunc {
db, err := sql.Open("sqlite3", ipdb)
if err != nil {
panic(err)
}
rc := redis.New(redisaddr)
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")
return
default:
w.Header().Set("Allow", "GET, OPTIONS")
http.Error(w, http.StatusText(405), 405)
return
}
// GET
// Check quota
var ipkey string
if ip, _, err := net.SplitHostPort(r.RemoteAddr); err != nil {
ipkey = r.RemoteAddr // support for XHeaders
} else {
ipkey = ip
}
if qcs, err := rc.Get(ipkey); err != nil {
http.Error(w, http.StatusText(503), 503) // redis down
return
} else if qcs == "" {
if err := rc.Set(ipkey, "1"); err == nil {
rc.Expire(ipkey, expire)
}
} else if qc, _ := strconv.Atoi(qcs); qc < maxrequests {
rc.Incr(ipkey)
} else {
// Out of quota, soz :(
http.Error(w, http.StatusText(403), 403)
return
}
// Parse URL and build the query.
var ip string
a := strings.SplitN(r.URL.Path, "/", 3)
if len(a) == 3 && a[2] != "" {
// e.g. /csv/google.com
addrs, err := net.LookupHost(a[2])
if err != nil {
http.Error(w, http.StatusText(404), 404)
return
}
ip = addrs[0]
} else {
ip = ipkey
}
geoip, err := GeoipLookup(db, ip)
if err != nil {
http.NotFound(w, r)
return
}
switch a[1][0] {
case 'c':
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)
case 'j':
resp, err := json.Marshal(geoip)
if err != nil {
if debug {
log.Println("JSON error:", err.Error())
}
http.NotFound(w, r)
return
}
callback := r.FormValue("callback")
if callback != "" {
w.Header().Set("Content-Type", "text/javascript")
fmt.Fprintf(w, "%s(%s);", callback, resp)
} else {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "%s", resp)
}
case 'x':
w.Header().Set("Content-Type", "application/xml")
resp, err := xml.MarshalIndent(geoip, "", " ")
if err != nil {
if debug {
log.Println("XML error:", err.Error())
}
http.Error(w, http.StatusText(500), 500)
return
}
fmt.Fprintf(w, xml.Header+"%s\n", resp)
}
}
}
func GeoipLookup(db *sql.DB, ip string) (*GeoIP, error) {
IP := net.ParseIP(ip)
reserved := false
for _, net := range reservedIPs {
if net.Contains(IP) {
reserved = true
break
}
}
geoip := GeoIP{Ip: ip}
if reserved {
geoip.CountryCode = "RD"
geoip.CountryName = "Reserved"
} else {
q := `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`
stmt, err := db.Prepare(q)
if err != nil {
if debug {
log.Println("[debug] SQLite", err.Error())
}
return nil, err
}
defer stmt.Close()
var uintIP uint32
b := bytes.NewBuffer(IP.To4())
binary.Read(b, binary.BigEndian, &uintIP)
err = stmt.QueryRow(uintIP).Scan(
&geoip.CountryCode,
&geoip.CountryName,
&geoip.RegionCode,
&geoip.RegionName,
&geoip.CityName,
&geoip.ZipCode,
&geoip.Latitude,
&geoip.Longitude,
&geoip.MetroCode,
&geoip.AreaCode)
if 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)},
}