Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Open sidebar
DNS Witch
Nomilo
Commits
b7fa4677
Commit
b7fa4677
authored
Mar 20, 2021
by
Gaël Berthaud-Müller
Browse files
better error handling
parent
7155c1cc
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
74 additions
and
8 deletions
+74
-8
src/main.rs
src/main.rs
+18
-8
src/models/dns.rs
src/models/dns.rs
+0
-0
src/models/errors.rs
src/models/errors.rs
+55
-0
src/models/mod.rs
src/models/mod.rs
+1
-0
No files found.
src/main.rs
View file @
b7fa4677
#![feature(proc_macro_hygiene,
decl_macro)]
use
std
::
str
::
FromStr
;
#[macro_use]
extern
crate
rocket
;
use
rocket
::
State
;
use
rocket
::
http
::
Status
;
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
::
op
::
{
DnsResponse
,
ResponseCode
}
;
use
trust_dns_client
::
rr
::{
DNSClass
,
Name
,
Record
,
RecordType
};
mod
type
s
;
mod
model
s
;
mod
config
;
use
models
::
errors
::
ErrorResponse
;
#[get(
"/zones/<zone>/records"
)]
fn
zone_records
(
client
:
State
<
SyncClient
<
TcpClientConnection
>>
,
zone
:
String
)
->
Json
<
Vec
<
type
s
::
dns
::
Record
>>
{
fn
zone_records
(
client
:
State
<
SyncClient
<
TcpClientConnection
>>
,
zone
:
String
)
->
Result
<
Json
<
Vec
<
model
s
::
dns
::
Record
>>
,
ErrorResponse
<
()
>>
{
// TODO: Implement FromParam for Name
let
name
=
Name
::
from_
str
(
&
zone
)
.unwrap
();
let
name
=
Name
::
from_
utf8
(
&
zone
)
.unwrap
();
let
response
:
DnsResponse
=
client
.query
(
&
name
,
DNSClass
::
IN
,
RecordType
::
AXFR
)
.unwrap
();
if
response
.response_code
()
!=
ResponseCode
::
NoError
{
return
ErrorResponse
::
new
(
Status
::
NotFound
,
format!
(
"zone {} could not be found"
,
name
.to_utf8
())
)
.err
()
}
let
answers
:
&
[
Record
]
=
response
.answers
();
let
mut
records
:
Vec
<
_
>
=
answers
.to_vec
()
.into_iter
()
.map
(|
record
|
type
s
::
dns
::
Record
::
from
(
record
))
.map
(|
record
|
model
s
::
dns
::
Record
::
from
(
record
))
.filter
(|
record
|
match
record
.rdata
{
type
s
::
dns
::
RData
::
NULL
{
..
}
|
type
s
::
dns
::
RData
::
DNSSEC
(
_
)
=>
false
,
model
s
::
dns
::
RData
::
NULL
{
..
}
|
model
s
::
dns
::
RData
::
DNSSEC
(
_
)
=>
false
,
_
=>
true
,
})
.collect
();
// AXFR response ends with SOA, we remove it so it is not doubled in the response.
records
.pop
();
Json
(
records
)
Ok
(
Json
(
records
)
)
}
fn
main
()
{
...
...
src/
type
s/dns.rs
→
src/
model
s/dns.rs
View file @
b7fa4677
File moved
src/models/errors.rs
0 → 100644
View file @
b7fa4677
use
serde
::
Serialize
;
use
rocket
::
http
::
Status
;
use
rocket
::
request
::
Request
;
use
rocket
::
response
::{
self
,
Response
,
Responder
};
use
rocket_contrib
::
json
::
Json
;
#[derive(Serialize,
Debug)]
pub
struct
ErrorResponse
<
T
>
{
#[serde(with
=
"StatusDef"
)]
#[serde(flatten)]
pub
status
:
Status
,
pub
message
:
String
,
#[serde(skip_serializing_if
=
"Option::is_none"
)]
pub
details
:
Option
<
T
>
}
#[derive(Serialize)]
#[serde(remote
=
"Status"
)]
struct
StatusDef
{
code
:
u16
,
#[serde(rename
=
"status"
)]
reason
:
&
'static
str
,
}
impl
<
T
>
ErrorResponse
<
T
>
{
pub
fn
new
(
status
:
Status
,
message
:
String
)
->
ErrorResponse
<
T
>
{
ErrorResponse
{
status
,
message
,
details
:
None
,
}
}
pub
fn
with_details
(
self
,
details
:
T
)
->
ErrorResponse
<
T
>
{
ErrorResponse
{
details
:
Some
(
details
),
..
self
}
}
pub
fn
err
<
R
>
(
self
)
->
Result
<
R
,
ErrorResponse
<
T
>>
{
Err
(
self
)
}
}
impl
<
'r
,
T
:
Serialize
>
Responder
<
'r
>
for
ErrorResponse
<
T
>
{
fn
respond_to
(
self
,
req
:
&
Request
)
->
response
::
Result
<
'r
>
{
let
status
=
self
.status
;
Response
::
build_from
(
Json
(
self
)
.respond_to
(
req
)
?
)
.status
(
status
)
.ok
()
}
}
src/
type
s/mod.rs
→
src/
model
s/mod.rs
View file @
b7fa4677
pub
mod
dns
;
pub
mod
errors
;
pub
mod
trust_dns_types
{
pub
use
trust_dns_client
::
rr
::
rdata
::{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment