2022-03-15 01:30:17 +08:00
|
|
|
package mmdb
|
|
|
|
|
|
|
|
import (
|
2023-04-10 21:03:31 +08:00
|
|
|
"context"
|
2023-01-09 21:07:31 +08:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2022-03-15 01:30:17 +08:00
|
|
|
"sync"
|
2023-04-10 21:03:31 +08:00
|
|
|
"time"
|
2022-03-15 01:30:17 +08:00
|
|
|
|
2024-03-05 10:57:25 +08:00
|
|
|
mihomoOnce "github.com/metacubex/mihomo/common/once"
|
2023-11-03 21:01:45 +08:00
|
|
|
mihomoHttp "github.com/metacubex/mihomo/component/http"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
"github.com/metacubex/mihomo/log"
|
2023-04-10 21:03:31 +08:00
|
|
|
|
2023-07-14 22:28:24 +08:00
|
|
|
"github.com/oschwald/maxminddb-golang"
|
|
|
|
)
|
|
|
|
|
|
|
|
type databaseType = uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
typeMaxmind databaseType = iota
|
|
|
|
typeSing
|
2023-07-17 10:33:20 +08:00
|
|
|
typeMetaV0
|
2022-03-15 01:30:17 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-03-12 03:14:25 +08:00
|
|
|
IPreader IPReader
|
|
|
|
ASNreader ASNReader
|
|
|
|
IPonce sync.Once
|
|
|
|
ASNonce sync.Once
|
2022-03-15 01:30:17 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func LoadFromBytes(buffer []byte) {
|
2024-03-12 03:14:25 +08:00
|
|
|
IPonce.Do(func() {
|
2023-07-14 22:28:24 +08:00
|
|
|
mmdb, err := maxminddb.FromBytes(buffer)
|
2022-03-15 01:30:17 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Can't load mmdb: %s", err.Error())
|
|
|
|
}
|
2024-03-12 03:14:25 +08:00
|
|
|
IPreader = IPReader{Reader: mmdb}
|
2023-07-17 10:33:20 +08:00
|
|
|
switch mmdb.Metadata.DatabaseType {
|
|
|
|
case "sing-geoip":
|
2024-03-12 03:14:25 +08:00
|
|
|
IPreader.databaseType = typeSing
|
2023-07-17 10:33:20 +08:00
|
|
|
case "Meta-geoip0":
|
2024-03-12 03:14:25 +08:00
|
|
|
IPreader.databaseType = typeMetaV0
|
2023-07-17 10:33:20 +08:00
|
|
|
default:
|
2024-03-12 03:14:25 +08:00
|
|
|
IPreader.databaseType = typeMaxmind
|
2023-07-14 22:28:24 +08:00
|
|
|
}
|
2022-03-15 01:30:17 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-12 03:14:25 +08:00
|
|
|
func Verify(path string) bool {
|
|
|
|
instance, err := maxminddb.Open(path)
|
2022-03-15 01:30:17 +08:00
|
|
|
if err == nil {
|
|
|
|
instance.Close()
|
|
|
|
}
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2024-03-12 03:14:25 +08:00
|
|
|
func IPInstance() IPReader {
|
|
|
|
IPonce.Do(func() {
|
2023-07-17 10:33:20 +08:00
|
|
|
mmdbPath := C.Path.MMDB()
|
2024-02-21 21:56:20 +08:00
|
|
|
log.Infoln("Load MMDB file: %s", mmdbPath)
|
2023-07-17 10:33:20 +08:00
|
|
|
mmdb, err := maxminddb.Open(mmdbPath)
|
2022-03-15 01:30:17 +08:00
|
|
|
if err != nil {
|
2023-07-17 10:33:20 +08:00
|
|
|
log.Fatalln("Can't load MMDB: %s", err.Error())
|
2022-03-15 01:30:17 +08:00
|
|
|
}
|
2024-03-12 03:14:25 +08:00
|
|
|
IPreader = IPReader{Reader: mmdb}
|
2023-07-17 10:33:20 +08:00
|
|
|
switch mmdb.Metadata.DatabaseType {
|
|
|
|
case "sing-geoip":
|
2024-03-12 03:14:25 +08:00
|
|
|
IPreader.databaseType = typeSing
|
2023-07-17 10:33:20 +08:00
|
|
|
case "Meta-geoip0":
|
2024-03-12 03:14:25 +08:00
|
|
|
IPreader.databaseType = typeMetaV0
|
2023-07-17 10:33:20 +08:00
|
|
|
default:
|
2024-03-12 03:14:25 +08:00
|
|
|
IPreader.databaseType = typeMaxmind
|
2023-07-14 22:28:24 +08:00
|
|
|
}
|
2022-03-15 01:30:17 +08:00
|
|
|
})
|
|
|
|
|
2024-03-12 03:14:25 +08:00
|
|
|
return IPreader
|
2022-03-15 01:30:17 +08:00
|
|
|
}
|
2023-01-09 21:07:31 +08:00
|
|
|
|
|
|
|
func DownloadMMDB(path string) (err error) {
|
2023-04-10 21:03:31 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*90)
|
|
|
|
defer cancel()
|
2023-11-12 02:44:55 +08:00
|
|
|
resp, err := mihomoHttp.HttpRequest(ctx, C.MmdbUrl, http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil)
|
2023-01-09 21:07:31 +08: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
|
|
|
|
}
|
2024-02-21 21:56:20 +08:00
|
|
|
|
2024-03-12 03:14:25 +08:00
|
|
|
func ASNInstance() ASNReader {
|
|
|
|
ASNonce.Do(func() {
|
|
|
|
ASNPath := C.Path.ASN()
|
|
|
|
log.Infoln("Load ASN file: %s", ASNPath)
|
|
|
|
asn, err := maxminddb.Open(ASNPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Can't load ASN: %s", err.Error())
|
|
|
|
}
|
|
|
|
ASNreader = ASNReader{Reader: asn}
|
|
|
|
})
|
|
|
|
|
|
|
|
return ASNreader
|
|
|
|
}
|
|
|
|
|
|
|
|
func DownloadASN(path string) (err error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*90)
|
|
|
|
defer cancel()
|
|
|
|
resp, err := mihomoHttp.HttpRequest(ctx, C.ASNUrl, http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReloadIP() {
|
|
|
|
mihomoOnce.Reset(&IPonce)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReloadASN() {
|
|
|
|
mihomoOnce.Reset(&ASNonce)
|
2024-02-21 21:56:20 +08:00
|
|
|
}
|