-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull request: do not apply denyallow to IP addresses
Merge in DNS/urlfilter from denyallowips to master Related to AdguardTeam/AdGuardHome#3175 Squashed commit of the following: commit adb8306 Author: Andrey Meshkov <am@adguard.com> Date: Tue Dec 14 17:41:40 2021 +0300 fix golangci-lint commit becb0d7 Author: Andrey Meshkov <am@adguard.com> Date: Tue Dec 14 17:37:44 2021 +0300 fix review issues commit 028d7f8 Author: Andrey Meshkov <am@adguard.com> Date: Tue Dec 14 17:28:46 2021 +0300 added bench, fixed specs commit 13a71a8 Author: Andrey Meshkov <am@adguard.com> Date: Tue Dec 14 17:14:12 2021 +0300 do not apply denyallow to IP addresses
- Loading branch information
Showing
10 changed files
with
173 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package filterutil | ||
|
||
import "net" | ||
|
||
// ParseIP parses the string and checks if it's a valid IP address or not. It | ||
// uses net.ParseIP internally and the purpose of this wrapper is to first do a | ||
// quick check without additional allocations. | ||
func ParseIP(s string) (ip net.IP) { | ||
for _, c := range s { | ||
if c != '.' && c != ':' && | ||
(c < '0' || c > '9') && | ||
(c < 'A' || c > 'F') && | ||
(c < 'a' || c > 'f') && | ||
c != '[' && c != ']' { | ||
return nil | ||
} | ||
} | ||
|
||
return net.ParseIP(s) | ||
} |
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,48 @@ | ||
package filterutil | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func BenchmarkParseIP(b *testing.B) { | ||
b.Run("filterutil.ParseIP", func(b *testing.B) { | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
ParseIP("domain.com") | ||
ParseIP("invalid:ip") | ||
} | ||
}) | ||
|
||
b.Run("net.ParseIP", func(b *testing.B) { | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
net.ParseIP("domain.com") | ||
net.ParseIP("invalid:ip") | ||
} | ||
}) | ||
} | ||
|
||
func TestParseIP(t *testing.T) { | ||
testCases := []struct { | ||
s string | ||
}{{ | ||
s: "127.0.0.1", | ||
}, { | ||
s: "random string", | ||
}, { | ||
s: "2001:0db8:0000:0000:0000:8a2e:0370:7334", | ||
}, { | ||
s: "[2001:db8::8a2e:370:7334]", | ||
}} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.s, func(t *testing.T) { | ||
require.Equal(t, net.ParseIP(tc.s), ParseIP(tc.s)) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.