-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add sing-geoip database support
- Loading branch information
Showing
7 changed files
with
73 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package mmdb | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
"github.com/oschwald/maxminddb-golang" | ||
) | ||
|
||
type geoip2Country struct { | ||
Country struct { | ||
IsoCode string `maxminddb:"iso_code"` | ||
} `maxminddb:"country"` | ||
} | ||
|
||
type Reader struct { | ||
*maxminddb.Reader | ||
databaseType | ||
} | ||
|
||
func (r Reader) LookupCode(ipAddress net.IP) string { | ||
switch r.databaseType { | ||
case typeMaxmind: | ||
var country geoip2Country | ||
_ = r.Lookup(ipAddress, &country) | ||
return country.Country.IsoCode | ||
|
||
case typeSing: | ||
var code string | ||
_ = r.Lookup(ipAddress, &code) | ||
return code | ||
|
||
default: | ||
panic(fmt.Sprint("unknown geoip database type:", r.databaseType)) | ||
} | ||
} |
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