-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_dns_message.tsx
141 lines (135 loc) · 4.94 KB
/
parse_dns_message.tsx
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import {
bodyToBuffer,
Context,
NextFunction,
RetHandler,
} from "https://cdn.jsdelivr.net/gh/masx200/[email protected]/mod.ts";
import { dns_query_path_name } from "./dns_query_path_name.tsx";
import { get_path_name } from "./get_path_name.tsx";
import Packet from "npm:[email protected]";
// console.log(JSONSTRINGIFYNULL4({ Packet });
import Buffer from "npm:[email protected]";
import { base64Decode } from "./base64Decode.tsx";
import { JSONSTRINGIFYNULL4 } from "./JSONSTRINGIFYNULL4.ts";
import { DNSPACKETInterface } from "./DNSPACKETInterface.ts";
// console.log(JSONSTRINGIFYNULL4({ Buffer });
/**
* 解析DNS消息。
* 此函数用于处理DNS查询和响应的消息,支持通过GET和POST方法传递DNS消息。
*
* @param ctx 上下文对象,包含请求和响应的信息。
* @param next 中间件的下一个函数,用于继续处理请求链。
* @returns 返回一个Promise,解析为处理结果的RetHandler对象。
*/
export async function parse_dns_message(
ctx: Context,
next: NextFunction,
): Promise<RetHandler> {
const req = ctx.request;
const { url } = req;
const pathname = get_path_name(url);
if (
pathname === dns_query_path_name() &&
(ctx.request.method === "POST" || ctx.request.method === "GET")
) {
if (
ctx.request.method === "GET" &&
new URL(url).searchParams.get("dns")?.length
) {
try {
const data = base64Decode(
new URL(url).searchParams.get("dns") ?? "",
);
const packet = Packet.parse(
Buffer.Buffer.from(data as Uint8Array),
) as DNSPACKETInterface;
console.log(
JSONSTRINGIFYNULL4(
{
request: { packet /* data: data */ },
},
null,
4,
),
);
} catch (error) {
console.error(error);
return new Response(
"bad request\ninvalid dns message\n" + String(error),
{ status: 400 },
);
}
} else if (
ctx.request.method === "POST" &&
req.headers.get("content-type") === "application/dns-message"
) {
const body = req.body && (await bodyToBuffer(req.body));
req.body = body;
if (body?.length) {
try {
const packet = Packet.parse(
Buffer.Buffer.from(body as Uint8Array),
) as DNSPACKETInterface;
console.log(
JSONSTRINGIFYNULL4(
{
request: { packet /* data: body */ },
},
null,
4,
),
);
// console.log();
// console.log(JSONSTRINGIFYNULL4({ packet });
} catch (error) {
console.error(error);
return new Response(
"bad request\ninvalid dns message\n" + String(error),
{ status: 400 },
);
}
}
}
const res = await next();
// console.log("parse_dns_message", res.body);
if (
res.status === 200 &&
res.headers.get("content-type") === "application/dns-message" &&
!res.body?.locked
) {
// console.log(res.body);
const resbody = res.body && (await bodyToBuffer(res.body));
res.body = resbody;
if (resbody?.length) {
const packet = Packet.parse(
Buffer.Buffer.from(resbody as Uint8Array),
) as DNSPACKETInterface;
console.log(
JSONSTRINGIFYNULL4(
{
response: { packet /* data: resbody */ },
},
null,
4,
),
);
// console.log(JSONSTRINGIFYNULL4({ resbody });
// console.log(JSONSTRINGIFYNULL4({ packet });
/* 删除dns数据包中的ADDITIONAL */
packet.additional = [];
const buff = new Buffer.Buffer(1096 * 1000);
const written = Packet.write(buff, packet);
return new Response(buff.slice(0, written), {
...res,
headers: {
...Object.fromEntries(res.headers),
"content-length": String(written),
},
});
}
}
return;
} else {
return await next();
}
}