forked from axllent/goiplookup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.go
169 lines (135 loc) · 3.44 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
package main
import (
"archive/tar"
"compress/bzip2"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
)
// Update GeoLite2-Country.mmdb
func UpdateGeoLite2Country() {
Verbose("Updating GeoLite2-Country.mmdb")
tmp_dir := os.TempDir()
gzfile := filepath.Join(tmp_dir, "GeoLite2-Country.tar.gz")
// check the output directory is writeable
if _, err := os.Stat(*data_dir); os.IsNotExist(err) {
os.MkdirAll(*data_dir, os.ModePerm)
}
if _, err := os.Stat(*data_dir); err != nil {
fmt.Println("Error: Cannot create", *data_dir)
os.Exit(1)
}
if err := DownloadToFile(gzfile, db_update_url); err != nil {
fmt.Println(err)
os.Exit(1)
}
if err := ExtractDatabaseFile(*data_dir, gzfile); err != nil {
fmt.Println(err)
os.Exit(1)
}
if err := os.Remove(gzfile); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// Extract 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")
f, err := os.OpenFile(outfile, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
if err != nil {
return err
}
Verbose(fmt.Sprintf("Copy GeoLite2-Country.mmdb to %s", outfile))
if _, err := io.Copy(f, tr); err != nil {
return err
}
f.Close()
}
}
}
}
// Built-in updater
func SelfUpdate() {
tmp_dir := os.TempDir()
bz2file := filepath.Join(tmp_dir, "goiplookup.bz2")
newexec := filepath.Join(tmp_dir, "goiplookup.tmp")
download_url, err := GetUpdateURL()
if err != nil {
fmt.Println(fmt.Sprintf("Error: %s", err))
os.Exit(1)
}
if err := DownloadToFile(bz2file, download_url); err != nil {
fmt.Println(err)
os.Exit(1)
}
Verbose(fmt.Sprintf("Opening %s", bz2file))
f, err := os.OpenFile(bz2file, 0, 0)
if err != nil {
fmt.Println(fmt.Sprintf("Error: %s", err))
os.Exit(1)
}
defer f.Close()
// create a bzip2 reader
br := bzip2.NewReader(f)
// write the file
out, err := os.OpenFile(newexec, os.O_CREATE|os.O_RDWR, 0755)
if err != nil {
fmt.Println(fmt.Sprintf("Error: %s", err))
os.Exit(1)
}
Verbose(fmt.Sprintf("Extracting %s", newexec))
_, err = io.Copy(out, br)
if err != nil {
fmt.Println(fmt.Sprintf("Error: %s", err))
os.Exit(1)
}
// replace os.Args[0] with new file
// cannot overwrite open file so rename then delete
// get executable's absolute path
oldexec, _ := os.Readlink("/proc/self/exe")
err = ReplaceFile(oldexec, newexec)
if err != nil {
fmt.Println(fmt.Sprintf("Error: %s", err))
os.Exit(1)
}
// remove the src file
Verbose(fmt.Sprintf("Deleting %s", bz2file))
if err := os.Remove(bz2file); err != nil {
fmt.Println(fmt.Sprintf("Error: %s", err))
os.Exit(1)
}
}