Compare commits

...

15 Commits

Author SHA1 Message Date
zu1k
6421e09c8e Bump version 2020-08-26 13:09:16 +08:00
zu1k
c3698f8ed5 Add flags.json to bindata and use map instead of slices 2020-08-26 13:07:30 +08:00
zu1k
84d037c0b8 Merge pull request #11 from sparkyvxcx/develop
Add country flag emoji before country code in each proxy name field, some typo fix
2020-08-26 12:49:01 +08:00
Sparkvix
ff52fc0ed1 Add getFlag method for GeoIP struct 2020-08-26 00:10:00 -04:00
Sparkvix
2f731b0acb Add country flag emoji before country code in each proxy name field, some typo fix 2020-08-25 13:41:59 -04:00
zu1k
cedd5b4ec4 update readme 2020-08-25 14:37:15 +08:00
zu1k
a6387cfefd remove windows platform 2020-08-25 14:30:51 +08:00
zu1k
b54d2d4c01 fix linux file path 2020-08-25 14:29:14 +08:00
zu1k
933a2ea73b Merge pull request #10 from zu1k/dependabot/go_modules/github.com/cloudflare/cloudflare-go-0.13.1
Bump github.com/cloudflare/cloudflare-go from 0.13.0 to 0.13.1
2020-08-25 14:26:35 +08:00
zu1k
673da5b59a fix for github action 2020-08-25 14:25:32 +08:00
zu1k
0828209ae1 bump version 2020-08-25 14:25:32 +08:00
zu1k
386b302208 use bindata 2020-08-25 14:25:32 +08:00
dependabot[bot]
bbebdca897 Bump github.com/cloudflare/cloudflare-go from 0.13.0 to 0.13.1
Bumps [github.com/cloudflare/cloudflare-go](https://github.com/cloudflare/cloudflare-go) from 0.13.0 to 0.13.1.
- [Release notes](https://github.com/cloudflare/cloudflare-go/releases)
- [Commits](https://github.com/cloudflare/cloudflare-go/compare/v0.13.0...v0.13.1)

Signed-off-by: dependabot[bot] <support@github.com>
2020-08-25 06:21:26 +00:00
zu1k
88ad9e8ba1 Merge pull request #9 from zu1k/dependabot/github_actions/actions/checkout-v2.3.2
Bump actions/checkout from v2.3.1 to v2.3.2
2020-08-25 07:39:59 +08:00
dependabot[bot]
69025eb9c2 Bump actions/checkout from v2.3.1 to v2.3.2
Bumps [actions/checkout](https://github.com/actions/checkout) from v2.3.1 to v2.3.2.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v2.3.1...2036a08e25fa78bbd946711a407b529a0a1204bf)

Signed-off-by: dependabot[bot] <support@github.com>
2020-08-24 08:06:44 +00:00
13 changed files with 2612 additions and 50 deletions

View File

@@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v2.3.1
uses: actions/checkout@v2.3.2
- name: Build and push Docker images
uses: docker/build-push-action@v1.1.0

View File

@@ -12,7 +12,7 @@ jobs:
go-version: 1.14.x
- name: Check out code into the Go module directory
uses: actions/checkout@v2.3.1
uses: actions/checkout@v2.3.2
- name: Cache go module
uses: actions/cache@v2
@@ -26,6 +26,13 @@ jobs:
run: |
go test ./...
- name: gen go-bindata
if: startsWith(github.ref, 'refs/tags/')
run: |
go get -u github.com/go-bindata/go-bindata/...
go-bindata -o internal/bindata/geoip/geoip.go -pkg bingeoip assets/GeoLite2-City.mmdb assets/flags.json
go-bindata -o internal/bindata/html/html.go -pkg binhtml assets/html/
- name: Build
if: startsWith(github.ref, 'refs/tags/')
env:

View File

@@ -20,11 +20,8 @@ PLATFORM_LIST = \
freebsd-386 \
freebsd-amd64
WINDOWS_ARCH_LIST = \
windows-386 \
windows-amd64
all: linux-amd64 darwin-amd64 windows-amd64 # Most used
all: linux-amd64 darwin-amd64
docker:
$(GOBUILD) -o $(BINDIR)/$(NAME)-$@
@@ -74,24 +71,14 @@ freebsd-386:
freebsd-amd64:
GOARCH=amd64 GOOS=freebsd $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
windows-386:
GOARCH=386 GOOS=windows $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe
windows-amd64:
GOARCH=amd64 GOOS=windows $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe
gz_releases=$(addsuffix .gz, $(PLATFORM_LIST))
zip_releases=$(addsuffix .zip, $(WINDOWS_ARCH_LIST))
$(gz_releases): %.gz : %
chmod +x $(BINDIR)/$(NAME)-$(basename $@)
gzip -f -S -$(VERSION).gz $(BINDIR)/$(NAME)-$(basename $@)
$(zip_releases): %.zip : %
zip -m -j $(BINDIR)/$(NAME)-$(basename $@)-$(VERSION).zip $(BINDIR)/$(NAME)-$(basename $@).exe
all-arch: $(PLATFORM_LIST)
all-arch: $(PLATFORM_LIST) $(WINDOWS_ARCH_LIST)
releases: $(gz_releases) $(zip_releases)
releases: $(gz_releases)
clean:
rm $(BINDIR)/*

View File

@@ -53,8 +53,6 @@ $ go get -u -v github.com/zu1k/proxypool
从这里下载预编译好的程序 [release](https://github.com/zu1k/proxypool/releases)
除了编译好的二进制程序,你还需要下载仓库 `assets` 文件夹,放置在可执行文件同目录
### 使用docker
```sh

View File

@@ -1,29 +1,34 @@
package api
import (
"html/template"
"net/http"
"os"
"github.com/zu1k/proxypool/config"
"github.com/gin-gonic/gin"
_ "github.com/heroku/x/hmetrics/onload"
"github.com/zu1k/proxypool/config"
binhtml "github.com/zu1k/proxypool/internal/bindata/html"
"github.com/zu1k/proxypool/internal/cache"
"github.com/zu1k/proxypool/pkg/provider"
)
const version = "v0.3.1"
const version = "v0.3.3"
var router *gin.Engine
func setupRouter() {
gin.SetMode(gin.ReleaseMode)
router = gin.New()
router.Use(gin.Recovery())
router.LoadHTMLGlob("assets/html/*")
router.Use(gin.Logger(), gin.Recovery())
temp, err := loadTemplate()
if err != nil {
panic(err)
}
router.SetHTMLTemplate(temp)
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
c.HTML(http.StatusOK, "assets/html/index.html", gin.H{
"domain": config.Config.Domain,
"getters_count": cache.GettersCount,
"all_proxies_count": cache.AllProxiesCount,
@@ -38,25 +43,25 @@ func setupRouter() {
})
router.GET("/clash", func(c *gin.Context) {
c.HTML(http.StatusOK, "clash.html", gin.H{
c.HTML(http.StatusOK, "assets/html/clash.html", gin.H{
"domain": config.Config.Domain,
})
})
router.GET("/surge", func(c *gin.Context) {
c.HTML(http.StatusOK, "surge.html", gin.H{
c.HTML(http.StatusOK, "assets/html/surge.html", gin.H{
"domain": config.Config.Domain,
})
})
router.GET("/clash/config", func(c *gin.Context) {
c.HTML(http.StatusOK, "clash-config.yaml", gin.H{
c.HTML(http.StatusOK, "assets/html/clash-config.yaml", gin.H{
"domain": config.Config.Domain,
})
})
router.GET("/surge/config", func(c *gin.Context) {
c.HTML(http.StatusOK, "surge.conf", gin.H{
c.HTML(http.StatusOK, "assets/html/surge.conf", gin.H{
"domain": config.Config.Domain,
})
})
@@ -104,3 +109,15 @@ func Run() {
}
router.Run(":" + port)
}
func loadTemplate() (t *template.Template, err error) {
t = template.New("")
for _, fileName := range binhtml.AssetNames() {
data := binhtml.MustAsset(fileName)
t, err = t.New(fileName).Parse(string(data))
if err != nil {
return nil, err
}
}
return t, nil
}

2000
assets/flags.json Normal file

File diff suppressed because it is too large Load Diff

4
go.mod
View File

@@ -9,9 +9,10 @@ require (
github.com/antchfx/htmlquery v1.2.3 // indirect
github.com/antchfx/xmlquery v1.2.4 // indirect
github.com/antchfx/xpath v1.1.8 // indirect
github.com/cloudflare/cloudflare-go v0.13.0
github.com/cloudflare/cloudflare-go v0.13.1
github.com/ghodss/yaml v1.0.0
github.com/gin-gonic/gin v1.6.3
github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect
github.com/go-playground/validator/v10 v10.3.0 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gocolly/colly v1.2.0
@@ -29,7 +30,6 @@ require (
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect
github.com/temoto/robotstxt v1.1.1 // indirect
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de // indirect
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc // indirect
golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed // indirect
golang.org/x/text v0.3.3 // indirect
google.golang.org/appengine v1.6.6 // indirect

7
go.sum
View File

@@ -28,6 +28,8 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudflare/cloudflare-go v0.13.0 h1:Mx2gNz/r4H0eHETENq5ZPtc/kOX/uzEfT2UFuA+/XAU=
github.com/cloudflare/cloudflare-go v0.13.0/go.mod h1:6rZ6s/XWxP8WIWMATgDM7aUop0Z0ZOAfcm/4rEd0lOY=
github.com/cloudflare/cloudflare-go v0.13.1 h1:8i8E5CubfAlD/2zWM7VGKoIPS2/x/lO/nRnqDl0j5Dk=
github.com/cloudflare/cloudflare-go v0.13.1/go.mod h1:27kfc1apuifUmJhp069y0+hwlKDg4bd8LWlu7oKeZvM=
github.com/codegangsta/negroni v1.0.0/go.mod h1:v0y3T5G7Y1UlFfyxFn/QLRU4a2EuNau2iZY63YTKWo0=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -45,6 +47,9 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-bindata/go-bindata v1.0.0 h1:DZ34txDXWn1DyWa+vQf7V9ANc2ILTtrEjtlsdJRF26M=
github.com/go-bindata/go-bindata v3.1.2+incompatible h1:5vjJMVhowQdPzjE1LdxyFF7YFTXg5IgGVW4gBr5IbvE=
github.com/go-bindata/go-bindata v3.1.2+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo=
github.com/go-chi/chi v4.1.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/go-chi/cors v1.1.1/go.mod h1:K2Yje0VW/SJzxiyMYu6iPQYa7hMjQX2i/F491VChg1I=
github.com/go-chi/render v1.0.1/go.mod h1:pq4Rr7HbnsdaeHagklXub+p6Wd16Af5l9koip1OvJns=
@@ -266,6 +271,8 @@ golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc h1:zK/HqS5bZxDptfPJNq8v7vJfXtkU7r9TLIoSr1bXaP4=
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200822124328-c89045814202 h1:VvcQYSHwXgi7W+TpUR6A9g6Up98WAHf3f/ulnJ62IyA=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=

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

@@ -0,0 +1,213 @@
// Code generated for package bingeoip by go-bindata DO NOT EDIT. (@generated)
// sources:
// assets/GeoLite2-City.mmdb
// assets/flags.json
package bingeoip
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// bindataRead reads the given file from disk. It returns an error on failure.
func bindataRead(path, name string) ([]byte, error) {
buf, err := ioutil.ReadFile(path)
if err != nil {
err = fmt.Errorf("Error reading asset %s at %s: %v", name, path, err)
}
return buf, err
}
type asset struct {
bytes []byte
info os.FileInfo
}
// assetsGeolite2CityMmdb reads file data from disk. It returns an error on failure.
func assetsGeolite2CityMmdb() (*asset, error) {
path := "assets/GeoLite2-City.mmdb"
name := "assets/GeoLite2-City.mmdb"
bytes, err := bindataRead(path, name)
if err != nil {
return nil, err
}
fi, err := os.Stat(path)
if err != nil {
err = fmt.Errorf("Error reading asset info %s at %s: %v", name, path, err)
}
a := &asset{bytes: bytes, info: fi}
return a, err
}
// assetsFlagsJson reads file data from disk. It returns an error on failure.
func assetsFlagsJson() (*asset, error) {
path := "assets/flags.json"
name := "assets/flags.json"
bytes, err := bindataRead(path, name)
if err != nil {
return nil, err
}
fi, err := os.Stat(path)
if err != nil {
err = fmt.Errorf("Error reading asset info %s at %s: %v", name, path, err)
}
a := &asset{bytes: bytes, info: fi}
return a, err
}
// Asset loads and returns the asset for the given name.
// It returns an error if the asset could not be found or
// could not be loaded.
func Asset(name string) ([]byte, error) {
cannonicalName := strings.Replace(name, "\\", "/", -1)
if f, ok := _bindata[cannonicalName]; ok {
a, err := f()
if err != nil {
return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err)
}
return a.bytes, nil
}
return nil, fmt.Errorf("Asset %s not found", name)
}
// MustAsset is like Asset but panics when Asset would return an error.
// It simplifies safe initialization of global variables.
func MustAsset(name string) []byte {
a, err := Asset(name)
if err != nil {
panic("asset: Asset(" + name + "): " + err.Error())
}
return a
}
// AssetInfo loads and returns the asset info for the given name.
// It returns an error if the asset could not be found or
// could not be loaded.
func AssetInfo(name string) (os.FileInfo, error) {
cannonicalName := strings.Replace(name, "\\", "/", -1)
if f, ok := _bindata[cannonicalName]; ok {
a, err := f()
if err != nil {
return nil, fmt.Errorf("AssetInfo %s can't read by error: %v", name, err)
}
return a.info, nil
}
return nil, fmt.Errorf("AssetInfo %s not found", name)
}
// AssetNames returns the names of the assets.
func AssetNames() []string {
names := make([]string, 0, len(_bindata))
for name := range _bindata {
names = append(names, name)
}
return names
}
// _bindata is a table, holding each asset generator, mapped to its name.
var _bindata = map[string]func() (*asset, error){
"assets/GeoLite2-City.mmdb": assetsGeolite2CityMmdb,
"assets/flags.json": assetsFlagsJson,
}
// AssetDir returns the file names below a certain
// directory embedded in the file by go-bindata.
// For example if you run go-bindata on data/... and data contains the
// following hierarchy:
// data/
// foo.txt
// img/
// a.png
// b.png
// then AssetDir("data") would return []string{"foo.txt", "img"}
// AssetDir("data/img") would return []string{"a.png", "b.png"}
// AssetDir("foo.txt") and AssetDir("notexist") would return an error
// AssetDir("") will return []string{"data"}.
func AssetDir(name string) ([]string, error) {
node := _bintree
if len(name) != 0 {
cannonicalName := strings.Replace(name, "\\", "/", -1)
pathList := strings.Split(cannonicalName, "/")
for _, p := range pathList {
node = node.Children[p]
if node == nil {
return nil, fmt.Errorf("Asset %s not found", name)
}
}
}
if node.Func != nil {
return nil, fmt.Errorf("Asset %s not found", name)
}
rv := make([]string, 0, len(node.Children))
for childName := range node.Children {
rv = append(rv, childName)
}
return rv, nil
}
type bintree struct {
Func func() (*asset, error)
Children map[string]*bintree
}
var _bintree = &bintree{nil, map[string]*bintree{
"assets": &bintree{nil, map[string]*bintree{
"GeoLite2-City.mmdb": &bintree{assetsGeolite2CityMmdb, map[string]*bintree{}},
"flags.json": &bintree{assetsFlagsJson, map[string]*bintree{}},
}},
}}
// RestoreAsset restores an asset under the given directory
func RestoreAsset(dir, name string) error {
data, err := Asset(name)
if err != nil {
return err
}
info, err := AssetInfo(name)
if err != nil {
return err
}
err = os.MkdirAll(_filePath(dir, filepath.Dir(name)), os.FileMode(0755))
if err != nil {
return err
}
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
if err != nil {
return err
}
err = os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime())
if err != nil {
return err
}
return nil
}
// RestoreAssets restores an asset under the given directory recursively
func RestoreAssets(dir, name string) error {
children, err := AssetDir(name)
// File
if err != nil {
return RestoreAsset(dir, name)
}
// Dir
for _, child := range children {
err = RestoreAssets(dir, filepath.Join(name, child))
if err != nil {
return err
}
}
return nil
}
func _filePath(dir, name string) string {
cannonicalName := strings.Replace(name, "\\", "/", -1)
return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...)
}

View File

@@ -0,0 +1,278 @@
// Code generated for package binhtml by go-bindata DO NOT EDIT. (@generated)
// sources:
// assets/html/clash-config.yaml
// assets/html/clash.html
// assets/html/index.html
// assets/html/surge.conf
// assets/html/surge.html
package binhtml
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// bindataRead reads the given file from disk. It returns an error on failure.
func bindataRead(path, name string) ([]byte, error) {
buf, err := ioutil.ReadFile(path)
if err != nil {
err = fmt.Errorf("Error reading asset %s at %s: %v", name, path, err)
}
return buf, err
}
type asset struct {
bytes []byte
info os.FileInfo
}
// assetsHtmlClashConfigYaml reads file data from disk. It returns an error on failure.
func assetsHtmlClashConfigYaml() (*asset, error) {
path := "assets/html/clash-config.yaml"
name := "assets/html/clash-config.yaml"
bytes, err := bindataRead(path, name)
if err != nil {
return nil, err
}
fi, err := os.Stat(path)
if err != nil {
err = fmt.Errorf("Error reading asset info %s at %s: %v", name, path, err)
}
a := &asset{bytes: bytes, info: fi}
return a, err
}
// assetsHtmlClashHtml reads file data from disk. It returns an error on failure.
func assetsHtmlClashHtml() (*asset, error) {
path := "assets/html/clash.html"
name := "assets/html/clash.html"
bytes, err := bindataRead(path, name)
if err != nil {
return nil, err
}
fi, err := os.Stat(path)
if err != nil {
err = fmt.Errorf("Error reading asset info %s at %s: %v", name, path, err)
}
a := &asset{bytes: bytes, info: fi}
return a, err
}
// assetsHtmlIndexHtml reads file data from disk. It returns an error on failure.
func assetsHtmlIndexHtml() (*asset, error) {
path := "assets/html/index.html"
name := "assets/html/index.html"
bytes, err := bindataRead(path, name)
if err != nil {
return nil, err
}
fi, err := os.Stat(path)
if err != nil {
err = fmt.Errorf("Error reading asset info %s at %s: %v", name, path, err)
}
a := &asset{bytes: bytes, info: fi}
return a, err
}
// assetsHtmlSurgeConf reads file data from disk. It returns an error on failure.
func assetsHtmlSurgeConf() (*asset, error) {
path := "assets/html/surge.conf"
name := "assets/html/surge.conf"
bytes, err := bindataRead(path, name)
if err != nil {
return nil, err
}
fi, err := os.Stat(path)
if err != nil {
err = fmt.Errorf("Error reading asset info %s at %s: %v", name, path, err)
}
a := &asset{bytes: bytes, info: fi}
return a, err
}
// assetsHtmlSurgeHtml reads file data from disk. It returns an error on failure.
func assetsHtmlSurgeHtml() (*asset, error) {
path := "assets/html/surge.html"
name := "assets/html/surge.html"
bytes, err := bindataRead(path, name)
if err != nil {
return nil, err
}
fi, err := os.Stat(path)
if err != nil {
err = fmt.Errorf("Error reading asset info %s at %s: %v", name, path, err)
}
a := &asset{bytes: bytes, info: fi}
return a, err
}
// Asset loads and returns the asset for the given name.
// It returns an error if the asset could not be found or
// could not be loaded.
func Asset(name string) ([]byte, error) {
cannonicalName := strings.Replace(name, "\\", "/", -1)
if f, ok := _bindata[cannonicalName]; ok {
a, err := f()
if err != nil {
return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err)
}
return a.bytes, nil
}
return nil, fmt.Errorf("Asset %s not found", name)
}
// MustAsset is like Asset but panics when Asset would return an error.
// It simplifies safe initialization of global variables.
func MustAsset(name string) []byte {
a, err := Asset(name)
if err != nil {
panic("asset: Asset(" + name + "): " + err.Error())
}
return a
}
// AssetInfo loads and returns the asset info for the given name.
// It returns an error if the asset could not be found or
// could not be loaded.
func AssetInfo(name string) (os.FileInfo, error) {
cannonicalName := strings.Replace(name, "\\", "/", -1)
if f, ok := _bindata[cannonicalName]; ok {
a, err := f()
if err != nil {
return nil, fmt.Errorf("AssetInfo %s can't read by error: %v", name, err)
}
return a.info, nil
}
return nil, fmt.Errorf("AssetInfo %s not found", name)
}
// AssetNames returns the names of the assets.
func AssetNames() []string {
names := make([]string, 0, len(_bindata))
for name := range _bindata {
names = append(names, name)
}
return names
}
// _bindata is a table, holding each asset generator, mapped to its name.
var _bindata = map[string]func() (*asset, error){
"assets/html/clash-config.yaml": assetsHtmlClashConfigYaml,
"assets/html/clash.html": assetsHtmlClashHtml,
"assets/html/index.html": assetsHtmlIndexHtml,
"assets/html/surge.conf": assetsHtmlSurgeConf,
"assets/html/surge.html": assetsHtmlSurgeHtml,
}
// AssetDir returns the file names below a certain
// directory embedded in the file by go-bindata.
// For example if you run go-bindata on data/... and data contains the
// following hierarchy:
// data/
// foo.txt
// img/
// a.png
// b.png
// then AssetDir("data") would return []string{"foo.txt", "img"}
// AssetDir("data/img") would return []string{"a.png", "b.png"}
// AssetDir("foo.txt") and AssetDir("notexist") would return an error
// AssetDir("") will return []string{"data"}.
func AssetDir(name string) ([]string, error) {
node := _bintree
if len(name) != 0 {
cannonicalName := strings.Replace(name, "\\", "/", -1)
pathList := strings.Split(cannonicalName, "/")
for _, p := range pathList {
node = node.Children[p]
if node == nil {
return nil, fmt.Errorf("Asset %s not found", name)
}
}
}
if node.Func != nil {
return nil, fmt.Errorf("Asset %s not found", name)
}
rv := make([]string, 0, len(node.Children))
for childName := range node.Children {
rv = append(rv, childName)
}
return rv, nil
}
type bintree struct {
Func func() (*asset, error)
Children map[string]*bintree
}
var _bintree = &bintree{nil, map[string]*bintree{
"assets": &bintree{nil, map[string]*bintree{
"html": &bintree{nil, map[string]*bintree{
"clash-config.yaml": &bintree{assetsHtmlClashConfigYaml, map[string]*bintree{}},
"clash.html": &bintree{assetsHtmlClashHtml, map[string]*bintree{}},
"index.html": &bintree{assetsHtmlIndexHtml, map[string]*bintree{}},
"surge.conf": &bintree{assetsHtmlSurgeConf, map[string]*bintree{}},
"surge.html": &bintree{assetsHtmlSurgeHtml, map[string]*bintree{}},
}},
}},
}}
// RestoreAsset restores an asset under the given directory
func RestoreAsset(dir, name string) error {
data, err := Asset(name)
if err != nil {
return err
}
info, err := AssetInfo(name)
if err != nil {
return err
}
err = os.MkdirAll(_filePath(dir, filepath.Dir(name)), os.FileMode(0755))
if err != nil {
return err
}
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
if err != nil {
return err
}
err = os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime())
if err != nil {
return err
}
return nil
}
// RestoreAssets restores an asset under the given directory recursively
func RestoreAssets(dir, name string) error {
children, err := AssetDir(name)
// File
if err != nil {
return RestoreAsset(dir, name)
}
// Dir
for _, child := range children {
err = RestoreAssets(dir, filepath.Join(name, child))
if err != nil {
return err
}
}
return nil
}
func _filePath(dir, name string) string {
cannonicalName := strings.Replace(name, "\\", "/", -1)
return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...)
}

View File

@@ -1,37 +1,79 @@
package proxy
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"github.com/oschwald/geoip2-golang"
bingeoip "github.com/zu1k/proxypool/internal/bindata/geoip"
)
var geoIp GeoIP
func InitGeoIpDB() {
geoIp = NewGeoIP("assets/GeoLite2-City.mmdb")
err := bingeoip.RestoreAsset("", "assets/GeoLite2-City.mmdb")
if err != nil {
panic(err)
}
err = bingeoip.RestoreAsset("", "assets/flags.json")
if err != nil {
panic(err)
}
geoIp = NewGeoIP("assets/GeoLite2-City.mmdb", "assets/flags.json")
}
// GeoIP2
type GeoIP struct {
db *geoip2.Reader
db *geoip2.Reader
emojiMap map[string]string
}
type CountryEmoji struct {
Code string `json:"code"`
Emoji string `json:"emoji"`
}
// new geoip from db file
func NewGeoIP(filePath string) (geoip GeoIP) {
func NewGeoIP(geodb, flags string) (geoip GeoIP) {
// 判断文件是否存在
_, err := os.Stat(filePath)
_, err := os.Stat(geodb)
if err != nil && os.IsNotExist(err) {
log.Println("文件不存在,请自行下载 Geoip2 City库并保存在", filePath)
log.Println("文件不存在,请自行下载 Geoip2 City库并保存在", geodb)
os.Exit(1)
} else {
db, err := geoip2.Open(filePath)
db, err := geoip2.Open(geodb)
if err != nil {
log.Fatal(err)
}
geoip = GeoIP{db: db}
geoip.db = db
}
_, err = os.Stat(flags)
if err != nil && os.IsNotExist(err) {
log.Println("flags 文件不存在,请自行下载 flags.json并保存在", flags)
os.Exit(1)
} else {
data, err := ioutil.ReadFile(flags)
if err != nil {
log.Fatal(err)
return
}
var countryEmojiList = make([]CountryEmoji, 0)
err = json.Unmarshal(data, &countryEmojiList)
if err != nil {
log.Fatalln(err.Error())
return
}
emojiMap := make(map[string]string)
for _, i := range countryEmojiList {
emojiMap[i.Code] = i.Emoji
}
geoip.emojiMap = emojiMap
}
return
}
@@ -42,10 +84,22 @@ func (g GeoIP) Find(ipORdomain string) (ip, country string, err error) {
if err != nil {
return "", "", err
}
ipData := net.ParseIP(ips[0].String())
record, err := g.db.City(ipData)
ip = ips[0].String()
var record *geoip2.City
record, err = g.db.City(ips[0])
if err != nil {
return "", "", err
return
}
return ips[0].String(), record.Country.IsoCode, nil
countryIsoCode := record.Country.IsoCode
if countryIsoCode == "" {
country = fmt.Sprintf("🏁 ZZ")
}
emoji, found := g.emojiMap[countryIsoCode]
if found {
country = fmt.Sprintf("%v %v", emoji, countryIsoCode)
} else {
country = fmt.Sprintf("🏁 ZZ")
}
return
}

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
}