diff --git a/config.yml b/config.yml index c4a7ab1..1f3d0c9 100644 --- a/config.yml +++ b/config.yml @@ -54,4 +54,7 @@ domains: - /bar https: false zone: dmz + foo_bar: bar: + port: 443 + bar_baz: diff --git a/index.js b/index.js index 1c6ed10..24506b5 100644 --- a/index.js +++ b/index.js @@ -15,7 +15,7 @@ const isProduction = process.env.NODE_ENV === 'production'; const tmpDir = isProduction ? `/tmp/${appName}` : `${__dirname}/tmp`; const hashFile = `${tmpDir}/config.hash`; const confDir = conf.rproxy.confDir; -const httpServices = new Set(); +const httpServices = new Map(); async function updateProxy(firstRun) { console.log('Updating reverse proxy configuration.'); @@ -61,17 +61,30 @@ async function updateProxy(firstRun) { const match = name.match(/^(.+)_main$/); if (match) name = match[1]; + let httpPortIndex = null; + + if (httpServices.has(name)) { + if (ports.length > 1) { + const httpPort = httpServices.get(name) || 80; + const index = ports.findIndex( + p => p.TargetPort == httpPort && p.Protocol === 'tcp'); + if (index !== -1) httpPortIndex = index; + } + if (httpPortIndex === null) { + const index = ports.findIndex(p => p.Protocol === 'tcp'); + if (index !== -1) httpPortIndex = index; + } + } + for (let i = 0; i < ports.length; i++) { const port = ports[i]; - const protocol = port.Protocol; - let backends; - if (protocol == 'tcp') { - if (i == 0 && httpServices.has(name)) - backends = http; - else - backends = tcp; - } else + + if (i === httpPortIndex) + backends = http; + else if (port.Protocol === 'tcp') + backends = tcp; + else backends = udp; backends.push({ @@ -188,8 +201,6 @@ async function updateProxy(firstRun) { } function addService(service, serviceConf, mainDomain) { - httpServices.add(service); - let rules; if (typeof serviceConf == 'string') { rules = serviceConf; @@ -201,6 +212,8 @@ async function updateProxy(firstRun) { serviceConf ); + httpServices.set(service, serviceConf.port); + if (serviceConf.https) https.push(service); if (serviceConf.zone) { diff --git a/test.json b/test.json index c3a1b8d..3c08427 100644 --- a/test.json +++ b/test.json @@ -6,6 +6,22 @@ "Ports": [ { "PublishedPort": 10001, + "TargetPort": 80, + "Protocol": "tcp" + }, { + "PublishedPort": 10002, + "TargetPort": 3306, + "Protocol": "tcp" + } + ] + } + }, { + "Spec": {"Name": "foo_bar"}, + "Endpoint": { + "Ports": [ + { + "PublishedPort": 10003, + "TargetPort": 443, "Protocol": "tcp" } ] @@ -15,10 +31,12 @@ "Endpoint": { "Ports": [ { - "PublishedPort": 10003, + "PublishedPort": 10004, + "TargetPort": 3584, "Protocol": "tcp" }, { - "PublishedPort": 10004, + "PublishedPort": 10005, + "TargetPort": 443, "Protocol": "tcp" } ] @@ -28,10 +46,12 @@ "Endpoint": { "Ports": [ { - "PublishedPort": 10005, + "PublishedPort": 10006, + "TargetPort": 101, "Protocol": "tcp" }, { - "PublishedPort": 10006, + "PublishedPort": 10007, + "TargetPort": 80, "Protocol": "tcp" } ] @@ -42,6 +62,7 @@ "Ports": [ { "PublishedPort": 10001, + "TargetPort": 100, "Protocol": "udp" } ]