forked from zu1k/proxypool
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zu1k
committed
Aug 16, 2020
1 parent
e13892a
commit 23a337e
Showing
11 changed files
with
101 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package app | ||
|
||
import "github.com/zu1k/proxypool/proxy" | ||
|
||
var ( | ||
GeoIp proxy.GeoIP | ||
ProjectName = "proxypool" | ||
) | ||
|
||
func init() { | ||
GeoIp = proxy.NewGeoIP("assets/GeoLite2-City.mmdb") | ||
} |
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
Binary file not shown.
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,45 @@ | ||
package proxy | ||
|
||
import ( | ||
"log" | ||
"net" | ||
"os" | ||
|
||
"github.com/oschwald/geoip2-golang" | ||
) | ||
|
||
// GeoIP2 | ||
type GeoIP struct { | ||
db *geoip2.Reader | ||
} | ||
|
||
// new geoip from db file | ||
func NewGeoIP(filePath string) (geoip GeoIP) { | ||
// 判断文件是否存在 | ||
_, err := os.Stat(filePath) | ||
if err != nil && os.IsNotExist(err) { | ||
log.Println("文件不存在,请自行下载 Geoip2 City库,并保存在", filePath) | ||
os.Exit(1) | ||
} else { | ||
db, err := geoip2.Open(filePath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
geoip = GeoIP{db: db} | ||
} | ||
return | ||
} | ||
|
||
// find ip info | ||
func (g GeoIP) Find(ipORdomain string) (country string, err error) { | ||
ips, err := net.LookupIP(ipORdomain) | ||
if err != nil { | ||
return "", err | ||
} | ||
ipData := net.ParseIP(ips[0].String()) | ||
record, err := g.db.City(ipData) | ||
if err != nil { | ||
return "", err | ||
} | ||
return record.Country.IsoCode, nil | ||
} |
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