-
Notifications
You must be signed in to change notification settings - Fork 6
/
updater.go
196 lines (156 loc) · 4.13 KB
/
updater.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
package main
import (
"archive/tar"
"compress/gzip"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"path"
"path/filepath"
"regexp"
"time"
"github.com/oschwald/geoip2-golang"
)
// UpdateGeoLite2Country updates GeoLite2-Country.mmdb
func UpdateGeoLite2Country() {
key := os.Getenv("LICENSEKEY")
if key == "" && licenseKey != "" {
key = licenseKey
}
if key == "" {
fmt.Println("Error: GeoIP License Key not set.\nPlease see https://github.com/axllent/goiplookup#database-updates")
os.Exit(1)
}
dbUpdateURL := fmt.Sprintf("https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key=%s&suffix=tar.gz", key)
updateRequired, err := requiresDBUpdate(dbUpdateURL)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if !updateRequired {
Verbose("No database update available")
os.Exit(0)
}
Verbose("Updating GeoLite2-Country.mmdb")
tmpDir := os.TempDir()
gzFile := filepath.Join(tmpDir, "GeoLite2-Country.tar.gz")
// check the output directory is writeable
if _, err := os.Stat(dataDir); os.IsNotExist(err) {
os.MkdirAll(dataDir, os.ModePerm)
}
if _, err := os.Stat(dataDir); err != nil {
fmt.Println("Error: Cannot create", dataDir)
os.Exit(1)
}
if err := DownloadToFile(gzFile, dbUpdateURL); err != nil {
fmt.Println(err)
os.Exit(1)
}
if err := ExtractDatabaseFile(dataDir, gzFile); err != nil {
fmt.Println(err)
os.Exit(1)
}
if err := os.Remove(gzFile); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// get last-modified header to see if it is an update
func requiresDBUpdate(updateURL string) (bool, error) {
dstFile := path.Join(dataDir, "GeoLite2-Country.mmdb")
if !isFile(dstFile) {
// missing local file, update
return true, nil
}
info, err := os.Stat(dstFile)
if err != nil {
return false, err
}
lastModifiedLocal := info.ModTime()
res, err := http.Head(updateURL)
if err != nil {
return false, err
}
lmHdr := res.Header.Get("last-modified")
if lmHdr == "" {
return false, errors.New("update server returned unexpected response")
}
lastModifiedRemote, err := time.Parse(time.RFC1123, lmHdr)
if err != nil {
return false, err
}
return lastModifiedRemote.After(lastModifiedLocal), nil
}
func getLastModifiedFromHeader(h string) time.Time {
var t time.Time
if h == "" {
return t
}
t, _ = time.Parse(time.RFC1123, h)
return t
}
// ExtractDatabaseFile extracts just the GeoLite2-Country.mmdb from the tar.gz
func ExtractDatabaseFile(dst string, tarGz string) error {
Verbose(fmt.Sprintf("Opening %s", tarGz))
re, _ := regexp.Compile(`GeoLite2\-Country\.mmdb$`)
r, err := os.Open(tarGz)
if err != nil {
return err
}
gzr, err := gzip.NewReader(r)
if err != nil {
return err
}
defer gzr.Close()
tr := tar.NewReader(gzr)
for {
header, err := tr.Next()
switch {
// if no more files are found return
case err == io.EOF:
return nil
// return any other error
case err != nil:
return err
// if the header is nil, just skip it (not sure how this happens)
case header == nil:
continue
}
// the target location where the dir/file should be created
target := filepath.Join(dst, header.Name)
// check the file type
switch header.Typeflag {
case tar.TypeReg:
if re.Match([]byte(target)) {
outFile := filepath.Join(dst, "GeoLite2-Country.mmdb")
// tmpFile is used to first ensure the extracted database is valid before replacing the previous one
tmpFile, err := os.CreateTemp("", "testDBFile")
if err != nil {
log.Fatal(err)
}
defer os.Remove(tmpFile.Name()) // clean up
Verbose(fmt.Sprintf("Copy GeoLite2-Country.mmdb to %s for testing", tmpFile.Name()))
if _, err := io.Copy(tmpFile, tr); err != nil {
return err
}
db, err := geoip2.Open(tmpFile.Name())
if err != nil {
return fmt.Errorf("Downloaded GeoLite2-Country.mmdb database (%s) corrupt, aborting updating", tmpFile.Name())
}
db.Close()
Verbose(fmt.Sprintf("Copy %s to %s", tmpFile.Name(), outFile))
input, err := os.ReadFile(tmpFile.Name())
if err != nil {
return err
}
err = os.WriteFile(outFile, input, 0644)
if err != nil {
return err
}
}
}
}
}