-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use embed geoip database (#396)
* feat: use embed geoip database * mention ipinfo * only read once * comments comments * chore: installer version --------- Co-authored-by: naiba <[email protected]>
- Loading branch information
Showing
14 changed files
with
261 additions
and
48 deletions.
There are no files selected for viewing
File renamed without changes.
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 @@ | ||
stub |
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,54 @@ | ||
package geoip | ||
|
||
import ( | ||
"embed" | ||
"fmt" | ||
"log" | ||
"net" | ||
"strings" | ||
|
||
maxminddb "github.com/oschwald/maxminddb-golang" | ||
) | ||
|
||
//go:embed geoip.db | ||
var geoDBFS embed.FS | ||
|
||
var ( | ||
dbData []byte | ||
err error | ||
) | ||
|
||
type IPInfo struct { | ||
Country string `maxminddb:"country"` | ||
CountryName string `maxminddb:"country_name"` | ||
Continent string `maxminddb:"continent"` | ||
ContinentName string `maxminddb:"continent_name"` | ||
} | ||
|
||
func init() { | ||
dbData, err = geoDBFS.ReadFile("geoip.db") | ||
if err != nil { | ||
log.Printf("NEZHA>> Failed to open geoip database: %v", err) | ||
} | ||
} | ||
|
||
func Lookup(ip net.IP, record *IPInfo) (string, error) { | ||
db, err := maxminddb.FromBytes(dbData) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer db.Close() | ||
|
||
err = db.Lookup(ip, record) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if record.Country != "" { | ||
return strings.ToLower(record.Country), nil | ||
} else if record.Continent != "" { | ||
return strings.ToLower(record.Continent), nil | ||
} | ||
|
||
return "", fmt.Errorf("IP not found") | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.