-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
core: add modular network_proxy
support
#6399
Open
mohammed90
wants to merge
8
commits into
master
Choose a base branch
from
forward-proxy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+350
−12
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
62c711c
core: add modular `network_proxy` support
mohammed90 5436774
move modules around
mohammed90 32d1c5e
Merge branch 'master' into forward-proxy
mohammed90 6836afb
Merge branch 'master' into forward-proxy
mohammed90 18cd1c0
add caddyfile implementation
mohammed90 38bb05c
address feedbcak
mohammed90 908fa5e
Apply suggestions from code review
mohammed90 b8a9870
adapt ForwardProxyURL to use the NetworkProxyRaw
mohammed90 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package caddy | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
func init() { | ||
RegisterModule(ProxyFromEnvironment{}) | ||
mohammed90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
RegisterModule(ProxyFromURL{}) | ||
} | ||
|
||
type ProxyFuncProducer interface { | ||
ProxyFunc() func(*http.Request) (*url.URL, error) | ||
} | ||
|
||
type ProxyFromEnvironment struct{} | ||
|
||
// ProxyFunc implements ProxyFuncProducer. | ||
func (p ProxyFromEnvironment) ProxyFunc() func(*http.Request) (*url.URL, error) { | ||
return http.ProxyFromEnvironment | ||
} | ||
|
||
// CaddyModule implements Module. | ||
func (p ProxyFromEnvironment) CaddyModule() ModuleInfo { | ||
return ModuleInfo{ | ||
ID: "caddy.network_proxy.source.environment", | ||
New: func() Module { | ||
return ProxyFromEnvironment{} | ||
}, | ||
} | ||
} | ||
|
||
type ProxyFromURL struct { | ||
URL string `json:"url"` | ||
|
||
ctx Context | ||
logger *zap.Logger | ||
} | ||
|
||
// CaddyModule implements Module. | ||
func (p ProxyFromURL) CaddyModule() ModuleInfo { | ||
return ModuleInfo{ | ||
ID: "caddy.network_proxy.source.url", | ||
New: func() Module { | ||
return &ProxyFromURL{} | ||
}, | ||
} | ||
} | ||
|
||
func (p *ProxyFromURL) Provision(ctx Context) error { | ||
p.ctx = ctx | ||
p.logger = ctx.Logger() | ||
return nil | ||
} | ||
|
||
// Validate implements Validator. | ||
func (p ProxyFromURL) Validate() error { | ||
if _, err := url.Parse(p.URL); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// ProxyFunc implements ProxyFuncProducer. | ||
func (p ProxyFromURL) ProxyFunc() func(*http.Request) (*url.URL, error) { | ||
if strings.Contains(p.URL, "{") && strings.Contains(p.URL, "}") { | ||
// courtesy of @ImpostorKeanu: https://github.com/caddyserver/caddy/pull/6397 | ||
return func(r *http.Request) (*url.URL, error) { | ||
// retrieve the replacer from context. | ||
repl, ok := r.Context().Value(ReplacerCtxKey).(*Replacer) | ||
if !ok { | ||
err := errors.New("failed to obtain replacer from request") | ||
p.logger.Error(err.Error()) | ||
return nil, err | ||
} | ||
|
||
// apply placeholders to the value | ||
// note: h.ForwardProxyURL should never be empty at this point | ||
s := repl.ReplaceAll(p.URL, "") | ||
if s == "" { | ||
p.logger.Error("forward_proxy_url was empty after applying placeholders", | ||
zap.String("initial_value", p.URL), | ||
zap.String("final_value", s), | ||
zap.String("hint", "check for invalid placeholders")) | ||
return nil, errors.New("empty value for forward_proxy_url") | ||
} | ||
|
||
// parse the url | ||
pUrl, err := url.Parse(s) | ||
if err != nil { | ||
p.logger.Warn("failed to derive transport proxy from forward_proxy_url") | ||
pUrl = nil | ||
} else if pUrl.Host == "" || strings.Split("", pUrl.Host)[0] == ":" { | ||
// url.Parse does not return an error on these values: | ||
// | ||
// - http://:80 | ||
// - pUrl.Host == ":80" | ||
// - /some/path | ||
// - pUrl.Host == "" | ||
// | ||
// Super edge cases, but humans are human. | ||
err = errors.New("supplied forward_proxy_url is missing a host value") | ||
pUrl = nil | ||
} else { | ||
p.logger.Debug("setting transport proxy url", zap.String("url", s)) | ||
} | ||
|
||
return pUrl, err | ||
} | ||
} | ||
return func(*http.Request) (*url.URL, error) { | ||
return url.Parse(p.URL) | ||
} | ||
} | ||
|
||
var ( | ||
_ Module = ProxyFromEnvironment{} | ||
_ ProxyFuncProducer = ProxyFromEnvironment{} | ||
_ Module = ProxyFromURL{} | ||
_ Provisioner = &ProxyFromURL{} | ||
_ Validator = ProxyFromURL{} | ||
_ ProxyFuncProducer = ProxyFromURL{} | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs expanded godoc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.