-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fix serverClientIp function that panic if non RouteHandler scope.
- Loading branch information
tigerwill90
committed
Jan 25, 2025
1 parent
fdb4379
commit 7783d1e
Showing
7 changed files
with
141 additions
and
72 deletions.
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
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,50 @@ | ||
package clientip | ||
|
||
import ( | ||
"github.com/tigerwill90/fox" | ||
"github.com/tigerwill90/fox/clientip" | ||
"net" | ||
) | ||
|
||
var DefaultResolver = newChain( | ||
must(clientip.NewLeftmostNonPrivate(clientip.XForwardedForKey, 15)), | ||
must(clientip.NewLeftmostNonPrivate(clientip.ForwardedKey, 15)), | ||
must(clientip.NewSingleIPHeader(fox.HeaderXRealIP)), | ||
must(clientip.NewSingleIPHeader(fox.HeaderCFConnectionIP)), | ||
must(clientip.NewSingleIPHeader(fox.HeaderTrueClientIP)), | ||
must(clientip.NewSingleIPHeader(fox.HeaderFastClientIP)), | ||
must(clientip.NewSingleIPHeader(fox.HeaderXAzureClientIP)), | ||
must(clientip.NewSingleIPHeader(fox.HeaderXAppengineRemoteAddr)), | ||
must(clientip.NewSingleIPHeader(fox.HeaderFlyClientIP)), | ||
must(clientip.NewSingleIPHeader(fox.HeaderXAzureSocketIP)), | ||
clientip.NewRemoteAddr(), | ||
) | ||
|
||
type chain struct { | ||
resolvers []fox.ClientIPResolver | ||
} | ||
|
||
func newChain(resolvers ...fox.ClientIPResolver) chain { | ||
return chain{resolvers: resolvers} | ||
} | ||
|
||
// ClientIP try to derive the client IP using this resolver chain. | ||
func (s chain) ClientIP(c fox.Context) (*net.IPAddr, error) { | ||
var lastErr error | ||
for _, sub := range s.resolvers { | ||
ipAddr, err := sub.ClientIP(c) | ||
if err == nil { | ||
return ipAddr, nil | ||
} | ||
lastErr = err | ||
} | ||
|
||
return nil, lastErr | ||
} | ||
|
||
func must(resolver fox.ClientIPResolver, err error) fox.ClientIPResolver { | ||
if err != nil { | ||
panic(err) | ||
} | ||
return resolver | ||
} |
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