-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace submodule for generated test db (#63)
Finally addresses #35.
- Loading branch information
Showing
12 changed files
with
139 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +0,0 @@ | ||
[submodule "maxmind-db"] | ||
path = maxmind-db | ||
url = https://github.com/maxmind/MaxMind-DB.git | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule maxmind-db
deleted from
c1c744
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/csv" | ||
"encoding/hex" | ||
"io" | ||
"log" | ||
"net" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/maxmind/mmdbwriter" | ||
"github.com/maxmind/mmdbwriter/inserter" | ||
"github.com/maxmind/mmdbwriter/mmdbtype" | ||
) | ||
|
||
func makeRecord(key, mmdbType, value string) mmdbtype.DataType { | ||
var mmdbValue mmdbtype.DataType | ||
switch mmdbType { | ||
case "bool": | ||
v, _ := strconv.ParseBool(value) | ||
mmdbValue = mmdbtype.Bool(v) | ||
case "bytes": | ||
v, _ := hex.DecodeString(value) | ||
mmdbValue = mmdbtype.Bytes(v) | ||
case "double": | ||
v, _ := strconv.ParseFloat(value, 64) | ||
mmdbValue = mmdbtype.Float64(v) | ||
case "float": | ||
v, _ := strconv.ParseFloat(value, 32) | ||
mmdbValue = mmdbtype.Float32(v) | ||
case "int32": | ||
v, _ := strconv.ParseInt(value, 10, 32) | ||
mmdbValue = mmdbtype.Int32(v) | ||
case "uint16": | ||
v, _ := strconv.ParseInt(value, 10, 16) | ||
mmdbValue = mmdbtype.Uint16(v) | ||
case "uint32": | ||
v, _ := strconv.ParseInt(value, 10, 32) | ||
mmdbValue = mmdbtype.Uint32(v) | ||
case "uint64": | ||
v, _ := strconv.ParseInt(value, 10, 64) | ||
mmdbValue = mmdbtype.Uint64(v) | ||
case "string": | ||
mmdbValue = mmdbtype.String(value) | ||
case "map": | ||
keys := strings.Split(key, "/") | ||
key = keys[0] | ||
mmdbValue = mmdbtype.Map{ | ||
mmdbtype.String(keys[1]): mmdbtype.Map{ | ||
mmdbtype.String(keys[2]): mmdbtype.String(value), | ||
}, | ||
} | ||
} | ||
record := mmdbtype.Map{} | ||
record[mmdbtype.String(key)] = mmdbValue | ||
|
||
return record | ||
} | ||
|
||
func main() { | ||
writer, err := mmdbwriter.New( | ||
mmdbwriter.Options{ | ||
DatabaseType: "libvmod-geoip2", | ||
Description: map[string]string{"en": "libvmod-geoip2 test database"}, | ||
IncludeReservedNetworks: true, | ||
Inserter: inserter.TopLevelMergeWith, | ||
}, | ||
) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
file, err := os.Open("test-data.csv") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
reader := csv.NewReader(file) | ||
|
||
for { | ||
row, err := reader.Read() | ||
if err == io.EOF { | ||
break | ||
} | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
_, network, err := net.ParseCIDR(row[0]) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
record := makeRecord(row[1], row[2], row[3]) | ||
err = writer.Insert(network, record) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
file, err = os.Create("libvmod-geoip2.mmdb") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
_, err = writer.WriteTo(file) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
"1.0.128.1/32","connection_type","string","Cable/DSL" | ||
"1.0.128.1/32","isp","string","TOT Public Company Limited" | ||
"1.1.1.1/32","boolean","bool","true" | ||
"1.1.1.1/32","bytes","bytes","0000002a" | ||
"1.1.1.1/32","double","double","42.123456" | ||
"1.1.1.1/32","float","float","1.100000" | ||
"1.1.1.1/32","int32","int32","-268435456" | ||
"1.1.1.1/32","map/mapX/utf8_stringX","map","hello" | ||
"1.1.1.1/32","uint16","uint16","100" | ||
"1.1.1.1/32","uint32","uint32","268435456" | ||
"1.1.1.1/32","uint64","uint64","1152921504606846976" | ||
"1.1.1.1/32","utf8_string","string","unicode! ☯ - ♫" | ||
"127.0.0.1/32","long_string","string","thequickbrownfoxjumpsoverthelazydog1234567890" | ||
"2001:218::1/32","country/names/en","map","Japan" | ||
"81.2.69.192/32","city/names/en","map","London" |
This file was deleted.
Oops, something went wrong.