2021-08-02 12:01:25 +08:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2023-05-19 15:40:45 +08:00
|
|
|
"log"
|
2024-12-30 14:04:06 +08:00
|
|
|
"net"
|
2022-05-09 14:48:28 +08:00
|
|
|
|
2023-05-19 17:34:57 +08:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2021-08-02 12:01:25 +08:00
|
|
|
"github.com/zu1k/nali/pkg/cdn"
|
|
|
|
"github.com/zu1k/nali/pkg/dbif"
|
2021-08-11 17:52:26 +08:00
|
|
|
"github.com/zu1k/nali/pkg/geoip"
|
2021-08-02 12:01:25 +08:00
|
|
|
"github.com/zu1k/nali/pkg/qqwry"
|
|
|
|
"github.com/zu1k/nali/pkg/zxipv6wry"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetDB(typ dbif.QueryType) (db dbif.DB) {
|
2022-05-09 14:48:28 +08:00
|
|
|
if db, found := dbTypeCache[typ]; found {
|
2021-08-02 12:01:25 +08:00
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
2022-05-09 14:48:28 +08:00
|
|
|
lang := viper.GetString("selected.lang")
|
|
|
|
if lang == "" {
|
|
|
|
lang = "zh-CN"
|
|
|
|
}
|
2022-03-02 12:34:11 +08:00
|
|
|
|
2022-05-09 14:48:28 +08:00
|
|
|
var err error
|
2021-08-02 12:01:25 +08:00
|
|
|
switch typ {
|
|
|
|
case dbif.TypeIPv4:
|
2022-05-09 14:48:28 +08:00
|
|
|
selected := viper.GetString("selected.ipv4")
|
|
|
|
if selected != "" {
|
|
|
|
db = getDbByName(selected).get()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if lang == "zh-CN" {
|
|
|
|
db, err = qqwry.NewQQwry(getDbByName("qqwry").File)
|
2021-08-11 10:02:54 +08:00
|
|
|
} else {
|
2022-05-09 14:48:28 +08:00
|
|
|
db, err = geoip.NewGeoIP(getDbByName("geoip").File)
|
2021-08-11 10:02:54 +08:00
|
|
|
}
|
2021-08-02 12:01:25 +08:00
|
|
|
case dbif.TypeIPv6:
|
2022-05-09 14:48:28 +08:00
|
|
|
selected := viper.GetString("selected.ipv6")
|
|
|
|
if selected != "" {
|
|
|
|
db = getDbByName(selected).get()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if lang == "zh-CN" {
|
|
|
|
db, err = zxipv6wry.NewZXwry(getDbByName("zxipv6wry").File)
|
2021-08-11 10:02:54 +08:00
|
|
|
} else {
|
2022-05-09 14:48:28 +08:00
|
|
|
db, err = geoip.NewGeoIP(getDbByName("geoip").File)
|
2021-08-11 10:02:54 +08:00
|
|
|
}
|
2021-08-02 12:01:25 +08:00
|
|
|
case dbif.TypeDomain:
|
2022-05-09 14:48:28 +08:00
|
|
|
selected := viper.GetString("selected.cdn")
|
|
|
|
if selected != "" {
|
|
|
|
db = getDbByName(selected).get()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err = cdn.NewCDN(getDbByName("cdn").File)
|
2021-08-02 12:01:25 +08:00
|
|
|
default:
|
|
|
|
panic("Query type not supported!")
|
|
|
|
}
|
2021-08-11 10:02:54 +08:00
|
|
|
|
2022-03-02 12:34:11 +08:00
|
|
|
if err != nil || db == nil {
|
|
|
|
log.Fatalln("Database init failed:", err)
|
|
|
|
}
|
|
|
|
|
2022-05-09 14:48:28 +08:00
|
|
|
dbTypeCache[typ] = db
|
2021-08-02 12:01:25 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-20 17:53:43 +08:00
|
|
|
func Find(typ dbif.QueryType, query string) *Result {
|
2022-07-28 09:53:59 +08:00
|
|
|
if result, found := queryCache.Load(query); found {
|
2023-05-20 17:53:43 +08:00
|
|
|
return result.(*Result)
|
2021-08-11 10:02:54 +08:00
|
|
|
}
|
2024-12-30 14:04:06 +08:00
|
|
|
// Convert NAT64 64:ff9b::/96 to IPv4
|
|
|
|
if typ == dbif.TypeIPv6 {
|
|
|
|
ip := net.ParseIP(query)
|
|
|
|
if ip != nil {
|
|
|
|
_, NAT64, _ := net.ParseCIDR("64:ff9b::/96")
|
|
|
|
if NAT64.Contains(ip) {
|
|
|
|
ip4 := make(net.IP, 4)
|
|
|
|
copy(ip4, ip[12:16])
|
|
|
|
query = ip4.String()
|
|
|
|
typ = dbif.TypeIPv4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-20 17:53:43 +08:00
|
|
|
db := GetDB(typ)
|
|
|
|
result, err := db.Find(query)
|
2021-08-11 10:02:54 +08:00
|
|
|
if err != nil {
|
2023-05-19 15:40:45 +08:00
|
|
|
return nil
|
2021-08-11 10:02:54 +08:00
|
|
|
}
|
2023-05-20 17:53:43 +08:00
|
|
|
res := &Result{db.Name(), result}
|
2023-05-19 15:40:45 +08:00
|
|
|
queryCache.Store(query, res)
|
|
|
|
return res
|
2021-08-11 10:02:54 +08:00
|
|
|
}
|