diff --git a/lib/browser/client.js b/lib/browser/client.js index 6d0a6d8be..a5d4bb88d 100644 --- a/lib/browser/client.js +++ b/lib/browser/client.js @@ -17,6 +17,7 @@ const pkg = require('./version'); const dateFormat = require('dateformat'); const bowser = require('bowser'); const signUtils = require('../common/signUtils'); +const utils = require('../common/utils'); const globalHttpAgent = new AgentKeepalive(); @@ -341,8 +342,7 @@ proto._getResource = function _getResource(params) { }; proto._isIP = function _isIP(host) { - const ipv4Regex = /^(\d{1,3}\.){3,3}\d{1,3}$/; - return ipv4Regex.test(host); + return utils._isIP(host) }; proto._escape = function _escape(name) { diff --git a/lib/client.js b/lib/client.js index c71f657f4..560aa413d 100644 --- a/lib/client.js +++ b/lib/client.js @@ -17,6 +17,7 @@ const pkg = require('../package.json'); const dateFormat = require('dateformat'); const bowser = require('bowser'); const signUtils = require('./common/signUtils'); +const utils = require('./common/utils'); const globalHttpAgent = new AgentKeepalive(); @@ -328,8 +329,7 @@ proto._getResource = function _getResource(params) { }; proto._isIP = function _isIP(host) { - const ipv4Regex = /^(\d{1,3}\.){3,3}\d{1,3}$/; - return ipv4Regex.test(host); + return utils._isIP(host) }; proto._escape = function _escape(name) { diff --git a/lib/common/utils.js b/lib/common/utils.js new file mode 100644 index 000000000..16332830b --- /dev/null +++ b/lib/common/utils.js @@ -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; +}; \ No newline at end of file diff --git a/test/node/utils.test.js b/test/node/utils.test.js new file mode 100644 index 000000000..12b214ae4 --- /dev/null +++ b/test/node/utils.test.js @@ -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) + }) +}) \ No newline at end of file