{% extends 'config/base.html.j2' %}

{% block config_base %}

<div id="page-dns" class="config-page">
    {% include '_messages.html.j2' %}
    <p>{% trans %}Router Turris uses its own DNS resolver with DNSSEC support. It is capable of working independently or it can forward your DNS queries your internet service provider's DNS resolver.{% endtrans %}</p>
    <p>{% trans %}The following setting determines the behavior of the DNS resolver. Usually, it is better to use the ISP's resolver in networks where it works properly. If it does not work for some reason, it is necessary to use direct resolving without forwarding.{% endtrans %}</p>
    <p>{% trans %}In rare cases ISP's have improperly configured network which interferes with DNSSEC validation. If you experience problems with DNS, you can <strong>temporarily</strong> disable DNSSEC validation to determine the source of the problem. However, keep in mind that without DNSSEC validation, you are vulnerable to DNS spoofing attacks! Therefore we <strong>recommend keeping DNSSEC turned on</strong> and resolving the situation with your ISP as this is a serious flaw on their side.{% endtrans %}</p>
    <form id="main-form" class="config-form" action="{{ request.fullpath }}" method="post" enctype="multipart/form-data" autocomplete="off" novalidate>
        <input type="hidden" name="csrf_token" value="{{ get_csrf_token() }}">
        {% for field in form.active_fields %}
            {% include '_field.html.j2' %}
        {% endfor %}
        <div class="form-buttons">
            <a href="{{ request.fullpath }}" class="button grayed">{% trans %}Discard changes{% endtrans %}</a>
            <button type="submit" name="send" class="button">{% trans %}Save{% endtrans %}</button>
        </div>
    </form>

    <h2>{% trans %}Connection test{% endtrans %}</h2>
    <p>{% trans %}Here you can test your internet connection. This test is also useful when you need to check that your DNS resolving works as expected. Remember to click on the <strong>Save</strong> button if you changed your forwarder setting.{% endtrans %}</p>
    <div class="message error" id="connection-test-fail">
        {% trans %}Unable to verify network connection.{% endtrans %}
    </div>
    <table id="test-results">
        <thead>
            <tr>
              <th>{% trans %}Test type{% endtrans %}</th>
              <th>{% trans %}Status{% endtrans %}</th>
            </tr>
        </thead>
        <tbody>
            <tr><td>{% trans %}DNS{% endtrans %}</td><td class="result" data-result-type="dns-conn-test"><i class="fas fa-question-circle"></i></td></tr>
            <tr><td>{% trans %}DNSSEC{% endtrans %}</td><td class="result" data-result-type="dnssec-conn-test"><i class="fas fa-question-circle"></i></td></tr>
        </tbody>
    </table>
    <a href="#" id="test-connection" class="button" {% if foris_info.guide.current == active_config_page_key %}disabled{% endif %}>{% trans %}Test connection{% endtrans %}</a>
</div>
<script>
    Foris.watched_test = null;
    var update_conn_test_field = function(key, result) {
        var field = key + "-conn-test";
        var resultBox = $("#test-results").find(".result[data-result-type=" + field + "]");
        if (result) {
            resultBox.removeClass("test-fail").removeClass("test-loading").addClass("test-success").html('<i class="fas fa-check-circle"></i>');
        } else {
            resultBox.removeClass("test-success").removeClass("test-loading").addClass("test-fail").html('<i class="fas fa-exclamation-circle"></i>');
        }
    }
    Foris.addWsHanlder("wan", (msg) => {
        switch(msg["action"]) {
            case "connection_test":
                if (msg["data"]["test_id"] != Foris.watched_test) {
                    break;
                }
                for (var key in msg["data"]["data"]) {
                    update_conn_test_field(key, msg["data"]["data"][key]);
                }
                break;
            case "connection_test_finished":
                if (msg["data"]["test_id"] != Foris.watched_test) {
                    break;
                }
                if (!msg["data"]["passed"]) {
                    break;
                }
                for (var key in msg["data"]["data"]) {
                    update_conn_test_field(key, msg["data"]["data"][key]);
                }
                Foris.watched_test = null;
                $("#test-connection").show();
                break;
        }
    });
    $(document).ready(function() {
        $("#test-connection").click(function(e) {
            $("#connection-test-fail").hide();
            var self = $(this);
            e.preventDefault();
            if (self.attr("disabled") == "disabled") {
                return;
            }
            self.attr("disabled", "disabled");
            $.get('{{ url("config_ajax", page_name="dns") }}', {action: "check-connection"})
                    .done(function(response) {
                        $("#test-results").find(".result").removeClass("test-success").removeClass("test-fail").addClass("test-loading").html('<i class="fas fa-spinner rotate"></i>');
                        $("#test-connection").hide();
                        Foris.watched_test = response["test_id"];
                    })
                    .fail(function(xhr) {
                        if (xhr.responseJSON && xhr.responseJSON.loggedOut && xhr.responseJSON.loginUrl) {
                            window.location.replace(xhr.responseJSON.loginUrl);
                            return;
                        }
                        $("#connection-test-fail").show();
                    })
                    .always(function() {
                        self.removeAttr("disabled");
                    });
        });
        function initDisableDNSSEC() {
            let element = $('#field-dnssec_disabled_1');
            element.change(function () {
              if ($(this).is(':checked')) {
                Foris.confirmDialog(
                    {
                        unsafeMessage: Foris.messages.confirmDisabledDNSSEC,
                        callback: (value) => element.prop("checked", value)
                    }
                );
              } else {
                  element.prop("checked", false);
                  return false;
              }
            });
        }
        initDisableDNSSEC();
        Foris.afterAjaxUpdateFunctions.push(initDisableDNSSEC);
    });
</script>
{% endblock %}
