Merge pull request #506 from glaszig/feature/dhcp-upstream-server

[WIP] added ui to manage upstream dns servers
This commit is contained in:
Bill Zimmerman 2020-03-04 23:07:35 +01:00 committed by GitHub
commit 3ba6c135a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 186 additions and 19 deletions

View file

@ -143,6 +143,47 @@ $(document).on("submit", ".js-dhcp-settings-form", function(e) {
$(".js-add-dhcp-static-lease").trigger("click");
});
$(document).on("click", ".js-add-dhcp-upstream-server", function(e) {
e.preventDefault();
var field = $("#add-dhcp-upstream-server-field")
var row = $("#dhcp-upstream-server").html().replace("{{ server }}", field.val())
if (field.val().trim() == "") { return }
$(".js-dhcp-upstream-servers").append(row)
field.val("")
});
$(document).on("click", ".js-remove-dhcp-upstream-server", function(e) {
e.preventDefault();
$(this).parents(".js-dhcp-upstream-server").remove();
});
$(document).on("submit", ".js-dhcp-settings-form", function(e) {
$(".js-add-dhcp-upstream-server").trigger("click");
});
/**
* mark a form field, e.g. a select box, with the class `.js-field-preset`
* and give it an attribute `data-field-preset-target` with a text field's
* css selector.
*
* now, if the element marked `.js-field-preset` receives a `change` event,
* its value will be copied to all elements matching the selector in
* data-field-preset-target.
*/
$(document).on("change", ".js-field-preset", function(e) {
var selector = this.getAttribute("data-field-preset-target")
var value = "" + this.value
var syncValue = function(el) { el.value = value }
if (value.trim() === "") { return }
document.querySelectorAll(selector).forEach(syncValue)
});
$(document).on("click", "#gen_wpa_passphrase", function(e) {
$('#txtwpapassphrase').val(genPassword(63));
});

27
config/dns-servers.json Normal file
View file

@ -0,0 +1,27 @@
{
"Cloudflare": [
"1.0.0.1",
"1.1.1.1"
],
"German Privacy Foundation": [
"62.141.58.13",
"85.25.251.254",
"87.118.100.175",
"94.75.228.29"
],
"Google": [
"8.8.4.4",
"8.8.8.8"
],
"OpenDNS": [
"208.67.220.220",
"208.67.222.222"
],
"Quad9": [
"9.9.9.9"
],
"Yandex.DNS": [
"77.88.8.2",
"77.88.8.88"
]
}

View file

