Skip to content

core: add modular network_proxy support #6399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Mar 21, 2025
Merged

core: add modular network_proxy support #6399

merged 19 commits into from
Mar 21, 2025

Conversation

mohammed90
Copy link
Member

@mohammed90 mohammed90 commented Jun 14, 2024

The PR #5369 introduced support for the standard env vars HTTP_PROXY, HTTPS_PROXY, and NO_PROXY in the reverse_proxy handler. The ACME client in Caddy has always respected the vars. The issue #6111 shows a need for configurable forward-proxy outside the env vars due to the global state of env vars.

The post (https://caddy.community/t/routing-acme-requests-via-http-proxy/24363) in the Caddy forum shows a need for forward-proxy support for ACME (external requests) but not for reverse-proxy upstreams. Again, the global state nature of the env vars impedes any effort to separate those concerns.

To brainstorm the best solution, I'm introducing modular approach, where the proxy address can be explicitly configured via module and falls back to env var. Discussion and iteration on this PR is necessary to ensure a common satisfactory solution is reached. I haven't wired up the Caddyfile parts.

CC/ @ImpostorKeanu

Note if this approach is acceptable, the ForwardProxyURL field will be deprecated in favor of the "from": "url" module.

TODO:

  • Caddyfile
  • Tests

Co-authored-by: @ImpostorKeanu
Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com>
@mohammed90 mohammed90 added in progress 🏃‍♂️ Being actively worked on discussion 💬 The right solution needs to be found needs docs ✍️ Requires documentation changes needs tests 💯 Requires automated tests labels Jun 14, 2024
@ImpostorKeanu
Copy link
Contributor

Slick! I support everything about this!

Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com>
Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com>
@mohammed90 mohammed90 marked this pull request as ready for review August 24, 2024 13:11
Copy link
Member

@francislavoie francislavoie left a comment

Choose a reason for hiding this comment

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

Design makes sense to me 👍

caddy.RegisterModule(ProxyFromNone{})
}

// The "url" proxy source uses the defined URL as the proxy
Copy link
Member

Choose a reason for hiding this comment

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

Don't we usually start godoc comments with the name of the type as the first word? I don't know why that's the convention but I think that's what we typically do?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a Go idiom. However, we pick up the same doc lines for our documentation, so I had to make a judgement call to either make it sensible for our documentation or meet the informal convention of Go docs. It's less confusing for our users to see the module name instead of the other way around.

Copy link
Member

Choose a reason for hiding this comment

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

Sometimes linters/vet will catch this, but if not, I guess we can keep it as-is.

I had planned at one point to tune our Caddy docs to account for the Godoc convention and help eliminate the repetition, but I haven't gotten around to it yet.

mohammed90 and others added 2 commits August 27, 2024 10:48
Co-authored-by: Francis Lavoie <lavofr@gmail.com>
Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com>
@mohammed90 mohammed90 linked an issue Aug 29, 2024 that may be closed by this pull request
@mohammed90
Copy link
Member Author

I forgot about this PR. @mholt, @francislavoie, should we move it to 2.10? I'm fine either way.

@mohammed90 mohammed90 removed in progress 🏃‍♂️ Being actively worked on needs docs ✍️ Requires documentation changes needs tests 💯 Requires automated tests labels Dec 29, 2024
@mohammed90 mohammed90 added the under review 🧐 Review is pending before merging label Dec 29, 2024
@mholt
Copy link
Member

mholt commented Dec 30, 2024

Hm, yeah, that might be best. Thanks for updating and we'll get back to this soon!

@mholt mholt added this to the v2.10.0-beta.1 milestone Dec 30, 2024
@mholt mholt modified the milestones: v2.10.0-beta.1, v2.10.0-beta.2 Mar 6, 2025
Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

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

Thanks Mohammed! Looking pretty good, just a few nits on my end.

caddy.RegisterModule(ProxyFromNone{})
}

// The "url" proxy source uses the defined URL as the proxy
Copy link
Member

Choose a reason for hiding this comment

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

Sometimes linters/vet will catch this, but if not, I guess we can keep it as-is.

I had planned at one point to tune our Caddy docs to account for the Godoc convention and help eliminate the repetition, but I haven't gotten around to it yet.

Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
@mholt mholt modified the milestones: v2.10.0-beta.2, v2.10.0-beta.3 Mar 8, 2025
mohammed90 and others added 4 commits March 14, 2025 04:42
Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com>
Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com>
Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

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

LGTM, we can give it a try. Thanks @mohammed90 !

@mohammed90 mohammed90 enabled auto-merge (squash) March 21, 2025 17:01
@mohammed90 mohammed90 merged commit 1735730 into master Mar 21, 2025
20 checks passed
@mohammed90 mohammed90 deleted the forward-proxy branch March 21, 2025 17:06
@mholt mholt removed the under review 🧐 Review is pending before merging label Mar 21, 2025
@CzBiX
Copy link

CzBiX commented Apr 27, 2025

This chages seems to have broken the existing forward_proxy_url usage with panic:

panic: reflect: call of reflect.Value.Type on zero Value
goroutine 1 [running]:
reflect.Value.abiTypeSlow({0x0?, 0x0?, 0x0?})
	reflect/value.go:2400 +0xee
reflect.Value.typeSlow({0x0?, 0x0?, 0x8?})
	reflect/value.go:2388 +0x1d
reflect.Value.Type(...)
	reflect/value.go:2383
github.com/caddyserver/caddy/v2.Context.LoadModule({{0x1ed44f8, 0xc0002269c0}, 0xc0001fc690, 0xc0002073b0, {0xc000259b40, 0x4, 0x4}, {0x0, 0x0, 0x0}, ...}, ...)
	github.com/caddyserver/caddy/v2@v2.10.0/context.go:181 +0xee
github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy.(*HTTPTransport).NewTransport(0xc000441a40, {{0x1ed44f8, 0xc0002269c0}, 0xc0001fc690, 0xc0002073b0, {0xc000259b40, 0x4, 0x4}, {0x0, 0x0, ...}, ...})
	github.com/caddyserver/caddy/v2@v2.10.0/modules/caddyhttp/reverseproxy/httptransport.go:356 +0x80f
github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy.(*HTTPTransport).Provision(0xc000441a40, {{0x1ed44f8, 0xc0002269c0}, 0xc0001fc690, 0xc0002073b0, {0xc000259b40, 0x4, 0x4}, {0x0, 0x0, ...}, ...})
	github.com/caddyserver/caddy/v2@v2.10.0/modules/caddyhttp/reverseproxy/httptransport.go:181 +0xc5
github.com/caddyserver/caddy/v2.Context.LoadModuleByID({{0x1ed44f8, 0xc0002269c0}, 0xc0001fc690, 0xc0002073b0, {0xc000259b40, 0x4, 0x4}, {0x0, 0x0, 0x0}, ...}, ...)
	github.com/caddyserver/caddy/v2@v2.10.0/context.go:410 +0x755
github.com/caddyserver/caddy/v2.Context.loadModuleInline({{0x1ed44f8, 0xc0002269c0}, 0xc0001fc690, 0xc0002073b0, {0xc000259b40, 0x3, 0x4}, {0x0, 0x0, 0x0}, ...}, ...)
	github.com/caddyserver/caddy/v2@v2.10.0/context.go:468 +0xf0
github.com/caddyserver/caddy/v2.Context.LoadModule({{0x1ed44f8, 0xc0002269c0}, 0xc0001fc690, 0xc0002073b0, {0xc000259b40, 0x3, 0x4}, {0x0, 0x0, 0x0}, ...}, ...)
	github.com/caddyserver/caddy/v2@v2.10.0/context.go:209 +0x426
