From 445d52ed5b80cd8a8d952419ebb2675a3ada7348 Mon Sep 17 00:00:00 2001 From: delvedor Date: Fri, 3 May 2019 12:49:25 +0200 Subject: [PATCH 1/2] Better handling of hostname/ip:port format --- lib/ConnectionPool.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/ConnectionPool.js b/lib/ConnectionPool.js index fe116b442..6a164d4ae 100644 --- a/lib/ConnectionPool.js +++ b/lib/ConnectionPool.js @@ -336,16 +336,14 @@ class ConnectionPool { // if we encounter the second case, we should // use the hostname instead of the ip var address = node.http.publish_address - const hostAndIpRegex = /^[a-z0-9_.-]*\/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/gi - const match = address.match(hostAndIpRegex) - if (match !== null) { - const ipRegex = /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/ - const ip = address.match(ipRegex)[0] - // extract the hostname, the -1 at the end removes the final / - const hostname = address.slice(0, address.indexOf(ip) - 1) - const port = address.split(':')[1] + const parts = address.split('/') + // the url is in the form of hostname/ip:port + if (parts.length > 1) { + const hostname = parts[0] + const port = parts[1].match(/((?::))(?:[0-9]+)$/g)[0].slice(1) address = `${hostname}:${port}` } + address = address.slice(0, 4) === 'http' ? address : `${protocol}//${address}` From 5f0084feb28cf955baf7636c37bef1e43724313c Mon Sep 17 00:00:00 2001 From: delvedor Date: Fri, 3 May 2019 12:49:30 +0200 Subject: [PATCH 2/2] Updated test --- test/unit/connection-pool.test.js | 88 ++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 2 deletions(-) diff --git a/test/unit/connection-pool.test.js b/test/unit/connection-pool.test.js index d5ef6320e..b288a6a43 100644 --- a/test/unit/connection-pool.test.js +++ b/test/unit/connection-pool.test.js @@ -282,7 +282,7 @@ test('API', t => { }) t.test('nodesToHost', t => { - t.test('publish_address as ip address', t => { + t.test('publish_address as ip address (IPv4)', t => { const pool = new ConnectionPool({ Connection }) const nodes = { a1: { @@ -324,7 +324,49 @@ test('API', t => { t.end() }) - t.test('publish_address as host/ip', t => { + t.test('publish_address as ip address (IPv6)', t => { + const pool = new ConnectionPool({ Connection }) + const nodes = { + a1: { + http: { + publish_address: '[::1]:9200' + }, + roles: ['master', 'data', 'ingest'] + }, + a2: { + http: { + publish_address: '[::1]:9201' + }, + roles: ['master', 'data', 'ingest'] + } + } + + t.deepEqual(pool.nodesToHost(nodes, 'http:'), [{ + url: new URL('http://[::1]:9200'), + id: 'a1', + roles: { + master: true, + data: true, + ingest: true, + ml: false + } + }, { + url: new URL('http://[::1]:9201'), + id: 'a2', + roles: { + master: true, + data: true, + ingest: true, + ml: false + } + }]) + + t.strictEqual(pool.nodesToHost(nodes, 'http:')[0].url.host, '[::1]:9200') + t.strictEqual(pool.nodesToHost(nodes, 'http:')[1].url.host, '[::1]:9201') + t.end() + }) + + t.test('publish_address as host/ip (IPv4)', t => { const pool = new ConnectionPool({ Connection }) const nodes = { a1: { @@ -366,6 +408,48 @@ test('API', t => { t.end() }) + t.test('publish_address as host/ip (IPv6)', t => { + const pool = new ConnectionPool({ Connection }) + const nodes = { + a1: { + http: { + publish_address: 'example.com/[::1]:9200' + }, + roles: ['master', 'data', 'ingest'] + }, + a2: { + http: { + publish_address: 'example.com/[::1]:9201' + }, + roles: ['master', 'data', 'ingest'] + } + } + + t.deepEqual(pool.nodesToHost(nodes, 'http:'), [{ + url: new URL('http://example.com:9200'), + id: 'a1', + roles: { + master: true, + data: true, + ingest: true, + ml: false + } + }, { + url: new URL('http://example.com:9201'), + id: 'a2', + roles: { + master: true, + data: true, + ingest: true, + ml: false + } + }]) + + t.strictEqual(pool.nodesToHost(nodes, 'http:')[0].url.host, 'example.com:9200') + t.strictEqual(pool.nodesToHost(nodes, 'http:')[1].url.host, 'example.com:9201') + t.end() + }) + t.test('Should use the configure protocol', t => { const pool = new ConnectionPool({ Connection }) const nodes = {