Files
v2ray_simple/netLayer/geoip.go
e1732a364fed f28f0d0bee 修订代码, 默认loglevel 改为 Log_info.
对一般用户而言,还是需要使用Info等级 来了解一下 一般的 日志情况,等到使用熟练之后,且确认运行没有错误后, 可以自行调为 warning 来提升性能

发现 bubble包 还自己引入了 命令行参数,这十分不可取,所以我们还是直接使用其代码。

将其它包中 的 命令行参数 统一 移动 到 cmd/verysimple 中;tls lazy 特性因为还在 调试阶段,所以 命令行参数 仍然放到 v2ray_simple 包中。
2022-04-26 13:22:18 +08:00

81 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package netLayer
import (
_ "embed"
"log"
"net"
"os"
"github.com/e1732a364fed/v2ray_simple/utils"
"github.com/oschwald/maxminddb-golang"
"go.uber.org/zap"
)
var (
the_geoipdb *maxminddb.Reader
embedGeoip bool
GeoipFileName string
)
func HasEmbedGeoip() bool {
return embedGeoip
}
func loadMaxmindGeoipBytes(bs []byte) {
db, err := maxminddb.FromBytes(bs)
if err != nil {
log.Println("loadMaxmindGeoipBytes err,", err)
return
}
the_geoipdb = db
}
//将一个外部的文件加载为我们默认的 geoip文件;若fn=="",则会自动使用 GeoipFileName 的值
func LoadMaxmindGeoipFile(fn string) {
if fn == "" {
fn = GeoipFileName
}
if fn == "" { //因为 GeoipFileName 是公有变量,所以可能会被设成""
return
}
bs, e := os.ReadFile(fn)
if e != nil {
log.Println("LoadMaxmindGeoipFile err", e)
return
}
loadMaxmindGeoipBytes(bs)
}
//使用默认的 geoip文件会调用 GetIP_ISO_byReader
func GetIP_ISO(ip net.IP) string {
if the_geoipdb == nil {
return ""
}
return GetIP_ISO_byReader(the_geoipdb, ip)
}
//返回 iso 3166 字符串, 见 https://dev.maxmind.com/geoip/legacy/codes?lang=en ,大写,两字节
func GetIP_ISO_byReader(db *maxminddb.Reader, ip net.IP) string {
var record struct {
Country struct {
ISOCode string `maxminddb:"iso_code"`
} `maxminddb:"country"`
}
err := db.Lookup(ip, &record)
if err != nil {
if utils.ZapLogger != nil {
if ce := utils.CanLogErr("GetIP_ISO_byReader db.Lookup err"); ce != nil {
ce.Write(zap.Error(err))
}
}
return ""
}
return record.Country.ISOCode
}