-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql.go
302 lines (242 loc) · 7.43 KB
/
mysql.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package main
import (
"database/sql"
"errors"
"fmt"
"net"
"os"
"strings"
mysql "github.com/go-sql-driver/mysql"
)
var mysqlDb *sql.DB
func mysqlConnect() {
config := mysql.Config{
User: os.Getenv("DB_USER"),
Passwd: os.Getenv("DB_PASS"),
Addr: os.Getenv("DB_HOST") + ":" + os.Getenv("DB_PORT"),
DBName: os.Getenv("DB_NAME"),
Net: "tcp",
AllowNativePasswords: true,
}
conn, err := sql.Open("mysql", config.FormatDSN())
if err != nil {
panic(err)
}
mysqlDb = conn
}
func mysqlClose() {
err := mysqlDb.Close()
if err != nil {
panic(err)
}
}
func mysqlInitialised(key string) bool {
var table string
switch key {
case "COUNTRY": table = "ip_country"
case "ASN": table = "ip_asn"
case "CITY": table = "ip_city"
}
var total int
sqlString := fmt.Sprintf("SELECT COUNT(*) AS `total` FROM `%s` ", table)
row := mysqlDb.QueryRow(sqlString)
if err := row.Scan(&total); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return false
}
panic(err)
}
return total > 0
}
func mysqlIp(ip net.IP) *Ip {
ipString := ip.String();
ipVersion := getIpVersion(ipString)
function := mysqlGetConversionFunction(ipVersion)
ipStruct := NewIp(ipString, ipVersion)
if hasCityDatabase() {
// Could also use: `SET SESSION sql_mode = 'ANSI_QUOTES';` but, meh
sqlString := fmt.Sprintf(`
SELECT `+"`"+`country_code`+"`"+`,
`+"`"+`state1`+"`"+`,
`+"`"+`state2`+"`"+`,
`+"`"+`city`+"`"+`,
`+"`"+`postcode`+"`"+`,
`+"`"+`latitude`+"`"+`,
`+"`"+`longitude`+"`"+`,
`+"`"+`timezone`+"`"+`
FROM `+"`"+`ip_city`+"`"+`
WHERE `+"`"+`ip_number_start`+"`"+` <= %s(?)
ORDER BY `+"`"+`ip_number_start`+"`"+` DESC
LIMIT 1`,
function)
row := mysqlDb.QueryRow(sqlString, ipString)
if err := row.Scan(&ipStruct.CountryCode, &ipStruct.State1, &ipStruct.State2, &ipStruct.City, &ipStruct.Postcode, &ipStruct.Latitude, &ipStruct.Longitude, &ipStruct.Timezone); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return ipStruct
}
panic(err)
}
if len(ipStruct.City) > 0 {
ipStruct.FoundCity = true
}
}
if len(ipStruct.CountryCode) > 0 {
ipStruct.FoundCountry = true
} else {
if hasCountryDatabase() {
// Could also use: `SET SESSION sql_mode = 'ANSI_QUOTES';` but, meh
sqlString := fmt.Sprintf(`
SELECT `+"`"+`country_code`+"`"+`
FROM `+"`"+`ip_country`+"`"+`
WHERE `+"`"+`ip_number_start`+"`"+` <= %s(?)
ORDER BY `+"`"+`ip_number_start`+"`"+` DESC
LIMIT 1`,
function)
row := mysqlDb.QueryRow(sqlString, ipString)
if err := row.Scan(&ipStruct.CountryCode); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return ipStruct
}
panic(err)
}
if len(ipStruct.CountryCode) > 0 {
ipStruct.FoundCountry = true
}
}
}
if hasASNDatabase() {
// Could also use: `SET SESSION sql_mode = 'ANSI_QUOTES';` but, meh
sqlString := fmt.Sprintf(`
SELECT `+"`"+`as_number`+"`"+`,
`+"`"+`as_organisation`+"`"+`
FROM `+"`"+`ip_asn`+"`"+`
WHERE `+"`"+`ip_number_start`+"`"+` <= %s(?)
ORDER BY `+"`"+`ip_number_start`+"`"+` DESC
LIMIT 1`,
function)
row := mysqlDb.QueryRow(sqlString, ipString)
if err := row.Scan(&ipStruct.OrganisationNumber, &ipStruct.OrganisationName); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return ipStruct
}
panic(err)
}
if len(ipStruct.OrganisationName) > 0 {
ipStruct.FoundASN = true
}
}
return ipStruct
}
func mysqlQueryMaxVersion(table string, ipVersion int) int {
var version int
sqlString := fmt.Sprintf("SELECT `db_version` FROM `%s` WHERE `ip_version` = ? ORDER BY `db_version` DESC LIMIT 1", table)
row := mysqlDb.QueryRow(sqlString, ipVersion)
if err := row.Scan(&version); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return 0
}
panic(err)
}
return version
}
func mysqlDropOld(table string, ipVersion int, dbVersion int) {
fmt.Printf("Dropping old MySQL data: `%s` ipv%d... ", table, ipVersion)
sqlString := fmt.Sprintf("DELETE FROM `%s` WHERE `ip_version` = ? AND `db_version` < ?", table)
_, err := mysqlDb.Exec(sqlString, ipVersion, dbVersion)
if err != nil {
panic(err)
}
fmt.Println("Complete")
}
func mysqlSaveCountries(countries []IpCountry) {
var params []any
function := mysqlGetConversionFunction(countries[0].IpVersion)
sqlString := "INSERT INTO `ip_country` (`ip_range_start`, `ip_range_end`, `ip_number_start`, `ip_number_end`, `country_code`, `ip_version`, `db_version`) VALUES "
for _, country := range countries {
sqlString += `(?, ?, ` + function + `(?), ` + function + `(?), ?, ?, ?), `
params = append(params, country.IpRangeStart, country.IpRangeEnd, country.IpRangeStart, country.IpRangeEnd, country.CountryCode, country.IpVersion, country.DbVersion)
}
sqlString = sqlString[0:len(sqlString) - 2]
stmt, err := mysqlDb.Prepare(sqlString)
if err != nil {
panic(err)
}
defer stmt.Close()
_, err = stmt.Exec(params...)
if err != nil {
panic(err)
}
}
func mysqlSaveASNs(ASNs []IpASN) {
var params []any
function := mysqlGetConversionFunction(ASNs[0].IpVersion)
sqlString := "INSERT INTO `ip_asn` (`ip_range_start`, `ip_range_end`, `ip_number_start`, `ip_number_end`, `as_number`, `as_organisation`, `ip_version`, `db_version`) VALUES "
for _, asn := range ASNs {
sqlString += `(?, ?, ` + function + `(?), ` + function + `(?), ?, ?, ?, ?), `
params = append(params, asn.IpRangeStart, asn.IpRangeEnd, asn.IpRangeStart, asn.IpRangeEnd, asn.AsNumber, asn.AsOrganisation, asn.IpVersion, asn.DbVersion)
}
sqlString = sqlString[0:len(sqlString) - 2]
stmt, err := mysqlDb.Prepare(sqlString)
if err != nil {
panic(err)
}
defer stmt.Close()
_, err = stmt.Exec(params...)
if err != nil {
panic(err)
}
}
func mysqlSaveCities(cities []IpCity) {
var params []any
function := mysqlGetConversionFunction(cities[0].IpVersion)
sqlString := "INSERT INTO `ip_city` (`ip_range_start`, `ip_range_end`, `ip_number_start`, `ip_number_end`, `country_code`, `state1`, `state2`, `city`, `postcode`, `latitude`, `longitude`, `timezone`, `ip_version`, `db_version`) VALUES "
for _, city := range cities {
sqlString += `(?, ?, ` + function + `(?), ` + function + `(?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?), `
params = append(params, city.IpRangeStart, city.IpRangeEnd, city.IpRangeStart, city.IpRangeEnd, city.CountryCode, city.State1, city.State2, city.City, city.Postcode, city.Latitude, city.Longitude, city.Timezone, city.IpVersion, city.DbVersion)
}
sqlString = sqlString[0:len(sqlString) - 2]
stmt, err := mysqlDb.Prepare(sqlString)
if err != nil {
panic(err)
}
defer stmt.Close()
_, err = stmt.Exec(params...)
if err != nil {
panic(err)
}
}
func mysqlFile(sqlPath string) {
sqlBytes, err := dbStructures.ReadFile(sqlPath)
//sqlBytes, err := os.ReadFile(sqlPath)
if err != nil {
panic(err)
}
sqlString := string(sqlBytes)
sqlStatements := strings.Split(sqlString, ";")
transaction, err := mysqlDb.Begin()
if err != nil {
panic(err)
}
defer transaction.Rollback()
for _, sqlStatement := range sqlStatements {
sqlStatement = strings.Trim(sqlStatement, " ")
if len(sqlStatement) > 0 {
_, err := transaction.Exec(sqlStatement)
if err != nil {
panic(err)
}
}
}
err = transaction.Commit()
if err != nil {
panic(err)
}
}
// Probably don't actually need to do this
func mysqlGetConversionFunction(ipVersion int) string {
function := "INET_ATON"
if ipVersion == 6 {
function = "INET6_ATON"
}
return function
}