Skip to content

[codex] Restore Windows --dev source selection#364

Merged
tsosunchia merged 2 commits into
mainfrom
main
Apr 20, 2026
Merged

[codex] Restore Windows --dev source selection#364
tsosunchia merged 2 commits into
mainfrom
main

Conversation

@tsosunchia

@tsosunchia tsosunchia commented Apr 20, 2026

Copy link
Copy Markdown
Member

Summary

  • restore Windows --dev handling so ICMP/TCP/UDP resolve the selected device to a source IP instead of rejecting TCP
  • document that Windows uses --dev as source-address selection, not real egress-interface binding
  • clarify that standalone --mtu follows the same source-address behavior and only uses the device name for local MTU lookup

Why

A previous change made Windows TCP + --dev fail before reaching the tracer. That regressed the v1.6.2-compatible behavior where Windows derives a source IP from the selected device and lets Windows routing choose the actual egress path.

Validation

  • go test ./trace ./cmd
  • go test ./...

Summary by CodeRabbit

  • Bug Fixes

    • 修复 Windows 下使用 --dev 时的行为:TCP/ICMP/UDP 探测可通过指定网卡推导源 IP 并继续执行(不再因 TCP+--dev 而报错)。
  • Documentation

    • 更新 CLI 和中/英文文档,说明 Windows 下 --dev/--mtu 将基于设备推导源地址,但路由仍可能选择不同出口。
  • Tests

    • 更新并扩展相关单元测试以覆盖 Windows 上设备解析为源地址的行为。

Keep Windows --dev usable by resolving the selected device to a source IP and clearing SourceDevice before the tracer reaches Windows send paths. Update docs and project notes to describe that Windows routing may still choose the actual egress interface.
@coderabbitai

coderabbitai Bot commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

本次变更移除了对 Windows 上 TCP + --dev 的显式拒绝,文档与 CLI 帮助文本同步更新;实现上 NormalizeExplicitSourceConfig 在 Windows 下会把指定设备解析成源 IP 写入 SrcAddr 并清空 SourceDevice,相关单元测试调整为断言成功解析该地址。

Changes

Cohort / File(s) Summary
文档更新
AGENTS.md, README.md, README_zh_CN.md
更新 Windows 上 --dev 的描述:说明 --dev 会从指定网卡派生源 IP 并用于 ICMP/TCP/UDP 探测,但不等同于强制绑定出口接口;补充 --mtu 单独模式遵循相同源地址语义。
CLI 帮助与注释
cmd/cmd.go
调整 --dev/-D 的帮助文本与两处内联注释措辞,移除“TCP + --dev 不支持/报错”表述,改为“选择设备源地址;路由可能仍选出口接口”。
核心逻辑
trace/source_config.go
删除基于 method 的 Windows 拒绝分支(不再因 TCP 而报错);参数名去掉未使用的 method 依赖,保持其他解析/清理逻辑不变。
单元测试
trace/source_config_test.go
替换拒绝型测试为表驱动测试(ICMP/TCP/UDP),调整存根以返回 192.0.2.44/24 并断言 SrcAddr=="192.0.2.44"SourceDevice==""

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 分钟

诗句

🐰 Windows 兔子跳一跳,
网卡化作小小源地址,
TCP、UDP、ICMP 一同笑,
不再拒绝,只把源记牢,
路由自由去往远方跑。

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题清晰准确地反映了本次变更的主要目的:恢复Windows下--dev选项的源地址选择功能。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@tsosunchia tsosunchia marked this pull request as ready for review April 20, 2026 01:19
Copilot AI review requested due to automatic review settings April 20, 2026 01:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Restores Windows --dev behavior so TCP traces no longer fail early and instead derive a source IP from the selected device, aligning Windows behavior across ICMP/TCP/UDP and updating documentation to reflect that Windows --dev is source-address selection (not strict egress-interface binding).

Changes:

  • Remove the Windows+TCP SourceDevice rejection in NormalizeExplicitSourceConfig, allowing device→source-IP resolution.
  • Update/extend tests to validate Windows TCP resolves --dev to SrcAddr and clears SourceDevice.
  • Update CLI help text and READMEs to clarify Windows --dev and standalone --mtu semantics.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
trace/source_config.go Removes Windows TCP --dev rejection; normalizes Windows --dev into SrcAddr while clearing SourceDevice.
trace/source_config_test.go Updates test to assert Windows TCP resolves device to a source IP instead of erroring.
cmd/cmd.go Adjusts --dev help text and related comments to match restored Windows behavior.
README.md Documents Windows --dev as source-IP selection for ICMP/TCP/UDP and clarifies --mtu behavior.
README_zh_CN.md Same documentation updates as README.md (Chinese).
AGENTS.md Records the Windows compatibility rule: resolve --dev to SrcAddr and clear SourceDevice (v1.6.2-compatible behavior).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread trace/source_config.go Outdated
Comment on lines +122 to +124
func NormalizeExplicitSourceConfig(method Method, config Config) (Config, error) {
_ = method

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
trace/source_config_test.go (1)

303-330: 建议把 Windows 协议覆盖做成表驱动。

当前新增用例覆盖了这次 TCP 回归;可以顺手把 ICMPTrace / TCPTrace / UDPTrace 都放进同一个表,锁住 PR 描述里的三种协议行为,避免后续重新引入 method-specific 分支时漏掉 UDP。

🧪 可选测试重构
-func TestNormalizeExplicitSourceConfigWindowsTCPResolvesDeviceToSourceAddress(t *testing.T) {
+func TestNormalizeExplicitSourceConfigWindowsResolvesDeviceToSourceAddress(t *testing.T) {
 	restore := stubSourceDeviceResolver(t, func(device string) (*net.Interface, error) {
 		if device != "Ethernet0" {
 			t.Fatalf("ResolveSourceDevice device = %q, want Ethernet0", device)
 		}
 		return &net.Interface{Name: device}, nil
 	}, func(_ *net.Interface) ([]net.Addr, error) {
 		return []net.Addr{&net.IPNet{IP: net.ParseIP("192.0.2.44"), Mask: net.CIDRMask(24, 32)}}, nil
 	})
 	defer restore()
 
-	cfg := Config{
-		OSType:       osTypeWindows,
-		DstIP:        net.ParseIP("1.1.1.1"),
-		SourceDevice: "Ethernet0",
-	}
-
-	got, err := NormalizeExplicitSourceConfig(TCPTrace, cfg)
-	if err != nil {
-		t.Fatalf("NormalizeExplicitSourceConfig() error = %v", err)
-	}
-	if got.SrcAddr != "192.0.2.44" {
-		t.Fatalf("SrcAddr = %q, want 192.0.2.44", got.SrcAddr)
-	}
-	if got.SourceDevice != "" {
-		t.Fatalf("SourceDevice = %q, want empty", got.SourceDevice)
+	for _, method := range []Method{ICMPTrace, TCPTrace, UDPTrace} {
+		t.Run(string(method), func(t *testing.T) {
+			cfg := Config{
+				OSType:       osTypeWindows,
+				DstIP:        net.ParseIP("1.1.1.1"),
+				SourceDevice: "Ethernet0",
+			}
+
+			got, err := NormalizeExplicitSourceConfig(method, cfg)
+			if err != nil {
+				t.Fatalf("NormalizeExplicitSourceConfig() error = %v", err)
+			}
+			if got.SrcAddr != "192.0.2.44" {
+				t.Fatalf("SrcAddr = %q, want 192.0.2.44", got.SrcAddr)
+			}
+			if got.SourceDevice != "" {
+				t.Fatalf("SourceDevice = %q, want empty", got.SourceDevice)
+			}
+		})
 	}
 }

As per coding guidelines, **/cmd/cmd.go: For Windows ICMP/TCP/UDP, use source IP to influence routing selection, not actual per-device binding; document in README that device specification may be inaccurate.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@trace/source_config_test.go` around lines 303 - 330, The test
TestNormalizeExplicitSourceConfigWindowsTCPResolvesDeviceToSourceAddress
currently only covers TCP; change it to a table-driven test iterating over trace
types (use TCPTrace, ICMPTrace, UDPTrace) and assert the same expectations for
Windows: when SourceDevice resolves to an IP,
NormalizeExplicitSourceConfig(traceType, cfg) returns SrcAddr == resolved IP and
clears SourceDevice; update the test function name (e.g.
TestNormalizeExplicitSourceConfigWindowsProtocolResolvesDeviceToSourceAddress)
and loop over the trace types, calling NormalizeExplicitSourceConfig for each
and running the same assertions, and add a short README note where Windows
behavior is documented (per-cmd behavior in cmd.go) that device spec affects
routing via source IP rather than guaranteed device binding.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@trace/source_config_test.go`:
- Around line 303-330: The test
TestNormalizeExplicitSourceConfigWindowsTCPResolvesDeviceToSourceAddress
currently only covers TCP; change it to a table-driven test iterating over trace
types (use TCPTrace, ICMPTrace, UDPTrace) and assert the same expectations for
Windows: when SourceDevice resolves to an IP,
NormalizeExplicitSourceConfig(traceType, cfg) returns SrcAddr == resolved IP and
clears SourceDevice; update the test function name (e.g.
TestNormalizeExplicitSourceConfigWindowsProtocolResolvesDeviceToSourceAddress)
and loop over the trace types, calling NormalizeExplicitSourceConfig for each
and running the same assertions, and add a short README note where Windows
behavior is documented (per-cmd behavior in cmd.go) that device spec affects
routing via source IP rather than guaranteed device binding.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ecef484f-d74c-43f5-982d-ca57aff80c09

📥 Commits

Reviewing files that changed from the base of the PR and between e1115a2 and e66e266.

📒 Files selected for processing (6)
  • AGENTS.md
  • README.md
  • README_zh_CN.md
  • cmd/cmd.go
  • trace/source_config.go
  • trace/source_config_test.go

Remove the in-body unused method assignment and extend the Windows source-device regression test across ICMP, TCP, and UDP so future method-specific gating is caught.
@tsosunchia

Copy link
Copy Markdown
Member Author

@coderabbitai run pre-merge checks

@coderabbitai

coderabbitai Bot commented Apr 20, 2026

Copy link
Copy Markdown
Contributor
✅ Actions performed

Running Pre-merge checks. Results will be posted in the summary comment once complete.

@tsosunchia

Copy link
Copy Markdown
Member Author

@codex run pre-merge checks

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create an environment for this repo.

@tsosunchia

Copy link
Copy Markdown
Member Author

@codex run pre-merge checks

@chatgpt-codex-connector

Copy link
Copy Markdown

Summary

  • Ran the requested pre-merge checks locally for this PR branch.
  • No code changes were needed or made (git status --short was clean), so no commit/PR was created.

Testing

  • go test ./trace ./cmd
  • go test ./...
  • go build ./...

If you want, I can also run any repo-specific CI scripts/workflows that mirror GitHub Actions more closely (if present), but the core Go checks are all passing locally.

View task →

@tsosunchia tsosunchia merged commit 3ba3d0f into nxtrace:main Apr 20, 2026
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants