main.rs 1.98 KB
Newer Older
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
1 2 3
#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
4 5 6
#[macro_use] extern crate rocket_contrib;
#[macro_use] extern crate diesel;

Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
7
use rocket::State;
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
8
use rocket::http::Status;
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
9

Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
10 11 12 13
use rocket_contrib::json::Json;

use trust_dns_client::client::{Client, SyncClient};
use trust_dns_client::tcp::TcpClientConnection;
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
14
use trust_dns_client::op::{DnsResponse, ResponseCode};
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
15
use trust_dns_client::rr::{DNSClass, Name, RecordType};
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
16

Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
17
mod models;
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
18
mod config;
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
19 20
mod auth;
mod schema;
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
21

Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
22
use models::errors::ErrorResponse;
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
23 24
use auth::routes::*;

Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
25

Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
26 27
#[database("db")]
pub struct DbConn(diesel::SqliteConnection);
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
28 29

#[get("/zones/<zone>/records")]
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
30
fn get_zone_records(client: State<SyncClient<TcpClientConnection>>, zone: String) -> Result<Json<Vec<models::dns::Record>>, ErrorResponse<()>> {
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
31
    // TODO: Implement FromParam for Name
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
32
    let name = Name::from_utf8(&zone).unwrap();
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
33

34
    let response: DnsResponse = client.query(&name, DNSClass::IN, RecordType::AXFR).unwrap();
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
35 36 37 38 39 40 41 42

    if response.response_code() != ResponseCode::NoError {
        return ErrorResponse::new(
            Status::NotFound,
            format!("zone {} could not be found", name.to_utf8())
        ).err()
    }

Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
43
    let answers = response.answers();
44
    let mut records: Vec<_> = answers.to_vec().into_iter()
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
45
        .map(|record| models::dns::Record::from(record))
46
        .filter(|record| match record.rdata {
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
47
            models::dns::RData::NULL { .. } | models::dns::RData::DNSSEC(_) => false,
48 49 50 51 52
            _ => true,
        }).collect();

    // AXFR response ends with SOA, we remove it so it is not doubled in the response.
    records.pop();
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
53

Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
54
    Ok(Json(records))
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
55 56 57 58 59 60 61 62 63 64
}

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)
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
65
        .attach(DbConn::fairing())
66
        .mount("/api/v1", routes![get_zone_records, create_auth_token, create_user]).launch();
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
67
}