-
Notifications
You must be signed in to change notification settings - Fork 2
/
update.go
104 lines (82 loc) · 1.95 KB
/
update.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
package main
import (
"archive/tar"
"compress/gzip"
"io"
"net/http"
"os"
"path/filepath"
)
func updateDB() {
serverLog("Updating database...\n")
// Build the maxmind url for downloading
dbURL := "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=" + Settings.License + "&suffix=tar.gz"
// Download the new database
err := downloadFile("maxmind/GeoLite2-City.tar.gz", dbURL)
if err != nil {
serverLog("error updating database [5]\n")
run = false
return
}
// Extracting the downloaded file
serverLog("Extracting database...\n")
file, err := os.Open("maxmind/GeoLite2-City.tar.gz")
gzr, err := gzip.NewReader(file)
if err != nil {
serverLog("error updating database [4]\n")
run = false
return
}
defer gzr.Close()
tr := tar.NewReader(gzr)
waiting := true
for waiting {
header, err := tr.Next()
switch {
case err == io.EOF:
case err != nil:
serverLog("error updating database [3]\n")
run = false
return
case header == nil:
continue
}
fileName := filepath.Base(header.Name)
if header.Typeflag == tar.TypeReg && fileName == "GeoLite2-City.mmdb" {
serverLog("Found database in archive file\n")
f, err := os.OpenFile(filepath.Join("maxmind/", fileName), os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
if err != nil {
serverLog("error updating database [1]\n")
run = false
return
}
// copy over contents
if _, err := io.Copy(f, tr); err != nil {
serverLog("error updating database [2]\n")
run = false
return
}
f.Close()
waiting = false
}
}
os.Remove("maxmind/GeoLite2-City.tar.gz")
serverLog("Archive File Deleted\n")
}
func downloadFile(filepath string, url string) error {
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}