-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dnsproxy: Add DNS proxying functionality.
There are services that don't use the libc resolver in their service containers (for example some Go-based services). This addition installs dnsmasq into the MDNS services which can be enabled by an envvar. Change-type: minor Signed-off-by: Heds Simons <[email protected]>
- Loading branch information
Heds Simons
committed
Aug 27, 2019
1 parent
796e108
commit 88db097
Showing
9 changed files
with
292 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
#!/usr/bin/env node | ||
require('../build/app'); | ||
require('../build/mdns-publisher'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* @license | ||
* Copyright (C) 2018-2019 Balena Ltd. | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
import { spawn } from 'child_process'; | ||
import * as _ from 'lodash'; | ||
import * as fs from 'mz/fs'; | ||
|
||
import { getFullHostnames, getHostAddress } from './utils'; | ||
|
||
/** | ||
* Creates the dnsmasq config from the subdomains. | ||
* | ||
* @param hosts The subdomains to provide DNS for. | ||
* @param ipAddr The IP address to point DNS records to. | ||
* @returns void promise. | ||
*/ | ||
const configureDnsmasq = async ( | ||
hosts: string[], | ||
ipAddr: string, | ||
): Promise<void> => { | ||
// Write all the host entries to a new dnsmasq configuration | ||
let config = 'log-queries\n'; | ||
|
||
_.map(hosts, host => { | ||
config += `address=/${host}/${ipAddr}`; | ||
}); | ||
|
||
await fs.writeFile('/etc/dnsmasq.conf', config); | ||
}; | ||
|
||
/** | ||
* Start the dnsmasq process in debug mode for DNS proxying. | ||
* | ||
* @returns Void promise. | ||
*/ | ||
export async function startDnsProxy(): Promise<void> { | ||
// Configure the dnsmasq config | ||
// Get the list of hostnames to DNS proxy for | ||
const hosts = getFullHostnames(); | ||
|
||
try { | ||
const ipAddr = await getHostAddress(process.env.INTERFACE); | ||
|
||
// For each address, publish the interface IP address. | ||
await configureDnsmasq(hosts, ipAddr); | ||
} catch (err) { | ||
console.log(`balena DNS proxier configuration error:\n${err}`); | ||
} | ||
|
||
// Start dnsmasq, log output to console | ||
try { | ||
const dnsmasq = spawn('/usr/sbin/dnsmasq', [ | ||
'-d', | ||
'-x', | ||
'/run/dnsmasq.pid', | ||
]); | ||
|
||
dnsmasq.stdout.on('data', data => { | ||
console.log(`dnsmasq: ${data.toString()}`); | ||
}); | ||
dnsmasq.stderr.on('data', data => { | ||
console.error(`dnsmasq: Error - ${data}`); | ||
}); | ||
dnsmasq.on('close', code => { | ||
if (code !== 0) { | ||
console.log(`dnsmasq: process exited with code ${code}`); | ||
} | ||
}); | ||
} catch (err) { | ||
console.log(`dnsmasq: Could not launch daemon - ${err}`); | ||
} | ||
} |
Oops, something went wrong.