-
Notifications
You must be signed in to change notification settings - Fork 576
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support ipv6 address when set endpoint (#482)
* fix: support ipv6 address when set endpoint * test: add test case for ipv6
- Loading branch information
1 parent
ec8ae25
commit 1c7616d
Showing
4 changed files
with
97 additions
and
4 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,14 @@ | ||
|
||
// it provide commont methods for node and browser , we will add more solutions later in this file | ||
|
||
/** | ||
* Judge isIP include ipv4 or ipv6 | ||
* @param {String} options | ||
* @return {Array} the multipart uploads | ||
*/ | ||
exports._isIP = function(host) { | ||
const ipv4Regex = /^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$/; | ||
const ipv6Regex = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/; | ||
const isIP = ipv4Regex.test(host) || ipv6Regex.test(host); | ||
return isIP; | ||
}; |
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,79 @@ | ||
|
||
const utils = require('../../lib/common/utils') | ||
const assert = require('assert'); | ||
|
||
describe('test/utils.test.js', () => { | ||
it('ipv4 test', ()=> { | ||
//first length is 3 | ||
assert.equal(utils._isIP("200.255.255.255"),true) | ||
assert.equal(utils._isIP("223.255.255.255"),true) | ||
assert.equal(utils._isIP("224.255.255.255"),true) | ||
assert.equal(utils._isIP("192.0.0.1"),true) | ||
assert.equal(utils._isIP("127.0.0.1"),true) | ||
assert.equal(utils._isIP("100.0.0.1"),true) | ||
assert.equal(utils._isIP("090.0.0.1"),true) | ||
assert.equal(utils._isIP("009.0.0.1"),true) | ||
assert.equal(utils._isIP("200.001.255.255"),true) | ||
|
||
// first length is 1 or 2 | ||
assert.equal(utils._isIP("09.255.255.255"),true) | ||
assert.equal(utils._isIP("90.255.255.255"),true) | ||
assert.equal(utils._isIP("00.255.255.255"),true) | ||
assert.equal(utils._isIP("-.0.0.1"),false) | ||
assert.equal(utils._isIP("0.0.0.1"),true) | ||
assert.equal(utils._isIP("1.0.0.1"),true) | ||
|
||
// test last 3 byte | ||
assert.equal(utils._isIP("200.0.255.255"),true) | ||
assert.equal(utils._isIP("200.01.255.255"),true) | ||
assert.equal(utils._isIP("200.10.255.255"),true) | ||
assert.equal(utils._isIP("200.256.255.255"),false) | ||
assert.equal(utils._isIP("200.001.255.255"),true) | ||
|
||
assert.equal(utils._isIP("200.255.0.255"),true) | ||
assert.equal(utils._isIP("200.255.01.255"),true) | ||
assert.equal(utils._isIP("200.255.10.255"),true) | ||
assert.equal(utils._isIP("200.255.256.255"),false) | ||
assert.equal(utils._isIP("200.255.001.255"),true) | ||
|
||
assert.equal(utils._isIP("200.255.255.0"),true) | ||
assert.equal(utils._isIP("200.255.255.01"),true) | ||
assert.equal(utils._isIP("200.255.255.10"),true) | ||
assert.equal(utils._isIP("200.255.255.256"),false) | ||
assert.equal(utils._isIP("200.255.255.001"),true) | ||
|
||
//excetion | ||
assert.equal(utils._isIP("200.255.255.001"),true) | ||
assert.equal(utils._isIP("200"),false) | ||
assert.equal(utils._isIP("200.1"),false) | ||
assert.equal(utils._isIP("200.1.1"),false) | ||
assert.equal(utils._isIP("200.1.1.1.1"),false) | ||
}) | ||
it("ipv6 test", () => { | ||
assert.equal(utils._isIP("1:2:3:4:5:6:7::"),true) | ||
assert.equal(utils._isIP("1:2:3:4:5:6:7:8"),true) | ||
assert.equal(utils._isIP("1:2:3:4:5:6::"),true) | ||
assert.equal(utils._isIP("1:2:3:4:5:6::8"),true) | ||
assert.equal(utils._isIP("1:2:3:4:5::"),true) | ||
assert.equal(utils._isIP("1:2:3:4:5::8"),true) | ||
assert.equal(utils._isIP("1:2:3:4::"),true) | ||
assert.equal(utils._isIP("1:2:3:4::8"),true) | ||
assert.equal(utils._isIP("1:2:3::"),true) | ||
assert.equal(utils._isIP("1:2:3::8"),true) | ||
assert.equal(utils._isIP("1:2::"),true) | ||
assert.equal(utils._isIP("1:2::8"),true) | ||
assert.equal(utils._isIP("1::"),true) | ||
assert.equal(utils._isIP("1::8"),true) | ||
assert.equal(utils._isIP("::"),true) | ||
assert.equal(utils._isIP("::8"),true) | ||
assert.equal(utils._isIP("::7:8"),true) | ||
assert.equal(utils._isIP("::6:7:8"),true) | ||
assert.equal(utils._isIP("::5:6:7:8"),true) | ||
assert.equal(utils._isIP("::4:5:6:7:8"),true) | ||
assert.equal(utils._isIP("::3:4:5:6:7:8"),true) | ||
assert.equal(utils._isIP("::2:3:4:5:6:7:8"),true) | ||
assert.equal(utils._isIP("A:0f:0F:FFFF:5:6:7:8"),true) | ||
assert.equal(utils._isIP("A:0f:0F:FFFF1:5:6:7:8"),false) | ||
assert.equal(utils._isIP("G:0f:0F:FFFF:5:6:7:8"),false) | ||
}) | ||
}) |