@solomonneas/adguard-mcp 0.2.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +246 -33
- package/dist/cli.js +3779 -0
- package/dist/index.js +1429 -86
- package/dist/mcp-bin.js +3215 -0
- package/dist/mcp-server.js +2581 -446
- package/openclaw.plugin.json +8 -4
- package/package.json +39 -10
package/dist/index.js
CHANGED
|
@@ -2,9 +2,437 @@
|
|
|
2
2
|
|
|
3
3
|
// index.ts
|
|
4
4
|
import { Buffer as Buffer2 } from "buffer";
|
|
5
|
+
|
|
6
|
+
// node_modules/@lidless-labs/effect-operator-kit/dist/index.js
|
|
7
|
+
import { Data } from "effect";
|
|
8
|
+
import { Effect } from "effect";
|
|
9
|
+
import { Effect as Effect2 } from "effect";
|
|
10
|
+
import { Duration, Effect as Effect3, Schedule } from "effect";
|
|
11
|
+
import { Cause, Effect as Effect4, Exit } from "effect";
|
|
12
|
+
import { Cause as Cause2, Effect as Effect5 } from "effect";
|
|
13
|
+
var ConfigError = class extends Data.TaggedError("ConfigError") {
|
|
14
|
+
};
|
|
15
|
+
var AuthError = class extends Data.TaggedError("AuthError") {
|
|
16
|
+
};
|
|
17
|
+
var ForbiddenError = class extends Data.TaggedError("ForbiddenError") {
|
|
18
|
+
};
|
|
19
|
+
var NotFoundError = class extends Data.TaggedError("NotFoundError") {
|
|
20
|
+
};
|
|
21
|
+
var TimeoutError = class extends Data.TaggedError("TimeoutError") {
|
|
22
|
+
};
|
|
23
|
+
var TransportError = class extends Data.TaggedError("TransportError") {
|
|
24
|
+
};
|
|
25
|
+
var ParseError = class extends Data.TaggedError("ParseError") {
|
|
26
|
+
};
|
|
27
|
+
var UnexpectedStatusError = class extends Data.TaggedError("UnexpectedStatusError") {
|
|
28
|
+
};
|
|
29
|
+
var fromProcessEnv = (env) => ({
|
|
30
|
+
get: (key) => (env ?? process.env)[key]
|
|
31
|
+
});
|
|
32
|
+
var isBlank = (raw) => raw === void 0 || raw.trim() === "";
|
|
33
|
+
var failConfig = (partial) => Effect.fail(new ConfigError(partial));
|
|
34
|
+
var readRaw = (env, key) => env.get(key);
|
|
35
|
+
var parseTimeoutEnv = (env, key, opts) => {
|
|
36
|
+
const raw = readRaw(env, key);
|
|
37
|
+
if (isBlank(raw)) {
|
|
38
|
+
return Effect.succeed(opts.fallbackMs);
|
|
39
|
+
}
|
|
40
|
+
const parsed = Number(raw.trim());
|
|
41
|
+
if (!Number.isFinite(parsed) || parsed <= 0) {
|
|
42
|
+
return failConfig({
|
|
43
|
+
key,
|
|
44
|
+
value: raw,
|
|
45
|
+
message: `${key} must be a positive timeout`
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
const unit = opts.unit ?? "s";
|
|
49
|
+
const ms = unit === "ms" ? parsed : parsed * 1e3;
|
|
50
|
+
if (opts.minMs !== void 0 && ms < opts.minMs) {
|
|
51
|
+
return failConfig({
|
|
52
|
+
key,
|
|
53
|
+
value: raw,
|
|
54
|
+
message: `${key} must be at least ${opts.minMs}ms`
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
if (opts.maxMs !== void 0 && ms > opts.maxMs) {
|
|
58
|
+
return failConfig({
|
|
59
|
+
key,
|
|
60
|
+
value: raw,
|
|
61
|
+
message: `${key} must be at most ${opts.maxMs}ms`
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
return Effect.succeed(ms);
|
|
65
|
+
};
|
|
66
|
+
var SECRET_PATTERNS = [
|
|
67
|
+
/(authorization:\s*)(basic|bearer)\s+[a-z0-9._~+/=-]+/gi,
|
|
68
|
+
/\b(basic|bearer)\s+[a-z0-9._~+/=-]+/gi,
|
|
69
|
+
/(authorization:\s*)(?:token|digest|hawk|oauth|apikey)\s+\S+/gi,
|
|
70
|
+
/(x-n8n-api-key|x-auth-token|x-emby-token|x-api-key)\s*:\s*\S+/gi,
|
|
71
|
+
/(https?:\/\/)([^@\s/]+)@/gi,
|
|
72
|
+
/([?&](?:access_token|refresh_token|client_secret|id_token|x-api-key|\w+_(?:token|secret)|password|token|secret|api[_-]?key|key)=)[^&\s]+/gi,
|
|
73
|
+
/\beyJ[a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]+\b/g
|
|
74
|
+
];
|
|
75
|
+
var MAX_LENGTH = 500;
|
|
76
|
+
var redactString = (message, secrets = []) => {
|
|
77
|
+
let sanitized = message;
|
|
78
|
+
for (const secret of secrets) {
|
|
79
|
+
if (secret) {
|
|
80
|
+
sanitized = sanitized.split(secret).join("[REDACTED]");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
sanitized = sanitized.replace(SECRET_PATTERNS[0], "$1$2 [REDACTED]").replace(SECRET_PATTERNS[1], "$1 [REDACTED]").replace(SECRET_PATTERNS[2], "$1[REDACTED]").replace(SECRET_PATTERNS[3], "$1: [REDACTED]").replace(SECRET_PATTERNS[4], "$1[REDACTED]@").replace(SECRET_PATTERNS[5], "$1[REDACTED]").replace(SECRET_PATTERNS[6], "[REDACTED]");
|
|
84
|
+
return sanitized.length > MAX_LENGTH ? `${sanitized.slice(0, MAX_LENGTH)}...` : sanitized;
|
|
85
|
+
};
|
|
86
|
+
var defaultRedact = (value) => redactString(value);
|
|
87
|
+
var DEFAULT_SUCCESS_STATUSES = Array.from({ length: 100 }, (_, i) => 200 + i);
|
|
88
|
+
function redactBody(text, redact2) {
|
|
89
|
+
return (redact2 ?? defaultRedact)(text);
|
|
90
|
+
}
|
|
91
|
+
function mergeHeaders(...sources) {
|
|
92
|
+
const merged = new Headers();
|
|
93
|
+
for (const source of sources) {
|
|
94
|
+
if (!source) continue;
|
|
95
|
+
const headers = new Headers(source);
|
|
96
|
+
headers.forEach((value, key) => merged.set(key, value));
|
|
97
|
+
}
|
|
98
|
+
return merged;
|
|
99
|
+
}
|
|
100
|
+
function resolveExpectedStatuses(req) {
|
|
101
|
+
return req.expectedStatuses ?? DEFAULT_SUCCESS_STATUSES;
|
|
102
|
+
}
|
|
103
|
+
function isExpectedStatus(status, expected) {
|
|
104
|
+
return expected.includes(status);
|
|
105
|
+
}
|
|
106
|
+
function resolveBodyEncoding(req) {
|
|
107
|
+
if (req.bodyEncoding !== void 0) return req.bodyEncoding;
|
|
108
|
+
return req.body !== void 0 ? "json" : "none";
|
|
109
|
+
}
|
|
110
|
+
function setContentTypeIfAbsent(headers, contentType) {
|
|
111
|
+
if (!headers.has("content-type")) {
|
|
112
|
+
headers.set("content-type", contentType);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function buildRequestBody(req, headers) {
|
|
116
|
+
const encoding = resolveBodyEncoding(req);
|
|
117
|
+
switch (encoding) {
|
|
118
|
+
case "none":
|
|
119
|
+
return void 0;
|
|
120
|
+
case "json":
|
|
121
|
+
setContentTypeIfAbsent(headers, "application/json");
|
|
122
|
+
return JSON.stringify(req.body);
|
|
123
|
+
case "text":
|
|
124
|
+
setContentTypeIfAbsent(headers, "text/plain");
|
|
125
|
+
return String(req.body);
|
|
126
|
+
case "form": {
|
|
127
|
+
setContentTypeIfAbsent(
|
|
128
|
+
headers,
|
|
129
|
+
"application/x-www-form-urlencoded"
|
|
130
|
+
);
|
|
131
|
+
const params = new URLSearchParams();
|
|
132
|
+
if (req.body && typeof req.body === "object") {
|
|
133
|
+
for (const [key, value] of Object.entries(
|
|
134
|
+
req.body
|
|
135
|
+
)) {
|
|
136
|
+
params.set(key, String(value));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return params.toString();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function mapUnexpectedStatus(req, status, bodyText, expectedStatuses, redact2) {
|
|
144
|
+
const redacted = redactBody(bodyText, redact2);
|
|
145
|
+
const mapperInput = {
|
|
146
|
+
status,
|
|
147
|
+
method: req.method,
|
|
148
|
+
path: req.path,
|
|
149
|
+
bodyText: redacted,
|
|
150
|
+
expectedStatuses
|
|
151
|
+
};
|
|
152
|
+
if (req.statusMapper) {
|
|
153
|
+
const custom = req.statusMapper(mapperInput);
|
|
154
|
+
if (custom !== null) return custom;
|
|
155
|
+
}
|
|
156
|
+
const base = {
|
|
157
|
+
method: req.method,
|
|
158
|
+
path: req.path,
|
|
159
|
+
status,
|
|
160
|
+
body: redacted
|
|
161
|
+
};
|
|
162
|
+
switch (status) {
|
|
163
|
+
case 401:
|
|
164
|
+
return new AuthError(base);
|
|
165
|
+
case 403:
|
|
166
|
+
return new ForbiddenError(base);
|
|
167
|
+
case 404:
|
|
168
|
+
return new NotFoundError(base);
|
|
169
|
+
default:
|
|
170
|
+
return new UnexpectedStatusError({
|
|
171
|
+
method: req.method,
|
|
172
|
+
path: req.path,
|
|
173
|
+
status,
|
|
174
|
+
body: redacted,
|
|
175
|
+
expected: expectedStatuses
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
function parseResponseBody(req, bodyText, redact2) {
|
|
180
|
+
const responseType = req.responseType ?? "json";
|
|
181
|
+
if (responseType === "none") {
|
|
182
|
+
return Effect2.succeed(void 0);
|
|
183
|
+
}
|
|
184
|
+
if (responseType === "text") {
|
|
185
|
+
return Effect2.succeed(bodyText);
|
|
186
|
+
}
|
|
187
|
+
if (bodyText.length === 0) {
|
|
188
|
+
return Effect2.succeed(void 0);
|
|
189
|
+
}
|
|
190
|
+
return Effect2.try({
|
|
191
|
+
try: () => JSON.parse(bodyText),
|
|
192
|
+
catch: (cause) => new ParseError({
|
|
193
|
+
path: req.path,
|
|
194
|
+
body: redactBody(bodyText, redact2),
|
|
195
|
+
message: "Failed to parse JSON response",
|
|
196
|
+
cause
|
|
197
|
+
})
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
function fetchWithTimeout(ctx, url, init, timeoutMs, method, path) {
|
|
201
|
+
const fetchImpl = ctx.fetch ?? globalThis.fetch;
|
|
202
|
+
return Effect2.async((resume) => {
|
|
203
|
+
const controller = new AbortController();
|
|
204
|
+
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
|
205
|
+
const finish = (effect) => {
|
|
206
|
+
clearTimeout(timeoutId);
|
|
207
|
+
resume(effect);
|
|
208
|
+
};
|
|
209
|
+
void fetchImpl(url, { ...init, signal: controller.signal }).then((response) => finish(Effect2.succeed(response))).catch((cause) => {
|
|
210
|
+
if (cause instanceof Error && cause.name === "AbortError") {
|
|
211
|
+
finish(
|
|
212
|
+
Effect2.fail(new TimeoutError({ method, path, timeoutMs }))
|
|
213
|
+
);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
finish(
|
|
217
|
+
Effect2.fail(new TransportError({ method, path, cause }))
|
|
218
|
+
);
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
var buildUrl = (baseUrl, path, query) => {
|
|
223
|
+
if (path.includes("\\")) {
|
|
224
|
+
throw new TypeError("buildUrl: path must not contain backslash");
|
|
225
|
+
}
|
|
226
|
+
const url = new URL(path, baseUrl);
|
|
227
|
+
if (url.username || url.password) {
|
|
228
|
+
throw new TypeError("buildUrl: path must not contain URL userinfo");
|
|
229
|
+
}
|
|
230
|
+
if (url.origin !== baseUrl.origin) {
|
|
231
|
+
throw new TypeError(
|
|
232
|
+
`buildUrl: path "${path}" resolves to a different origin (${url.origin}) than baseUrl (${baseUrl.origin})`
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
if (query) {
|
|
236
|
+
for (const [key, value] of Object.entries(query)) {
|
|
237
|
+
if (value === null || value === void 0) continue;
|
|
238
|
+
if (Array.isArray(value)) {
|
|
239
|
+
for (const item of value) {
|
|
240
|
+
if (item === null || item === void 0) continue;
|
|
241
|
+
url.searchParams.append(key, String(item));
|
|
242
|
+
}
|
|
243
|
+
} else {
|
|
244
|
+
url.searchParams.set(key, String(value));
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return url;
|
|
249
|
+
};
|
|
250
|
+
function buildHeaders(ctx, req) {
|
|
251
|
+
const auth = ctx.auth;
|
|
252
|
+
if (!auth) {
|
|
253
|
+
return Effect2.succeed(mergeHeaders(ctx.defaultHeaders, req.headers));
|
|
254
|
+
}
|
|
255
|
+
return Effect2.gen(function* () {
|
|
256
|
+
let headers = mergeHeaders(ctx.defaultHeaders);
|
|
257
|
+
headers = yield* auth.apply(headers);
|
|
258
|
+
return mergeHeaders(headers, req.headers);
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
var sendRequest = (ctx, req) => {
|
|
262
|
+
const url = buildUrl(ctx.baseUrl, req.path, req.query);
|
|
263
|
+
const timeoutMs = req.timeoutMs ?? ctx.timeoutMs;
|
|
264
|
+
const expectedStatuses = resolveExpectedStatuses(req);
|
|
265
|
+
return buildHeaders(ctx, req).pipe(
|
|
266
|
+
Effect2.flatMap((headers) => {
|
|
267
|
+
const body = buildRequestBody(req, headers);
|
|
268
|
+
const init = {
|
|
269
|
+
method: req.method,
|
|
270
|
+
headers,
|
|
271
|
+
...body !== void 0 ? { body } : {}
|
|
272
|
+
};
|
|
273
|
+
return fetchWithTimeout(
|
|
274
|
+
ctx,
|
|
275
|
+
url,
|
|
276
|
+
init,
|
|
277
|
+
timeoutMs,
|
|
278
|
+
req.method,
|
|
279
|
+
req.path
|
|
280
|
+
).pipe(
|
|
281
|
+
Effect2.flatMap(
|
|
282
|
+
(response) => Effect2.tryPromise({
|
|
283
|
+
try: () => response.text(),
|
|
284
|
+
catch: (cause) => new TransportError({
|
|
285
|
+
method: req.method,
|
|
286
|
+
path: req.path,
|
|
287
|
+
cause
|
|
288
|
+
})
|
|
289
|
+
}).pipe(
|
|
290
|
+
Effect2.flatMap((bodyText) => {
|
|
291
|
+
if (!isExpectedStatus(response.status, expectedStatuses)) {
|
|
292
|
+
return Effect2.fail(
|
|
293
|
+
mapUnexpectedStatus(
|
|
294
|
+
req,
|
|
295
|
+
response.status,
|
|
296
|
+
bodyText,
|
|
297
|
+
expectedStatuses,
|
|
298
|
+
ctx.redact
|
|
299
|
+
)
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
return parseResponseBody(req, bodyText, ctx.redact).pipe(
|
|
303
|
+
Effect2.map((parsedBody) => ({
|
|
304
|
+
status: response.status,
|
|
305
|
+
headers: response.headers,
|
|
306
|
+
bodyText,
|
|
307
|
+
body: parsedBody
|
|
308
|
+
}))
|
|
309
|
+
);
|
|
310
|
+
})
|
|
311
|
+
)
|
|
312
|
+
)
|
|
313
|
+
);
|
|
314
|
+
})
|
|
315
|
+
);
|
|
316
|
+
};
|
|
317
|
+
var defaultShouldRetry = (error) => {
|
|
318
|
+
switch (error._tag) {
|
|
319
|
+
case "TransportError":
|
|
320
|
+
case "TimeoutError":
|
|
321
|
+
return true;
|
|
322
|
+
case "UnexpectedStatusError":
|
|
323
|
+
return error.status >= 500;
|
|
324
|
+
default:
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
var DEFAULT_POLICY_FIELDS = {
|
|
329
|
+
maxAttempts: 3,
|
|
330
|
+
initialDelayMs: 100,
|
|
331
|
+
maxDelayMs: 5e3,
|
|
332
|
+
factor: 2,
|
|
333
|
+
jitter: true,
|
|
334
|
+
shouldRetry: defaultShouldRetry
|
|
335
|
+
};
|
|
336
|
+
var noRetry = {
|
|
337
|
+
enabled: false,
|
|
338
|
+
...DEFAULT_POLICY_FIELDS,
|
|
339
|
+
shouldRetry: () => false
|
|
340
|
+
};
|
|
341
|
+
var exponentialRetry = (opts) => ({
|
|
342
|
+
enabled: opts.enabled ?? true,
|
|
343
|
+
maxAttempts: opts.maxAttempts ?? DEFAULT_POLICY_FIELDS.maxAttempts,
|
|
344
|
+
initialDelayMs: opts.initialDelayMs ?? DEFAULT_POLICY_FIELDS.initialDelayMs,
|
|
345
|
+
maxDelayMs: opts.maxDelayMs ?? DEFAULT_POLICY_FIELDS.maxDelayMs,
|
|
346
|
+
factor: opts.factor ?? DEFAULT_POLICY_FIELDS.factor,
|
|
347
|
+
jitter: opts.jitter ?? DEFAULT_POLICY_FIELDS.jitter,
|
|
348
|
+
shouldRetry: opts.shouldRetry ?? DEFAULT_POLICY_FIELDS.shouldRetry
|
|
349
|
+
});
|
|
350
|
+
var buildSchedule = (policy) => {
|
|
351
|
+
const base = Schedule.exponential(`${policy.initialDelayMs} millis`, policy.factor);
|
|
352
|
+
const withJitter = policy.jitter ? base.pipe(Schedule.jittered) : base;
|
|
353
|
+
const capped = withJitter.pipe(
|
|
354
|
+
Schedule.modifyDelay((_out, duration) => {
|
|
355
|
+
const ms = Duration.toMillis(duration);
|
|
356
|
+
return ms > policy.maxDelayMs ? `${policy.maxDelayMs} millis` : duration;
|
|
357
|
+
})
|
|
358
|
+
);
|
|
359
|
+
return capped.pipe(Schedule.whileInput((error) => policy.shouldRetry(error)));
|
|
360
|
+
};
|
|
361
|
+
var withRetry = (effect, policy = noRetry) => {
|
|
362
|
+
if (!policy.enabled || policy.maxAttempts <= 1) {
|
|
363
|
+
return effect;
|
|
364
|
+
}
|
|
365
|
+
const retries = Math.max(0, policy.maxAttempts - 1);
|
|
366
|
+
return Effect3.retry(effect, {
|
|
367
|
+
times: retries,
|
|
368
|
+
while: (error) => policy.shouldRetry(error),
|
|
369
|
+
schedule: buildSchedule(policy)
|
|
370
|
+
});
|
|
371
|
+
};
|
|
372
|
+
var ok = (details) => ({
|
|
373
|
+
content: [{ type: "text", text: JSON.stringify(details, null, 2) }],
|
|
374
|
+
details
|
|
375
|
+
});
|
|
376
|
+
var fail = (message, details) => {
|
|
377
|
+
const result = {
|
|
378
|
+
content: [
|
|
379
|
+
{
|
|
380
|
+
type: "text",
|
|
381
|
+
text: JSON.stringify({ error: message }, null, 2)
|
|
382
|
+
}
|
|
383
|
+
],
|
|
384
|
+
isError: true
|
|
385
|
+
};
|
|
386
|
+
if (details !== void 0) {
|
|
387
|
+
result.details = details;
|
|
388
|
+
}
|
|
389
|
+
return result;
|
|
390
|
+
};
|
|
391
|
+
var refuseUnconfirmed = (operation) => fail(
|
|
392
|
+
`Refusing to ${operation} without explicit confirmation. Re-call this tool with confirm: true to proceed.`
|
|
393
|
+
);
|
|
394
|
+
var operatorErrorMessage = (error) => {
|
|
395
|
+
if (isTaggedShape(error)) {
|
|
396
|
+
if (typeof error.message === "string" && error.message.length > 0 && error.message !== error._tag) {
|
|
397
|
+
return error.message;
|
|
398
|
+
}
|
|
399
|
+
return formatTaggedThin(error);
|
|
400
|
+
}
|
|
401
|
+
if (error instanceof Error && error.message.length > 0) {
|
|
402
|
+
return error.message;
|
|
403
|
+
}
|
|
404
|
+
return String(error);
|
|
405
|
+
};
|
|
406
|
+
function isTaggedShape(error) {
|
|
407
|
+
return error !== null && typeof error === "object" && "_tag" in error && typeof error._tag === "string";
|
|
408
|
+
}
|
|
409
|
+
function formatTaggedThin(error) {
|
|
410
|
+
switch (error._tag) {
|
|
411
|
+
case "ConfigError":
|
|
412
|
+
return error.key ? `ConfigError: ${error.key}${error.message ? `: ${error.message}` : ""}` : error.message || "ConfigError";
|
|
413
|
+
case "TimeoutError":
|
|
414
|
+
return `TimeoutError: ${error.method ?? "?"} ${error.path ?? "?"} after ${error.timeoutMs ?? "?"}ms`;
|
|
415
|
+
case "AuthError":
|
|
416
|
+
case "ForbiddenError":
|
|
417
|
+
case "NotFoundError":
|
|
418
|
+
case "UnexpectedStatusError":
|
|
419
|
+
return `${error._tag}: ${error.method ?? "?"} ${error.path ?? "?"}${error.status !== void 0 ? ` (${error.status})` : ""}`;
|
|
420
|
+
case "TransportError":
|
|
421
|
+
return `TransportError: ${error.method ?? "?"} ${error.path ?? "?"}`;
|
|
422
|
+
case "ParseError":
|
|
423
|
+
return error.message || `ParseError${error.path ? `: ${error.path}` : ""}`;
|
|
424
|
+
default:
|
|
425
|
+
return error.message || error._tag;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// index.ts
|
|
5
430
|
import { definePluginEntry } from "openclaw/plugin-sdk/plugin-entry";
|
|
6
431
|
|
|
7
432
|
// src/config.ts
|
|
433
|
+
import { Effect as Effect6 } from "effect";
|
|
434
|
+
var DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
|
|
435
|
+
var MIN_REQUEST_TIMEOUT_MS = 1e3;
|
|
8
436
|
var NoInstancesError = class extends Error {
|
|
9
437
|
constructor() {
|
|
10
438
|
super("No AdGuard instances configured. Set ADGUARD_<NAME>_URL/USERNAME/PASSWORD for at least one instance.");
|
|
@@ -33,21 +461,61 @@ var UnknownDefaultInstanceError = class extends Error {
|
|
|
33
461
|
this.name = "UnknownDefaultInstanceError";
|
|
34
462
|
}
|
|
35
463
|
};
|
|
464
|
+
var NoSyncServerError = class extends Error {
|
|
465
|
+
constructor() {
|
|
466
|
+
super("No AdGuardHome Sync server configured. Set ADGUARDHOME_SYNC_URL or ADGUARD_SYNC_URL, and optionally matching USERNAME/PASSWORD.");
|
|
467
|
+
this.name = "NoSyncServerError";
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
var PartialSyncConfigError = class extends Error {
|
|
471
|
+
constructor(missing) {
|
|
472
|
+
super(
|
|
473
|
+
`Partial AdGuardHome Sync config: missing ${missing.join(", ")}. Set ADGUARDHOME_SYNC_URL or ADGUARD_SYNC_URL, and set USERNAME/PASSWORD together when the Sync API uses auth.`
|
|
474
|
+
);
|
|
475
|
+
this.name = "PartialSyncConfigError";
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
function presentEnvValue(env, key) {
|
|
479
|
+
const raw = env.get(key);
|
|
480
|
+
if (raw === void 0 || raw === "") return void 0;
|
|
481
|
+
return raw;
|
|
482
|
+
}
|
|
483
|
+
function firstPresentAlias(env, keys) {
|
|
484
|
+
for (const key of keys) {
|
|
485
|
+
const value = presentEnvValue(env, key);
|
|
486
|
+
if (value !== void 0) return value;
|
|
487
|
+
}
|
|
488
|
+
return void 0;
|
|
489
|
+
}
|
|
490
|
+
function readDefaultInstance(env) {
|
|
491
|
+
const raw = env.get("ADGUARD_DEFAULT_INSTANCE");
|
|
492
|
+
if (raw === void 0 || raw === "") return void 0;
|
|
493
|
+
return raw.toLowerCase();
|
|
494
|
+
}
|
|
495
|
+
function readRequiredInstanceField(env, key) {
|
|
496
|
+
const raw = env.get(key);
|
|
497
|
+
if (raw === void 0 || raw === "") {
|
|
498
|
+
throw new Error(`${key} is required`);
|
|
499
|
+
}
|
|
500
|
+
return raw;
|
|
501
|
+
}
|
|
36
502
|
function resolveInstances(env) {
|
|
503
|
+
const reader = fromProcessEnv(env);
|
|
37
504
|
const instances = {};
|
|
38
505
|
const candidateNames = /* @__PURE__ */ new Set();
|
|
39
506
|
const SUFFIX_RE = /^ADGUARD_([A-Z0-9_]+)_(URL|USERNAME|PASSWORD)$/;
|
|
40
507
|
for (const key of Object.keys(env)) {
|
|
41
508
|
const m = SUFFIX_RE.exec(key);
|
|
42
509
|
if (!m) continue;
|
|
510
|
+
if (m[1] === "SYNC") continue;
|
|
43
511
|
if (env[key] === void 0 || env[key] === "") continue;
|
|
44
512
|
candidateNames.add(m[1]);
|
|
45
513
|
}
|
|
46
514
|
for (const upperName of candidateNames) {
|
|
47
515
|
const name = upperName.toLowerCase();
|
|
48
|
-
const url =
|
|
49
|
-
const username =
|
|
50
|
-
const password =
|
|
516
|
+
const url = presentEnvValue(reader, `ADGUARD_${upperName}_URL`);
|
|
517
|
+
const username = presentEnvValue(reader, `ADGUARD_${upperName}_USERNAME`);
|
|
518
|
+
const password = presentEnvValue(reader, `ADGUARD_${upperName}_PASSWORD`);
|
|
51
519
|
const missing = [];
|
|
52
520
|
if (!url) missing.push("URL");
|
|
53
521
|
if (!username) missing.push("USERNAME");
|
|
@@ -55,10 +523,14 @@ function resolveInstances(env) {
|
|
|
55
523
|
if (missing.length > 0) {
|
|
56
524
|
throw new PartialInstanceConfigError(name, missing);
|
|
57
525
|
}
|
|
58
|
-
instances[name] = {
|
|
526
|
+
instances[name] = {
|
|
527
|
+
url: readRequiredInstanceField(reader, `ADGUARD_${upperName}_URL`),
|
|
528
|
+
username: readRequiredInstanceField(reader, `ADGUARD_${upperName}_USERNAME`),
|
|
529
|
+
password: readRequiredInstanceField(reader, `ADGUARD_${upperName}_PASSWORD`)
|
|
530
|
+
};
|
|
59
531
|
}
|
|
60
532
|
if (Object.keys(instances).length === 0) throw new NoInstancesError();
|
|
61
|
-
const explicitDefault =
|
|
533
|
+
const explicitDefault = readDefaultInstance(reader);
|
|
62
534
|
if (explicitDefault) {
|
|
63
535
|
if (!instances[explicitDefault]) {
|
|
64
536
|
throw new UnknownDefaultInstanceError(explicitDefault, Object.keys(instances));
|
|
@@ -68,12 +540,80 @@ function resolveInstances(env) {
|
|
|
68
540
|
const defaultInstance = instances.primary ? "primary" : Object.keys(instances).sort()[0];
|
|
69
541
|
return { instances, defaultInstance };
|
|
70
542
|
}
|
|
543
|
+
function resolveSyncConfig(env) {
|
|
544
|
+
const reader = fromProcessEnv(env);
|
|
545
|
+
const url = firstPresentAlias(reader, ["ADGUARDHOME_SYNC_URL", "ADGUARD_SYNC_URL"]);
|
|
546
|
+
const username = firstPresentAlias(reader, ["ADGUARDHOME_SYNC_USERNAME", "ADGUARD_SYNC_USERNAME"]);
|
|
547
|
+
const password = firstPresentAlias(reader, ["ADGUARDHOME_SYNC_PASSWORD", "ADGUARD_SYNC_PASSWORD"]);
|
|
548
|
+
const missing = [];
|
|
549
|
+
if (!url && (username || password)) missing.push("URL");
|
|
550
|
+
if (username && !password) missing.push("PASSWORD");
|
|
551
|
+
if (password && !username) missing.push("USERNAME");
|
|
552
|
+
if (missing.length > 0) throw new PartialSyncConfigError(missing);
|
|
553
|
+
if (!url) return void 0;
|
|
554
|
+
return username && password ? { url, username, password } : { url };
|
|
555
|
+
}
|
|
71
556
|
function getInstanceConfig(cfg, name) {
|
|
72
557
|
const resolved = (name ?? cfg.defaultInstance).toLowerCase();
|
|
73
558
|
const inst = cfg.instances[resolved];
|
|
74
559
|
if (!inst) throw new UnknownInstanceError(resolved, Object.keys(cfg.instances));
|
|
75
560
|
return inst;
|
|
76
561
|
}
|
|
562
|
+
function getSyncConfig(cfg) {
|
|
563
|
+
if (!cfg) throw new NoSyncServerError();
|
|
564
|
+
return cfg;
|
|
565
|
+
}
|
|
566
|
+
function resolveRequestTimeoutMs(env) {
|
|
567
|
+
const reader = fromProcessEnv(env);
|
|
568
|
+
return Effect6.runSync(
|
|
569
|
+
parseTimeoutEnv(reader, "ADGUARD_REQUEST_TIMEOUT_MS", {
|
|
570
|
+
fallbackMs: DEFAULT_REQUEST_TIMEOUT_MS,
|
|
571
|
+
minMs: MIN_REQUEST_TIMEOUT_MS,
|
|
572
|
+
unit: "ms"
|
|
573
|
+
})
|
|
574
|
+
);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
// src/adguard-client.ts
|
|
578
|
+
import { Effect as Effect7 } from "effect";
|
|
579
|
+
|
|
580
|
+
// src/security.ts
|
|
581
|
+
import { createHash, timingSafeEqual } from "crypto";
|
|
582
|
+
var SECRETS = /* @__PURE__ */ new Set();
|
|
583
|
+
function registerSecret(s) {
|
|
584
|
+
if (!s || s.length === 0) return;
|
|
585
|
+
SECRETS.add(s);
|
|
586
|
+
}
|
|
587
|
+
var BASE64_TOKEN = /[A-Za-z0-9+/]{12,}={0,2}/g;
|
|
588
|
+
function maskBase64Tokens(input) {
|
|
589
|
+
return input.replace(BASE64_TOKEN, (match) => {
|
|
590
|
+
let decoded;
|
|
591
|
+
try {
|
|
592
|
+
decoded = Buffer.from(match, "base64").toString("utf8");
|
|
593
|
+
} catch {
|
|
594
|
+
return match;
|
|
595
|
+
}
|
|
596
|
+
for (const secret of SECRETS) {
|
|
597
|
+
if (decoded.includes(secret)) return "REDACTED";
|
|
598
|
+
}
|
|
599
|
+
return match;
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
function redact(value) {
|
|
603
|
+
if (typeof value === "string") {
|
|
604
|
+
let out = value;
|
|
605
|
+
for (const secret of SECRETS) out = out.split(secret).join("REDACTED");
|
|
606
|
+
out = maskBase64Tokens(out);
|
|
607
|
+
return out;
|
|
608
|
+
}
|
|
609
|
+
if (Array.isArray(value)) return value.map(redact);
|
|
610
|
+
if (value && typeof value === "object") {
|
|
611
|
+
const out = {};
|
|
612
|
+
for (const [k, v] of Object.entries(value)) out[k] = redact(v);
|
|
613
|
+
return out;
|
|
614
|
+
}
|
|
615
|
+
return value;
|
|
616
|
+
}
|
|
77
617
|
|
|
78
618
|
// src/adguard-client.ts
|
|
79
619
|
var AdGuardClientError = class extends Error {
|
|
@@ -82,7 +622,6 @@ var AdGuardClientError = class extends Error {
|
|
|
82
622
|
this.status = status;
|
|
83
623
|
this.name = "AdGuardClientError";
|
|
84
624
|
}
|
|
85
|
-
status;
|
|
86
625
|
};
|
|
87
626
|
var AdGuardUnreachableError = class extends Error {
|
|
88
627
|
constructor(cause) {
|
|
@@ -94,11 +633,23 @@ var AdGuardClient = class {
|
|
|
94
633
|
constructor(cfg, opts = {}) {
|
|
95
634
|
this.cfg = cfg;
|
|
96
635
|
this.authHeader = "Basic " + Buffer.from(`${cfg.username}:${cfg.password}`).toString("base64");
|
|
636
|
+
registerSecret(cfg.password);
|
|
637
|
+
registerSecret(this.authHeader);
|
|
97
638
|
this.retryDelayMs = opts.retryDelayMs ?? 1e3;
|
|
639
|
+
this.timeoutMs = opts.timeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
|
|
640
|
+
this.baseUrl = baseUrlForConcat(cfg.url);
|
|
641
|
+
this.auth = {
|
|
642
|
+
apply: (headers) => {
|
|
643
|
+
headers.set("authorization", this.authHeader);
|
|
644
|
+
return Effect7.succeed(headers);
|
|
645
|
+
}
|
|
646
|
+
};
|
|
98
647
|
}
|
|
99
|
-
cfg;
|
|
100
648
|
authHeader;
|
|
101
649
|
retryDelayMs;
|
|
650
|
+
timeoutMs;
|
|
651
|
+
baseUrl;
|
|
652
|
+
auth;
|
|
102
653
|
async get(path) {
|
|
103
654
|
return this.request("GET", path);
|
|
104
655
|
}
|
|
@@ -109,82 +660,294 @@ var AdGuardClient = class {
|
|
|
109
660
|
return this.request("PUT", path, body);
|
|
110
661
|
}
|
|
111
662
|
async request(method, path, body) {
|
|
112
|
-
const
|
|
113
|
-
const
|
|
114
|
-
let bodyStr;
|
|
115
|
-
if (body !== void 0) {
|
|
116
|
-
headers["content-type"] = "application/json";
|
|
117
|
-
bodyStr = JSON.stringify(body);
|
|
118
|
-
}
|
|
119
|
-
let lastErr;
|
|
120
|
-
for (let attempt = 0; attempt < 2; attempt++) {
|
|
663
|
+
const reqPath = pathForConcat(path);
|
|
664
|
+
const httpEffect = (() => {
|
|
121
665
|
try {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
} catch (
|
|
140
|
-
|
|
141
|
-
lastErr = new AdGuardUnreachableError(e.message);
|
|
142
|
-
if (attempt === 0) await sleep(this.retryDelayMs);
|
|
666
|
+
return sendRequest(
|
|
667
|
+
{
|
|
668
|
+
baseUrl: this.baseUrl,
|
|
669
|
+
auth: this.auth,
|
|
670
|
+
timeoutMs: this.timeoutMs,
|
|
671
|
+
fetch: fetchWithPlainHeaders,
|
|
672
|
+
redact: identityRedact
|
|
673
|
+
},
|
|
674
|
+
{
|
|
675
|
+
method,
|
|
676
|
+
path: reqPath,
|
|
677
|
+
body,
|
|
678
|
+
responseType: "text",
|
|
679
|
+
expectedStatuses: successStatuses,
|
|
680
|
+
statusMapper: ({ status, method: method2, path: path2, bodyText, expectedStatuses }) => new UnexpectedStatusError({ status, method: method2, path: path2, body: bodyText, expected: expectedStatuses })
|
|
681
|
+
}
|
|
682
|
+
);
|
|
683
|
+
} catch (error) {
|
|
684
|
+
throw new AdGuardUnreachableError(errorMessage(error));
|
|
143
685
|
}
|
|
144
|
-
}
|
|
145
|
-
|
|
686
|
+
})();
|
|
687
|
+
const effect = withRetry(
|
|
688
|
+
httpEffect,
|
|
689
|
+
exponentialRetry({
|
|
690
|
+
maxAttempts: 2,
|
|
691
|
+
initialDelayMs: this.retryDelayMs,
|
|
692
|
+
maxDelayMs: this.retryDelayMs,
|
|
693
|
+
jitter: false,
|
|
694
|
+
shouldRetry: (error) => error._tag === "TransportError" || error._tag === "TimeoutError" || error._tag === "UnexpectedStatusError" && error.status >= 500
|
|
695
|
+
})
|
|
696
|
+
).pipe(
|
|
697
|
+
Effect7.map((res) => parseSuccessBody(res.bodyText)),
|
|
698
|
+
Effect7.mapError((error) => toAdGuardError(error))
|
|
699
|
+
);
|
|
700
|
+
return Effect7.runPromise(Effect7.either(effect)).then((result) => {
|
|
701
|
+
if (result._tag === "Right") return result.right;
|
|
702
|
+
throw result.left;
|
|
703
|
+
});
|
|
146
704
|
}
|
|
147
705
|
};
|
|
148
|
-
|
|
149
|
-
|
|
706
|
+
var successStatuses = Array.from({ length: 100 }, (_, i) => 200 + i);
|
|
707
|
+
function toAdGuardError(error) {
|
|
708
|
+
if (error._tag === "UnexpectedStatusError") {
|
|
709
|
+
if (error.status >= 500) return new AdGuardUnreachableError(`HTTP ${error.status}`);
|
|
710
|
+
return new AdGuardClientError(error.status, extractErrorMessage(error.body));
|
|
711
|
+
}
|
|
712
|
+
if (error._tag === "TransportError") {
|
|
713
|
+
return new AdGuardUnreachableError(errorMessage(error.cause));
|
|
714
|
+
}
|
|
715
|
+
if (error._tag === "TimeoutError") return new AdGuardUnreachableError("The operation was aborted.");
|
|
716
|
+
if (error._tag === "ParseError") return new AdGuardUnreachableError(error.message);
|
|
717
|
+
return new AdGuardUnreachableError(String(error));
|
|
150
718
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
719
|
+
function extractErrorMessage(text) {
|
|
720
|
+
let msg = text;
|
|
721
|
+
try {
|
|
722
|
+
msg = JSON.parse(text).message ?? text;
|
|
723
|
+
} catch {
|
|
724
|
+
}
|
|
725
|
+
return redact(msg);
|
|
157
726
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
727
|
+
function errorMessage(error) {
|
|
728
|
+
return error instanceof Error ? error.message : String(error);
|
|
729
|
+
}
|
|
730
|
+
function identityRedact(value) {
|
|
731
|
+
return value;
|
|
732
|
+
}
|
|
733
|
+
function baseUrlForConcat(raw) {
|
|
734
|
+
return new URL(raw.replace(/\/+$/, "") + "/");
|
|
735
|
+
}
|
|
736
|
+
function pathForConcat(path) {
|
|
737
|
+
return path.replace(/^\/+/, "");
|
|
738
|
+
}
|
|
739
|
+
async function fetchWithPlainHeaders(input, init) {
|
|
740
|
+
const headers = init?.headers instanceof Headers ? Object.fromEntries(init.headers.entries()) : init?.headers;
|
|
741
|
+
const signal = init?.signal;
|
|
742
|
+
const response = await fetch(input.toString(), { ...init, headers });
|
|
743
|
+
const bodyText = await new Promise((resolve, reject) => {
|
|
744
|
+
const abortError = () => Object.assign(new Error("The operation was aborted"), { name: "AbortError" });
|
|
745
|
+
if (signal?.aborted) {
|
|
746
|
+
void response.body?.cancel().catch(() => {
|
|
747
|
+
});
|
|
748
|
+
reject(abortError());
|
|
749
|
+
return;
|
|
169
750
|
}
|
|
170
|
-
|
|
751
|
+
const onAbort = () => {
|
|
752
|
+
void response.body?.cancel().catch(() => {
|
|
753
|
+
});
|
|
754
|
+
reject(abortError());
|
|
755
|
+
};
|
|
756
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
757
|
+
response.text().then(
|
|
758
|
+
(text) => {
|
|
759
|
+
signal?.removeEventListener("abort", onAbort);
|
|
760
|
+
resolve(text);
|
|
761
|
+
},
|
|
762
|
+
(err) => {
|
|
763
|
+
signal?.removeEventListener("abort", onAbort);
|
|
764
|
+
reject(err);
|
|
765
|
+
}
|
|
766
|
+
);
|
|
767
|
+
});
|
|
768
|
+
return new Response(bodyText, {
|
|
769
|
+
status: response.status,
|
|
770
|
+
statusText: response.statusText,
|
|
771
|
+
headers: response.headers
|
|
171
772
|
});
|
|
172
773
|
}
|
|
173
|
-
function
|
|
174
|
-
if (
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
return
|
|
774
|
+
function parseSuccessBody(text) {
|
|
775
|
+
if (!text) return void 0;
|
|
776
|
+
try {
|
|
777
|
+
return JSON.parse(text);
|
|
778
|
+
} catch {
|
|
779
|
+
return text;
|
|
179
780
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
// src/adguard-sync-client.ts
|
|
784
|
+
import { Effect as Effect8 } from "effect";
|
|
785
|
+
var AdGuardSyncClientError = class extends Error {
|
|
786
|
+
constructor(status, message) {
|
|
787
|
+
super(`AdGuardHome Sync ${status}: ${message}`);
|
|
788
|
+
this.status = status;
|
|
789
|
+
this.name = "AdGuardSyncClientError";
|
|
790
|
+
}
|
|
791
|
+
};
|
|
792
|
+
var AdGuardSyncUnreachableError = class extends Error {
|
|
793
|
+
constructor(cause) {
|
|
794
|
+
super(`AdGuardHome Sync unreachable: ${cause}`);
|
|
795
|
+
this.name = "AdGuardSyncUnreachableError";
|
|
796
|
+
}
|
|
797
|
+
};
|
|
798
|
+
var AdGuardSyncClient = class {
|
|
799
|
+
constructor(cfg, opts = {}) {
|
|
800
|
+
this.cfg = cfg;
|
|
801
|
+
if (cfg.username && cfg.password) {
|
|
802
|
+
this.authHeader = "Basic " + Buffer.from(`${cfg.username}:${cfg.password}`).toString("base64");
|
|
803
|
+
registerSecret(cfg.password);
|
|
804
|
+
registerSecret(this.authHeader);
|
|
805
|
+
this.auth = {
|
|
806
|
+
apply: (headers) => {
|
|
807
|
+
headers.set("authorization", this.authHeader);
|
|
808
|
+
return Effect8.succeed(headers);
|
|
809
|
+
}
|
|
810
|
+
};
|
|
811
|
+
}
|
|
812
|
+
this.retryDelayMs = opts.retryDelayMs ?? 1e3;
|
|
813
|
+
this.timeoutMs = opts.timeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
|
|
814
|
+
this.baseUrl = baseUrlForConcat2(cfg.url);
|
|
815
|
+
}
|
|
816
|
+
authHeader;
|
|
817
|
+
retryDelayMs;
|
|
818
|
+
timeoutMs;
|
|
819
|
+
baseUrl;
|
|
820
|
+
auth;
|
|
821
|
+
async get(path) {
|
|
822
|
+
return this.request("GET", path);
|
|
185
823
|
}
|
|
824
|
+
async post(path, body) {
|
|
825
|
+
return this.request("POST", path, body);
|
|
826
|
+
}
|
|
827
|
+
async head(path) {
|
|
828
|
+
await this.request("HEAD", path);
|
|
829
|
+
return { ok: true };
|
|
830
|
+
}
|
|
831
|
+
async request(method, path, body) {
|
|
832
|
+
const reqPath = pathForConcat2(path);
|
|
833
|
+
const httpEffect = (() => {
|
|
834
|
+
try {
|
|
835
|
+
return sendRequest(
|
|
836
|
+
{
|
|
837
|
+
baseUrl: this.baseUrl,
|
|
838
|
+
auth: this.auth,
|
|
839
|
+
timeoutMs: this.timeoutMs,
|
|
840
|
+
fetch: fetchWithPlainHeaders2,
|
|
841
|
+
redact: identityRedact2
|
|
842
|
+
},
|
|
843
|
+
{
|
|
844
|
+
method,
|
|
845
|
+
path: reqPath,
|
|
846
|
+
body,
|
|
847
|
+
responseType: method === "HEAD" ? "none" : "text",
|
|
848
|
+
expectedStatuses: successStatuses2,
|
|
849
|
+
statusMapper: ({ status, method: method2, path: path2, bodyText, expectedStatuses }) => new UnexpectedStatusError({ status, method: method2, path: path2, body: bodyText, expected: expectedStatuses })
|
|
850
|
+
}
|
|
851
|
+
);
|
|
852
|
+
} catch (error) {
|
|
853
|
+
throw new AdGuardSyncUnreachableError(errorMessage2(error));
|
|
854
|
+
}
|
|
855
|
+
})();
|
|
856
|
+
const effect = withRetry(
|
|
857
|
+
httpEffect,
|
|
858
|
+
exponentialRetry({
|
|
859
|
+
maxAttempts: 2,
|
|
860
|
+
initialDelayMs: this.retryDelayMs,
|
|
861
|
+
maxDelayMs: this.retryDelayMs,
|
|
862
|
+
jitter: false,
|
|
863
|
+
shouldRetry: (error) => error._tag === "TransportError" || error._tag === "TimeoutError" || error._tag === "UnexpectedStatusError" && error.status >= 500
|
|
864
|
+
})
|
|
865
|
+
).pipe(
|
|
866
|
+
Effect8.map((res) => method === "HEAD" ? void 0 : parseSuccessBody2(res.bodyText)),
|
|
867
|
+
Effect8.mapError((error) => toSyncError(error))
|
|
868
|
+
);
|
|
869
|
+
return Effect8.runPromise(Effect8.either(effect)).then((result) => {
|
|
870
|
+
if (result._tag === "Right") return result.right;
|
|
871
|
+
throw result.left;
|
|
872
|
+
});
|
|
873
|
+
}
|
|
874
|
+
};
|
|
875
|
+
var successStatuses2 = Array.from({ length: 100 }, (_, i) => 200 + i);
|
|
876
|
+
function toSyncError(error) {
|
|
877
|
+
if (error._tag === "UnexpectedStatusError") {
|
|
878
|
+
if (error.status >= 500) return new AdGuardSyncUnreachableError(`HTTP ${error.status}`);
|
|
879
|
+
return new AdGuardSyncClientError(error.status, extractErrorMessage2(error.body));
|
|
880
|
+
}
|
|
881
|
+
if (error._tag === "TransportError") {
|
|
882
|
+
return new AdGuardSyncUnreachableError(errorMessage2(error.cause));
|
|
883
|
+
}
|
|
884
|
+
if (error._tag === "TimeoutError") return new AdGuardSyncUnreachableError("The operation was aborted.");
|
|
885
|
+
if (error._tag === "ParseError") return new AdGuardSyncUnreachableError(error.message);
|
|
886
|
+
return new AdGuardSyncUnreachableError(String(error));
|
|
887
|
+
}
|
|
888
|
+
function extractErrorMessage2(text) {
|
|
889
|
+
let msg = text;
|
|
890
|
+
try {
|
|
891
|
+
msg = JSON.parse(text).message ?? text;
|
|
892
|
+
} catch {
|
|
893
|
+
}
|
|
894
|
+
return redact(msg);
|
|
895
|
+
}
|
|
896
|
+
function errorMessage2(error) {
|
|
897
|
+
return error instanceof Error ? error.message : String(error);
|
|
898
|
+
}
|
|
899
|
+
function identityRedact2(value) {
|
|
186
900
|
return value;
|
|
187
901
|
}
|
|
902
|
+
function baseUrlForConcat2(raw) {
|
|
903
|
+
return new URL(raw.replace(/\/+$/, "") + "/");
|
|
904
|
+
}
|
|
905
|
+
function pathForConcat2(path) {
|
|
906
|
+
return path.replace(/^\/+/, "");
|
|
907
|
+
}
|
|
908
|
+
async function fetchWithPlainHeaders2(input, init) {
|
|
909
|
+
const headers = init?.headers instanceof Headers ? Object.fromEntries(init.headers.entries()) : init?.headers;
|
|
910
|
+
const signal = init?.signal;
|
|
911
|
+
const response = await fetch(input.toString(), { ...init, headers });
|
|
912
|
+
const bodyText = await new Promise((resolve, reject) => {
|
|
913
|
+
const abortError = () => Object.assign(new Error("The operation was aborted"), { name: "AbortError" });
|
|
914
|
+
if (signal?.aborted) {
|
|
915
|
+
void response.body?.cancel().catch(() => {
|
|
916
|
+
});
|
|
917
|
+
reject(abortError());
|
|
918
|
+
return;
|
|
919
|
+
}
|
|
920
|
+
const onAbort = () => {
|
|
921
|
+
void response.body?.cancel().catch(() => {
|
|
922
|
+
});
|
|
923
|
+
reject(abortError());
|
|
924
|
+
};
|
|
925
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
926
|
+
response.text().then(
|
|
927
|
+
(text) => {
|
|
928
|
+
signal?.removeEventListener("abort", onAbort);
|
|
929
|
+
resolve(text);
|
|
930
|
+
},
|
|
931
|
+
(err) => {
|
|
932
|
+
signal?.removeEventListener("abort", onAbort);
|
|
933
|
+
reject(err);
|
|
934
|
+
}
|
|
935
|
+
);
|
|
936
|
+
});
|
|
937
|
+
return new Response(bodyText, {
|
|
938
|
+
status: response.status,
|
|
939
|
+
statusText: response.statusText,
|
|
940
|
+
headers: response.headers
|
|
941
|
+
});
|
|
942
|
+
}
|
|
943
|
+
function parseSuccessBody2(text) {
|
|
944
|
+
if (!text) return void 0;
|
|
945
|
+
try {
|
|
946
|
+
return JSON.parse(text);
|
|
947
|
+
} catch {
|
|
948
|
+
return text;
|
|
949
|
+
}
|
|
950
|
+
}
|
|
188
951
|
|
|
189
952
|
// src/tools/adguard_status.ts
|
|
190
953
|
import { Type as Type2 } from "@sinclair/typebox";
|
|
@@ -195,8 +958,9 @@ var InstanceArg = Type.Optional(Type.String({
|
|
|
195
958
|
description: "Named instance to target (e.g. 'primary', 'secondary'). Default: the configured default instance."
|
|
196
959
|
}));
|
|
197
960
|
function jsonToolResult(payload) {
|
|
961
|
+
const kitResult = ok(payload);
|
|
198
962
|
return {
|
|
199
|
-
content:
|
|
963
|
+
content: kitResult.content
|
|
200
964
|
};
|
|
201
965
|
}
|
|
202
966
|
function hhmmToMs(value) {
|
|
@@ -258,19 +1022,42 @@ var Schema3 = Type4.Object({
|
|
|
258
1022
|
limit: Type4.Optional(Type4.Integer({ minimum: 1, maximum: 500, description: "Max entries to return. Default 50." })),
|
|
259
1023
|
client: Type4.Optional(Type4.String({ description: "Filter by client IP or hostname. AGH's querylog supports a single free-text `search` parameter; client/domain are combined (space-joined) when both passed." })),
|
|
260
1024
|
domain: Type4.Optional(Type4.String({ description: "Filter by domain. AGH's querylog supports a single free-text `search` parameter; client/domain are combined (space-joined) when both passed." })),
|
|
261
|
-
blocked_only: Type4.Optional(Type4.Boolean({ description: "When true, only return blocked queries
|
|
1025
|
+
blocked_only: Type4.Optional(Type4.Boolean({ description: "When true, only return blocked queries using AGH's modern `reason` filter." })),
|
|
1026
|
+
reasons: Type4.Optional(Type4.Array(Type4.Union([
|
|
1027
|
+
Type4.Literal("NotFilteredNotFound"),
|
|
1028
|
+
Type4.Literal("NotFilteredWhiteList"),
|
|
1029
|
+
Type4.Literal("NotFilteredError"),
|
|
1030
|
+
Type4.Literal("FilteredBlackList"),
|
|
1031
|
+
Type4.Literal("FilteredSafeBrowsing"),
|
|
1032
|
+
Type4.Literal("FilteredParental"),
|
|
1033
|
+
Type4.Literal("FilteredInvalid"),
|
|
1034
|
+
Type4.Literal("FilteredSafeSearch"),
|
|
1035
|
+
Type4.Literal("FilteredBlockedService"),
|
|
1036
|
+
Type4.Literal("Rewrite"),
|
|
1037
|
+
Type4.Literal("RewriteEtcHosts"),
|
|
1038
|
+
Type4.Literal("RewriteRule")
|
|
1039
|
+
]), { description: "Filter by one or more AGH FilteringReason values. Takes precedence over blocked_only." }))
|
|
262
1040
|
}, { additionalProperties: false });
|
|
1041
|
+
var BLOCKED_REASONS = [
|
|
1042
|
+
"FilteredBlackList",
|
|
1043
|
+
"FilteredSafeBrowsing",
|
|
1044
|
+
"FilteredParental",
|
|
1045
|
+
"FilteredInvalid",
|
|
1046
|
+
"FilteredSafeSearch",
|
|
1047
|
+
"FilteredBlockedService"
|
|
1048
|
+
];
|
|
263
1049
|
function createAdguardQueryLogTool(getClient) {
|
|
264
1050
|
return {
|
|
265
1051
|
name: "adguard_query_log",
|
|
266
1052
|
label: "adguard: query log",
|
|
267
|
-
description: "Read recent DNS queries via GET /control/querylog. Filter by client, domain, blocked-only. client + domain are combined into AGH's single `search` param when both passed.",
|
|
1053
|
+
description: "Read recent DNS queries via GET /control/querylog. Filter by client, domain, blocked-only, or specific FilteringReason values. client + domain are combined into AGH's single `search` param when both passed.",
|
|
268
1054
|
parameters: Schema3,
|
|
269
1055
|
execute: async (_id, raw) => {
|
|
270
1056
|
const args = raw;
|
|
271
1057
|
const params = new URLSearchParams();
|
|
272
1058
|
params.set("limit", String(args.limit ?? 50));
|
|
273
|
-
|
|
1059
|
+
const reasons = args.reasons && args.reasons.length > 0 ? args.reasons : args.blocked_only ? BLOCKED_REASONS : [];
|
|
1060
|
+
for (const reason of reasons) params.append("reason", reason);
|
|
274
1061
|
const searchTerms = [];
|
|
275
1062
|
if (args.client) searchTerms.push(args.client);
|
|
276
1063
|
if (args.domain) searchTerms.push(args.domain);
|
|
@@ -343,12 +1130,12 @@ function createAdguardListBlockedServicesCatalogTool(getClient) {
|
|
|
343
1130
|
return {
|
|
344
1131
|
name: "adguard_list_blocked_services_catalog",
|
|
345
1132
|
label: "adguard: blocked services catalog",
|
|
346
|
-
description: "List the services AdGuard Home can block per-client (youtube, tiktok, instagram, etc) via GET /control/blocked_services/
|
|
1133
|
+
description: "List the services AdGuard Home can block per-client (youtube, tiktok, instagram, etc) via GET /control/blocked_services/all.",
|
|
347
1134
|
parameters: Schema7,
|
|
348
1135
|
execute: async (_id, raw) => {
|
|
349
1136
|
const args = raw;
|
|
350
1137
|
const client = getClient(args.instance);
|
|
351
|
-
const catalog = await client.get("/control/blocked_services/
|
|
1138
|
+
const catalog = await client.get("/control/blocked_services/all");
|
|
352
1139
|
return jsonToolResult(catalog);
|
|
353
1140
|
}
|
|
354
1141
|
};
|
|
@@ -364,16 +1151,28 @@ var WriteGateError = class extends Error {
|
|
|
364
1151
|
this.name = "WriteGateError";
|
|
365
1152
|
}
|
|
366
1153
|
};
|
|
1154
|
+
function throwGateRefusal(message) {
|
|
1155
|
+
const kitResult = fail(message);
|
|
1156
|
+
let extracted = message;
|
|
1157
|
+
try {
|
|
1158
|
+
const parsed = JSON.parse(kitResult.content[0]?.text ?? "");
|
|
1159
|
+
if (typeof parsed.error === "string") extracted = parsed.error;
|
|
1160
|
+
} catch {
|
|
1161
|
+
}
|
|
1162
|
+
throw new WriteGateError(extracted);
|
|
1163
|
+
}
|
|
367
1164
|
function assertConfirmedWrite(args, toolName) {
|
|
368
1165
|
if (args.confirm !== true) {
|
|
369
|
-
|
|
1166
|
+
void refuseUnconfirmed(toolName);
|
|
1167
|
+
throwGateRefusal(
|
|
370
1168
|
`${toolName} is a write operation. Pass {"confirm": true} to proceed.`
|
|
371
1169
|
);
|
|
372
1170
|
}
|
|
373
1171
|
}
|
|
374
1172
|
function assertDestructive(args, toolName) {
|
|
375
1173
|
if (args.confirm !== true || args.destructive !== true) {
|
|
376
|
-
|
|
1174
|
+
void refuseUnconfirmed(toolName);
|
|
1175
|
+
throwGateRefusal(
|
|
377
1176
|
`${toolName} is a destructive operation. Pass {"confirm": true, "destructive": true} to proceed.`
|
|
378
1177
|
);
|
|
379
1178
|
}
|
|
@@ -993,8 +1792,509 @@ function createAdguardResetStatsTool(getClient) {
|
|
|
993
1792
|
};
|
|
994
1793
|
}
|
|
995
1794
|
|
|
1795
|
+
// src/tools/adguard_list_rewrites.ts
|
|
1796
|
+
import { Type as Type30 } from "@sinclair/typebox";
|
|
1797
|
+
var Schema29 = Type30.Object({ instance: InstanceArg }, { additionalProperties: false });
|
|
1798
|
+
var NAME22 = "adguard_list_rewrites";
|
|
1799
|
+
function createAdguardListRewritesTool(getClient) {
|
|
1800
|
+
return {
|
|
1801
|
+
name: NAME22,
|
|
1802
|
+
label: "adguard: list rewrites",
|
|
1803
|
+
description: "List DNS rewrite rules via GET /control/rewrite/list.",
|
|
1804
|
+
parameters: Schema29,
|
|
1805
|
+
execute: async (_id, raw) => {
|
|
1806
|
+
const args = raw;
|
|
1807
|
+
const client = getClient(args.instance);
|
|
1808
|
+
const rewrites = await client.get("/control/rewrite/list");
|
|
1809
|
+
return jsonToolResult(rewrites);
|
|
1810
|
+
}
|
|
1811
|
+
};
|
|
1812
|
+
}
|
|
1813
|
+
|
|
1814
|
+
// src/tools/adguard_add_rewrite.ts
|
|
1815
|
+
import { Type as Type31 } from "@sinclair/typebox";
|
|
1816
|
+
var Schema30 = Type31.Object({
|
|
1817
|
+
instance: InstanceArg,
|
|
1818
|
+
domain: Type31.String({ description: "Domain name to rewrite, e.g. router.home.arpa." }),
|
|
1819
|
+
answer: Type31.String({ description: "A, AAAA, or CNAME answer value." }),
|
|
1820
|
+
enabled: Type31.Optional(Type31.Boolean({ description: "Whether the rewrite is enabled. Defaults to AGH's default true when omitted." })),
|
|
1821
|
+
confirm: Type31.Boolean({ description: "Must be true. Tier-2 safe-write gate." })
|
|
1822
|
+
}, { additionalProperties: false });
|
|
1823
|
+
var NAME23 = "adguard_add_rewrite";
|
|
1824
|
+
function createAdguardAddRewriteTool(getClient) {
|
|
1825
|
+
return {
|
|
1826
|
+
name: NAME23,
|
|
1827
|
+
label: "adguard: add rewrite",
|
|
1828
|
+
description: "Add a DNS rewrite rule via POST /control/rewrite/add. Tier-2 write; requires confirm:true.",
|
|
1829
|
+
parameters: Schema30,
|
|
1830
|
+
execute: async (_id, raw) => {
|
|
1831
|
+
assertConfirmedWrite(raw, NAME23);
|
|
1832
|
+
const args = raw;
|
|
1833
|
+
const body = { domain: args.domain, answer: args.answer };
|
|
1834
|
+
if (args.enabled !== void 0) body.enabled = args.enabled;
|
|
1835
|
+
const client = getClient(args.instance);
|
|
1836
|
+
await client.post("/control/rewrite/add", body);
|
|
1837
|
+
return jsonToolResult({ added: true, domain: args.domain, answer: args.answer });
|
|
1838
|
+
}
|
|
1839
|
+
};
|
|
1840
|
+
}
|
|
1841
|
+
|
|
1842
|
+
// src/tools/adguard_update_rewrite.ts
|
|
1843
|
+
import { Type as Type32 } from "@sinclair/typebox";
|
|
1844
|
+
var RewriteEntrySchema = Type32.Object({
|
|
1845
|
+
domain: Type32.String({ description: "Domain name." }),
|
|
1846
|
+
answer: Type32.String({ description: "A, AAAA, or CNAME answer value." }),
|
|
1847
|
+
enabled: Type32.Optional(Type32.Boolean({ description: "Whether this rewrite is enabled." }))
|
|
1848
|
+
}, { additionalProperties: false });
|
|
1849
|
+
var Schema31 = Type32.Object({
|
|
1850
|
+
instance: InstanceArg,
|
|
1851
|
+
target: RewriteEntrySchema,
|
|
1852
|
+
update: RewriteEntrySchema,
|
|
1853
|
+
confirm: Type32.Boolean({ description: "Must be true. Tier-2 safe-write gate." })
|
|
1854
|
+
}, { additionalProperties: false });
|
|
1855
|
+
var NAME24 = "adguard_update_rewrite";
|
|
1856
|
+
function createAdguardUpdateRewriteTool(getClient) {
|
|
1857
|
+
return {
|
|
1858
|
+
name: NAME24,
|
|
1859
|
+
label: "adguard: update rewrite",
|
|
1860
|
+
description: "Update a DNS rewrite rule via PUT /control/rewrite/update with {target, update}. Tier-2 write; requires confirm:true.",
|
|
1861
|
+
parameters: Schema31,
|
|
1862
|
+
execute: async (_id, raw) => {
|
|
1863
|
+
assertConfirmedWrite(raw, NAME24);
|
|
1864
|
+
const args = raw;
|
|
1865
|
+
const client = getClient(args.instance);
|
|
1866
|
+
await client.put("/control/rewrite/update", { target: args.target, update: args.update });
|
|
1867
|
+
return jsonToolResult({ updated: true, target: args.target, update: args.update });
|
|
1868
|
+
}
|
|
1869
|
+
};
|
|
1870
|
+
}
|
|
1871
|
+
|
|
1872
|
+
// src/tools/adguard_delete_rewrite.ts
|
|
1873
|
+
import { Type as Type33 } from "@sinclair/typebox";
|
|
1874
|
+
var Schema32 = Type33.Object({
|
|
1875
|
+
instance: InstanceArg,
|
|
1876
|
+
domain: Type33.String({ description: "Domain name of the rewrite to delete." }),
|
|
1877
|
+
answer: Type33.String({ description: "Answer value of the rewrite to delete." }),
|
|
1878
|
+
enabled: Type33.Optional(Type33.Boolean({ description: "Enabled state, when needed to identify the exact rewrite." })),
|
|
1879
|
+
confirm: Type33.Boolean({ description: "Must be true. Tier-2 safe-write gate." })
|
|
1880
|
+
}, { additionalProperties: false });
|
|
1881
|
+
var NAME25 = "adguard_delete_rewrite";
|
|
1882
|
+
function createAdguardDeleteRewriteTool(getClient) {
|
|
1883
|
+
return {
|
|
1884
|
+
name: NAME25,
|
|
1885
|
+
label: "adguard: delete rewrite",
|
|
1886
|
+
description: "Delete a DNS rewrite rule via POST /control/rewrite/delete. Tier-2 write; requires confirm:true.",
|
|
1887
|
+
parameters: Schema32,
|
|
1888
|
+
execute: async (_id, raw) => {
|
|
1889
|
+
assertConfirmedWrite(raw, NAME25);
|
|
1890
|
+
const args = raw;
|
|
1891
|
+
const body = { domain: args.domain, answer: args.answer };
|
|
1892
|
+
if (args.enabled !== void 0) body.enabled = args.enabled;
|
|
1893
|
+
const client = getClient(args.instance);
|
|
1894
|
+
await client.post("/control/rewrite/delete", body);
|
|
1895
|
+
return jsonToolResult({ deleted: true, domain: args.domain, answer: args.answer });
|
|
1896
|
+
}
|
|
1897
|
+
};
|
|
1898
|
+
}
|
|
1899
|
+
|
|
1900
|
+
// src/tools/adguard_get_rewrite_settings.ts
|
|
1901
|
+
import { Type as Type34 } from "@sinclair/typebox";
|
|
1902
|
+
var Schema33 = Type34.Object({ instance: InstanceArg }, { additionalProperties: false });
|
|
1903
|
+
var NAME26 = "adguard_get_rewrite_settings";
|
|
1904
|
+
function createAdguardGetRewriteSettingsTool(getClient) {
|
|
1905
|
+
return {
|
|
1906
|
+
name: NAME26,
|
|
1907
|
+
label: "adguard: get rewrite settings",
|
|
1908
|
+
description: "Get DNS rewrite enabled state via GET /control/rewrite/settings.",
|
|
1909
|
+
parameters: Schema33,
|
|
1910
|
+
execute: async (_id, raw) => {
|
|
1911
|
+
const args = raw;
|
|
1912
|
+
const client = getClient(args.instance);
|
|
1913
|
+
const settings = await client.get("/control/rewrite/settings");
|
|
1914
|
+
return jsonToolResult(settings);
|
|
1915
|
+
}
|
|
1916
|
+
};
|
|
1917
|
+
}
|
|
1918
|
+
|
|
1919
|
+
// src/tools/adguard_toggle_rewrites.ts
|
|
1920
|
+
import { Type as Type35 } from "@sinclair/typebox";
|
|
1921
|
+
var Schema34 = Type35.Object({
|
|
1922
|
+
instance: InstanceArg,
|
|
1923
|
+
enabled: Type35.Boolean({ description: "Target DNS rewrite enabled state." }),
|
|
1924
|
+
confirm: Type35.Boolean({ description: "Must be true. Tier-2 safe-write gate." })
|
|
1925
|
+
}, { additionalProperties: false });
|
|
1926
|
+
var NAME27 = "adguard_toggle_rewrites";
|
|
1927
|
+
function createAdguardToggleRewritesTool(getClient) {
|
|
1928
|
+
return {
|
|
1929
|
+
name: NAME27,
|
|
1930
|
+
label: "adguard: toggle rewrites",
|
|
1931
|
+
description: "Enable or disable DNS rewrites via PUT /control/rewrite/settings/update. Tier-2 write; requires confirm:true.",
|
|
1932
|
+
parameters: Schema34,
|
|
1933
|
+
execute: async (_id, raw) => {
|
|
1934
|
+
assertConfirmedWrite(raw, NAME27);
|
|
1935
|
+
const args = raw;
|
|
1936
|
+
const client = getClient(args.instance);
|
|
1937
|
+
await client.put("/control/rewrite/settings/update", { enabled: args.enabled });
|
|
1938
|
+
return jsonToolResult({ updated: true, enabled: args.enabled });
|
|
1939
|
+
}
|
|
1940
|
+
};
|
|
1941
|
+
}
|
|
1942
|
+
|
|
1943
|
+
// src/tools/adguard_get_access_list.ts
|
|
1944
|
+
import { Type as Type36 } from "@sinclair/typebox";
|
|
1945
|
+
var Schema35 = Type36.Object({ instance: InstanceArg }, { additionalProperties: false });
|
|
1946
|
+
var NAME28 = "adguard_get_access_list";
|
|
1947
|
+
function createAdguardGetAccessListTool(getClient) {
|
|
1948
|
+
return {
|
|
1949
|
+
name: NAME28,
|
|
1950
|
+
label: "adguard: get access list",
|
|
1951
|
+
description: "Get allowed clients, disallowed clients, and blocked hosts via GET /control/access/list.",
|
|
1952
|
+
parameters: Schema35,
|
|
1953
|
+
execute: async (_id, raw) => {
|
|
1954
|
+
const args = raw;
|
|
1955
|
+
const client = getClient(args.instance);
|
|
1956
|
+
const list = await client.get("/control/access/list");
|
|
1957
|
+
return jsonToolResult(list);
|
|
1958
|
+
}
|
|
1959
|
+
};
|
|
1960
|
+
}
|
|
1961
|
+
|
|
1962
|
+
// src/tools/adguard_set_access_list.ts
|
|
1963
|
+
import { Type as Type37 } from "@sinclair/typebox";
|
|
1964
|
+
var Schema36 = Type37.Object({
|
|
1965
|
+
instance: InstanceArg,
|
|
1966
|
+
allowed_clients: Type37.Array(Type37.String(), { description: "Allowlist of clients: IP addresses, CIDRs, or ClientIDs. Empty array disables the allowlist." }),
|
|
1967
|
+
disallowed_clients: Type37.Array(Type37.String(), { description: "Blocklist of clients: IP addresses, CIDRs, or ClientIDs." }),
|
|
1968
|
+
blocked_hosts: Type37.Array(Type37.String(), { description: "Blocklist of hostnames." }),
|
|
1969
|
+
confirm: Type37.Boolean({ description: "Must be true. Tier-2 safe-write gate." })
|
|
1970
|
+
}, { additionalProperties: false });
|
|
1971
|
+
var NAME29 = "adguard_set_access_list";
|
|
1972
|
+
function createAdguardSetAccessListTool(getClient) {
|
|
1973
|
+
return {
|
|
1974
|
+
name: NAME29,
|
|
1975
|
+
label: "adguard: set access list",
|
|
1976
|
+
description: "Replace allowed clients, disallowed clients, and blocked hosts via POST /control/access/set. Tier-2 write; requires confirm:true.",
|
|
1977
|
+
parameters: Schema36,
|
|
1978
|
+
execute: async (_id, raw) => {
|
|
1979
|
+
assertConfirmedWrite(raw, NAME29);
|
|
1980
|
+
const args = raw;
|
|
1981
|
+
const body = {
|
|
1982
|
+
allowed_clients: args.allowed_clients,
|
|
1983
|
+
disallowed_clients: args.disallowed_clients,
|
|
1984
|
+
blocked_hosts: args.blocked_hosts
|
|
1985
|
+
};
|
|
1986
|
+
const client = getClient(args.instance);
|
|
1987
|
+
await client.post("/control/access/set", body);
|
|
1988
|
+
return jsonToolResult({ updated: true, ...body });
|
|
1989
|
+
}
|
|
1990
|
+
};
|
|
1991
|
+
}
|
|
1992
|
+
|
|
1993
|
+
// src/tools/adguard_get_querylog_config.ts
|
|
1994
|
+
import { Type as Type38 } from "@sinclair/typebox";
|
|
1995
|
+
var Schema37 = Type38.Object({ instance: InstanceArg }, { additionalProperties: false });
|
|
1996
|
+
var NAME30 = "adguard_get_querylog_config";
|
|
1997
|
+
function createAdguardGetQuerylogConfigTool(getClient) {
|
|
1998
|
+
return {
|
|
1999
|
+
name: NAME30,
|
|
2000
|
+
label: "adguard: get query log config",
|
|
2001
|
+
description: "Get query log settings via GET /control/querylog/config.",
|
|
2002
|
+
parameters: Schema37,
|
|
2003
|
+
execute: async (_id, raw) => {
|
|
2004
|
+
const args = raw;
|
|
2005
|
+
const client = getClient(args.instance);
|
|
2006
|
+
const config = await client.get("/control/querylog/config");
|
|
2007
|
+
return jsonToolResult(config);
|
|
2008
|
+
}
|
|
2009
|
+
};
|
|
2010
|
+
}
|
|
2011
|
+
|
|
2012
|
+
// src/tools/adguard_update_querylog_config.ts
|
|
2013
|
+
import { Type as Type39 } from "@sinclair/typebox";
|
|
2014
|
+
var Schema38 = Type39.Object({
|
|
2015
|
+
instance: InstanceArg,
|
|
2016
|
+
config: Type39.Object({}, { additionalProperties: true, description: "Query log config body for PUT /control/querylog/config/update." }),
|
|
2017
|
+
confirm: Type39.Boolean({ description: "Must be true. Tier-2 safe-write gate." })
|
|
2018
|
+
}, { additionalProperties: false });
|
|
2019
|
+
var NAME31 = "adguard_update_querylog_config";
|
|
2020
|
+
function createAdguardUpdateQuerylogConfigTool(getClient) {
|
|
2021
|
+
return {
|
|
2022
|
+
name: NAME31,
|
|
2023
|
+
label: "adguard: update query log config",
|
|
2024
|
+
description: "Update query log settings via PUT /control/querylog/config/update. Pass the AGH config fields inside `config`. Tier-2 write; requires confirm:true.",
|
|
2025
|
+
parameters: Schema38,
|
|
2026
|
+
execute: async (_id, raw) => {
|
|
2027
|
+
assertConfirmedWrite(raw, NAME31);
|
|
2028
|
+
const args = raw;
|
|
2029
|
+
const client = getClient(args.instance);
|
|
2030
|
+
await client.put("/control/querylog/config/update", args.config);
|
|
2031
|
+
return jsonToolResult({ updated: true, config: args.config });
|
|
2032
|
+
}
|
|
2033
|
+
};
|
|
2034
|
+
}
|
|
2035
|
+
|
|
2036
|
+
// src/tools/adguard_get_stats_config.ts
|
|
2037
|
+
import { Type as Type40 } from "@sinclair/typebox";
|
|
2038
|
+
var Schema39 = Type40.Object({ instance: InstanceArg }, { additionalProperties: false });
|
|
2039
|
+
var NAME32 = "adguard_get_stats_config";
|
|
2040
|
+
function createAdguardGetStatsConfigTool(getClient) {
|
|
2041
|
+
return {
|
|
2042
|
+
name: NAME32,
|
|
2043
|
+
label: "adguard: get stats config",
|
|
2044
|
+
description: "Get statistics settings via GET /control/stats/config.",
|
|
2045
|
+
parameters: Schema39,
|
|
2046
|
+
execute: async (_id, raw) => {
|
|
2047
|
+
const args = raw;
|
|
2048
|
+
const client = getClient(args.instance);
|
|
2049
|
+
const config = await client.get("/control/stats/config");
|
|
2050
|
+
return jsonToolResult(config);
|
|
2051
|
+
}
|
|
2052
|
+
};
|
|
2053
|
+
}
|
|
2054
|
+
|
|
2055
|
+
// src/tools/adguard_update_stats_config.ts
|
|
2056
|
+
import { Type as Type41 } from "@sinclair/typebox";
|
|
2057
|
+
var Schema40 = Type41.Object({
|
|
2058
|
+
instance: InstanceArg,
|
|
2059
|
+
config: Type41.Object({}, { additionalProperties: true, description: "Stats config body for PUT /control/stats/config/update." }),
|
|
2060
|
+
confirm: Type41.Boolean({ description: "Must be true. Tier-2 safe-write gate." })
|
|
2061
|
+
}, { additionalProperties: false });
|
|
2062
|
+
var NAME33 = "adguard_update_stats_config";
|
|
2063
|
+
function createAdguardUpdateStatsConfigTool(getClient) {
|
|
2064
|
+
return {
|
|
2065
|
+
name: NAME33,
|
|
2066
|
+
label: "adguard: update stats config",
|
|
2067
|
+
description: "Update statistics settings via PUT /control/stats/config/update. Pass the AGH config fields inside `config`. Tier-2 write; requires confirm:true.",
|
|
2068
|
+
parameters: Schema40,
|
|
2069
|
+
execute: async (_id, raw) => {
|
|
2070
|
+
assertConfirmedWrite(raw, NAME33);
|
|
2071
|
+
const args = raw;
|
|
2072
|
+
const client = getClient(args.instance);
|
|
2073
|
+
await client.put("/control/stats/config/update", args.config);
|
|
2074
|
+
return jsonToolResult({ updated: true, config: args.config });
|
|
2075
|
+
}
|
|
2076
|
+
};
|
|
2077
|
+
}
|
|
2078
|
+
|
|
2079
|
+
// src/tools/adguard_dhcp_status.ts
|
|
2080
|
+
import { Type as Type42 } from "@sinclair/typebox";
|
|
2081
|
+
var Schema41 = Type42.Object({ instance: InstanceArg }, { additionalProperties: false });
|
|
2082
|
+
var NAME34 = "adguard_dhcp_status";
|
|
2083
|
+
function createAdguardDhcpStatusTool(getClient) {
|
|
2084
|
+
return {
|
|
2085
|
+
name: NAME34,
|
|
2086
|
+
label: "adguard: dhcp status",
|
|
2087
|
+
description: "Get DHCP server settings, leases, and status via GET /control/dhcp/status.",
|
|
2088
|
+
parameters: Schema41,
|
|
2089
|
+
execute: async (_id, raw) => {
|
|
2090
|
+
const args = raw;
|
|
2091
|
+
const client = getClient(args.instance);
|
|
2092
|
+
const status = await client.get("/control/dhcp/status");
|
|
2093
|
+
return jsonToolResult(status);
|
|
2094
|
+
}
|
|
2095
|
+
};
|
|
2096
|
+
}
|
|
2097
|
+
|
|
2098
|
+
// src/tools/adguard_dhcp_interfaces.ts
|
|
2099
|
+
import { Type as Type43 } from "@sinclair/typebox";
|
|
2100
|
+
var Schema42 = Type43.Object({ instance: InstanceArg }, { additionalProperties: false });
|
|
2101
|
+
var NAME35 = "adguard_dhcp_interfaces";
|
|
2102
|
+
function createAdguardDhcpInterfacesTool(getClient) {
|
|
2103
|
+
return {
|
|
2104
|
+
name: NAME35,
|
|
2105
|
+
label: "adguard: dhcp interfaces",
|
|
2106
|
+
description: "List network interfaces available to the DHCP server via GET /control/dhcp/interfaces.",
|
|
2107
|
+
parameters: Schema42,
|
|
2108
|
+
execute: async (_id, raw) => {
|
|
2109
|
+
const args = raw;
|
|
2110
|
+
const client = getClient(args.instance);
|
|
2111
|
+
const interfaces = await client.get("/control/dhcp/interfaces");
|
|
2112
|
+
return jsonToolResult(interfaces);
|
|
2113
|
+
}
|
|
2114
|
+
};
|
|
2115
|
+
}
|
|
2116
|
+
|
|
2117
|
+
// src/tools/adguard_tls_status.ts
|
|
2118
|
+
import { Type as Type44 } from "@sinclair/typebox";
|
|
2119
|
+
var Schema43 = Type44.Object({ instance: InstanceArg }, { additionalProperties: false });
|
|
2120
|
+
var NAME36 = "adguard_tls_status";
|
|
2121
|
+
function createAdguardTlsStatusTool(getClient) {
|
|
2122
|
+
return {
|
|
2123
|
+
name: NAME36,
|
|
2124
|
+
label: "adguard: tls status",
|
|
2125
|
+
description: "Get TLS configuration and validation status via GET /control/tls/status. Private key values are redacted from tool output.",
|
|
2126
|
+
parameters: Schema43,
|
|
2127
|
+
execute: async (_id, raw) => {
|
|
2128
|
+
const args = raw;
|
|
2129
|
+
const client = getClient(args.instance);
|
|
2130
|
+
const status = await client.get("/control/tls/status");
|
|
2131
|
+
return jsonToolResult(redactTlsSecrets(status));
|
|
2132
|
+
}
|
|
2133
|
+
};
|
|
2134
|
+
}
|
|
2135
|
+
function redactTlsSecrets(value) {
|
|
2136
|
+
if (Array.isArray(value)) return value.map(redactTlsSecrets);
|
|
2137
|
+
if (!value || typeof value !== "object") return value;
|
|
2138
|
+
const out = {};
|
|
2139
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
2140
|
+
out[key] = key === "private_key" ? "[REDACTED]" : redactTlsSecrets(entry);
|
|
2141
|
+
}
|
|
2142
|
+
return out;
|
|
2143
|
+
}
|
|
2144
|
+
|
|
2145
|
+
// src/tools/adguard_validate_tls_config.ts
|
|
2146
|
+
import { Type as Type45 } from "@sinclair/typebox";
|
|
2147
|
+
var Schema44 = Type45.Object({
|
|
2148
|
+
instance: InstanceArg,
|
|
2149
|
+
config: Type45.Object({}, { additionalProperties: true, description: "TLS config body for POST /control/tls/validate. May include certificate_chain/private_key or file paths." }),
|
|
2150
|
+
confirm: Type45.Boolean({ description: "Must be true. Tier-2 gate because TLS validation may include sensitive key material." })
|
|
2151
|
+
}, { additionalProperties: false });
|
|
2152
|
+
var NAME37 = "adguard_validate_tls_config";
|
|
2153
|
+
function createAdguardValidateTlsConfigTool(getClient) {
|
|
2154
|
+
return {
|
|
2155
|
+
name: NAME37,
|
|
2156
|
+
label: "adguard: validate tls config",
|
|
2157
|
+
description: "Validate a TLS config via POST /control/tls/validate without applying it. Tier-2 gated because the request can carry certificate/key material; private key values are redacted from tool output.",
|
|
2158
|
+
parameters: Schema44,
|
|
2159
|
+
execute: async (_id, raw) => {
|
|
2160
|
+
assertConfirmedWrite(raw, NAME37);
|
|
2161
|
+
const args = raw;
|
|
2162
|
+
const client = getClient(args.instance);
|
|
2163
|
+
const result = await client.post("/control/tls/validate", args.config);
|
|
2164
|
+
return jsonToolResult(redactTlsSecrets(result));
|
|
2165
|
+
}
|
|
2166
|
+
};
|
|
2167
|
+
}
|
|
2168
|
+
|
|
2169
|
+
// src/tools/adguard_test_upstream_dns.ts
|
|
2170
|
+
import { Type as Type46 } from "@sinclair/typebox";
|
|
2171
|
+
var Schema45 = Type46.Object({
|
|
2172
|
+
instance: InstanceArg,
|
|
2173
|
+
bootstrap_dns: Type46.Array(Type46.String(), { description: "Bootstrap DNS servers, port optional." }),
|
|
2174
|
+
upstream_dns: Type46.Array(Type46.String(), { description: "Upstream DNS servers to test, port optional." }),
|
|
2175
|
+
fallback_dns: Type46.Optional(Type46.Array(Type46.String(), { description: "Fallback DNS servers to test, port optional." })),
|
|
2176
|
+
private_upstream: Type46.Optional(Type46.Array(Type46.String(), { description: "Local PTR resolvers to test, port optional." })),
|
|
2177
|
+
confirm: Type46.Boolean({ description: "Must be true. Tier-2 gate because the AGH server will probe the provided upstreams." })
|
|
2178
|
+
}, { additionalProperties: false });
|
|
2179
|
+
var NAME38 = "adguard_test_upstream_dns";
|
|
2180
|
+
function createAdguardTestUpstreamDnsTool(getClient) {
|
|
2181
|
+
return {
|
|
2182
|
+
name: NAME38,
|
|
2183
|
+
label: "adguard: test upstream dns",
|
|
2184
|
+
description: "Test upstream DNS server configuration via POST /control/test_upstream_dns. Tier-2 gated because it causes the AGH server to probe the provided upstreams.",
|
|
2185
|
+
parameters: Schema45,
|
|
2186
|
+
execute: async (_id, raw) => {
|
|
2187
|
+
assertConfirmedWrite(raw, NAME38);
|
|
2188
|
+
const args = raw;
|
|
2189
|
+
const body = {
|
|
2190
|
+
bootstrap_dns: args.bootstrap_dns,
|
|
2191
|
+
upstream_dns: args.upstream_dns
|
|
2192
|
+
};
|
|
2193
|
+
if (args.fallback_dns !== void 0) body.fallback_dns = args.fallback_dns;
|
|
2194
|
+
if (args.private_upstream !== void 0) body.private_upstream = args.private_upstream;
|
|
2195
|
+
const client = getClient(args.instance);
|
|
2196
|
+
const result = await client.post("/control/test_upstream_dns", body);
|
|
2197
|
+
return jsonToolResult(result);
|
|
2198
|
+
}
|
|
2199
|
+
};
|
|
2200
|
+
}
|
|
2201
|
+
|
|
2202
|
+
// src/tools/adguard_sync_status.ts
|
|
2203
|
+
import { Type as Type47 } from "@sinclair/typebox";
|
|
2204
|
+
var Schema46 = Type47.Object({}, { additionalProperties: false });
|
|
2205
|
+
function createAdguardSyncStatusTool(getSyncClient) {
|
|
2206
|
+
return {
|
|
2207
|
+
name: "adguard_sync_status",
|
|
2208
|
+
label: "adguard sync: status",
|
|
2209
|
+
description: "Get AdGuardHome Sync origin/replica status via GET /api/v1/status.",
|
|
2210
|
+
parameters: Schema46,
|
|
2211
|
+
execute: async (_id, _raw) => {
|
|
2212
|
+
const client = getSyncClient();
|
|
2213
|
+
const status = await client.get("/api/v1/status");
|
|
2214
|
+
return jsonToolResult(status);
|
|
2215
|
+
}
|
|
2216
|
+
};
|
|
2217
|
+
}
|
|
2218
|
+
|
|
2219
|
+
// src/tools/adguard_sync_health.ts
|
|
2220
|
+
import { Type as Type48 } from "@sinclair/typebox";
|
|
2221
|
+
var Schema47 = Type48.Object({}, { additionalProperties: false });
|
|
2222
|
+
function createAdguardSyncHealthTool(getSyncClient) {
|
|
2223
|
+
return {
|
|
2224
|
+
name: "adguard_sync_health",
|
|
2225
|
+
label: "adguard sync: health",
|
|
2226
|
+
description: "Check AdGuardHome Sync health via HEAD /healthz. The endpoint is unauthenticated upstream and returns 2xx only when origin and replicas are healthy.",
|
|
2227
|
+
parameters: Schema47,
|
|
2228
|
+
execute: async (_id, _raw) => {
|
|
2229
|
+
const client = getSyncClient();
|
|
2230
|
+
const health = await client.head("/healthz");
|
|
2231
|
+
return jsonToolResult(health);
|
|
2232
|
+
}
|
|
2233
|
+
};
|
|
2234
|
+
}
|
|
2235
|
+
|
|
2236
|
+
// src/tools/adguard_sync_logs.ts
|
|
2237
|
+
import { Type as Type49 } from "@sinclair/typebox";
|
|
2238
|
+
var Schema48 = Type49.Object({}, { additionalProperties: false });
|
|
2239
|
+
function createAdguardSyncLogsTool(getSyncClient) {
|
|
2240
|
+
return {
|
|
2241
|
+
name: "adguard_sync_logs",
|
|
2242
|
+
label: "adguard sync: logs",
|
|
2243
|
+
description: "Read AdGuardHome Sync in-memory logs via GET /api/v1/logs.",
|
|
2244
|
+
parameters: Schema48,
|
|
2245
|
+
execute: async (_id, _raw) => {
|
|
2246
|
+
const client = getSyncClient();
|
|
2247
|
+
const logs = await client.get("/api/v1/logs");
|
|
2248
|
+
return jsonToolResult({ logs });
|
|
2249
|
+
}
|
|
2250
|
+
};
|
|
2251
|
+
}
|
|
2252
|
+
|
|
2253
|
+
// src/tools/adguard_sync_run.ts
|
|
2254
|
+
import { Type as Type50 } from "@sinclair/typebox";
|
|
2255
|
+
var Schema49 = Type50.Object({
|
|
2256
|
+
confirm: Type50.Boolean({ description: "Must be true. Tier-2 safe-write gate. Starts a sync from origin to replicas." })
|
|
2257
|
+
}, { additionalProperties: false });
|
|
2258
|
+
var NAME39 = "adguard_sync_run";
|
|
2259
|
+
function createAdguardSyncRunTool(getSyncClient) {
|
|
2260
|
+
return {
|
|
2261
|
+
name: NAME39,
|
|
2262
|
+
label: "adguard sync: run",
|
|
2263
|
+
description: "Trigger AdGuardHome Sync immediately via POST /api/v1/sync. Tier-2 write; requires confirm:true.",
|
|
2264
|
+
parameters: Schema49,
|
|
2265
|
+
execute: async (_id, raw) => {
|
|
2266
|
+
assertConfirmedWrite(raw, NAME39);
|
|
2267
|
+
const client = getSyncClient();
|
|
2268
|
+
await client.post("/api/v1/sync");
|
|
2269
|
+
return jsonToolResult({ started: true });
|
|
2270
|
+
}
|
|
2271
|
+
};
|
|
2272
|
+
}
|
|
2273
|
+
|
|
2274
|
+
// src/tools/adguard_sync_clear_logs.ts
|
|
2275
|
+
import { Type as Type51 } from "@sinclair/typebox";
|
|
2276
|
+
var Schema50 = Type51.Object({
|
|
2277
|
+
confirm: Type51.Boolean({ description: "Must be true. Tier-3 destructive gate." }),
|
|
2278
|
+
destructive: Type51.Boolean({ description: "Must be true. Tier-3 destructive gate. Clears AdGuardHome Sync logs." })
|
|
2279
|
+
}, { additionalProperties: false });
|
|
2280
|
+
var NAME40 = "adguard_sync_clear_logs";
|
|
2281
|
+
function createAdguardSyncClearLogsTool(getSyncClient) {
|
|
2282
|
+
return {
|
|
2283
|
+
name: NAME40,
|
|
2284
|
+
label: "adguard sync: clear logs",
|
|
2285
|
+
description: "Clear AdGuardHome Sync in-memory logs via POST /api/v1/clear-logs. Tier-3 destructive; requires confirm:true and destructive:true.",
|
|
2286
|
+
parameters: Schema50,
|
|
2287
|
+
execute: async (_id, raw) => {
|
|
2288
|
+
assertDestructive(raw, NAME40);
|
|
2289
|
+
const client = getSyncClient();
|
|
2290
|
+
await client.post("/api/v1/clear-logs");
|
|
2291
|
+
return jsonToolResult({ cleared: true });
|
|
2292
|
+
}
|
|
2293
|
+
};
|
|
2294
|
+
}
|
|
2295
|
+
|
|
996
2296
|
// src/tools/index.ts
|
|
997
|
-
function buildAllTools(getClient) {
|
|
2297
|
+
function buildAllTools(getClient, getSyncClient) {
|
|
998
2298
|
return [
|
|
999
2299
|
// Tier 1: reads
|
|
1000
2300
|
createAdguardStatusTool(getClient),
|
|
@@ -1008,6 +2308,17 @@ function buildAllTools(getClient) {
|
|
|
1008
2308
|
createAdguardGetBlockedServicesTool(getClient),
|
|
1009
2309
|
createAdguardGetDnsConfigTool(getClient),
|
|
1010
2310
|
createAdguardGetSafesearchSettingsTool(getClient),
|
|
2311
|
+
createAdguardListRewritesTool(getClient),
|
|
2312
|
+
createAdguardGetRewriteSettingsTool(getClient),
|
|
2313
|
+
createAdguardGetAccessListTool(getClient),
|
|
2314
|
+
createAdguardGetQuerylogConfigTool(getClient),
|
|
2315
|
+
createAdguardGetStatsConfigTool(getClient),
|
|
2316
|
+
createAdguardDhcpStatusTool(getClient),
|
|
2317
|
+
createAdguardDhcpInterfacesTool(getClient),
|
|
2318
|
+
createAdguardTlsStatusTool(getClient),
|
|
2319
|
+
createAdguardSyncStatusTool(getSyncClient),
|
|
2320
|
+
createAdguardSyncHealthTool(getSyncClient),
|
|
2321
|
+
createAdguardSyncLogsTool(getSyncClient),
|
|
1011
2322
|
// Tier 2: safe writes (confirm: true)
|
|
1012
2323
|
createAdguardAddUserRuleTool(getClient),
|
|
1013
2324
|
createAdguardRemoveUserRuleTool(getClient),
|
|
@@ -1021,16 +2332,34 @@ function buildAllTools(getClient) {
|
|
|
1021
2332
|
createAdguardSetBlockedServicesTool(getClient),
|
|
1022
2333
|
createAdguardToggleSafesearchTool(getClient),
|
|
1023
2334
|
createAdguardToggleSafebrowsingTool(getClient),
|
|
2335
|
+
createAdguardAddRewriteTool(getClient),
|
|
2336
|
+
createAdguardUpdateRewriteTool(getClient),
|
|
2337
|
+
createAdguardDeleteRewriteTool(getClient),
|
|
2338
|
+
createAdguardToggleRewritesTool(getClient),
|
|
2339
|
+
createAdguardSetAccessListTool(getClient),
|
|
2340
|
+
createAdguardUpdateQuerylogConfigTool(getClient),
|
|
2341
|
+
createAdguardUpdateStatsConfigTool(getClient),
|
|
2342
|
+
createAdguardValidateTlsConfigTool(getClient),
|
|
2343
|
+
createAdguardTestUpstreamDnsTool(getClient),
|
|
2344
|
+
createAdguardSyncRunTool(getSyncClient),
|
|
1024
2345
|
// Tier 3: destructive (confirm: true + destructive: true)
|
|
1025
2346
|
createAdguardReplaceUserRulesTool(getClient),
|
|
1026
2347
|
createAdguardToggleProtectionTool(getClient),
|
|
1027
2348
|
createAdguardDeleteClientTool(getClient),
|
|
1028
2349
|
createAdguardClearQueryLogTool(getClient),
|
|
1029
|
-
createAdguardResetStatsTool(getClient)
|
|
2350
|
+
createAdguardResetStatsTool(getClient),
|
|
2351
|
+
createAdguardSyncClearLogsTool(getSyncClient)
|
|
1030
2352
|
];
|
|
1031
2353
|
}
|
|
1032
2354
|
|
|
1033
2355
|
// index.ts
|
|
2356
|
+
function mcpToolError(message) {
|
|
2357
|
+
const failed = fail(message);
|
|
2358
|
+
return {
|
|
2359
|
+
content: [{ type: "text", text: JSON.stringify({ error: message }) }],
|
|
2360
|
+
isError: failed.isError
|
|
2361
|
+
};
|
|
2362
|
+
}
|
|
1034
2363
|
function withRedactedErrors(tool) {
|
|
1035
2364
|
const orig = tool.execute.bind(tool);
|
|
1036
2365
|
return {
|
|
@@ -1039,13 +2368,12 @@ function withRedactedErrors(tool) {
|
|
|
1039
2368
|
try {
|
|
1040
2369
|
return await orig(id, args);
|
|
1041
2370
|
} catch (e) {
|
|
1042
|
-
|
|
1043
|
-
return { content: [{ type: "text", text: JSON.stringify({ error: msg }) }], isError: true };
|
|
2371
|
+
return mcpToolError(redact(operatorErrorMessage(e)));
|
|
1044
2372
|
}
|
|
1045
2373
|
}
|
|
1046
2374
|
};
|
|
1047
2375
|
}
|
|
1048
|
-
function makeFactory(cfg) {
|
|
2376
|
+
function makeFactory(cfg, timeoutMs) {
|
|
1049
2377
|
for (const inst of Object.values(cfg.instances)) {
|
|
1050
2378
|
registerSecret(inst.password);
|
|
1051
2379
|
const basicValue = "Basic " + Buffer2.from(`${inst.username}:${inst.password}`).toString("base64");
|
|
@@ -1053,9 +2381,21 @@ function makeFactory(cfg) {
|
|
|
1053
2381
|
}
|
|
1054
2382
|
return (name) => {
|
|
1055
2383
|
const ic = getInstanceConfig(cfg, name);
|
|
1056
|
-
return new AdGuardClient(ic);
|
|
2384
|
+
return new AdGuardClient(ic, { timeoutMs });
|
|
1057
2385
|
};
|
|
1058
2386
|
}
|
|
2387
|
+
function registerSyncSecrets(syncCfg) {
|
|
2388
|
+
if (!syncCfg?.password) return;
|
|
2389
|
+
registerSecret(syncCfg.password);
|
|
2390
|
+
if (syncCfg.username) {
|
|
2391
|
+
const basicValue = "Basic " + Buffer2.from(`${syncCfg.username}:${syncCfg.password}`).toString("base64");
|
|
2392
|
+
registerSecret(basicValue);
|
|
2393
|
+
}
|
|
2394
|
+
}
|
|
2395
|
+
function makeSyncFactory(syncCfg, timeoutMs) {
|
|
2396
|
+
registerSyncSecrets(syncCfg);
|
|
2397
|
+
return () => new AdGuardSyncClient(getSyncConfig(syncCfg), { timeoutMs });
|
|
2398
|
+
}
|
|
1059
2399
|
var index_default = definePluginEntry({
|
|
1060
2400
|
id: "adguard",
|
|
1061
2401
|
name: "AdGuard",
|
|
@@ -1063,9 +2403,12 @@ var index_default = definePluginEntry({
|
|
|
1063
2403
|
register(api) {
|
|
1064
2404
|
if (api.registrationMode !== "full") return;
|
|
1065
2405
|
const cfg = resolveInstances(process.env);
|
|
1066
|
-
const
|
|
2406
|
+
const syncCfg = resolveSyncConfig(process.env);
|
|
2407
|
+
const timeoutMs = resolveRequestTimeoutMs(process.env);
|
|
2408
|
+
const getClient = makeFactory(cfg, timeoutMs);
|
|
2409
|
+
const getSyncClient = makeSyncFactory(syncCfg, timeoutMs);
|
|
1067
2410
|
const register = (t) => api.registerTool(withRedactedErrors(t));
|
|
1068
|
-
for (const t of buildAllTools(getClient)) register(t);
|
|
2411
|
+
for (const t of buildAllTools(getClient, getSyncClient)) register(t);
|
|
1069
2412
|
}
|
|
1070
2413
|
});
|
|
1071
2414
|
export {
|