-
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: slightly redesign of the strategy package
- Loading branch information
1 parent
8fcce12
commit 0f75e70
Showing
4 changed files
with
359 additions
and
248 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package strategy | ||
|
||
import "net" | ||
|
||
type config struct { | ||
ipRanges []net.IPNet | ||
} | ||
|
||
type TrustedRangeOption interface { | ||
applyRight(*config) | ||
} | ||
|
||
type BlacklistRangeOption interface { | ||
applyLeft(*config) | ||
} | ||
|
||
type rightmostNonPrivateOptionFunc func(*config) | ||
|
||
func (o rightmostNonPrivateOptionFunc) applyRight(c *config) { | ||
o(c) | ||
} | ||
|
||
type leftmostNonPrivateOptionFunc func(*config) | ||
|
||
func (o leftmostNonPrivateOptionFunc) applyLeft(c *config) { | ||
o(c) | ||
} | ||
|
||
// TrustLoopback enables or disables the inclusion of loopback ip ranges in the trusted ip ranges. | ||
func TrustLoopback(enable bool) TrustedRangeOption { | ||
return rightmostNonPrivateOptionFunc(func(c *config) { | ||
if enable { | ||
c.ipRanges = append(c.ipRanges, loopbackRanges...) | ||
} | ||
}) | ||
} | ||
|
||
// TrustLinkLocal enables or disables the inclusion of link local ip ranges in the trusted ip ranges. | ||
func TrustLinkLocal(enable bool) TrustedRangeOption { | ||
return rightmostNonPrivateOptionFunc(func(c *config) { | ||
if enable { | ||
c.ipRanges = append(c.ipRanges, linkLocalRanges...) | ||
} | ||
}) | ||
} | ||
|
||
// TrustPrivateNet enables or disables the inclusion of private-space ip ranges in the trusted ip ranges. | ||
func TrustPrivateNet(enable bool) TrustedRangeOption { | ||
return rightmostNonPrivateOptionFunc(func(c *config) { | ||
if enable { | ||
c.ipRanges = append(c.ipRanges, privateRange...) | ||
} | ||
}) | ||
} | ||
|
||
// ExcludeLoopback enables or disables the inclusion of loopback ip ranges in the blacklisted ip ranges. | ||
func ExcludeLoopback(enable bool) BlacklistRangeOption { | ||
return leftmostNonPrivateOptionFunc(func(c *config) { | ||
if enable { | ||
c.ipRanges = append(c.ipRanges, loopbackRanges...) | ||
} | ||
}) | ||
} | ||
|
||
// ExcludeLinkLocal enables or disables the inclusion of link local ip ranges in the blacklisted ip ranges. | ||
func ExcludeLinkLocal(enable bool) BlacklistRangeOption { | ||
return leftmostNonPrivateOptionFunc(func(c *config) { | ||
if enable { | ||
c.ipRanges = append(c.ipRanges, linkLocalRanges...) | ||
} | ||
}) | ||
} | ||
|
||
// ExcludePrivateNet enables or disables the inclusion of private-space ip ranges in the blacklisted ip ranges. | ||
func ExcludePrivateNet(enable bool) BlacklistRangeOption { | ||
return leftmostNonPrivateOptionFunc(func(c *config) { | ||
if enable { | ||
c.ipRanges = append(c.ipRanges, privateRange...) | ||
} | ||
}) | ||
} |
Oops, something went wrong.