Skip to content

Commit

Permalink
handle adding a prefix for plain v4 and v6 addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronhurt authored and magiconair committed Feb 18, 2018
1 parent b88044e commit cce3288
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
12 changes: 10 additions & 2 deletions route/access_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ func (t *Target) denyByIP(ip net.IP) bool {

func (t *Target) parseAccessRule(allowDeny string) error {
var accessTag string
var value string
var temps []string
var value string
var ip net.IP

// init rules if needed
if t.accessRules == nil {
Expand All @@ -123,7 +124,14 @@ func (t *Target) parseAccessRule(allowDeny string) error {
switch accessTag {
case ipAllowTag, ipDenyTag:
if value = strings.TrimSpace(temps[1]); !strings.Contains(value, "/") {
value = value + "/32"
if ip = net.ParseIP(value); ip == nil {
return fmt.Errorf("failed to parse IP %s with error", value)
}
if ip.To4() != nil {
value = ip.String() + "/32"
} else {
value = ip.String() + "/128"
}
}
_, net, err := net.ParseCIDR(value)
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion route/access_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ func TestAccessRules_parseAccessRule(t *testing.T) {
fail: true,
},
{
desc: "single ip with no mask",
desc: "single ipv4 with no mask",
allowDeny: "ip:1.2.3.4",
fail: false,
},
{
desc: "single ipv6 with no mask",
allowDeny: "ip:fe80::1",
fail: false,
},
}

for i, tt := range tests {
Expand Down

0 comments on commit cce3288

Please sign in to comment.