-
-
Notifications
You must be signed in to change notification settings - Fork 185
/
locations.go
206 lines (181 loc) · 5.38 KB
/
locations.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
// Copyright © Martin Tournoij – This file is part of GoatCounter and published
// under the terms of a slightly modified EUPL v1.2 license, which can be found
// in the LICENSE file or at https://license.goatcounter.com
package goatcounter
import (
"bytes"
"compress/gzip"
"context"
"io"
"net"
"strings"
"github.com/oschwald/geoip2-golang"
"zgo.at/errors"
"zgo.at/zdb"
"zgo.at/zlog"
)
var geodb *geoip2.Reader
// InitGeoDB sets up the geoDB database located at the given path.
//
// The database can be the "Countries" or "Cities" version.
//
// It will use the embeded "Countries" database if path is an empty string.
func InitGeoDB(path string) {
var err error
if path != "" {
geodb, err = geoip2.Open(path)
if err != nil {
panic(err)
}
GeoDB = nil // Save some memory.
return
}
gz, err := gzip.NewReader(bytes.NewReader(GeoDB))
if err != nil {
panic(err)
}
d, err := io.ReadAll(gz)
if err != nil {
panic(err)
}
geodb, err = geoip2.FromBytes(d)
if err != nil {
panic(err)
}
}
type Location struct {
ID int64 `db:"location_id"`
Country string `db:"country"`
Region string `db:"region"`
CountryName string `db:"country_name"`
RegionName string `db:"region_name"`
// TODO: send patch to staticcheck to deal with this better. This shouldn't
// errror since "ISO" is an initialism.
ISO3166_2 string `db:"iso_3166_2"` //lint:ignore ST1003 staticcheck bug
}
// ByCode gets a location by ISO-3166-2 code; e.g. "US" or "US-TX".
func (l *Location) ByCode(ctx context.Context, code string) error {
if ll, ok := cacheLoc(ctx).Get(code); ok {
*l = *ll.(*Location)
return nil
}
err := zdb.Get(ctx, l, `select * from locations where iso_3166_2 = $1`, code)
if zdb.ErrNoRows(err) {
l.ISO3166_2 = code
l.Country, l.Region, _ = strings.Cut(code, "-")
l.CountryName, l.RegionName = findGeoName(l.Country, l.Region)
err = l.insert(ctx)
}
if err != nil {
return errors.Wrap(err, "Location.ByCode")
}
cacheLoc(ctx).SetDefault(l.ISO3166_2, l)
return nil
}
// Lookup a location by IPv4 or IPv6 address.
//
// This will insert a row in the locations table if one doesn't exist yet.
func (l *Location) Lookup(ctx context.Context, ip string) error {
if geodb == nil {
panic("Location.Lookup: geo.Init not called")
}
loc, err := geodb.City(net.ParseIP(ip))
if err != nil {
return errors.Wrap(err, "Location.Lookup")
}
l.Country = loc.Country.IsoCode
l.CountryName = loc.Country.Names["en"]
if len(loc.Subdivisions) > 0 {
l.Region, l.RegionName = loc.Subdivisions[0].IsoCode, loc.Subdivisions[0].Names["en"]
}
l.ISO3166_2 = loc.Country.IsoCode
if l.Region != "" {
l.ISO3166_2 += "-" + l.Region
}
if ll, ok := cacheLoc(ctx).Get(l.ISO3166_2); ok {
*l = *ll.(*Location)
return nil
}
err = zdb.Get(ctx, l,
`select * from locations where country = $1 and region = $2`,
l.Country, l.Region)
if zdb.ErrNoRows(err) {
err = l.insert(ctx)
}
if err != nil {
return errors.Wrap(err, "Location.Lookup")
}
cacheLoc(ctx).SetDefault(l.ISO3166_2, l)
return nil
}
// LookupIP is a shorthand for Lookup(); returns id 1 on errors ("unknown").
func (l Location) LookupIP(ctx context.Context, ip string) string {
err := l.Lookup(ctx, ip)
if err != nil {
return "" // Special ID: "unknown".
}
return l.ISO3166_2
}
func (l *Location) insert(ctx context.Context) (err error) {
l.ID, err = zdb.InsertID(ctx, "location_id",
`insert into locations (country, region, country_name, region_name) values (?, ?, ?, ?)`,
l.Country, l.Region, l.CountryName, l.RegionName)
if err != nil {
return err
}
// Make sure there is an entry for the country as well.
if l.Region != "" {
err := (&Location{}).ByCode(ctx, l.Country)
if err != nil {
return err
}
}
return nil
}
type Locations []Location
// ListCountries lists all counties. The region code/name will always be blank.
func (l *Locations) ListCountries(ctx context.Context) error {
err := zdb.Select(ctx, l, `
select country, country_name
from locations
where country != '' and country_name != '' and region = ''
order by country_name`)
return errors.Wrap(err, "Locations.ListCountries")
}
// This takes ~13s for a full iteration for the Cities database on my laptop
// (Countries is much faster, ~100ms) which is not a great worst case scenario,
// but in most cases it should be (much) faster, and this should get called
// extremely infrequently anyway, if ever.
func findGeoName(country, region string) (string, string) {
hasRegions := geodb.Metadata().DatabaseType == "City"
iter := geodb.DB().Data()
for iter.Next() {
var r struct {
Country struct {
ISOCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
} `maxminddb:"country"`
Subdivisions []struct {
ISOCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
} `maxminddb:"subdivisions"`
}
err := iter.Data(&r)
if err != nil {
zlog.Error(err)
return "", ""
}
switch {
// Country database, no region.
case r.Country.ISOCode == country && !hasRegions:
return r.Country.Names["en"], ""
// City database, no region requested.
case r.Country.ISOCode == country && region == "":
return r.Country.Names["en"], ""
// Match region.
case r.Country.ISOCode == country && len(r.Subdivisions) > 0 && r.Subdivisions[0].ISOCode == region:
return r.Country.Names["en"], r.Subdivisions[0].Names["en"]
}
}
return "", ""
}