mirror of
https://github.com/nxtrace/NTrace-core.git
synced 2026-12-05 18:57:32 +00:00
- Enhanced error handling in `mtr_ui.go` to log warnings when raw mode cannot be enabled. - Updated `geofeed.go` to return a more informative error instead of panicking when geofeed data cannot be loaded. - Refined error handling in `ptr.go` to return an error if `ptrPath` is not configured. - Improved error handling in `fast_trace.go` for network interface address retrieval. - Added error checks in `chunzhen.go` and `ipapicom.go` for HTTP request creation and response reading. - Enhanced error handling in `ipdbone.go` to return descriptive errors for authentication failures. - Updated `ipfilter.go` to ensure proper IP parsing before filtering. - Improved error handling in `ipinfoLocal.go` for database access and record parsing. - Enhanced error handling in `ipsb.go` for HTTP request creation and response reading. - Added checks for closed channels in `leo.go` to prevent panics. - Improved panic recovery in `ws_handler.go` to log errors in the write loop. - Added read deadline to WebSocket connections in `ws_handler.go` to prevent blocking. - Enhanced configuration checks in `globalping.go` to avoid nil pointer dereferences. - Improved error logging in `icmp_ipv4.go` and `icmp_ipv6.go` for send errors. - Enhanced error handling in `icmp_darwin.go`, `icmp_unix.go`, and `icmp_windows.go` for checksum setting. - Improved error handling in TCP and UDP implementations across various platforms for checksum settings. - Enhanced error handling in `tracemap.go` for proxy configuration. - Improved type assertions in `util.go` for DNS cache lookups.
106 lines
2.1 KiB
Go
106 lines
2.1 KiB
Go
package dn42
|
|
|
|
import (
|
|
"encoding/csv"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"sort"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type GeoFeedRow struct {
|
|
IPNet *net.IPNet
|
|
CIDR string
|
|
LtdCode string
|
|
ISO3166 string
|
|
City string
|
|
ASN string
|
|
IPWhois string
|
|
}
|
|
|
|
func GetGeoFeed(ip string) (GeoFeedRow, bool) {
|
|
rows, err := ReadGeoFeed()
|
|
if err != nil {
|
|
// 无法加载 geofeed 数据,返回未找到
|
|
return GeoFeedRow{}, false
|
|
}
|
|
|
|
row, find := FindGeoFeedRow(ip, rows)
|
|
return row, find
|
|
|
|
}
|
|
|
|
func ReadGeoFeed() ([]GeoFeedRow, error) {
|
|
path := viper.GetString("geoFeedPath")
|
|
if path == "" {
|
|
return nil, fmt.Errorf("geoFeedPath not configured")
|
|
}
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
|
|
r := csv.NewReader(f)
|
|
rows, err := r.ReadAll()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 将 CSV 中的每一行转换为 GeoFeedRow 类型,并保存到 rowsSlice 中
|
|
var rowsSlice []GeoFeedRow
|
|
for _, row := range rows {
|
|
cidr := row[0] // 假设第一列是 CIDR 字段
|
|
_, ipnet, err := net.ParseCIDR(cidr)
|
|
if err != nil {
|
|
// 如果解析 CIDR 失败,跳过这一行
|
|
continue
|
|
}
|
|
if len(row) == 4 {
|
|
rowsSlice = append(rowsSlice, GeoFeedRow{
|
|
IPNet: ipnet,
|
|
CIDR: cidr,
|
|
LtdCode: row[1],
|
|
ISO3166: row[2],
|
|
City: row[3],
|
|
})
|
|
} else if len(row) >= 6 {
|
|
rowsSlice = append(rowsSlice, GeoFeedRow{
|
|
IPNet: ipnet,
|
|
CIDR: cidr,
|
|
LtdCode: row[1],
|
|
ISO3166: row[2],
|
|
City: row[3],
|
|
ASN: row[4],
|
|
IPWhois: row[5],
|
|
})
|
|
}
|
|
|
|
}
|
|
// 根据 CIDR 范围从小到大排序,方便后面查找
|
|
sort.Slice(rowsSlice, func(i, j int) bool {
|
|
return rowsSlice[i].IPNet.Mask.String() > rowsSlice[j].IPNet.Mask.String()
|
|
})
|
|
|
|
return rowsSlice, nil
|
|
}
|
|
|
|
func FindGeoFeedRow(ipStr string, rows []GeoFeedRow) (GeoFeedRow, bool) {
|
|
ip := net.ParseIP(ipStr)
|
|
if ip == nil {
|
|
// 如果传入的 IP 无效,直接返回
|
|
return GeoFeedRow{}, false
|
|
}
|
|
|
|
// 遍历每个 CIDR 范围,找到第一个包含传入的 IP 的 CIDR
|
|
for _, row := range rows {
|
|
if row.IPNet.Contains(ip) {
|
|
return row, true
|
|
}
|
|
}
|
|
|
|
return GeoFeedRow{}, false
|
|
}
|