Better handling of hostname/ip:port format (#837)

* Better handling of hostname/ip:port format

* Updated test
This commit is contained in:
Tomas Della Vedova
2019-05-03 14:36:17 +02:00
committed by delvedor
parent d6577b7fa1
commit 9dacd9d9ee
2 changed files with 92 additions and 10 deletions

View File

@ -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}`

View File

@ -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 = {