-
-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ipv6): replace bad regex with custom IPv6 extract function
- Fix HTTP IPv6 fetching invalid extraction result - Affects IPv6 comparison for allinkl, dnsomatic, google, he and noip
- Loading branch information
Showing
11 changed files
with
230 additions
and
155 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
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 |
---|---|---|
@@ -1,41 +1,13 @@ | ||
package utils | ||
|
||
import ( | ||
"net/netip" | ||
"regexp" | ||
) | ||
|
||
var ( | ||
regexEmail = regexp.MustCompile(`[a-zA-Z0-9-_.+]+@[a-zA-Z0-9-_.]+\.[a-zA-Z]{2,10}`) | ||
regexIPv4 = regexp.MustCompile(`(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}`) | ||
regexIPv6 = regexp.MustCompile(`(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))`) //nolint:lll | ||
) | ||
|
||
func MatchEmail(email string) bool { | ||
return regexEmail.MatchString(email) | ||
} | ||
|
||
func FindIPv4Addresses(text string) (addresses []netip.Addr) { | ||
const n = -1 | ||
ipv4Strings := regexIPv4.FindAllString(text, n) | ||
return mustParseIPAddresses(ipv4Strings) | ||
} | ||
|
||
func FindIPv6Addresses(text string) (addresses []netip.Addr) { | ||
const n = -1 | ||
ipv6Strings := regexIPv6.FindAllString(text, n) | ||
return mustParseIPAddresses(ipv6Strings) | ||
} | ||
|
||
func mustParseIPAddresses(ipStrings []string) (addresses []netip.Addr) { | ||
if len(ipStrings) == 0 { | ||
return nil | ||
} | ||
|
||
addresses = make([]netip.Addr, len(ipStrings)) | ||
for i, ipString := range ipStrings { | ||
addresses[i] = netip.MustParseAddr(ipString) | ||
} | ||
|
||
return addresses | ||
} |
This file was deleted.
Oops, something went wrong.
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,52 @@ | ||
package ipextract | ||
|
||
import ( | ||
"net/netip" | ||
"strings" | ||
) | ||
|
||
// IPv4 extracts all valid IPv4 addresses from a given | ||
// text string. Each IPv4 address must be separated by a character | ||
// not part of the IPv4 alphabet (0123456789.). | ||
// Performance-wise, this extraction is at least x3 times faster | ||
// than using a regular expression. | ||
func IPv4(text string) (addresses []netip.Addr) { | ||
const ipv4Alphabet = "0123456789." | ||
return extract(text, ipv4Alphabet) | ||
} | ||
|
||
// IPv6 extracts all valid IPv6 addresses from a given | ||
// text string. Each IPv6 address must be separated by a character | ||
// not part of the IPv6 alphabet (0123456789abcdefABCDEF:). | ||
// Performance-wise, this extraction is at least x3 times faster | ||
// than using a regular expression. | ||
func IPv6(text string) (addresses []netip.Addr) { | ||
const ipv6Alphabet = "0123456789abcdefABCDEF:" | ||
return extract(text, ipv6Alphabet) | ||
} | ||
|
||
func extract(text string, alphabet string) (addresses []netip.Addr) { | ||
var start, end int | ||
for { | ||
for i := start; i < len(text); i++ { | ||
r := rune(text[i]) | ||
if !strings.ContainsRune(alphabet, r) { | ||
break | ||
} | ||
end++ | ||
} | ||
|
||
possibleIPString := text[start:end] | ||
ipAddress, err := netip.ParseAddr(possibleIPString) | ||
if err == nil { // Valid IP address found | ||
addresses = append(addresses, ipAddress) | ||
} | ||
|
||
if end == len(text) { | ||
return addresses | ||
} | ||
|
||
start = end + 1 // + 1 to skip non alphabet match character | ||
end = start | ||
} | ||
} |
Oops, something went wrong.