Merge pull request #11 from sparkyvxcx/develop

Add country flag emoji before country code in each proxy name field, some typo fix
This commit is contained in:
zu1k
2020-08-26 12:49:01 +08:00
committed by GitHub
4 changed files with 2081 additions and 10 deletions

2000
assets/flags.json Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -51,9 +51,9 @@ func CrawlGo() {
cache.LastCrawlTime = time.Now().In(location).Format("2006-01-02 15:04:05")
// 可用性检测
log.Println("Now check proxy health...")
log.Println("Now proceed proxy health check...")
proxies = proxy.CleanBadProxiesWithGrpool(proxies)
log.Println("CrawlGo clash usefull node count:", len(proxies))
log.Println("CrawlGo clash usable node count:", len(proxies))
proxies.NameReIndex()
cache.SetProxies("proxies", proxies)
cache.UsefullProxiesCount = proxies.Len()

View File

@@ -1,6 +1,9 @@
package proxy
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"os"
@@ -21,11 +24,22 @@ func InitGeoIpDB() {
// GeoIP2
type GeoIP struct {
db *geoip2.Reader
db *geoip2.Reader
flags cclist
}
type CountryCode struct {
Code string `json:"code"`
Emoji string `json:"emoji"`
Unicode string `json:"unicode`
Name string `json:"name"`
Title string `json:"title"`
Dialcode string `json:"dialCode`
}
// new geoip from db file
func NewGeoIP(filePath string) (geoip GeoIP) {
var countrycodes cclist
// 判断文件是否存在
_, err := os.Stat(filePath)
if err != nil && os.IsNotExist(err) {
@@ -36,7 +50,14 @@ func NewGeoIP(filePath string) (geoip GeoIP) {
if err != nil {
log.Fatal(err)
}
geoip = GeoIP{db: db}
data, err := ioutil.ReadFile("assets/flags.json")
if err != nil {
log.Fatal(err)
return
}
json.Unmarshal(data, &countrycodes)
geoip = GeoIP{db: db, flags: countrycodes}
}
return
}
@@ -52,5 +73,54 @@ func (g GeoIP) Find(ipORdomain string) (ip, country string, err error) {
if err != nil {
return "", "", err
}
return ips[0].String(), record.Country.IsoCode, nil
countrycode := record.Country.IsoCode
if countrycode == "" {
// handle unknown country code
return ips[0].String(), fmt.Sprintf("🏁 ZZ"), nil
}
countryflag := g.getFlag(countrycode)
country = fmt.Sprintf("%v %v", countryflag, countrycode)
return ips[0].String(), country, nil
}
// getFlag method take country code as input, return its corresponding country/region flag
func (g GeoIP) getFlag(countrycode string) string {
result := find(g.flags, countrycode)
return result.Emoji
}
type cclist []CountryCode
func (c cclist) in(code string, left bool) bool {
length := len(c)
if left {
return c[length-1].Code >= code
}
return c[0].Code <= code
}
// find will find corresponding country flag emoji from flags for a given country code
func find(list cclist, target string) CountryCode {
var result CountryCode
length := len(list)
if length == 1 {
if list[0].Code == target {
return list[0]
}
return CountryCode{Emoji: "🏁"}
}
split := length / 2
left, right := list[:split], list[split:]
if left.in(target, true) {
result = find(left, target)
}
if right.in(target, false) {
result = find(right, target)
}
return result
}

View File

@@ -71,8 +71,8 @@ func (ps ProxyList) NameAddCounrty() ProxyList {
go func() {
defer wg.Done()
_, country, err := geoIp.Find(ps[ii].BaseInfo().Server)
if err != nil || country == "" {
country = "ZZ"
if err != nil {
country = "🏁 ZZ"
}
ps[ii].SetName(fmt.Sprintf("%s", country))
//ps[ii].SetIP(ip)
@@ -85,7 +85,7 @@ func (ps ProxyList) NameAddCounrty() ProxyList {
func (ps ProxyList) NameAddIndex() ProxyList {
num := len(ps)
for i := 0; i < num; i++ {
ps[i].SetName(fmt.Sprintf("%s_%d", ps[i].BaseInfo().Name, i+1))
ps[i].SetName(fmt.Sprintf("%s_%+02v", ps[i].BaseInfo().Name, i+1))
}
return ps
}
@@ -94,8 +94,9 @@ func (ps ProxyList) NameReIndex() ProxyList {
num := len(ps)
for i := 0; i < num; i++ {
originName := ps[i].BaseInfo().Name
country := string([]rune(originName)[:2])
ps[i].SetName(fmt.Sprintf("%s_%d", country, i+1))
// country := string([]rune(originName)[:2])
country := string([]rune(originName)[:5])
ps[i].SetName(fmt.Sprintf("%s_%+02v", country, i+1))
}
return ps
}