Skip to content

Implement the prereadable connection - #47

Open
weiiwang01 wants to merge 1 commit into
mainfrom
ng/preread-conn
Open

Implement the prereadable connection#47
weiiwang01 wants to merge 1 commit into
mainfrom
ng/preread-conn

Conversation

@weiiwang01

@weiiwang01 weiiwang01 commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

Stack created with GitHub Stacks CLIGive Feedback 💬

This is a part of a series of pull requests to create the next generation of the aproxy.

In this pull request, the core utility that enables the extraction of SNI or host name from a connection (prereadable connection) is implemented.

@weiiwang01
weiiwang01 marked this pull request as ready for review August 17, 2026 06:29
@weiiwang01
weiiwang01 requested a review from a team as a code owner August 17, 2026 06:29
@weiiwang01
weiiwang01 requested review from swetha1654 and yanksyoon and removed request for a team August 17, 2026 06:29
@florentianayuwono
florentianayuwono requested a lite review from Copilot August 17, 2026 09:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Implements a new internal conn package that wraps *net.TCPConn to support a “preread” mode: reads are buffered and can be rewound/replayed so callers can sniff initial bytes (e.g., SNI/Host) without losing stream integrity.

Changes:

  • Added internal/conn.Conn with preread buffering, rewind, preread limit enforcement, and optional metrics hooks.
  • Added an extensive test suite covering limit behavior, replay boundaries, EOF handling, and concurrency invariants.
  • Updated module Go version and dependency set.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 3 comments.

File Description
internal/conn/conn.go Adds the preread-capable TCPConn wrapper, options, and metrics hooks.
internal/conn/conn_test.go Adds comprehensive unit tests for preread/replay/limit/EOF and concurrency behaviors.
go.mod Updates Go version and introduces new module requirements/indirect requirements.
go.sum Adds checksums for new/updated module dependencies.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread internal/conn/conn.go
if m == nil {
panic("nil metrics")
}
if v := reflect.ValueOf(m); v.Kind() == reflect.Pointer && v.IsNil() {
Comment thread go.mod
Comment on lines +3 to 4
go 1.25.0

Comment thread internal/conn/conn.go

const defaultPrereadLimit = 64 * 1024

var ErrPrereadLimitExceeded = errors.New("exceed preread limit")
@yhaliaw
yhaliaw self-requested a review August 18, 2026 06:42

@yhaliaw yhaliaw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Major changes needed


🤝 Human review with AI assistance.

Comment thread internal/conn/conn.go
Comment on lines +91 to +94
// ReadFrom is not supported.
func (c *Conn) ReadFrom(r io.Reader) (n int64, err error) {
panic("not supported")
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ReadFrom panics unconditionally here, and WriteTo does the same at lines 113-116. Because *net.TCPConn is embedded (line 26), *Conn satisfies both io.ReaderFrom and io.WriterTo, and io.Copy checks those interfaces before falling back to Read/Write.

That means io.Copy(dst, conn) and io.Copy(conn, src) panic on the very first call. The repo's existing relay, RelayTCP in aproxy.go (lines 277 and 284), is built on exactly those two io.Copy calls, and there's no recover() anywhere outside test code — an unrecovered panic in a per-connection goroutine takes down the whole daemon. This PR doesn't wire Conn into the relay path yet, so nothing breaks today, but it's a landmine for the follow-up PR.

The cleanest fix is to stop embedding *net.TCPConn and instead hold it as an unexported field, forwarding only the safe methods (Close, CloseWrite, LocalAddr, RemoteAddr, the deadline setters). That removes ReadFrom/WriteTo from the method set entirely so io.Copy falls back to buffered copying. If embedding needs to stay for another reason, at minimum return an error instead of panicking so io.Copy surfaces a normal error rather than crashing the process.

🤖 AI-assisted

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.

3 participants