Skip to content

Commit

Permalink
fix: support ipv6 address when set endpoint (#482)
Browse files Browse the repository at this point in the history
* fix: support ipv6 address when set endpoint

* test: add test case for ipv6
  • Loading branch information
luozhang002 authored and binghaiwang committed Jun 11, 2018
1 parent ec8ae25 commit 1c7616d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/browser/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions lib/common/utils.js
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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regex.test(host);
return isIP;
};
79 changes: 79 additions & 0 deletions test/node/utils.test.js
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)
})
})

0 comments on commit 1c7616d

Please sign in to comment.