@ -59,6 +59,13 @@ function DisplayDHCPConfig()
}
}
if ($_POST['no-resolv'] == "1") {
$config .= "no-resolv".PHP_EOL;
}
foreach ($_POST['server'] as $server) {
$config .= "server=$server".PHP_EOL;
}
if ($_POST['DNS1']) {
$config .= "dhcp-option=6," . $_POST['DNS1'];
if ($_POST['DNS2']) {
@ -126,6 +133,8 @@ function DisplayDHCPConfig()
$dhcpHost = $conf["dhcp-host"];
$dhcpHost = empty($dhcpHost) ? [] : $dhcpHost;
$dhcpHost = is_array($dhcpHost) ? $dhcpHost : [ $dhcpHost ];
$upstreamServers = is_array($conf['server']) ? $conf['server'] : [ $conf['server'] ];
$upstreamServers = array_filter($upstreamServers);
$DNS1 = '';
$DNS2 = '';
@ -173,6 +182,7 @@ function DisplayDHCPConfig()
"RangeEnd",
"DNS1",
"DNS2",
"upstreamServers",
"arrRangeLeaseTime",
"mselected",
"hselected",

View file

@ -346,3 +346,27 @@ function mb_escapeshellarg($arg)
return "\"$escaped_arg\"";
}
function dnsServers()
{
$data = json_decode(file_get_contents("./config/dns-servers.json"));
return (array) $data;
}
function optionsForSelect($options)
{
$html = "";
foreach ($options as $key => $value) {
// optgroup
if (is_array($value)) {
$html .= "<optgroup label=\"$key\">";
$html .= optionsForSelect($value);
$html .= "</optgroup>";
}
// option
else {
$key = is_int($key) ? $value : $key;
$html .= "<option value=\"$value\">$key</option>";
}
}
return $html;
}

View file

@ -1,3 +1,14 @@
<?php ob_start() ?>
<?php if (!RASPI_MONITOR_ENABLED) : ?>
<input type="submit" class="btn btn-outline btn-primary" value="<?php echo _("Save settings"); ?>" name="savedhcpdsettings" />
<?php if ($dnsmasq_state) : ?>
<input type="submit" class="btn btn-warning" value="<?php echo _("Stop dnsmasq") ?>" name="stopdhcpd" />
<?php else : ?>
<input type="submit" class="btn btn-success" value="<?php echo _("Start dnsmasq") ?>" name="startdhcpd" />
<?php endif ?>
<?php endif ?>
<?php $buttons = ob_get_clean(); ob_end_clean() ?>
<div class="row">
<div class="col-lg-12">
<div class="card">
@ -19,8 +30,9 @@
<form method="POST" action="?page=dhcpd_conf" class="js-dhcp-settings-form">
<?php echo CSRFTokenFieldTag() ?>
<!-- Nav tabs -->
<ul class="nav nav-tabs">
<ul class="nav nav-tabs mb-4">
<li class="nav-item"><a class="nav-link active" href="#server-settings" data-toggle="tab"><?php echo _("Server settings"); ?></a></li>
<li class="nav-item"><a class="nav-link" href="#advanced" data-toggle="tab"><?php echo _("Advanced"); ?></a></li>
<li class="nav-item"><a class="nav-link" href="#static-leases" data-toggle="tab"><?php echo _("Static Leases") ?></a></li>
<li class="nav-item"><a class="nav-link" href="#client-list" data-toggle="tab"><?php echo _("Client list"); ?></a></li>
</ul>
@ -84,16 +96,77 @@
</div>
</div>
<?php if (!RASPI_MONITOR_ENABLED) : ?>
<input type="submit" class="btn btn-outline btn-primary" value="<?php echo _("Save settings"); ?>" name="savedhcpdsettings" />
<?php if ($dnsmasq_state) : ?>
<input type="submit" class="btn btn-warning" value="<?php echo _("Stop dnsmasq") ?>" name="stopdhcpd" />
<?php else : ?>
<input type="submit" class="btn btn-success" value="<?php echo _("Start dnsmasq") ?>" name="startdhcpd" />
<?php endif ?>
<?php endif ?>
<?php echo $buttons ?>
</div><!-- /.tab-pane -->
<!-- advanced tab -->
<div class="tab-pane" id="advanced">
<div class="row">
<div class="col-md-6">
<h5><?php echo _("Upstream DNS servers") ?></h5>
<div class="input-group">
<input type="hidden" name="no-resolv" value="0">
<div class="custom-control custom-switch">
<input class="custom-control-input" id="no-resolv" type="checkbox" name="no-resolv" value="1" <?php echo $conf['no-resolv'] ? ' checked="checked"' : "" ?> aria-describedby="no-resolv-description">
<label class="custom-control-label" for="no-resolv"><?php echo _("Only ever query DNS servers configured below") ?></label>
</div>
<p id="no-resolv-description">
<small><?php echo _("Enable this option if you want RaspAP to <b>send DNS queries to the servers configured below exclusively</b>. By default RaspAP also uses its upstream DHCP server's name servers.") ?></small>
<br><small class="text-muted"><?php echo _("This option adds <code>no-resolv</code> to the dnsmasq configuration.") ?></small>
</p>
</div>
<div class="js-dhcp-upstream-servers">
<?php foreach ($upstreamServers as $server): ?>
<div class="form-group input-group input-group-sm js-dhcp-upstream-server">
<input type="text" class="form-control" name="server[]" value="<?php echo $server ?>">
<div class="input-group-append">
<button class="btn btn-outline-secondary js-remove-dhcp-upstream-server" type="button"><i class="fas fa-minus"></i></button>
</div>
</div>
<?php endforeach ?>
</div>
<div class="form-group">
<label for="add-dhcp-upstream-server-field"><?php echo _("Add upstream DNS server") ?></label>
<div class="input-group">
<input type="text" class="form-control" id="add-dhcp-upstream-server-field" aria-describedby="new-dhcp-upstream-server" placeholder="<?php printf(_("e.g. %s"), "208.67.222.222") ?>">
<div class="input-group-append">
<button type="button" class="btn btn-outline-secondary js-add-dhcp-upstream-server"><i class="fas fa-plus"></i></button>
</div>
</div>
<p id="new-dhcp-upstream-server" class="form-text text-muted">
<small>
<?php echo _("Format: ") ?>
<code class="text-muted"><?php echo htmlspecialchars("[/[<domain>]/[domain/]][<ipaddr>[#<port>][@<source-ip>|<interface>[#<port>]]"); ?></code>
</small>
</p>
<select class="custom-select custom-select-sm js-field-preset" data-field-preset-target="#add-dhcp-upstream-server-field">
<option value=""><?php echo _("Choose a hosted server") ?></option>
<option disabled="disabled"></option>
<?php echo optionsForSelect(dnsServers()) ?>
</select>
</div>
</div>
<template id="dhcp-upstream-server">
<div class="form-group input-group input-group-sm js-dhcp-upstream-server">
<input type="text" class="form-control" name="server[]" value="{{ server }}">
<div class="input-group-append">
<button class="btn btn-outline-secondary js-remove-dhcp-upstream-server" type="button"><i class="fas fa-minus"></i></button>
</div>
</div>
</template>
</div><!-- /.row -->
<?php echo $buttons ?>
</div><!-- /.tab-pane | advanded tab -->
<div class="tab-pane fade" id="client-list">
<h4 class="mt-3 mb-3">Client list</h4>
<div class="row">
@ -175,16 +248,8 @@
</div>
</template>
<?php if (!RASPI_MONITOR_ENABLED) : ?>
<input type="submit" class="btn btn-outline btn-primary" value="<?php echo _("Save settings"); ?>" name="savedhcpdsettings" />
<?php
if ($dnsmasq_state) {
echo '<input type="submit" class="btn btn-warning" value="' . _("Stop dnsmasq") . '" name="stopdhcpd" />';
} else {
echo'<input type="submit" class="btn btn-success" value="' . _("Start dnsmasq") . '" name="startdhcpd" />';
}
?>
<?php endif ?>
<?php echo $buttons ?>
</div>
</div><!-- /.tab-content -->