package server import ( "context" "encoding/json" "errors" "fmt" "log" "net" "net/http" "net/url" "runtime" "strings" "sync" "time" "github.com/gin-gonic/gin" "github.com/nxtrace/NTrace-core/config" "github.com/nxtrace/NTrace-core/internal/service" "github.com/nxtrace/NTrace-core/ipgeo" "github.com/nxtrace/NTrace-core/trace" "github.com/nxtrace/NTrace-core/tracemap" "github.com/nxtrace/NTrace-core/util" "github.com/nxtrace/NTrace-core/wshandle" ) var traceMu = &service.RuntimeMu var nextTraceAPIV3ConnMu sync.Mutex var initDN42Config = sync.OnceFunc(config.InitConfig) var ensureNextTraceAPIV3ConnectionFn = ensureNextTraceAPIV3Connection var traceMapURLFn = tracemap.GetMapUrlWithContext var traceDomainLookupFn = util.DomainLookUpWithContext var withTraceMapScopeFn = func(setup *traceExecution, callback func() (string, error)) (string, error) { return withTraceGeoDNSScope(setup, callback) } type traceExecution struct { Req traceRequest Target string Protocol string DataProvider string Method trace.Method IP net.IP Config trace.Config NeedsNextTraceAPIV3 bool PowProvider string } type traceRequest struct { Target string `json:"target"` Protocol string `json:"protocol"` Port int `json:"port"` Queries int `json:"queries"` MaxHops int `json:"max_hops"` TimeoutMs int `json:"timeout_ms"` PacketSize *int `json:"packet_size"` TOS *int `json:"tos"` ParallelRequests int `json:"parallel_requests"` BeginHop int `json:"begin_hop"` IPv4Only bool `json:"ipv4_only"` IPv6Only bool `json:"ipv6_only"` DataProvider string `json:"data_provider"` PowProvider string `json:"pow_provider"` DotServer string `json:"dot_server"` DisableRDNS bool `json:"disable_rdns"` AlwaysRDNS bool `json:"always_rdns"` DisableMaptrace bool `json:"disable_maptrace"` DisableMPLS bool `json:"disable_mpls"` Language string `json:"language"` DN42 bool `json:"dn42"` SourceAddress string `json:"source_address"` SourcePort int `json:"source_port"` SourceDevice string `json:"source_device"` ICMPMode int `json:"icmp_mode"` PacketInterval int `json:"packet_interval"` TTLInterval int `json:"ttl_interval"` MaxAttempts int `json:"max_attempts"` AlwaysWaitRDNS bool `json:"always_wait_rdns"` Maptrace *bool `json:"maptrace"` // deprecated toggle compatibility LanguageOverride string `json:"language_override"` DataProviderAlias string `json:"data_provider_alias"` Mode string `json:"mode"` IntervalMs int `json:"interval_ms"` HopIntervalMs int `json:"hop_interval_ms"` MaxRounds int `json:"max_rounds"` } type hopAttempt struct { Success bool `json:"success"` IP string `json:"ip,omitempty"` Hostname string `json:"hostname,omitempty"` RTT float64 `json:"rtt_ms,omitempty"` Error string `json:"error,omitempty"` MPLS []string `json:"mpls,omitempty"` Geo *ipgeo.IPGeoData `json:"geo,omitempty"` } type hopResponse struct { TTL int `json:"ttl"` Attempts []hopAttempt `json:"attempts"` } type traceResponse struct { Target string `json:"target"` ResolvedIP string `json:"resolved_ip"` Protocol string `json:"protocol"` DataProvider string `json:"data_provider"` TraceMapURL string `json:"trace_map_url,omitempty"` Language string `json:"language"` Hops []hopResponse `json:"hops"` DurationMs int64 `json:"duration_ms"` StopReason *service.TraceStopReason `json:"stop_reason,omitempty"` } type traceProtocolSelection struct { protocol string method trace.Method dstPort int } func normalizeTraceRequest(req *traceRequest) (int, error) { if req == nil { return http.StatusBadRequest, errors.New("request is required") } req.Mode = strings.ToLower(strings.TrimSpace(req.Mode)) if req.Maptrace != nil { req.DisableMaptrace = !*req.Maptrace } if req.IPv4Only && req.IPv6Only { return http.StatusBadRequest, errors.New("ipv4_only and ipv6_only cannot be true at the same time") } if req.IntervalMs <= 0 { req.IntervalMs = 0 } if req.MaxRounds < 0 { req.MaxRounds = 0 } if req.TOS != nil && (*req.TOS < 0 || *req.TOS > 255) { return http.StatusBadRequest, errors.New("tos must be within range 0-255") } return 0, nil } func resolveTraceProtocol(req traceRequest) (traceProtocolSelection, int, error) { protocol := strings.ToLower(strings.TrimSpace(req.Protocol)) if protocol == "" { protocol = "icmp" } if !contains(supportedProtocols, protocol) { return traceProtocolSelection{}, http.StatusBadRequest, fmt.Errorf("unsupported protocol %q", protocol) } method := trace.ICMPTrace switch protocol { case "udp": method = trace.UDPTrace case "tcp": method = trace.TCPTrace } dstPort := req.Port if dstPort == 0 { switch method { case trace.UDPTrace: dstPort = 33494 case trace.TCPTrace: dstPort = 80 } } return traceProtocolSelection{ protocol: protocol, method: method, dstPort: dstPort, }, 0, nil } func resolveTraceDataProvider(req *traceRequest) (string, bool) { dataProvider := normalizeDataProvider(req.DataProvider, req.DataProviderAlias) if dataProvider == "" { dataProvider = defaults["data_provider"].(string) } if strings.EqualFold(dataProvider, "DN42") { req.DN42 = true } if req.DN42 { dataProvider = "DN42" } needsNextTraceAPIV3 := ipgeo.IsNextTraceAPIProvider(dataProvider) if needsNextTraceAPIV3 && util.EnvDataProvider != "" { dataProvider = normalizeDataProvider(util.EnvDataProvider, "") } if strings.EqualFold(dataProvider, "DN42") { req.DN42 = true initDN42Config() req.DisableMaptrace = true dataProvider = "DN42" } needsNextTraceAPIV3 = ipgeo.IsNextTraceAPIProvider(dataProvider) needsNextTraceAPIV3 = needsNextTraceAPIV3 && !ipgeo.NextTraceAPIV4TokenConfigured() return dataProvider, needsNextTraceAPIV3 } func resolveTraceIPVersion(req traceRequest) string { switch { case req.IPv4Only: return "4" case req.IPv6Only: return "6" default: return "all" } } func prepareTrace(ctx context.Context, req traceRequest) (*traceExecution, int, error) { exec := &traceExecution{ Req: req, } if statusCode, err := normalizeTraceRequest(&exec.Req); err != nil { return nil, statusCode, err } if err := service.ValidateProbeLimits(trace.Config{ MaxHops: exec.Req.MaxHops, BeginHop: exec.Req.BeginHop, NumMeasurements: exec.Req.Queries, ParallelRequests: exec.Req.ParallelRequests, MaxAttempts: exec.Req.MaxAttempts, }); err != nil { return nil, http.StatusBadRequest, err } target, err := normalizeTarget(exec.Req.Target) if err != nil { return nil, http.StatusBadRequest, err } exec.Target = target protocol, statusCode, err := resolveTraceProtocol(exec.Req) if err != nil { return nil, statusCode, err } exec.Protocol = protocol.protocol exec.Method = protocol.method dataProvider, needsNextTraceAPIV3 := resolveTraceDataProvider(&exec.Req) ip, err := traceDomainLookupFn(ctx, target, resolveTraceIPVersion(exec.Req), strings.ToLower(exec.Req.DotServer), true) if err != nil { return nil, http.StatusInternalServerError, err } exec.IP = ip exec.DataProvider = dataProvider exec.PowProvider = strings.TrimSpace(exec.Req.PowProvider) exec.NeedsNextTraceAPIV3 = needsNextTraceAPIV3 exec.Config, err = buildTraceConfig(exec.Req, exec.Method, ip, dataProvider, protocol.dstPort) if err != nil { return nil, http.StatusBadRequest, err } exec.Config.Context = ctx exec.Config, err = trace.NormalizeExplicitSourceConfig(exec.Method, exec.Config) if err != nil { return nil, http.StatusBadRequest, err } return exec, 0, nil } func traceHandler(c *gin.Context) { var req traceRequest c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxTraceRequestBodyBytes) if err := c.ShouldBindJSON(&req); err != nil { var maxBytesErr *http.MaxBytesError if errors.As(err, &maxBytesErr) { c.JSON(http.StatusRequestEntityTooLarge, gin.H{"error": "request payload too large"}) return } c.JSON(400, gin.H{"error": "invalid request payload", "details": err.Error()}) return } setup, statusCode, err := prepareTrace(c.Request.Context(), req) if err != nil { if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { return } if statusCode == 0 { statusCode = 500 } log.Printf("[deploy] prepare trace failed target=%s error=%v", sanitizeLogParam(req.Target), err) c.JSON(statusCode, gin.H{"error": err.Error()}) return } log.Printf("[deploy] trace request target=%s proto=%s provider=%s lang=%s ipv4_only=%t ipv6_only=%t", sanitizeLogParam(setup.Target), sanitizeLogParam(setup.Protocol), sanitizeLogParam(setup.DataProvider), sanitizeLogParam(setup.Config.Lang), setup.Req.IPv4Only, setup.Req.IPv6Only) log.Printf("[deploy] target resolved target=%s ip=%s via dot=%s", sanitizeLogParam(setup.Target), setup.IP, sanitizeLogParam(strings.ToLower(setup.Req.DotServer))) traceMu.Lock() defer traceMu.Unlock() if setup.NeedsNextTraceAPIV3 { if _, err := withTraceSetupContext(setup, func() (struct{}, error) { ensureNextTraceAPIV3ConnectionFn() return struct{}{}, nil }); err != nil { log.Printf("[deploy] failed to initialize NextTrace API v3 connection target=%s error=%v", sanitizeLogParam(setup.Target), err) c.JSON(500, gin.H{"error": err.Error()}) return } } configured := setup.Config log.Printf("[deploy] starting trace target=%s resolved=%s method=%s lang=%s queries=%d maxHops=%d", sanitizeLogParam(setup.Target), setup.IP.String(), string(setup.Method), sanitizeLogParam(configured.Lang), configured.NumMeasurements, configured.MaxHops) start := time.Now() res, err := withTraceSetupContext(setup, func() (*trace.Result, error) { return traceTracerouteFn(setup.Method, configured) }) duration := time.Since(start) if err != nil { if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { return } log.Printf("[deploy] trace failed target=%s error=%v", sanitizeLogParam(setup.Target), err) c.JSON(500, gin.H{"error": err.Error()}) return } traceMapURL := traceMapURLForResult(setup, res) if traceMapURL != "" { log.Printf("[deploy] trace map generated target=%s mapUrl=%s", sanitizeLogParam(setup.Target), traceMapURL) } response := traceResponse{ Target: setup.Target, ResolvedIP: setup.IP.String(), Protocol: setup.Protocol, DataProvider: setup.DataProvider, TraceMapURL: traceMapURL, Language: configured.Lang, Hops: convertHops(res, configured.Lang), DurationMs: duration.Milliseconds(), StopReason: service.NewTraceStopReason(res.StopReason), } log.Printf("[deploy] trace completed target=%s hops=%d duration=%s", sanitizeLogParam(setup.Target), len(response.Hops), duration) c.JSON(200, response) } func buildTraceConfig(req traceRequest, method trace.Method, ip net.IP, dataProvider string, port int) (trace.Config, error) { lang := strings.TrimSpace(req.Language) if lang == "" { lang = defaults["language"].(string) } timeout := req.TimeoutMs if timeout <= 0 { timeout = defaults["timeout_ms"].(int) } packetSize := trace.DefaultPacketSize(method, ip) if req.PacketSize != nil { packetSize = *req.PacketSize } packetSizeSpec, err := trace.NormalizePacketSize(method, ip, packetSize) if err != nil { return trace.Config{}, err } tos := defaults["tos"].(int) if req.TOS != nil { tos = *req.TOS } if req.PacketInterval <= 0 { req.PacketInterval = 50 } if req.TTLInterval <= 0 { req.TTLInterval = 50 } maxHops := req.MaxHops if maxHops <= 0 { maxHops = defaults["max_hops"].(int) } queries := req.Queries if queries <= 0 { queries = defaults["queries"].(int) } parallel := req.ParallelRequests if parallel <= 0 { parallel = defaults["parallel_requests"].(int) } beginHop := req.BeginHop if beginHop <= 0 { beginHop = defaults["begin_hop"].(int) } alwaysWait := req.AlwaysWaitRDNS || req.AlwaysRDNS ostype := 3 switch runtime.GOOS { case "darwin": ostype = 1 case "windows": ostype = 2 } descriptorSession := ipgeo.GetSourceDescriptorSessionWithGeoDNS(dataProvider, req.DotServer) descriptor := descriptorSession.Current() session := trace.CachedGeoSourceSession(descriptorSession) return trace.Config{ OSType: ostype, ICMPMode: req.ICMPMode, SrcAddr: req.SourceAddress, SrcPort: req.SourcePort, SourceDevice: strings.TrimSpace(req.SourceDevice), BeginHop: beginHop, MaxHops: maxHops, NumMeasurements: queries, MaxAttempts: req.MaxAttempts, ParallelRequests: parallel, Timeout: time.Duration(timeout) * time.Millisecond, DstIP: ip, DstPort: port, IPGeoSource: session.Source, IPGeoDescriptor: descriptorSession.Current, RefreshIPGeoSource: session.Refresh, RDNS: !req.DisableRDNS, AlwaysWaitRDNS: alwaysWait, PacketInterval: req.PacketInterval, TTLInterval: req.TTLInterval, Lang: lang, DN42: req.DN42 || descriptor.Namespace == ipgeo.SourceNamespaceDN42, PktSize: packetSizeSpec.PayloadSize, RandomPacketSize: packetSizeSpec.Random, TOS: tos, Maptrace: !req.DisableMaptrace, DisableMPLS: req.DisableMPLS, }, nil } func withTraceSetupContext[T any](setup *traceExecution, callback func() (T, error)) (T, error) { if callback == nil { var zero T return zero, nil } prevPowProvider := util.PowProviderParam util.PowProviderParam = "" if setup != nil { util.PowProviderParam = setup.PowProvider if setup.NeedsNextTraceAPIV3 { if setup.PowProvider != "" { log.Printf("[deploy] NextTrace API v3 using custom PoW provider=%s", sanitizeLogParam(setup.PowProvider)) } else { log.Printf("[deploy] NextTrace API v3 using default PoW provider") } } else if setup.PowProvider != "" { log.Printf("[deploy] overriding PoW provider=%s", sanitizeLogParam(setup.PowProvider)) } } defer func() { util.PowProviderParam = prevPowProvider }() return withTraceGeoDNSScope(setup, callback) } func withTraceGeoDNSScope[T any](setup *traceExecution, callback func() (T, error)) (T, error) { if callback == nil { var zero T return zero, nil } dotServer := "" if setup != nil { dotServer = strings.TrimSpace(strings.ToLower(setup.Req.DotServer)) } return util.WithGeoDNSResolver(dotServer, callback) } func traceMapURLForResult(setup *traceExecution, res *trace.Result) string { if setup == nil || res == nil || !setup.Config.Maptrace || !shouldGenerateMap(setup.DataProvider) { return "" } // Keep the historical tracemap payload shape stable. StopReason belongs to // local CLI/API output and must not be sent to the external map service. payload, err := json.Marshal(struct { Hops [][]trace.Hop TraceMapUrl string }{ Hops: res.Hops, TraceMapUrl: res.TraceMapUrl, }) if err != nil { return "" } url, err := withTraceMapScopeFn(setup, func() (string, error) { ctx := setup.Config.Context if ctx == nil { ctx = context.Background() } return traceMapURLFn(ctx, string(payload)) }) if err != nil { return "" } return url } func convertHops(res *trace.Result, lang string) []hopResponse { if res == nil || len(res.Hops) == 0 { return nil } hops := make([]hopResponse, 0, len(res.Hops)) for idx, attempts := range res.Hops { resp := buildHopResponse(attempts, idx, lang) if len(resp.Attempts) == 0 { continue } hops = append(hops, resp) } return hops } func buildHopResponse(attempts []trace.Hop, idx int, lang string) hopResponse { resp := hopResponse{ TTL: idx + 1, Attempts: make([]hopAttempt, 0, len(attempts)), } for _, attempt := range attempts { ha := hopAttempt{ Success: attempt.Success, MPLS: attempt.MPLS, } if attempt.Address != nil { ha.IP = attempt.Address.String() } if attempt.Hostname != "" { ha.Hostname = attempt.Hostname } if attempt.RTT > 0 { ha.RTT = float64(attempt.RTT) / float64(time.Millisecond) } if attempt.Error != nil { ha.Error = attempt.Error.Error() } if attempt.Geo != nil { ha.Geo = localizeGeo(attempt.Geo, lang) } resp.Attempts = append(resp.Attempts, ha) } return resp } func parseTargetURLHost(target string) (string, string, error) { fallbackSource := target if !strings.Contains(target, "://") { return "", fallbackSource, nil } u, err := url.Parse(target) if err != nil { return "", "", fmt.Errorf("invalid target format: %w", err) } if u.Host != "" { return u.Host, fallbackSource, nil } if u.Path != "" { fallbackSource = strings.TrimPrefix(target, u.Scheme+"://") } return "", fallbackSource, nil } func extractTargetHost(target, fallbackSource string) (string, error) { parseTarget := target if strings.Contains(target, "/") { if !strings.HasPrefix(parseTarget, "//") { parseTarget = "//" + parseTarget } if u, err := url.Parse(parseTarget); err == nil && u.Host != "" { return u.Host, nil } } if !strings.Contains(fallbackSource, "/") { return "", nil } idx := strings.Index(fallbackSource, "/") if idx <= 0 { return "", errors.New("invalid target format") } candidate := strings.TrimSpace(fallbackSource[:idx]) if candidate == "" { return "", errors.New("invalid target format") } return candidate, nil } func stripTargetPort(target string) string { // Try standard SplitHostPort first — handles host:port and [IPv6]:port. if host, _, err := net.SplitHostPort(target); err == nil { return host } // Bare [IPv6] without port. if open := strings.Index(target, "["); open >= 0 { close := strings.Index(target[open:], "]") if close > 1 { return target[open+1 : open+close] } } // host:port with exactly one colon (plain IPv4 / hostname). if strings.Count(target, ":") == 1 { return target[:strings.Index(target, ":")] } return target } func normalizeTarget(input string) (string, error) { target := strings.TrimSpace(input) if target == "" { return "", errors.New("target is required") } host, fallbackSource, err := parseTargetURLHost(target) if err != nil { return "", err } if host == "" { host, err = extractTargetHost(target, fallbackSource) if err != nil { return "", err } } if host != "" { target = host } return strings.TrimSpace(stripTargetPort(target)), nil } func normalizeDataProvider(provider string, alias string) string { candidate := strings.TrimSpace(provider) if candidate == "" { candidate = strings.TrimSpace(alias) } if candidate == "" { return "" } if canonical := ipgeo.CanonicalizeNextTraceAPIProvider(candidate); canonical != candidate { return canonical } upper := strings.ToUpper(candidate) switch upper { case "IP.SB": return "IP.SB" case "IP-API.COM", "IPAPI.COM": return "IPAPI.com" case "IPINFO", "IP INFO": return "IPInfo" case "IPINSIGHT", "IP INSIGHT": return "IPInsight" case "IPINFOLOCAL", "IP INFO LOCAL": return "IPInfoLocal" case "CHUNZHEN": return "chunzhen" case "DN42": return "DN42" case "DISABLE-GEOIP", "DISABLE_GEOIP": return "disable-geoip" case "IPDB.ONE": return "ipdb.one" default: return candidate } } func contains(list []string, v string) bool { for _, item := range list { if strings.EqualFold(item, v) { return true } } return false } func shouldGenerateMap(provider string) bool { if ipgeo.IsNextTraceAPIProvider(provider) { return true } allowed := []string{"IPINFO", "IP-API.COM", "IPAPI.COM"} for _, item := range allowed { if strings.EqualFold(provider, item) { return true } } return false } func ensureNextTraceAPIV3Connection() { nextTraceAPIV3ConnMu.Lock() defer nextTraceAPIV3ConnMu.Unlock() conn := wshandle.GetWsConn() if conn == nil || conn.MsgSendCh == nil || conn.MsgReceiveCh == nil { log.Println("[deploy] establishing initial NextTrace API v3 websocket") wshandle.New() return } if !conn.IsConnected() && !conn.IsConnecting() { log.Println("[deploy] reconnecting NextTrace API v3 websocket") wshandle.New() } } func localizeGeo(src *ipgeo.IPGeoData, lang string) *ipgeo.IPGeoData { if src == nil { return nil } dst := *src switch strings.ToLower(lang) { case "en": if dst.CountryEn != "" { dst.Country = dst.CountryEn } if dst.ProvEn != "" { dst.Prov = dst.ProvEn } if dst.CityEn != "" { dst.City = dst.CityEn } default: if dst.Country == "" && dst.CountryEn != "" { dst.Country = dst.CountryEn } if dst.Prov == "" && dst.ProvEn != "" { dst.Prov = dst.ProvEn } if dst.City == "" && dst.CityEn != "" { dst.City = dst.CityEn } } return &dst }