Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DNS BIND format #19

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion src/pages/directory/domains/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL
*/

use leptos::*;
use leptos_router::{use_navigate, use_params_map};
use serde::{Deserialize, Serialize};
use std::ops::Add;

use crate::{
components::{
Expand Down Expand Up @@ -34,6 +34,46 @@ struct DnsRecord {
content: String,
}

fn format_zonefile(records: &Vec<DnsRecord>, domain: &str) -> String {
let formatted_records: Vec<[&str; 3]> = records
.iter()
.filter_map(|record| {
record.name.strip_suffix(domain).map(|name| {
if name.is_empty() {
["@", &record.typ, &record.content]
} else {
[name, &record.typ, &record.content]
}
})
})
.collect();

let max_len = formatted_records.iter().fold([0, 0], |acc, x| {
[acc[0].max(x[0].len()), acc[1].max(x[1].len())]
});

formatted_records.iter().fold(String::new(), |acc, x| {
let key = format!(
"{}{: <width1$} IN {: <width2$}",
acc,
x[0],
x[1],
width1 = max_len[0],
width2 = max_len[1]
);
if x[1] == "TXT" {
x[2].as_bytes()
.chunks(255)
.fold(key, |acc, x| {
format!("{} \"{}\"", acc, String::from_utf8_lossy(x))
})
.add("\n")
} else {
format!("{} {}\n", key, x[2])
}
})
}

#[component]
pub fn DomainDisplay() -> impl IntoView {
let auth = use_authorization();
Expand Down Expand Up @@ -87,6 +127,7 @@ pub fn DomainDisplay() -> impl IntoView {
.filter(|r| r.typ == "TXT" && r.content.contains("DKIM"))
.count()
.to_string();
let zonefile = format_zonefile(&records, &params.get().get("id").cloned().unwrap_or_default());
Some(
view! {
<Card>
Expand Down Expand Up @@ -135,6 +176,17 @@ pub fn DomainDisplay() -> impl IntoView {
.collect_view()}

</Table>
<div class="sm:col-span-12 pb-4">
<h2 class="text-lg font-semibold text-gray-800 dark:text-gray-200">
Zonefile
</h2>
</div>
<textarea
class="py-3 px-4 block w-full border-gray-200 rounded-lg text-sm focus:border-blue-500 focus:ring-blue-500 disabled:opacity-50 disabled:pointer-events-none dark:bg-slate-900 dark:border-gray-700 dark:text-gray-400 dark:focus:ring-gray-600"
readonly=true
>
{zonefile}
</textarea>

</div>

Expand Down
Loading