main.rs 1.29 KB
Newer Older
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#![feature(proc_macro_hygiene, decl_macro)]
use std::str::FromStr;

#[macro_use] extern crate rocket;
use rocket::State;
use rocket_contrib::json::Json;

use trust_dns_client::client::{Client, SyncClient};
use trust_dns_client::tcp::TcpClientConnection;
use trust_dns_client::op::DnsResponse;
use trust_dns_client::rr::{DNSClass, Name, Record, RecordType};

mod types;
mod config;


#[get("/zones/<zone>/records")]
fn zone_records(client: State<SyncClient<TcpClientConnection>>, zone: String) -> Json<Vec<types::dns::Record>> {
    // TODO: Implement FromParam for Name
    let name = Name::from_str(&zone).unwrap();

    // TODO: add support for all trust-dns record types
    //       then use AXFR here and filter out dnssec related fields
    let response: DnsResponse = client.query(&name, DNSClass::IN, RecordType::AAAA).unwrap();
    let answers: &[Record] = response.answers();
    let records: Vec<types::dns::Record> = answers.to_vec().into_iter().map(|record| record.into()).collect();

    Json(records)
}

fn main() {
    let app_config = config::load("config.toml".into());

    let conn = TcpClientConnection::new(app_config.dns_server.address).unwrap();
    let client = SyncClient::new(conn);

    rocket::ignite()
        .manage(client)
        .mount("/api/v1", routes![zone_records]).launch();
}