1
- import { OpenAPIHono , z , createRoute } from "@hono/zod-openapi" ;
2
-
3
- /* DNS-QUERY */
1
+ import { z , createRoute } from "@hono/zod-openapi" ;
4
2
5
3
const resolvers = {
6
4
cloudflare : "cloudflare-dns.com/dns-query" ,
7
5
google : "dns.google/resolve" ,
8
6
quad9 : "dns.quad9.net:5053/dns-query" ,
9
7
} ;
10
8
9
+ /* QUERY */
10
+ interface NSLookupResponse {
11
+ A : DoHResponse ;
12
+ AAAA : DoHResponse ;
13
+ CNAME : DoHResponse ;
14
+ TXT : DoHResponse ;
15
+ NS : DoHResponse ;
16
+ MX : DoHResponse ;
17
+ }
18
+
19
+ interface DoHResponse {
20
+ Status : number ;
21
+ TC : boolean ;
22
+ RD : boolean ;
23
+ RA : boolean ;
24
+ AD : boolean ;
25
+ CD : boolean ;
26
+ Question : { name : string ; type : number } [ ] ;
27
+ Answer : { name : string ; type : number ; TTL : number ; data : string } [ ] ;
28
+ }
29
+
30
+ export async function query (
31
+ resolver : keyof typeof resolvers ,
32
+ name : string ,
33
+ type : string = "A" ,
34
+ DO : boolean = false ,
35
+ CD : boolean = false
36
+ ) : Promise < DoHResponse > {
37
+ const endpoint = resolvers [ resolver ] ;
38
+ const url = `https://${ endpoint } ?name=${ name } ${ type ? `&type=${ type } ` : "" } ${
39
+ DO ? `do=${ DO } ` : ""
40
+ } ${ CD ? `$cd=${ CD } ` : "" } `;
41
+ const response = await fetch ( url , {
42
+ method : "GET" ,
43
+ headers : {
44
+ accept : "application/dns-json" ,
45
+ } ,
46
+ } ) ;
47
+ if ( ! response . ok ) {
48
+ throw new Error (
49
+ `Failed to fetch data: ${ response . status } ${ response . statusText } `
50
+ ) ;
51
+ } // TODO handle returned errors
52
+ return ( await response . json ( ) ) as DoHResponse ;
53
+ }
54
+
55
+ /*SCHEMAS */
11
56
const ParamsSchema = z . object ( {
12
57
resolver : z . enum ( Object . keys ( resolvers ) ) . openapi ( {
13
58
param : {
@@ -60,17 +105,6 @@ const DoHQuerySchema = z.object({
60
105
} ) ,
61
106
} ) ;
62
107
63
- interface DoHResponse {
64
- Status : number ;
65
- TC : boolean ;
66
- RD : boolean ;
67
- RA : boolean ;
68
- AD : boolean ;
69
- CD : boolean ;
70
- Question : { name : string ; type : number } [ ] ;
71
- Answer : { name : string ; type : number ; TTL : number ; data : string } [ ] ;
72
- }
73
-
74
108
const DoHResponseSchema = z
75
109
. object ( {
76
110
Status : z . number ( ) . openapi ( { example : 0 } ) ,
@@ -98,37 +132,22 @@ const DoHResponseSchema = z
98
132
} )
99
133
. openapi ( "DoH Record" ) ;
100
134
101
- async function query (
102
- resolver : keyof typeof resolvers ,
103
- name : string ,
104
- type : string = "A" ,
105
- DO : boolean = false ,
106
- CD : boolean = false
107
- ) : Promise < DoHResponse > {
108
- const endpoint = resolvers [ resolver ] ;
109
- const url = `https://${ endpoint } ?name=${ name } ${ type ? `&type=${ type } ` : "" } ${
110
- DO ? `do=${ DO } ` : ""
111
- } ${ CD ? `$cd=${ CD } ` : "" } `;
112
- const response = await fetch ( url , {
113
- method : "GET" ,
114
- headers : {
115
- accept : "application/dns-json" ,
116
- } ,
117
- } ) ;
118
- if ( ! response . ok ) {
119
- throw new Error (
120
- `Failed to fetch data: ${ response . status } ${ response . statusText } `
121
- ) ;
122
- } // TODO handle returned errors
123
- return ( await response . json ( ) ) as DoHResponse ;
124
- }
125
-
126
- export const dnsQuery = new OpenAPIHono ( ) ;
135
+ const NSLookupResponseSchema = z
136
+ . object ( {
137
+ A : DoHResponseSchema ,
138
+ AAAA : DoHResponseSchema ,
139
+ CNAME : DoHResponseSchema ,
140
+ TXT : DoHResponseSchema ,
141
+ NS : DoHResponseSchema ,
142
+ MX : DoHResponseSchema ,
143
+ } )
144
+ . openapi ( "Nslookup" ) ;
127
145
128
- const dnsQueryRoute = createRoute ( {
146
+ /* ROUTE */
147
+ export const DNSQueryRoute = createRoute ( {
129
148
tags : [ "Domain" ] ,
130
149
method : "get" ,
131
- path : "/{resolver}/{domain}" ,
150
+ path : "/dns-query/ {resolver}/{domain}" ,
132
151
request : { params : ParamsSchema , query : DoHQuerySchema } ,
133
152
responses : {
134
153
// TODO customize error responses (https://github.com/honojs/middleware/tree/main/packages/zod-openapi)
@@ -148,46 +167,17 @@ const dnsQueryRoute = createRoute({
148
167
} ,
149
168
} ) ;
150
169
151
- dnsQuery . openapi ( dnsQueryRoute , async ( c : any ) => {
152
- const { type, DO , CD } = c . req . valid ( "query" ) ;
153
- const { resolver, domain } = c . req . valid ( "param" ) ;
154
- const response : DoHResponse = await query ( resolver , domain , type , DO , CD ) ;
155
- return c . json ( response ) ;
156
- } ) ;
157
-
158
- /* NSLOOKUP */
159
-
160
- interface NslookupResponse {
161
- A : DoHResponse ;
162
- AAAA : DoHResponse ;
163
- CNAME : DoHResponse ;
164
- TXT : DoHResponse ;
165
- NS : DoHResponse ;
166
- MX : DoHResponse ;
167
- }
168
-
169
- const NslookupResponseSchema = z
170
- . object ( {
171
- A : DoHResponseSchema ,
172
- AAAA : DoHResponseSchema ,
173
- CNAME : DoHResponseSchema ,
174
- TXT : DoHResponseSchema ,
175
- NS : DoHResponseSchema ,
176
- MX : DoHResponseSchema ,
177
- } )
178
- . openapi ( "Nslookup" ) ;
179
-
180
- const nslookupRoute = createRoute ( {
170
+ export const NSLookupRoute = createRoute ( {
181
171
tags : [ "Domain" ] ,
182
172
method : "get" ,
183
- path : "/{resolver}/{domain}" ,
173
+ path : "/nslookup/ {resolver}/{domain}" ,
184
174
request : { params : ParamsSchema } ,
185
175
responses : {
186
176
// TODO customize error responses (https://github.com/honojs/middleware/tree/main/packages/zod-openapi)
187
177
200 : {
188
178
content : {
189
179
"application/json" : {
190
- schema : NslookupResponseSchema ,
180
+ schema : NSLookupResponseSchema ,
191
181
} ,
192
182
} ,
193
183
description : "Fetch DoH Lookup" ,
@@ -199,19 +189,3 @@ const nslookupRoute = createRoute({
199
189
url : "https://www.rfc-editor.org/rfc/rfc8484" ,
200
190
} ,
201
191
} ) ;
202
-
203
- export const nslookup = new OpenAPIHono ( ) ;
204
-
205
- nslookup . openapi ( nslookupRoute , async ( c : any ) => {
206
- const { resolver, domain } = c . req . valid ( "param" ) ;
207
- const [ A , AAAA , CNAME , TXT , NS , MX ] = await Promise . all ( [
208
- query ( resolver , domain , "A" ) ,
209
- query ( resolver , domain , "AAAA" ) ,
210
- query ( resolver , domain , "CNAME" ) ,
211
- query ( resolver , domain , "TXT" ) ,
212
- query ( resolver , domain , "NS" ) ,
213
- query ( resolver , domain , "MX" ) ,
214
- ] ) ;
215
- const response : NslookupResponse = { A, AAAA , CNAME , TXT , NS , MX } ;
216
- return c . json ( response ) ;
217
- } ) ;
0 commit comments