-
Notifications
You must be signed in to change notification settings - Fork 681
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: support DB-IP country mmdb format as input & output
Download DB-IP free country lite mmdb file here: https://db-ip.com/db/download/ip-to-country-lite
- Loading branch information
1 parent
2e70d7f
commit b7daf13
Showing
10 changed files
with
347 additions
and
94 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 |
---|---|---|
|
@@ -69,8 +69,10 @@ $RECYCLE.BIN/ | |
tmp/ | ||
data/ | ||
geolite2/ | ||
db-ip/ | ||
output/ | ||
geoip | ||
*.dat | ||
*.mmdb | ||
*.srs | ||
*.mrs |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package maxmind | ||
|
||
import ( | ||
"encoding/json" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/Loyalsoldier/geoip/lib" | ||
) | ||
|
||
var ( | ||
defaultGeoLite2MMDBFile = filepath.Join("./", "geolite2", "GeoLite2-Country.mmdb") | ||
defaultDBIPCountryMMDBFile = filepath.Join("./", "db-ip", "dbip-country-lite.mmdb") | ||
) | ||
|
||
func newMMDBIn(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.InputConverter, error) { | ||
var tmp struct { | ||
URI string `json:"uri"` | ||
Want []string `json:"wantedList"` | ||
OnlyIPType lib.IPType `json:"onlyIPType"` | ||
} | ||
|
||
if len(data) > 0 { | ||
if err := json.Unmarshal(data, &tmp); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
if tmp.URI == "" { | ||
switch iType { | ||
case TypeMaxmindMMDBIn: | ||
tmp.URI = defaultGeoLite2MMDBFile | ||
|
||
case TypeDBIPCountryMMDBIn: | ||
tmp.URI = defaultDBIPCountryMMDBFile | ||
} | ||
} | ||
|
||
// Filter want list | ||
wantList := make(map[string]bool) | ||
for _, want := range tmp.Want { | ||
if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { | ||
wantList[want] = true | ||
} | ||
} | ||
|
||
return &MMDBIn{ | ||
Type: iType, | ||
Action: action, | ||
Description: iDesc, | ||
URI: tmp.URI, | ||
Want: wantList, | ||
OnlyIPType: tmp.OnlyIPType, | ||
}, nil | ||
} |
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,57 @@ | ||
package maxmind | ||
|
||
import ( | ||
"encoding/json" | ||
"path/filepath" | ||
|
||
"github.com/Loyalsoldier/geoip/lib" | ||
) | ||
|
||
var ( | ||
defaultOutputName = "Country.mmdb" | ||
defaultMaxmindOutputDir = filepath.Join("./", "output", "maxmind") | ||
defaultDBIPOutputDir = filepath.Join("./", "output", "db-ip") | ||
) | ||
|
||
func newMMDBOut(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.OutputConverter, error) { | ||
var tmp struct { | ||
OutputName string `json:"outputName"` | ||
OutputDir string `json:"outputDir"` | ||
Want []string `json:"wantedList"` | ||
Overwrite []string `json:"overwriteList"` | ||
Exclude []string `json:"excludedList"` | ||
OnlyIPType lib.IPType `json:"onlyIPType"` | ||
} | ||
|
||
if len(data) > 0 { | ||
if err := json.Unmarshal(data, &tmp); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
if tmp.OutputName == "" { | ||
tmp.OutputName = defaultOutputName | ||
} | ||
|
||
if tmp.OutputDir == "" { | ||
switch iType { | ||
case TypeMaxmindMMDBOut: | ||
tmp.OutputDir = defaultMaxmindOutputDir | ||
|
||
case TypeDBIPCountryMMDBOut: | ||
tmp.OutputDir = defaultDBIPOutputDir | ||
} | ||
} | ||
|
||
return &MMDBOut{ | ||
Type: iType, | ||
Action: action, | ||
Description: iDesc, | ||
OutputName: tmp.OutputName, | ||
OutputDir: tmp.OutputDir, | ||
Want: tmp.Want, | ||
Overwrite: tmp.Overwrite, | ||
Exclude: tmp.Exclude, | ||
OnlyIPType: tmp.OnlyIPType, | ||
}, nil | ||
} |
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,26 @@ | ||
package maxmind | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/Loyalsoldier/geoip/lib" | ||
) | ||
|
||
/* | ||
The types in this file extend the type `typeMaxmindMMDBIn`, | ||
which make it possible to support more formats for the project. | ||
*/ | ||
|
||
const ( | ||
TypeDBIPCountryMMDBIn = "dbipCountryMMDB" | ||
DescDBIPCountryMMDBIn = "Convert DB-IP country mmdb database to other formats" | ||
) | ||
|
||
func init() { | ||
lib.RegisterInputConfigCreator(TypeDBIPCountryMMDBIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) { | ||
return newMMDBIn(TypeDBIPCountryMMDBIn, DescDBIPCountryMMDBIn, action, data) | ||
}) | ||
lib.RegisterInputConverter(TypeDBIPCountryMMDBIn, &MMDBIn{ | ||
Description: DescDBIPCountryMMDBIn, | ||
}) | ||
} |
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,26 @@ | ||
package maxmind | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/Loyalsoldier/geoip/lib" | ||
) | ||
|
||
/* | ||
The types in this file extend the type `typeMaxmindMMDBOut`, | ||
which make it possible to support more formats for the project. | ||
*/ | ||
|
||
const ( | ||
TypeDBIPCountryMMDBOut = "dbipCountryMMDB" | ||
DescDBIPCountryMMDBOut = "Convert data to DB-IP country mmdb database format" | ||
) | ||
|
||
func init() { | ||
lib.RegisterOutputConfigCreator(TypeDBIPCountryMMDBOut, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) { | ||
return newMMDBOut(TypeDBIPCountryMMDBOut, DescDBIPCountryMMDBOut, action, data) | ||
}) | ||
lib.RegisterOutputConverter(TypeDBIPCountryMMDBOut, &MMDBOut{ | ||
Description: DescDBIPCountryMMDBOut, | ||
}) | ||
} |
Oops, something went wrong.