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.
49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
package ipgeo
|
||
|
||
import (
|
||
"fmt"
|
||
"io"
|
||
"log"
|
||
"net/http"
|
||
"time"
|
||
|
||
"github.com/tidwall/gjson"
|
||
|
||
"github.com/nxtrace/NTrace-core/util"
|
||
)
|
||
|
||
func IPSB(ip string, timeout time.Duration, _ string, _ bool) (*IPGeoData, error) {
|
||
url := token.BaseOrDefault("https://api.ip.sb/geoip/") + ip
|
||
client := util.NewGeoHTTPClient(timeout)
|
||
req, err := http.NewRequest("GET", url, nil)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("ip.sb: failed to create request: %w", err)
|
||
}
|
||
// 设置 UA,ip.sb 默认禁止 go-client User-Agent 的 api 请求
|
||
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:100.0) Gecko/20100101 Firefox/100.0")
|
||
content, err := client.Do(req)
|
||
if err != nil {
|
||
log.Println("api.ip.sb 请求超时(2s),请切换其他API使用")
|
||
return nil, err
|
||
}
|
||
defer content.Body.Close()
|
||
body, err := io.ReadAll(content.Body)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("ip.sb: failed to read response: %w", err)
|
||
}
|
||
res := gjson.ParseBytes(body)
|
||
|
||
if res.Get("country").String() == "" {
|
||
// 什么都拿不到,证明被Cloudflare风控了
|
||
return nil, fmt.Errorf("ip.sb: empty response, possibly blocked by Cloudflare")
|
||
}
|
||
|
||
return &IPGeoData{
|
||
Asnumber: res.Get("asn").String(),
|
||
Country: res.Get("country").String(),
|
||
City: res.Get("city").String(),
|
||
Prov: res.Get("region").String(),
|
||
Owner: res.Get("isp").String(),
|
||
}, nil
|
||
}
|