Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider adding ConnectionType and ISP Examples? #23

Open
DavidPowersOG opened this issue Jan 9, 2017 · 1 comment
Open

Consider adding ConnectionType and ISP Examples? #23

DavidPowersOG opened this issue Jan 9, 2017 · 1 comment

Comments

@DavidPowersOG
Copy link

Something like this:

//mmdb ConnectionType
func GetConnectionType(ipStr string) string {
ipObj := net.ParseIP(ipStr)
reader := dbReader("./assets/GeoIP2-Connection-Type.mmdb")
defer reader.Close()
record, err := reader.ConnectionType(ipObj)
if err != nil {
logBadIp(err)
return "unknown"
} else {
return record.ConnectionType
}
}

//mmdb ISP
func GetIsp(ipStr string) (string, string) {
ipObj := net.ParseIP(ipStr)
dbIsp := dbReader("./assets/GeoIP2-ISP.mmdb")
defer dbIsp.Close()
record, err := dbIsp.ISP(ipObj)
if err != nil {
logBadIp(err)
return "unknown", "unknown"
} else {
return record.ISP, record.Organization
}
}

func dbReader(mmdbPath string) *geoip2.Reader {
dbConn, err := geoip2.Open(mmdbPath)
if err != nil {
log.Println(err)
}
return dbConn
}
func logBadIp(err error) {
log.Println(err)
log.Println("bad IP")
}

@pavel-odintsov
Copy link

Awesome, thank you so much for sharing these examples. I appreciate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants