-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
set engine.TrustedProxies For items that don't use gin.RUN #2692
Changes from all commits
08dd235
16691a3
936c45d
824f8dd
3496c8a
126f8aa
84244fe
60dfff7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -326,11 +326,11 @@ func iterate(path, method string, routes RoutesInfo, root *node) RoutesInfo { | |
func (engine *Engine) Run(addr ...string) (err error) { | ||
defer func() { debugPrintError(err) }() | ||
|
||
trustedCIDRs, err := engine.prepareTrustedCIDRs() | ||
err = engine.parseTrustedProxies() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not add this to all There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the other My core idea was to be able to dynamically adjust the TurstedProxies. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if err != nil { | ||
return err | ||
} | ||
engine.trustedCIDRs = trustedCIDRs | ||
|
||
address := resolveAddress(addr) | ||
debugPrint("Listening and serving HTTP on %s\n", address) | ||
err = http.ListenAndServe(address, engine) | ||
|
@@ -366,6 +366,19 @@ func (engine *Engine) prepareTrustedCIDRs() ([]*net.IPNet, error) { | |
return cidr, nil | ||
} | ||
|
||
// SetTrustedProxies set Engine.TrustedProxies | ||
func (engine *Engine) SetTrustedProxies(trustedProxies []string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add some unit test, thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
engine.TrustedProxies = trustedProxies | ||
return engine.parseTrustedProxies() | ||
} | ||
|
||
// parseTrustedProxies parse Engine.TrustedProxies to Engine.trustedCIDRs | ||
func (engine *Engine) parseTrustedProxies() error { | ||
trustedCIDRs, err := engine.prepareTrustedCIDRs() | ||
engine.trustedCIDRs = trustedCIDRs | ||
return err | ||
} | ||
|
||
// parseIP parse a string representation of an IP and returns a net.IP with the | ||
// minimum byte representation or nil if input is invalid. | ||
func parseIP(ip string) net.IP { | ||
|
@@ -387,6 +400,11 @@ func (engine *Engine) RunTLS(addr, certFile, keyFile string) (err error) { | |
debugPrint("Listening and serving HTTPS on %s\n", addr) | ||
defer func() { debugPrintError(err) }() | ||
|
||
err = engine.parseTrustedProxies() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = http.ListenAndServeTLS(addr, certFile, keyFile, engine) | ||
return | ||
} | ||
|
@@ -398,6 +416,11 @@ func (engine *Engine) RunUnix(file string) (err error) { | |
debugPrint("Listening and serving HTTP on unix:/%s", file) | ||
defer func() { debugPrintError(err) }() | ||
|
||
err = engine.parseTrustedProxies() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
listener, err := net.Listen("unix", file) | ||
if err != nil { | ||
return | ||
|
@@ -416,6 +439,11 @@ func (engine *Engine) RunFd(fd int) (err error) { | |
debugPrint("Listening and serving HTTP on fd@%d", fd) | ||
defer func() { debugPrintError(err) }() | ||
|
||
err = engine.parseTrustedProxies() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
f := os.NewFile(uintptr(fd), fmt.Sprintf("fd@%d", fd)) | ||
listener, err := net.FileListener(f) | ||
if err != nil { | ||
|
@@ -431,6 +459,12 @@ func (engine *Engine) RunFd(fd int) (err error) { | |
func (engine *Engine) RunListener(listener net.Listener) (err error) { | ||
debugPrint("Listening and serving HTTP on listener what's bind with address@%s", listener.Addr()) | ||
defer func() { debugPrintError(err) }() | ||
|
||
err = engine.parseTrustedProxies() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = http.Serve(listener, engine) | ||
return | ||
} | ||
|
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.
I passed the previous unit tests, but I don't understand why the
foo
bar
bar
would expect such a result