Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support targeting by region/state with GeoIPCity
See also issue #39
- Loading branch information
Showing
9 changed files
with
260 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package countries | ||
|
||
import ( | ||
"log" | ||
) | ||
|
||
func CountryRegionGroup(country, region string) string { | ||
|
||
if country != "us" { | ||
return "" | ||
} | ||
|
||
regions := map[string]string{ | ||
"us-ak": "us-west", | ||
"us-az": "us-west", | ||
"us-ca": "us-west", | ||
"us-co": "us-west", | ||
"us-hi": "us-west", | ||
"us-id": "us-west", | ||
"us-mt": "us-west", | ||
"us-nm": "us-west", | ||
"us-nv": "us-west", | ||
"us-or": "us-west", | ||
"us-ut": "us-west", | ||
"us-wa": "us-west", | ||
"us-wy": "us-west", | ||
|
||
"us-ar": "us-central", | ||
"us-ia": "us-central", | ||
"us-in": "us-central", | ||
"us-ks": "us-central", | ||
"us-la": "us-central", | ||
"us-mn": "us-central", | ||
"us-mo": "us-central", | ||
"us-nd": "us-central", | ||
"us-ne": "us-central", | ||
"us-ok": "us-central", | ||
"us-sd": "us-central", | ||
"us-tx": "us-central", | ||
"us-wi": "us-central", | ||
|
||
"us-al": "us-east", | ||
"us-ct": "us-east", | ||
"us-dc": "us-east", | ||
"us-de": "us-east", | ||
"us-fl": "us-east", | ||
"us-ga": "us-east", | ||
"us-ky": "us-east", | ||
"us-ma": "us-east", | ||
"us-md": "us-east", | ||
"us-me": "us-east", | ||
"us-mi": "us-east", | ||
"us-ms": "us-east", | ||
"us-nc": "us-east", | ||
"us-nh": "us-east", | ||
"us-nj": "us-east", | ||
"us-ny": "us-east", | ||
"us-oh": "us-east", | ||
"us-pa": "us-east", | ||
"us-ri": "us-east", | ||
"us-sc": "us-east", | ||
"us-tn": "us-east", | ||
"us-va": "us-east", | ||
"us-vt": "us-east", | ||
"us-wv": "us-east", | ||
} | ||
|
||
if group, ok := regions[region]; ok { | ||
return group | ||
} | ||
|
||
log.Printf("Did not find a region group for '%s'/'%s'", country, region) | ||
return "" | ||
} |
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 |
---|---|---|
@@ -1,16 +1,101 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/abh/geodns/countries" | ||
"github.com/abh/geoip" | ||
"log" | ||
"net" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func setupGeoIP() *geoip.GeoIP { | ||
type GeoIP struct { | ||
country *geoip.GeoIP | ||
hasCountry bool | ||
countryLastLoad time.Time | ||
|
||
gi, err := geoip.Open() | ||
city *geoip.GeoIP | ||
cityLastLoad time.Time | ||
hasCity bool | ||
} | ||
|
||
var geoIP = new(GeoIP) | ||
|
||
func (g *GeoIP) GetCountry(ip net.IP) (country, continent string, netmask int) { | ||
if g.country == nil { | ||
return "", "", 0 | ||
} | ||
|
||
country, netmask = geoIP.country.GetCountry(ip.String()) | ||
if len(country) > 0 { | ||
country = strings.ToLower(country) | ||
continent = countries.CountryContinent[country] | ||
} | ||
return | ||
} | ||
|
||
func (g *GeoIP) GetCountryRegion(ip net.IP) (country, continent, regionGroup, region string, netmask int) { | ||
if g.city == nil { | ||
log.Println("No city database available") | ||
country, continent, netmask = g.GetCountry(ip) | ||
return | ||
} | ||
|
||
record := geoIP.city.GetRecord(ip.String()) | ||
|
||
country = record.CountryCode | ||
region = record.Region | ||
if len(country) > 0 { | ||
country = strings.ToLower(country) | ||
continent = countries.CountryContinent[country] | ||
|
||
if len(region) > 0 { | ||
region = country + "-" + strings.ToLower(region) | ||
regionGroup = countries.CountryRegionGroup(country, region) | ||
} | ||
|
||
} | ||
return | ||
} | ||
|
||
func (g *GeoIP) setDirectory() { | ||
if len(Config.GeoIP.Directory) > 0 { | ||
geoip.SetCustomDirectory(Config.GeoIP.Directory) | ||
} | ||
} | ||
|
||
func (g *GeoIP) setupGeoIPCountry() { | ||
if g.country != nil { | ||
return | ||
} | ||
|
||
g.setDirectory() | ||
|
||
gi, err := geoip.OpenType(geoip.GEOIP_COUNTRY_EDITION) | ||
if gi == nil || err != nil { | ||
log.Printf("Could not open GeoIP database: %s\n", err) | ||
return nil | ||
log.Printf("Could not open country GeoIP database: %s\n", err) | ||
return | ||
} | ||
return gi | ||
g.countryLastLoad = time.Now() | ||
g.hasCity = true | ||
g.country = gi | ||
|
||
} | ||
|
||
func (g *GeoIP) setupGeoIPCity() { | ||
if g.city != nil { | ||
return | ||
} | ||
|
||
g.setDirectory() | ||
|
||
gi, err := geoip.OpenType(geoip.GEOIP_CITY_EDITION_REV1) | ||
if gi == nil || err != nil { | ||
log.Printf("Could not open city GeoIP database: %s\n", err) | ||
return | ||
} | ||
g.countryLastLoad = time.Now() | ||
g.hasCity = true | ||
g.city = gi | ||
|
||
} |
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
Oops, something went wrong.