trace

package
v1.6.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2026 License: GPL-3.0 Imports: 26 Imported by: 1

Documentation

Index

Constants

View Source
const PendingGeoSource = "pending"

Variables

View Source
var (
	ErrEmptyGeoQuery = errors.New("empty geo lookup target")
	ErrNilGeoSource  = errors.New("nil geo source")
	ErrNotIPGeoQuery = errors.New("geo lookup target is not an IP address")
)

Functions

func AwaitGlobalpingMeasurement added in v1.6.5

func AwaitGlobalpingMeasurement(ctx context.Context, client globalping.Client, id string) (*globalping.Measurement, error)

func CanonicalHostname added in v1.5.0

func CanonicalHostname(s string) string

func ClearCaches added in v1.5.0

func ClearCaches()

func CreateGlobalpingMeasurement added in v1.6.5

func CreateGlobalpingMeasurement(ctx context.Context, client globalping.Client, req *globalping.MeasurementCreate) (*globalping.Measurement, error)

func DefaultPacketSize added in v1.6.1

func DefaultPacketSize(method Method, dstIP net.IP) int

func FormatPacketSizeLabel added in v1.6.1

func FormatPacketSizeLabel(packetSize int) string

func GenerateQuicPayloadWithRandomIds

func GenerateQuicPayloadWithRandomIds() []byte

func GlobalpingFormatLocation added in v1.5.0

func GlobalpingFormatLocation(m *globalping.ProbeMeasurement) string

func HopPrinter

func HopPrinter(h Hop)

func LookupIPGeo added in v1.6.4

func LookupIPGeo(ctx context.Context, source ipgeo.Source, lang string, maptrace bool, numMeasurements int, query string) (*ipgeo.IPGeoData, error)

LookupIPGeo reuses traceroute's shared GeoIP cache/retry path for direct IP metadata lookups.

func MinPacketSize added in v1.6.1

func MinPacketSize(method Method, dstIP net.IP) int

func NewGlobalpingClient added in v1.6.5

func NewGlobalpingClient(ctx context.Context) globalping.Client

func ResolveConfiguredSrcAddr added in v1.6.2

func ResolveConfiguredSrcAddr(dstIP net.IP, srcAddr, srcDev string) (resolved string, explicit bool, err error)

func ResolveFallbackSrcAddr added in v1.6.2

func ResolveFallbackSrcAddr(dstIP net.IP) string

func ResolveSourceDevice added in v1.6.2

func ResolveSourceDevice(device string) (*net.Interface, error)

func ResolveSourceDeviceAddr added in v1.6.2

func ResolveSourceDeviceAddr(dev *net.Interface, dstIP net.IP) (string, error)

func RunMTR added in v1.5.2

func RunMTR(ctx context.Context, method Method, baseConfig Config, opts MTROptions, onSnapshot MTROnSnapshot) error

RunMTR 启动 MTR 连续探测模式。

当 opts.HopInterval > 0 时使用 per-hop 独立调度(CLI MTR 模式):

每个 TTL 独立计时,完成后等 HopInterval 再发下一个。

当 opts.HopInterval == 0 时使用 legacy round-based 调度(Web MTR 兼容):

ICMP 使用持久 raw socket 跨轮复用;TCP/UDP 以 per-round Traceroute 回退。

func RunMTRRaw added in v1.5.2

func RunMTRRaw(ctx context.Context, method Method, cfg Config, opts MTRRawOptions, onRecord MTRRawOnRecord) error

RunMTRRaw runs continuous traceroute and emits probe-level streaming records.

When opts.HopInterval > 0, uses per-hop scheduling (each TTL independent); otherwise uses legacy round-based scheduling for backward compatibility.

Types

type Config

