Skip to content

Commit

Permalink
upgrade go1.16beta1 to use go:embed
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Jan 5, 2021
1 parent fdf4f82 commit 06dd693
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.15.x
stable: false
go-version: '1.16.0-beta1'

- name: Check out code into the Go module directory
uses: actions/checkout@v2
Expand Down
2 changes: 2 additions & 0 deletions build.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
cd %~dp0
rmdir /S /Q bin
mkdir bin
set CGO_ENABLED=0
set GOARCH=amd64
set GOOS=linux
Expand Down
Binary file added component/mmdb/Country.mmdb
Binary file not shown.
19 changes: 17 additions & 2 deletions component/mmdb/mmdb.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mmdb

import (
_ "embed"
"sync"

C "github.com/Dreamacro/clash/constant"
Expand All @@ -9,6 +10,9 @@ import (
"github.com/oschwald/geoip2-golang"
)

//go:embed Country.mmdb
var EmbedMMDB []byte

var mmdb *geoip2.Reader
var once sync.Once

Expand All @@ -22,8 +26,18 @@ func LoadFromBytes(buffer []byte) {
})
}

func getInstance() (instance *geoip2.Reader, err error) {
if path := C.Path.MMDB(); path == "embed" {
instance, err = geoip2.FromBytes(EmbedMMDB)
} else {
instance, err = geoip2.Open(C.Path.MMDB())
}

return
}

func Verify() bool {
instance, err := geoip2.Open(C.Path.MMDB())
instance, err := getInstance()
if err == nil {
instance.Close()
}
Expand All @@ -33,7 +47,8 @@ func Verify() bool {
func Instance() *geoip2.Reader {
once.Do(func() {
var err error
mmdb, err = geoip2.Open(C.Path.MMDB())
mmdb, err = getInstance()

if err != nil {
log.Fatalln("Can't load mmdb: %s", err.Error())
}
Expand Down
29 changes: 20 additions & 9 deletions config/initial.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,32 @@ func downloadMMDB(path string) (err error) {
}

func initMMDB() error {
C.SetExtraMMDB(true)
if _, err := os.Stat(C.Path.MMDB()); os.IsNotExist(err) {
log.Infoln("Can't find MMDB, start download")
if err := downloadMMDB(C.Path.MMDB()); err != nil {
return fmt.Errorf("can't download MMDB: %s", err.Error())
if len(mmdb.EmbedMMDB) > 0 {
log.Infoln("Can't find MMDB file, use embed MMDB")
C.SetExtraMMDB(false)
} else {
log.Infoln("Can't find MMDB, start download")
if err := downloadMMDB(C.Path.MMDB()); err != nil {
return fmt.Errorf("can't download MMDB: %s", err.Error())
}
}
}

if !mmdb.Verify() {
log.Warnln("MMDB invalid, remove and download")
if err := os.Remove(C.Path.MMDB()); err != nil {
return fmt.Errorf("can't remove invalid MMDB: %s", err.Error())
}
if len(mmdb.EmbedMMDB) > 0 {
log.Warnln("MMDB invalid, use embed MMDB")
C.SetExtraMMDB(false)
} else {
log.Warnln("MMDB invalid, remove and download")
if err := os.Remove(C.Path.MMDB()); err != nil {
return fmt.Errorf("can't remove invalid MMDB: %s", err.Error())
}

if err := downloadMMDB(C.Path.MMDB()); err != nil {
return fmt.Errorf("can't download MMDB: %s", err.Error())
if err := downloadMMDB(C.Path.MMDB()); err != nil {
return fmt.Errorf("can't download MMDB: %s", err.Error())
}
}
}

Expand Down
11 changes: 10 additions & 1 deletion constant/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var Path *path
type path struct {
homeDir string
configFile string
extraMMDB bool
}

func init() {
Expand All @@ -36,6 +37,10 @@ func SetConfig(file string) {
Path.configFile = file
}

func SetExtraMMDB(extraMMDB bool) {
Path.extraMMDB = extraMMDB
}

func (p *path) HomeDir() string {
return p.homeDir
}
Expand All @@ -54,5 +59,9 @@ func (p *path) Resolve(path string) string {
}

func (p *path) MMDB() string {
return P.Join(p.homeDir, "Country.mmdb")
if p.extraMMDB {
return P.Join(p.homeDir, "Country.mmdb")
} else {
return "embed"
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/Dreamacro/clash

go 1.15
go 1.16

require (
github.com/Dreamacro/go-shadowsocks2 v0.1.6
Expand Down

0 comments on commit 06dd693

Please sign in to comment.