Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Sep 9, 2024
1 parent 952e193 commit ad0238f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
28 changes: 14 additions & 14 deletions component/mmdb/mmdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ const (
var (
//go:embed Country.mmdb
EmbedMMDB []byte
IPreader IPReader
IPonce sync.Once
ipReader IPReader
ipOnce sync.Once
)

func LoadFromBytes(buffer []byte) {
IPonce.Do(func() {
ipOnce.Do(func() {
mmdb, err := maxminddb.FromBytes(buffer)
if err != nil {
log.Fatalln("Can't load mmdb: %s", err.Error())
}
IPreader = IPReader{Reader: mmdb}
ipReader = IPReader{Reader: mmdb}
switch mmdb.Metadata.DatabaseType {
case "sing-geoip":
IPreader.databaseType = typeSing
ipReader.databaseType = typeSing
case "Meta-geoip0":
IPreader.databaseType = typeMetaV0
ipReader.databaseType = typeMetaV0
default:
IPreader.databaseType = typeMaxmind
ipReader.databaseType = typeMaxmind
}
})
}
Expand All @@ -64,25 +64,25 @@ func Verify() bool {
}

func IPInstance() IPReader {
IPonce.Do(func() {
ipOnce.Do(func() {
mmdb, err := getMmdbReader()
if err != nil {
log.Fatalln("Can't load MMDB: %s", err.Error())
}
IPreader = IPReader{Reader: mmdb}
ipReader = IPReader{Reader: mmdb}
switch mmdb.Metadata.DatabaseType {
case "sing-geoip":
IPreader.databaseType = typeSing
ipReader.databaseType = typeSing
case "Meta-geoip0":
IPreader.databaseType = typeMetaV0
ipReader.databaseType = typeMetaV0
default:
IPreader.databaseType = typeMaxmind
ipReader.databaseType = typeMaxmind
}
})

return IPreader
return ipReader
}

func ReloadIP() {
mihomoOnce.Reset(&IPonce)
mihomoOnce.Reset(&ipOnce)
}
2 changes: 1 addition & 1 deletion config/initial.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func downloadMMDB(path string) (err error) {
resp, err := http.Get("https://cdn.jsdelivr.net/gh/Dreamacro/maxmind-geoip@release/Country.mmdb")
resp, err := http.Get("https://cdn.jsdelivr.net/gh/Loyalsoldier/geoip@release/Country.mmdb")
if err != nil {
return
}
Expand Down
7 changes: 1 addition & 6 deletions tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
N "github.com/metacubex/mihomo/common/net"
"github.com/metacubex/mihomo/common/utils"
"github.com/metacubex/mihomo/component/inner_dialer"
"github.com/metacubex/mihomo/component/mmdb"
"github.com/metacubex/mihomo/component/nat"
P "github.com/metacubex/mihomo/component/process"
"github.com/metacubex/mihomo/component/resolver"
Expand Down Expand Up @@ -536,11 +535,7 @@ func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) {
if err != nil {
log.Infoln("[DNS] resolve %s error: %s", metadata.Host, err.Error())
} else {
if record := mmdb.IPInstance().LookupCode(ip.AsSlice()); len(record) > 0 {
log.Infoln("[DNS] %s --> %s [GEO=%s]", metadata.Host, ip.String(), record)
} else {
log.Infoln("[DNS] %s --> %s", metadata.Host, ip.String())
}
log.Infoln("[DNS] %s --> %s", metadata.Host, ip.String())

metadata.DstIP = ip
}
Expand Down

0 comments on commit ad0238f

Please sign in to comment.