Skip to content

Commit

Permalink
Fix mixed domain names with .local, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
indykoning committed Jan 31, 2023
1 parent 7812417 commit 69a9261
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 54 deletions.
73 changes: 35 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ const nodemon = require('nodemon')
const docker = new Docker({socketPath: "/var/run/docker.sock"});

const re = /traefik\.http\.routers\.(.*)\.rule/
const re2 = /Host\(\s*`(.*?\.local)`\s*,*\s*\)/gi
const re3 = /`(.*?\.local)`/g
const checkRe = /Host\(\s*`(.*?\.local)`\s*,*\s*\)/gi
const domainRe = /`(?<domain>[^`]*?\.local)`/g

const cnames = [];
let cnames = [];

const matchDomainCnames = function (domainString) {
return [...domainString.matchAll(domainRe)].map(match => match.groups.domain)
}

docker.listContainers()
.then( list => {
Expand All @@ -18,19 +22,9 @@ docker.listContainers()
var name = cont.Names[0].substring(1)
var keys = Object.keys(cont.Labels)
keys.forEach(key =>{
if (re.test(key) && re2.test(cont.Labels[key])) {
re2.lastIndex=0
const matches = cont.Labels[key].matchAll(re2)
for (const m of matches){
if (m[1].includes(',')){
const parts = m[0].matchAll(re3)
for (const p of parts) {
cnames.push(p[1])
}
} else {
cnames.push(m[1])
}
}
if (re.test(key) && checkRe.test(cont.Labels[key])) {
checkRe.lastIndex=0
cnames = cnames.concat(matchDomainCnames(cont.Labels[key]))
}
})
}
Expand Down Expand Up @@ -58,32 +52,35 @@ docker.listContainers()
events.setEncoding('utf8');
events.on('data',ev => {
var eventJSON = JSON.parse(ev)
// console.log(eventJSON)
if (eventJSON.status == "start") {
var keys = Object.keys(eventJSON.Actor.Attributes)
keys.forEach(key => {
if (re.test(key)) {
var host = eventJSON.Actor.Attributes[key].match(re2)[1]
cnames.push(host)
fs.writeFile("cnames",cnames.join('\n'),'utf8', err =>{})
}
})
} else if (eventJSON.status == "stop") {
var keys = Object.keys(eventJSON.Actor.Attributes)
keys.forEach(key => {
if (re.test(key)) {
var host = eventJSON.Actor.Attributes[key].match(re2)[1]
var index = cnames.indexOf(host)
if (index != -2) {
cnames.splice(index,1)
}
fs.writeFile("cnames",cnames.join('\n'), 'utf8', err => {})
}
})
if(!['start', 'stop'].includes(eventJSON.status)){
return;
}

var keys = Object.keys(eventJSON.Actor.Attributes)
keys.forEach(key => {
if (!re.test(key)) {
return;
}

let hosts = matchDomainCnames(eventJSON.Actor.Attributes[key])
if (!hosts.length) {
return;
}

if (eventJSON.status === 'start') {
cnames = cnames.concat(hosts)
console.log('Adding', hosts);
} else if (eventJSON.status === 'stop') {
cnames = cnames.filter(host => !hosts.includes(host));
console.log('Removing', hosts);
}

fs.writeFile("cnames",cnames.join('\n'),'utf8', err =>{})
})
})
})
.catch(err => {
console.log(err)
})
})

31 changes: 15 additions & 16 deletions regex-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@ const testValues = [
"Host(`foo.example.local`) || Host(`bar.example.local`)",
"HOST(`foo.example.local`) || ( Host(`baz.example.local`) && Path(`/baz`) )",
"Host(`bill.example.local`) || ( Path(`/ben`) && Host(`ben.example.local`) )",
"Host( `foo.local`, `bar.local`)"
"Host( `foo.local`, `bar.local`)",
"Host(`example.com`)",
"Host(`foo.example.com`) || Host(`bar.example.local`)",
"HOST(`foo.example.local`) || ( Host(`baz.example.com`) && Path(`/baz`) )",
"Host(`bill.example.com`) || ( Path(`/ben`) && Host(`ben.example.local`) )",
"Host( `foo.com`, `bar.local`)"
]

const re = /Host\(\s*`(.*?\.local)`\s*,*\s*\)/gi
const re2 = /`(.*?\.local)`/g
const checkRe = /Host\(\s*`(.*?\.local)`\s*,*\s*\)/gi
const domainRe = /`(?<domain>[^`]*?\.local)`/g

const matchDomainCnames = function (domainString) {
return [...domainString.matchAll(domainRe)].map(match => match.groups.domain)
}

testValues.forEach( l => {
if (re.test(l)) {
re.lastIndex = 0
const matches = [...l.matchAll(re)]
for (const match of matches) {
if (match[1].includes(',')){
const parts = match[0].matchAll(re2)
for (const part of parts){
console.log(part[1])
}
} else {
console.log(match[1])
}
}
if (checkRe.test(l)) {
checkRe.lastIndex = 0
console.log(matchDomainCnames(l))
} else {
console.log("no match - " + l )
}
Expand Down

0 comments on commit 69a9261

Please sign in to comment.