type Config struct {
	Context          context.Context
	OSType           int
	ICMPMode         int
	SrcAddr          string
	SrcPort          int
	SourceDevice     string
	BeginHop         int
	MaxHops          int
	NumMeasurements  int
	MaxAttempts      int
	ParallelRequests int
	Timeout          time.Duration
	DstIP            net.IP
	DstPort          int
	Quic             bool
	IPGeoSource      ipgeo.Source
	RDNS             bool
	AlwaysWaitRDNS   bool
	PacketInterval   int
	TTLInterval      int
	Lang             string
	DN42             bool
	RealtimePrinter  func(res *Result, ttl int)
	AsyncPrinter     func(res *Result)
	PktSize          int
	RandomPacketSize bool
	TOS              int
	Maptrace         bool
	DisableMPLS      bool
}

func NormalizeExplicitSourceConfig added in v1.6.2

func NormalizeExplicitSourceConfig(_ Method, config Config) (Config, error)

type GlobalpingOptions added in v1.5.0

type GlobalpingOptions struct {
	Target  string
	From    string
	IPv4    bool
	IPv6    bool
	TCP     bool
	UDP     bool
	Port    int
	Packets int
	MaxHops int

	DisableMaptrace bool
	DataOrigin      string

	TablePrint   bool
	ClearScreen  bool
	ClassicPrint bool
	RawPrint     bool
	JSONPrint    bool
}

GlobalpingOptions configures a Globalping-based traceroute request. Defined in an untagged file so all build flavors can reference the type without pulling in the globalping-cli dependency.

type Hop

type Hop struct {
	Success  bool
	Address  net.Addr
	Hostname string
	TTL      int
	RTT      time.Duration
	Error    error
	Geo      *ipgeo.IPGeoData
	Lang     string
	MPLS     []string
}

type ICMPTracer

type ICMPTracer struct {
	Config

	SrcIP net.IP
	// contains filtered or unexported fields
}

func (*ICMPTracer) Execute

func (t *ICMPTracer) Execute() (res *Result, err error)

func (*ICMPTracer) PrintFunc

func (t *ICMPTracer) PrintFunc(ctx context.Context, cancel context.CancelCauseFunc)

type ICMPTracerv6

type ICMPTracerv6 struct {
	Config

	SrcIP net.IP
	// contains filtered or unexported fields
}

func (*ICMPTracerv6) Execute

func (t *ICMPTracerv6) Execute() (res *Result, err error)

func (*ICMPTracerv6) PrintFunc

func (t *ICMPTracerv6) PrintFunc(ctx context.Context, cancel context.CancelCauseFunc)

type MTRAggregator added in v1.5.2

type MTRAggregator struct {
	// contains filtered or unexported fields
}

MTRAggregator 跨轮次聚合 hop 统计。线程安全。

func NewMTRAggregator added in v1.5.2

func NewMTRAggregator() *MTRAggregator

NewMTRAggregator 创建新的聚合器。

func (*MTRAggregator) ClearHop added in v1.5.2

func (agg *MTRAggregator) ClearHop(ttl int)

ClearHop 删除指定 TTL 上的所有聚合数据。 用于 per-hop 调度器中 knownFinalTTL 下调时,擦除旧 finalTTL 的过期统计, 避免 ghost row,同时不会把旧 final 的 Snt 合并到新 final(防止 Snt 膨胀)。

func (*MTRAggregator) Clone added in v1.5.2

func (agg *MTRAggregator) Clone() *MTRAggregator

Clone 返回深拷贝的聚合器,用于流式预览(不影响原始数据)。

func (*MTRAggregator) MigrateStats added in v1.5.2

func (agg *MTRAggregator) MigrateStats(fromTTL, toTTL, maxPerHop int)

MigrateStats 将 fromTTL 上所有累加器迁移合并到 toTTL,然后删除 fromTTL。 用于 knownFinalTTL 下调时把旧 finalTTL 上已入账的 dst-ip 统计搬到新 finalTTL。 maxPerHop > 0 时,合并后对每个累加器的 sent/received 做上限裁剪, 保证 Snt 不超过预算。

func (*MTRAggregator) PatchMetadataByIP added in v1.6.4

func (agg *MTRAggregator) PatchMetadataByIP(ip, host string, geo *ipgeo.IPGeoData) bool

