Skip to content

fix(traffic): 抓包时重建 net/http 抹掉的 Host 头 - #114

Open
wuchulonly wants to merge 1 commit into
masterfrom
fix/capture-host-header
Open

fix(traffic): 抓包时重建 net/http 抹掉的 Host 头#114
wuchulonly wants to merge 1 commit into
masterfrom
fix/capture-host-header

Conversation

@wuchulonly

Copy link
Copy Markdown
Contributor

问题

任务重放/展示时从抓包 flow 重建的 HTTP 请求缺少 Host:,导致报文不完整、无法直接粘进 Burp Repeater 等工具重放。

根因

Go net/http 解析请求时把 HostRequest.Header 删掉,挪到 Request.Host。抓包处用 PairsFromHTTP(req.Header) 构造报文头,只遍历这个已不含 Host 的 map,于是 Host 在生成 flow 时就丢了(权威信息只留在 URL 里)。

改动

  • aop/traffic/exchange.go:新增 PairsFromHTTPWithHost(headers, host) —— 当 header map 无 Host 且 host 非空时,在最前补一条 Host(幂等,已有则不重复);ExchangeFromHTTPreq.Host 接入。
  • tools/proxy/mitm.go:newCaptureState 改用 PairsFromHTTPWithHost,Host 取客户端原样发的 Request.Raw().Host(保留非默认端口),回退 URL 授权部分。

不改变任何既有函数签名(纯新增),响应侧不动。

测试

  • aop/traffic:TestPairsFromHTTPWithHost(补/幂等/空 host)、TestExchangeFromHTTPAddsHost
  • tools/proxy:TestCaptureIncludesHostHeader —— 经 hub 抓包后断言请求头含正确 Host。
  • 全套 go test ./aop/traffic/ ./tools/proxy/ 通过;gofmt/vet 干净。

影响范围

仅对新抓的流量生效;头部原始顺序不变(本 PR 不涉及)。

🤖 Generated with Claude Code

net/http parses the request-line authority into Request.Host and deletes
"Host" from Request.Header, so a header sequence built from that map alone
never carries a Host line. A request reconstructed from a captured flow was
therefore incomplete and could not be replayed (e.g. pasted into a repeater).

Add PairsFromHTTPWithHost, which prepends the Host header when the map lacks
one, and use it on both capture paths: ExchangeFromHTTP (direct HTTP tools)
via req.Host, and the MITM newCaptureState via the client-sent Host (falling
back to the URL authority), keeping any non-default port.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.

1 participant