2022-03-14 17:30:17 +00:00
|
|
|
package mmdb
|
|
|
|
|
|
|
|
import (
|
2023-04-10 13:03:31 +00:00
|
|
|
"context"
|
2023-01-09 13:07:31 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2022-03-14 17:30:17 +00:00
|
|
|
"sync"
|
2023-04-10 13:03:31 +00:00
|
|
|
"time"
|
2022-03-14 17:30:17 +00:00
|
|
|
|
2023-04-10 13:03:31 +00:00
|
|
|
clashHttp "github.com/Dreamacro/clash/component/http"
|
2022-03-14 17:30:17 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/log"
|
2023-04-10 13:03:31 +00:00
|
|
|
|
2023-07-14 14:28:24 +00:00
|
|
|
"github.com/oschwald/maxminddb-golang"
|
|
|
|
)
|
|
|
|
|
|
|
|
type databaseType = uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
typeMaxmind databaseType = iota
|
|
|
|
typeSing
|
2023-07-17 02:33:20 +00:00
|
|
|
typeMetaV0
|
2022-03-14 17:30:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-07-14 14:28:24 +00:00
|
|
|
reader Reader
|
|
|
|
once sync.Once
|
2022-03-14 17:30:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func LoadFromBytes(buffer []byte) {
|
|
|
|
once.Do(func() {
|
2023-07-14 14:28:24 +00:00
|
|
|
mmdb, err := maxminddb.FromBytes(buffer)
|
2022-03-14 17:30:17 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Can't load mmdb: %s", err.Error())
|
|
|
|
}
|
2023-07-14 14:28:24 +00:00
|
|
|
reader = Reader{Reader: mmdb}
|
2023-07-17 02:33:20 +00:00
|
|
|
switch mmdb.Metadata.DatabaseType {
|
|
|
|
case "sing-geoip":
|
2023-07-14 14:28:24 +00:00
|
|
|
reader.databaseType = typeSing
|
2023-07-17 02:33:20 +00:00
|
|
|
case "Meta-geoip0":
|
|
|
|
reader.databaseType = typeMetaV0
|
|
|
|
default:
|
2023-07-14 14:28:24 +00:00
|
|
|
reader.databaseType = typeMaxmind
|
|
|
|
}
|
2022-03-14 17:30:17 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Verify() bool {
|
2023-07-14 14:28:24 +00:00
|
|
|
instance, err := maxminddb.Open(C.Path.MMDB())
|
2022-03-14 17:30:17 +00:00
|
|
|
if err == nil {
|
|
|
|
instance.Close()
|
|
|
|
}
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2023-07-14 14:28:24 +00:00
|
|
|
func Instance() Reader {
|
2022-03-14 17:30:17 +00:00
|
|
|
once.Do(func() {
|
2023-07-17 02:33:20 +00:00
|
|
|
mmdbPath := C.Path.MMDB()
|
|
|
|
log.Debugln("Load MMDB file: %s", mmdbPath)
|
|
|
|
mmdb, err := maxminddb.Open(mmdbPath)
|
2022-03-14 17:30:17 +00:00
|
|
|
if err != nil {
|
2023-07-17 02:33:20 +00:00
|
|
|
log.Fatalln("Can't load MMDB: %s", err.Error())
|
2022-03-14 17:30:17 +00:00
|
|
|
}
|
2023-07-14 14:28:24 +00:00
|
|
|
reader = Reader{Reader: mmdb}
|
2023-07-17 02:33:20 +00:00
|
|
|
switch mmdb.Metadata.DatabaseType {
|
|
|
|
case "sing-geoip":
|
2023-07-14 14:28:24 +00:00
|
|
|
reader.databaseType = typeSing
|
2023-07-17 02:33:20 +00:00
|
|
|
case "Meta-geoip0":
|
|
|
|
reader.databaseType = typeMetaV0
|
|
|
|
default:
|
2023-07-14 14:28:24 +00:00
|
|
|
reader.databaseType = typeMaxmind
|
|
|
|
}
|
2022-03-14 17:30:17 +00:00
|
|
|
})
|
|
|
|
|
2023-07-14 14:28:24 +00:00
|
|
|
return reader
|
2022-03-14 17:30:17 +00:00
|
|
|
}
|
2023-01-09 13:07:31 +00:00
|
|
|
|
|
|
|
func DownloadMMDB(path string) (err error) {
|
2023-04-10 13:03:31 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*90)
|
|
|
|
defer cancel()
|
|
|
|
resp, err := clashHttp.HttpRequest(ctx, C.MmdbUrl, http.MethodGet, http.Header{"User-Agent": {"clash"}}, nil)
|
2023-01-09 13:07:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, err = io.Copy(f, resp.Body)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|