-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
34e27a5
commit 7a87eb6
Showing
8 changed files
with
103 additions
and
6 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
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
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,48 @@ | ||
/* | ||
* Copyright (C) 2024 Nethesis S.r.l. | ||
* http://www.nethesis.it - [email protected] | ||
* | ||
* SPDX-License-Identifier: GPL-2.0-only | ||
* | ||
* author: Giacomo Sanchietti <[email protected]> | ||
*/ | ||
|
||
package utils | ||
|
||
import ( | ||
"log" | ||
"net" | ||
|
||
"github.com/NethServer/nethsecurity-controller/api/configuration" | ||
"github.com/NethServer/nethsecurity-controller/api/logs" | ||
"github.com/oschwald/geoip2-golang" | ||
) | ||
|
||
var db *geoip2.Reader | ||
|
||
func InitGeoIP() error { | ||
var err error | ||
// open geoip db, path from config, name is always the same: GeoLite2-Country.mmdb | ||
db, err = geoip2.Open(configuration.Config.GeoIPDbDir + "/GeoLite2-Country.mmdb") | ||
|
||
if err != nil { | ||
logs.Logs.Println("[ERR][GEOIP] error reading geoip db file :" + err.Error()) | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func GetCountryShort(ip string) string { | ||
if ip == "" || db == nil { | ||
return "" | ||
} | ||
|
||
// If you are using strings that may be invalid, check that ip is not nil | ||
record, err := db.City(net.ParseIP(ip)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
return record.Country.IsoCode | ||
} |