-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add network blocking features to HTTP loader (#241)
- Loading branch information
Showing
7 changed files
with
252 additions
and
5 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,34 @@ | ||
package config | ||
|
||
import ( | ||
"net" | ||
"strings" | ||
) | ||
|
||
// CIDRSliceFlag is a flag type which support comma separated CIDR expressions. | ||
type CIDRSliceFlag []*net.IPNet | ||
|
||
func (s *CIDRSliceFlag) String() string { | ||
var ss []string | ||
for _, v := range *s { | ||
ss = append(ss, v.String()) | ||
} | ||
return strings.Join(ss, ",") | ||
} | ||
|
||
func (s *CIDRSliceFlag) Set(value string) error { | ||
var res []*net.IPNet | ||
for _, v := range strings.Split(value, ",") { | ||
_, network, err := net.ParseCIDR(v) | ||
if err != nil { | ||
return err | ||
} | ||
res = append(res, network) | ||
} | ||
*s = res | ||
return nil | ||
} | ||
|
||
func (c *CIDRSliceFlag) Get() any { | ||
return c | ||
} |
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,23 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCIDRSliceFlag(t *testing.T) { | ||
t.Run("set and get", func(t *testing.T) { | ||
var f CIDRSliceFlag | ||
input := "127.0.0.0/12,200.100.0.0/28" | ||
assert.NoError(t, f.Set(input)) | ||
assert.Equal(t, input, f.String()) | ||
assert.Equal(t, &f, f.Get()) | ||
|
||
}) | ||
t.Run("parse error", func(t *testing.T) { | ||
var f CIDRSliceFlag | ||
input := "127.0.0.0/12,200.100.0.0/28." | ||
assert.Error(t, f.Set(input)) | ||
}) | ||
} |
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