mirror of
https://github.com/nxtrace/NTrace-core.git
synced 2026-12-01 17:02:33 +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.9 KiB
Go
106 lines
2.9 KiB
Go
package ipgeo
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/oschwald/maxminddb-golang"
|
|
|
|
"github.com/nxtrace/NTrace-core/util"
|
|
)
|
|
|
|
const (
|
|
ipinfoDataBaseFilename = "ipinfoLocal.mmdb"
|
|
)
|
|
|
|
// Cache the path of the ipinfoLocal.mmdb file
|
|
var ipinfoDataBasePath = ""
|
|
|
|
// We will try to get the path of the ipinfoLocal.mmdb file in the following order:
|
|
// 1. Use the value of the environment variable NEXTTRACE_IPINFOLOCALPATH
|
|
// 2. Search in the current folder and the executable folder
|
|
// 3. Search in /usr/local/share/nexttrace/ and /usr/share/nexttrace/ (for Unix/Linux)
|
|
// If the file is found, the path will be stored in the ipinfoDataBasePath variable
|
|
func getIPInfoLocalPath() error {
|
|
if ipinfoDataBasePath != "" {
|
|
return nil
|
|
}
|
|
// NEXTTRACE_IPINFOLOCALPATH
|
|
path := util.GetEnvDefault("NEXTTRACE_IPINFOLOCALPATH", "")
|
|
if path != "" {
|
|
if _, err := os.Stat(path); err == nil {
|
|
ipinfoDataBasePath = path
|
|
return nil
|
|
}
|
|
return errors.New("NEXTTRACE_IPINFOLOCALPATH is set but the file does not exist")
|
|
}
|
|
var folders []string
|
|
// current folder
|
|
if cur, err := os.Getwd(); err == nil {
|
|
folders = append(folders, cur+string(filepath.Separator))
|
|
}
|
|
// exeutable folder
|
|
if exe, err := os.Executable(); err == nil {
|
|
folders = append(folders, filepath.Dir(exe)+string(filepath.Separator))
|
|
}
|
|
if runtime.GOOS != "windows" {
|
|
folders = append(folders, "/usr/local/share/nexttrace/")
|
|
folders = append(folders, "/usr/share/nexttrace/")
|
|
}
|
|
for _, folder := range folders {
|
|
if _, err := os.Stat(folder + ipinfoDataBaseFilename); err == nil {
|
|
ipinfoDataBasePath = folder + ipinfoDataBaseFilename
|
|
return nil
|
|
}
|
|
}
|
|
return errors.New("no ipinfoLocal.mmdb found")
|
|
}
|
|
|
|
func IPInfoLocal(ip string, _ time.Duration, _ string, _ bool) (*IPGeoData, error) {
|
|
if err := getIPInfoLocalPath(); err != nil {
|
|
return nil, fmt.Errorf("ipinfoLocal: cannot find ipinfoLocal.mmdb: %w", err)
|
|
}
|
|
region, err := maxminddb.Open(ipinfoDataBasePath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ipinfoLocal: cannot open %s: %w", ipinfoDataBasePath, err)
|
|
}
|
|
defer func(region *maxminddb.Reader) {
|
|
_ = region.Close()
|
|
}(region)
|
|
var record interface{}
|
|
searchErr := region.Lookup(net.ParseIP(ip), &record)
|
|
if searchErr != nil {
|
|
return &IPGeoData{}, errors.New("no results")
|
|
}
|
|
recordMap, ok := record.(map[string]interface{})
|
|
if !ok {
|
|
return &IPGeoData{}, errors.New("ipinfoLocal: unexpected record format")
|
|
}
|
|
countryName, _ := recordMap["country_name"].(string)
|
|
countryCode, _ := recordMap["country"].(string)
|
|
prov := ""
|
|
if countryCode == "HK" {
|
|
countryName = "China"
|
|
prov = "Hong Kong"
|
|
}
|
|
if countryCode == "TW" {
|
|
countryName = "China"
|
|
prov = "Taiwan"
|
|
}
|
|
asnStr, _ := recordMap["asn"].(string)
|
|
asName, _ := recordMap["as_name"].(string)
|
|
return &IPGeoData{
|
|
Asnumber: strings.TrimPrefix(asnStr, "AS"),
|
|
Country: countryName,
|
|
City: "",
|
|
Prov: prov,
|
|
Owner: asName,
|
|
}, nil
|
|
}
|