github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy.(*Handler).Provision(0xc00077e960, {{0x1ed44f8, 0xc0002269c0}, 0xc0001fc690, 0xc0002073b0, {0xc000259b40, 0x3, 0x4}, {0x0, 0x0, ...}, ...})
	github.com/caddyserver/caddy/v2@v2.10.0/modules/caddyhttp/reverseproxy/reverseproxy.go:241 +0x2b5

@mohammed90
Copy link
Member Author

This chages seems to have broken the existing forward_proxy_url usage with panic:

panic: reflect: call of reflect.Value.Type on zero Value
goroutine 1 [running]:
reflect.Value.abiTypeSlow({0x0?, 0x0?, 0x0?})
	reflect/value.go:2400 +0xee
reflect.Value.typeSlow({0x0?, 0x0?, 0x8?})
	reflect/value.go:2388 +0x1d
reflect.Value.Type(...)
	reflect/value.go:2383
github.com/caddyserver/caddy/v2.Context.LoadModule({{0x1ed44f8, 0xc0002269c0}, 0xc0001fc690, 0xc0002073b0, {0xc000259b40, 0x4, 0x4}, {0x0, 0x0, 0x0}, ...}, ...)
	github.com/caddyserver/caddy/v2@v2.10.0/context.go:181 +0xee
github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy.(*HTTPTransport).NewTransport(0xc000441a40, {{0x1ed44f8, 0xc0002269c0}, 0xc0001fc690, 0xc0002073b0, {0xc000259b40, 0x4, 0x4}, {0x0, 0x0, ...}, ...})
	github.com/caddyserver/caddy/v2@v2.10.0/modules/caddyhttp/reverseproxy/httptransport.go:356 +0x80f
github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy.(*HTTPTransport).Provision(0xc000441a40, {{0x1ed44f8, 0xc0002269c0}, 0xc0001fc690, 0xc0002073b0, {0xc000259b40, 0x4, 0x4}, {0x0, 0x0, ...}, ...})
	github.com/caddyserver/caddy/v2@v2.10.0/modules/caddyhttp/reverseproxy/httptransport.go:181 +0xc5
github.com/caddyserver/caddy/v2.Context.LoadModuleByID({{0x1ed44f8, 0xc0002269c0}, 0xc0001fc690, 0xc0002073b0, {0xc000259b40, 0x4, 0x4}, {0x0, 0x0, 0x0}, ...}, ...)
	github.com/caddyserver/caddy/v2@v2.10.0/context.go:410 +0x755
github.com/caddyserver/caddy/v2.Context.loadModuleInline({{0x1ed44f8, 0xc0002269c0}, 0xc0001fc690, 0xc0002073b0, {0xc000259b40, 0x3, 0x4}, {0x0, 0x0, 0x0}, ...}, ...)
	github.com/caddyserver/caddy/v2@v2.10.0/context.go:468 +0xf0
github.com/caddyserver/caddy/v2.Context.LoadModule({{0x1ed44f8, 0xc0002269c0}, 0xc0001fc690, 0xc0002073b0, {0xc000259b40, 0x3, 0x4}, {0x0, 0x0, 0x0}, ...}, ...)
	github.com/caddyserver/caddy/v2@v2.10.0/context.go:209 +0x426
github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy.(*Handler).Provision(0xc00077e960, {{0x1ed44f8, 0xc0002269c0}, 0xc0001fc690, 0xc0002073b0, {0xc000259b40, 0x3, 0x4}, {0x0, 0x0, ...}, ...})
	github.com/caddyserver/caddy/v2@v2.10.0/modules/caddyhttp/reverseproxy/reverseproxy.go:241 +0x2b5

Fixed in 737936c

@mholt
Copy link
Member

mholt commented Apr 28, 2025

Thanks @mohammed90 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion 💬 The right solution needs to be found
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Placeholders for forward_proxy_url
6 participants