-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: regex for ipv6 #4
feat: regex for ipv6 #4
Conversation
I'll give it a test soon! Looks good. |
What was wrong with netmask? |
EDIT: rs/node-netmask#8 I would like support on that, but I am not familiar with coffeescript at all |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me
npm run test
168 passed Also tested the inverse of the test and 168 failed which is good. Here's upstream of is-ip anyway: https://github.com/sindresorhus/ip-regex/blob/master/index.js |
Thanks for the PR! Good work. Works fine for me as well. Could you please update the package version also, @vasco-santos & I'll merge it. |
Sure, added a new commit for the release. |
@vasco-santos Cool, thanks! |
@@ -29,9 +31,27 @@ function netmaskCheck (params) { | |||
for (let r of privateRanges) { | |||
if (r.contains(params)) return true | |||
} | |||
return false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, @vasco-santos, why did you remove this return statement? Now when I test it by passing public IP it returns undefiled
rather than true
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh this is a bug! I was initially doing everything in the same function, as ipv6 being use d as a fallback for the ipv4 validation. But I moved later. I am going to PR this
} | ||
|
||
export default (ip) => { | ||
if (isIp.v4(ip) || ip.startsWith('0')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, pardon me If I'm misunderstanding, but why add ip.startsWith('0')
if netmaskCheck()
already handles it? IMHO we could just have:
export default ip => netmaskCheck(ip) || ipv6Check(ip)
or
export default ip => {
if (isIp.v4(ip)) return netmaskCheck(ip)
else if (isIp.v6(ip)) return ipv6Check(ip)
else return false
}
Update: isIp.v6(ip)
check does not pass when running tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the first was my initial approach. But as I mentioned in the PR description the ipv4 with 0.0.0.0.0
is not a valid ipv4 per the isIp.v4()
, which is correct... So this should not really be handled in ipv4
.
The second approach suffer from the same issue I believe. 0.0.0.0.0
will not be nor ipv4, nor ipv6 and it will return false, when it should be true. We can figure out a better solution for such addresses
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've modified second code snippet:
export default ip => {
if (isIp.v4(ip) || ip.startsWith('0')) return netmaskCheck(ip)
else if (isIp.v6(ip)) return ipv6Check(ip)
else return false
}
and getting this:
t.truthy(isPrivate(ip), true)
| |
false "::ffff:0.0.255.255.255"
-----
t.truthy(isPrivate(ip), true)
| |
false "::ffff:0.255.255.255.255"
-----
t.truthy(isPrivate(ip), true)
| |
false "100::ffff::"
seems like isIp
is not picking up on those...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, perhaps creating an issue to rethink this logic is a better way
Sorry, @vasco-santos, for rising these points after code has been merged. I am just wondering for future improvements. |
This PR adds regex for
ipv6
addresses per #3 . I needed to do a special validation for addresses started by0
as they were not being consideredipv4
. Open to improve this solutionThis was based on https://en.m.wikipedia.org/wiki/Reserved_IP_addresses#IPv6
cc @sickcodes @frenchbread