Skip to content

Commit

Permalink
Guess the geoip2 directory if it's not explicitly configured
Browse files Browse the repository at this point in the history
  • Loading branch information
abh committed Dec 9, 2017
1 parent 91973fc commit 06a1091
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
7 changes: 6 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sync"
"time"

"github.com/abh/geodns/targeting/geoip2"

"gopkg.in/fsnotify.v1"
gcfg "gopkg.in/gcfg.v1"
)
Expand Down Expand Up @@ -64,7 +66,10 @@ func (conf *AppConfig) StatHatApiKey() string {
func (conf *AppConfig) GeoIPDirectory() string {
cfgMutex.RLock()
defer cfgMutex.RUnlock()
return conf.GeoIP.Directory
if len(conf.GeoIP.Directory) > 0 {
return conf.GeoIP.Directory
}
return geoip2.FindDB()
}

func configWatcher(fileName string) {
Expand Down
5 changes: 3 additions & 2 deletions dns/geodns.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
; with your .json zone files.

[geoip]
;; Directory containing the GeoIP .dat database files
;; Directory containing the GeoIP .dat database files; defaults
;; to looking through a list of directories looking for one that
;; exists.
;directory=/usr/local/share/GeoIP/
directory=/usr/local/share/GeoIP/

[querylog]
;; directory to save query logs; disabled if not specified
Expand Down
19 changes: 19 additions & 0 deletions targeting/geoip2/geoip2.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,26 @@ func init() {
asnDB: []string{"GeoIP2-ISP.mmdb"},
cityDB: []string{"GeoIP2-City.mmdb", "GeoLite2-City.mmdb"},
}
}

func FindDB() string {
dirs := []string{
"/usr/share/GeoIP/", // Linux default
"/usr/share/local/GeoIP/", // source install?
"/usr/local/share/GeoIP/", // FreeBSD
"/opt/local/share/GeoIP/", // MacPorts
"/usr/share/GeoIP/", // ArchLinux
}
for _, dir := range dirs {
if _, err := os.Stat(dir); err != nil {
if os.IsExist(err) {
log.Println(err)
}
continue
}
return dir
}
return ""
}

func (g *g2) open(t geoType, db string) (*geoip2.Reader, error) {
Expand Down
2 changes: 2 additions & 0 deletions targeting/targeting.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ func init() {

var g geo.Provider

// Setup sets the global geo provider
func Setup(gn geo.Provider) error {
g = gn
return nil
}

// Geo returns the global geo provider
func Geo() geo.Provider {
return g
}
Expand Down

0 comments on commit 06a1091

Please sign in to comment.