- Go 100%
* layer4: Don't abort matching when a later route conclusively fails A route that reaches a definitive non-match from partially delivered data buried the pending state of an earlier route that had returned ErrConsumedAllPrefetchedBytes. The non-match branch advances lastNeedsMoreIdx to i+1 unconditionally, and when the deciding route is the last one in the list, lastNeedsMoreIdx reaches len(routes) and the retry guard at the bottom of the loop stops looping. Matching is then abandoned within microseconds, well before matching_timeout, and the connection falls through to the next handler. The http matcher is the common trigger: isHttp() rules a stream out as soon as the prefetched bytes hold a 0x0a at index >= 10, which a binary TLS ClientHello reliably does (cipher suite 0xc00a, extension 0x000a, or the 32 random bytes). So an "http" route ordered after a "tls" route turns any ClientHello split across TCP segments into a fall-through. Post-quantum ClientHellos always exceed a typical MSS, making this a race on whether segments coalesce in the socket buffer before the first prefetch, which surfaces as intermittent TLS errors. routesStatus already tracks the pending state accurately, so consult it directly instead of relying on the lastNeedsMoreIdx watermark. Existing watermark semantics, including route skipping, are left untouched. Termination is unaffected: whenever a route still needs more data lastNeedsMoreIdx != -1, so the top of the loop still prefetches, and prefetch either makes progress or hits the read deadline. * layer4: Keep catch-all routes waiting for routes that need more data A catch-all route (one with no matchers) must not match while an earlier route still needs more data. The guard that enforces this compares the route index with lastNeedsMoreIdx, which a conclusive non-match moves forward to i+1. With tls, http and a catch-all route in this order, a ClientHello split across TCP segments makes the http route rule itself out from the first segment, which moves lastNeedsMoreIdx to the catch-all route's own index. The guard then lets the catch-all route match while the tls route is still waiting for the rest of the ClientHello, and the connection goes to the wrong route. Give anyRouteNeedsMore an upper bound, so that it only considers the routes before a given index, and consult it in the catch-all guard as well. Routes after the catch-all route have a lower priority, so they must not hold it back. The retry check at the bottom of the loop considers all routes, as before. Assisted-by: claude-code - Anthropic - Claude Opus 5.5 * http: Test a catch-all route after a tls route with a split ClientHello Add cases with a catch-all route (no matchers, no handlers) after the tls route, with and without an http route between them. The catch-all route must wait for the tls route that still needs more data, so the tls route must match the ClientHello split across TCP segments. If the catch-all route matches first, test_handler is never called and the case fails. Without the routes.go change, the case with the http route fails: the http route rules itself out from the first segment and lets the catch-all route match. Assisted-by: claude-code - Anthropic - Claude Opus 5.5 --------- Co-authored-by: vnxme <46669194+vnxme@users.noreply.github.com> |
History
|
|
|---|---|---|
| .github | ||
| docs | ||
| integration | ||
| layer4 | ||
| modules | ||
| .editorconfig | ||
| .gitattributes | ||
| .gitignore | ||
| .golangci.yml | ||
| go.mod | ||
| go.sum | ||
| imports.go | ||
| LICENSE | ||
| README.md | ||
Project Conncept: a TCP/UDP app for Caddy
Project Conncept is an experimental layer 4 app for Caddy. It facilitates composable handling of raw TCP/UDP connections based on properties of the connection or the beginning of the stream.
With it, you can listen on sockets/ports and express logic such as:
- "Echo all input back to the client."
- "Proxy all the raw bytes to 10.0.3.14:1592."
- "If connection is TLS, terminate TLS then proxy all bytes to :5000."
- "Terminate TLS; then if it is HTTP, proxy to localhost:80; otherwise echo."
- "If connection is TLS, proxy to :443 without terminating; if HTTP, proxy to :80; if SSH, proxy to :22."
- "If the HTTP Host is
example.comor the TLS ServerName isexample.com, then proxy to 192.168.0.4." - "Block connections from these IP ranges: ..."
- "Throttle data flow to simulate slow connections."
- And much more!
⚠️ This app is very capable and flexible, but is still in development. Please expect breaking changes.
Because this is a Caddy app, it can be used alongside other Caddy apps such as the HTTP server or TLS certificate manager.
Note
This is not an official repository of the Caddy Web Server organization.
Introduction
Important
Documentation is available in the docs directory. For better understanding, you may also read the code, especially type definitions and their comments. It's actually a pretty simple code base. See below for tips and examples writing config.
This app works similarly to the http app. You define servers, and each server consists of
routes. A route has a set of matchers and handlers;
if a connection matches, the associated handlers are invoked.
Refer the docs for lists of matchers and handlers included in the package.
Compiling
The recommended way is to use xcaddy:
$ xcaddy build --with github.com/mholt/caddy-l4
Alternatively, to hack on the plugin code, you can clone it down, then build and run like so:
- Download or clone this repo:
git clone https://github.com/mholt/caddy-l4.git - In the project folder, run
xcaddyjust like you would runcaddy. For example:xcaddy list-modules --versions(you should see thelayer4modules).
Writing config
This app supports Caddyfile, but you may also use Caddy's native JSON format to configure it. I highly recommend this caddy-json-schema plugin by @abiosoft which can give you auto-complete and documentation right in your editor as you write your config!
See below for some examples to help you get started.
Config examples
The following configuration examples are included in the documentation:
- Combining Apps
- DNS-over-TLS
- Echo Server
- HTTP & HTTPS Mix
- IMAPS with Proxy Protocol
- Mutual TLS
- Postgres Matching
- Postgres-over-TLS
- SOCKS Proxy
- SSH-over-TLS
- TLS ALPN Routing
- TLS SNI Dynamic Upstreams
Other examples could be found in the documentation files describing specific matchers and handlers, as well as in issues and pull requests.