PatchMetadataByIP updates existing rows for the given IP with late-arriving host/geo data without affecting sent/received/RTT statistics.

func (*MTRAggregator) Reset added in v1.5.2

func (agg *MTRAggregator) Reset()

Reset 清空所有统计数据,用于 r 键重置。

func (*MTRAggregator) Snapshot added in v1.5.2

func (agg *MTRAggregator) Snapshot() []MTRHopStat

Snapshot 返回当前聚合结果快照。

func (*MTRAggregator) Update added in v1.5.2

func (agg *MTRAggregator) Update(res *Result, queries int) []MTRHopStat

Update 接收一轮 traceroute 的 Result 并更新统计,返回当前快照。

type MTRHopStat added in v1.5.2

type MTRHopStat struct {
	TTL      int              `json:"ttl"`
	Host     string           `json:"host,omitempty"`
	IP       string           `json:"ip,omitempty"`
	Loss     float64          `json:"loss_percent"`
	Snt      int              `json:"snt"`
	Last     float64          `json:"last_ms"`
	Avg      float64          `json:"avg_ms"`
	Best     float64          `json:"best_ms"`
	Wrst     float64          `json:"wrst_ms"`
	StDev    float64          `json:"stdev_ms"`
	Geo      *ipgeo.IPGeoData `json:"geo,omitempty"`
	MPLS     []string         `json:"mpls,omitempty"`
	Received int              `json:"received"`
}

MTRHopStat 表示 MTR 输出中一行统计数据。

type MTROnSnapshot added in v1.5.2

type MTROnSnapshot func(iteration int, stats []MTRHopStat)

MTROnSnapshot 每轮完成后的回调,用于刷新 CLI 表格。 iteration 是当前轮次(从 1 开始),stats 是截至当前的聚合快照。

type MTROptions added in v1.5.2

type MTROptions struct {
	// Interval 每轮之间的等待间隔(默认 1s)。
	// 仅在 legacy round-based 模式(Web MTR)使用。
	Interval time.Duration
	// MaxRounds 最大轮次,0 表示无限运行直到取消。
	// 仅在 legacy round-based 模式使用。
	MaxRounds int
	// HopInterval 同一 TTL 两次探测之间的间隔(per-hop 调度模式)。
	// > 0 时启用 per-hop 调度,忽略 Interval / MaxRounds。
	HopInterval time.Duration
	// MaxPerHop 每个 TTL 最大探测次数,0 表示无限。
	MaxPerHop int
	// IsPaused 可选:返回 true 时暂停探测(轮询检查)。
	IsPaused func() bool
	// IsResetRequested 可选:返回 true(原子消费)时重置统计。
	IsResetRequested func() bool
	// ProgressThrottle 流式预览最小刷新间隔(默认 200ms)。
	ProgressThrottle time.Duration
	// AsyncMetadata causes interactive per-hop MTR to render bare hops first,
	// then patch Geo/RDNS metadata into existing rows asynchronously.
	AsyncMetadata bool
}

MTROptions 控制 MTR 连续探测行为。

type MTRRawOnRecord added in v1.5.2

type MTRRawOnRecord func(rec MTRRawRecord)

MTRRawOnRecord is called for each probe event.

type MTRRawOptions added in v1.5.2

type MTRRawOptions struct {
	// Interval is the delay between rounds (default: 1s).
	// Legacy round-based mode only.
	Interval time.Duration
	// MaxRounds is the max number of rounds. 0 means run forever until canceled.
	// Legacy round-based mode only.
	MaxRounds int
	// HopInterval is the per-hop probe interval (per-hop scheduling mode).
	// > 0 activates per-hop scheduling; Interval/MaxRounds are ignored.
	HopInterval time.Duration
	// MaxPerHop is the max probes per TTL in per-hop mode. 0 = unlimited.
	MaxPerHop int
	// RunRound optionally overrides the traceroute call for each round.
	// It is mainly for callers that need per-round locking or global-state setup.
	// Legacy round-based mode only.
	RunRound func(method Method, cfg Config) (*Result, error)
}

