-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #197 from HyNetwork/wip-geoip
ACL GeoIP Country
- Loading branch information
Showing
15 changed files
with
148 additions
and
7,086 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
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,51 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/oschwald/geoip2-golang" | ||
"github.com/sirupsen/logrus" | ||
"io" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
const ( | ||
mmdbURL = "https://github.com/P3TERX/GeoLite.mmdb/raw/download/GeoLite2-Country.mmdb" | ||
) | ||
|
||
func downloadMMDB(filename string) error { | ||
resp, err := http.Get(mmdbURL) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
file, err := os.Create(filename) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
_, err = io.Copy(file, resp.Body) | ||
return err | ||
} | ||
|
||
func loadMMDBReader(filename string) (*geoip2.Reader, error) { | ||
if _, err := os.Stat(filename); err != nil { | ||
if os.IsNotExist(err) { | ||
logrus.Info("GeoLite2 database not found, downloading...") | ||
if err := downloadMMDB(filename); err != nil { | ||
return nil, err | ||
} | ||
logrus.WithFields(logrus.Fields{ | ||
"file": filename, | ||
}).Info("GeoLite2 database downloaded") | ||
return geoip2.Open(filename) | ||
} else { | ||
// some other error | ||
return nil, err | ||
} | ||
} else { | ||
// file exists, just open it | ||
return geoip2.Open(filename) | ||
} | ||
} |
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.