MTRRawOptions controls MTR raw streaming behavior.

type MTRRawRecord added in v1.5.2

type MTRRawRecord struct {
	Iteration int      `json:"iteration"`
	TTL       int      `json:"ttl"`
	Success   bool     `json:"success"`
	IP        string   `json:"ip,omitempty"`
	Host      string   `json:"host,omitempty"`
	RTTMs     float64  `json:"rtt_ms"`
	ASN       string   `json:"asn,omitempty"`
	Country   string   `json:"country,omitempty"`
	Prov      string   `json:"prov,omitempty"`
	City      string   `json:"city,omitempty"`
	District  string   `json:"district,omitempty"`
	Owner     string   `json:"owner,omitempty"`
	Lat       float64  `json:"lat"`
	Lng       float64  `json:"lng"`
	MPLS      []string `json:"mpls,omitempty"`
}

MTRRawRecord is one stream record emitted by MTR raw mode. It keeps the same information family as classic --raw output.

type MTRSnapshot added in v1.5.2

type MTRSnapshot struct {
	Iteration int          `json:"iteration"`
	Stats     []MTRHopStat `json:"stats"`
}

MTRSnapshot 是某一时刻的完整快照。

type Method

type Method string
const (
	ICMPTrace Method = "icmp"
	UDPTrace  Method = "udp"
	TCPTrace  Method = "tcp"
)

type PacketSizeSpec added in v1.6.1

type PacketSizeSpec struct {
	PayloadSize int
	Random      bool
}

func NormalizePacketSize added in v1.6.1

func NormalizePacketSize(method Method, dstIP net.IP, packetSize int) (PacketSizeSpec, error)

type Result

type Result struct {
	Hops [][]Hop

	TraceMapUrl string
	// contains filtered or unexported fields
}

func GlobalpingTraceroute added in v1.5.0

func GlobalpingTraceroute(opts *GlobalpingOptions, config *Config) (*Result, *globalping.Measurement, error)

func Traceroute

func Traceroute(method Method, config Config) (*Result, error)

func TracerouteWithContext added in v1.5.2

func TracerouteWithContext(ctx context.Context, method Method, config Config) (*Result, error)

type TCPTracer

type TCPTracer struct {
	Config

	SrcIP net.IP
	// contains filtered or unexported fields
}

func (*TCPTracer) Execute

func (t *TCPTracer) Execute() (res *Result, err error)

func (*TCPTracer) PrintFunc added in v1.5.0

func (t *TCPTracer) PrintFunc(ctx context.Context, cancel context.CancelCauseFunc)

type TCPTracerIPv6 added in v1.3.9

type TCPTracerIPv6 struct {
	Config

	SrcIP net.IP
	// contains filtered or unexported fields
}

func (*TCPTracerIPv6) Execute added in v1.3.9

func (t *TCPTracerIPv6) Execute() (res *Result, err error)

func (*TCPTracerIPv6) PrintFunc added in v1.5.0

func (t *TCPTracerIPv6) PrintFunc(ctx context.Context, cancel context.CancelCauseFunc)

type Tracer

type Tracer interface {
	Execute() (*Result, error)
}

type UDPTracer

type UDPTracer struct {
	Config

	SrcIP net.IP
	// contains filtered or unexported fields
}

func (*UDPTracer) Execute

func (t *UDPTracer) Execute() (res *Result, err error)

func (*UDPTracer) PrintFunc added in v1.5.0

func (t *UDPTracer) PrintFunc(ctx context.Context, cancel context.CancelCauseFunc)

type UDPTracerIPv6 added in v1.3.9

type UDPTracerIPv6 struct {
	Config

	SrcIP net.IP
	// contains filtered or unexported fields
}

func (*UDPTracerIPv6) Execute added in v1.3.9

func (t *UDPTracerIPv6) Execute() (res *Result, err error)

func (*UDPTracerIPv6) PrintFunc added in v1.5.0

func (t *UDPTracerIPv6) PrintFunc(ctx context.Context, cancel context.CancelCauseFunc)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL