mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-20 10:00:58 +00:00
abd320994a
* Add enable toggle for external client links * Document external link enable API fields * Extend external client link metadata * Fix external subscription cache status updates * fix(sub): address the review on per-client external link controls Blocking: the expiry filter dropped legacy rows. expiry_time was added without a default, so AutoMigrate makes it nullable and backfills NULL, and `expiry_time = 0 OR expiry_time > ?` is false for NULL under three-valued logic — every external link written before the upgrade vanished from all subscriptions. Add `default:0` on expiry_time and last_fetch_at, make the predicate NULL-tolerant, and backfill the NULLs a pre-fix build could already have written. Rework fetch-status recording. It ran inside the singleflight in-flight window, so every goroutine parked on the shared fetch waited for a DB write to commit on the public, unauthenticated subscription path — and because it was keyed on the row id, waiters and cache hits recorded nothing, leaving rows that lost the race stuck on "Not fetched yet" forever. fetchSubscriptionLinks now reports whether it did the network fetch and expandEntry records afterwards, off the serving path, keyed on kind+value so every row sharing the URL is stamped by the one fetch. Keying on value also closes the recycled-rowid hazard: saves delete and re-insert rows, and SQLite reuses rowids, so an in-flight write could land on an unrelated client's row. The write no longer discards its error either. Drop the inert id round-trip. The panel never sent it, and the byId branch was guarded by the exact kind+value equality that byKindValue already keys on, so it could not change an outcome. Matching on kind+value alone is what actually preserves fetch status across saves. Reject a negative expiryTime instead of storing a row that is silently invisible in every subscription — elsewhere a negative expiryTime means "a duration from first use", so an API caller reusing that convention got no error and no links. Drop the ~50 lines of .client-form-* / .client-inbounds-field CSS that no component renders; it is leftover from the WireGuard PR this one was split from. i18n: reuse the already-translated pages.inbounds.leaveBlankToNeverExpire instead of shipping an English duplicate under pages.clients, and translate namePrefix, lastFetchAt, lastFetchError and neverFetched into all 12 non-English locales. Cover the persistence path that had no test: the fetch-status writer over a real DB against a failing then a succeeding server, a cache hit writing nothing, and the negative-expiry rejection. --------- Co-authored-by: MHSanaei <ho3ein.sanaei@gmail.com>
247 lines
7.1 KiB
JavaScript
247 lines
7.1 KiB
JavaScript
#!/usr/bin/env node
|
|
import { writeFileSync } from 'node:fs';
|
|
import { join, dirname } from 'node:path';
|
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
|
|
import { sections } from '../src/pages/api-docs/endpoints.ts';
|
|
import { EXAMPLES } from '../src/generated/examples.ts';
|
|
import { SCHEMAS } from '../src/generated/schemas.ts';
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
const outPath = join(__dirname, '..', 'public', 'openapi.json');
|
|
|
|
const PANEL_VERSION = process.env.X_UI_VERSION || '3.x';
|
|
|
|
const SECURITY_SCHEMES = {
|
|
bearerAuth: {
|
|
type: 'http',
|
|
scheme: 'bearer',
|
|
description: 'API token from Settings → Security → API Token. Send as `Authorization: Bearer <token>`.',
|
|
},
|
|
cookieAuth: {
|
|
type: 'apiKey',
|
|
in: 'cookie',
|
|
name: '3x-ui',
|
|
description: 'Session cookie set by POST /login. Browser-only.',
|
|
},
|
|
};
|
|
|
|
function ginPathToOpenApi(path) {
|
|
return path.replace(/:([A-Za-z_][A-Za-z0-9_]*)/g, '{$1}');
|
|
}
|
|
|
|
function extractPathParams(openApiPath) {
|
|
const params = [];
|
|
const re = /\{([A-Za-z_][A-Za-z0-9_]*)\}/g;
|
|
let m;
|
|
while ((m = re.exec(openApiPath)) !== null) params.push(m[1]);
|
|
return params;
|
|
}
|
|
|
|
function mapType(t) {
|
|
const v = String(t || '').toLowerCase();
|
|
if (v.endsWith('[]')) return 'array';
|
|
if (v === 'number' || v === 'integer' || v === 'int') return 'integer';
|
|
if (v === 'float' || v === 'double') return 'number';
|
|
if (v === 'boolean' || v === 'bool') return 'boolean';
|
|
if (v === 'array') return 'array';
|
|
if (v === 'object') return 'object';
|
|
return 'string';
|
|
}
|
|
|
|
function schemaFromType(t) {
|
|
const v = String(t || '').toLowerCase();
|
|
if (v.endsWith('[]')) {
|
|
const itemType = v.slice(0, -2);
|
|
return { type: 'array', items: { type: mapType(itemType) } };
|
|
}
|
|
return { type: mapType(v) };
|
|
}
|
|
|
|
function tryParseJson(raw) {
|
|
if (typeof raw !== 'string') return undefined;
|
|
try {
|
|
return JSON.parse(raw);
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
function paramToOpenApi(p) {
|
|
const out = {
|
|
name: p.name,
|
|
in: p.in,
|
|
required: p.in === 'path' ? true : !p.optional,
|
|
description: p.desc || '',
|
|
schema: schemaFromType(p.type),
|
|
};
|
|
if (p.defaultValue !== undefined) out.schema.default = p.defaultValue;
|
|
return out;
|
|
}
|
|
|
|
function buildOperation(ep, tag) {
|
|
const op = {
|
|
tags: [tag],
|
|
summary: ep.summary || '',
|
|
operationId: `${ep.method.toLowerCase()}_${ep.path.replace(/[^A-Za-z0-9]+/g, '_').replace(/^_|_$/g, '')}`,
|
|
};
|
|
if (ep.description) op.description = ep.description;
|
|
if (ep.deprecated) op.deprecated = true;
|
|
|
|
const params = [];
|
|
const bodyParams = [];
|
|
for (const p of ep.params || []) {
|
|
if (p.in === 'body') {
|
|
bodyParams.push(p);
|
|
} else if (p.in === 'path' || p.in === 'query' || p.in === 'header') {
|
|
params.push(paramToOpenApi(p));
|
|
}
|
|
}
|
|
|
|
const openApiPath = ginPathToOpenApi(ep.path);
|
|
const declared = new Set(params.filter((x) => x.in === 'path').map((x) => x.name));
|
|
for (const name of extractPathParams(openApiPath)) {
|
|
if (declared.has(name)) continue;
|
|
params.push({
|
|
name,
|
|
in: 'path',
|
|
required: true,
|
|
description: '',
|
|
schema: { type: 'string' },
|
|
});
|
|
}
|
|
|
|
if (params.length > 0) op.parameters = params;
|
|
|
|
if (ep.body || bodyParams.length > 0) {
|
|
const example = tryParseJson(ep.body);
|
|
const properties = {};
|
|
const required = [];
|
|
for (const bp of bodyParams) {
|
|
properties[bp.name] = {
|
|
...schemaFromType(bp.type),
|
|
description: bp.desc || '',
|
|
};
|
|
if (!bp.optional) required.push(bp.name);
|
|
}
|
|
const schema = bodyParams.length > 0
|
|
? { type: 'object', properties, ...(required.length > 0 ? { required } : {}) }
|
|
: { type: 'object' };
|
|
|
|
op.requestBody = {
|
|
required: required.length > 0 || bodyParams.length === 0,
|
|
content: {
|
|
'application/json': {
|
|
schema,
|
|
...(example !== undefined ? { example } : {}),
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
const responses = {};
|
|
let successExample = tryParseJson(ep.response);
|
|
let objSchema = {};
|
|
if (ep.responseSchema) {
|
|
const obj = EXAMPLES[ep.responseSchema];
|
|
if (obj === undefined) {
|
|
throw new Error(`${ep.method} ${ep.path}: responseSchema "${ep.responseSchema}" has no generated example`);
|
|
}
|
|
if (SCHEMAS[ep.responseSchema] === undefined) {
|
|
throw new Error(`${ep.method} ${ep.path}: responseSchema "${ep.responseSchema}" has no generated schema`);
|
|
}
|
|
const ref = { $ref: `#/components/schemas/${ep.responseSchema}` };
|
|
objSchema = ep.responseSchemaArray ? { type: 'array', items: ref } : ref;
|
|
if (successExample === undefined) {
|
|
successExample = { success: true, obj: ep.responseSchemaArray ? [obj] : obj };
|
|
}
|
|
}
|
|
responses['200'] = {
|
|
description: 'Successful response',
|
|
content: {
|
|
'application/json': {
|
|
schema: {
|
|
type: 'object',
|
|
properties: {
|
|
success: { type: 'boolean' },
|
|
msg: { type: 'string' },
|
|
obj: objSchema,
|
|
},
|
|
},
|
|
...(successExample !== undefined ? { example: successExample } : {}),
|
|
},
|
|
},
|
|
};
|
|
|
|
const errExample = tryParseJson(ep.errorResponse);
|
|
if (errExample !== undefined || ep.errorStatus) {
|
|
const code = String(ep.errorStatus || 400);
|
|
responses[code] = {
|
|
description: 'Error response',
|
|
content: {
|
|
'application/json': {
|
|
schema: {
|
|
type: 'object',
|
|
properties: {
|
|
success: { type: 'boolean' },
|
|
msg: { type: 'string' },
|
|
},
|
|
},
|
|
...(errExample !== undefined ? { example: errExample } : {}),
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
op.responses = responses;
|
|
return op;
|
|
}
|
|
|
|
function buildSpec() {
|
|
const paths = {};
|
|
for (const section of sections) {
|
|
const tag = section.title;
|
|
for (const ep of section.endpoints) {
|
|
const openApiPath = ginPathToOpenApi(ep.path);
|
|
if (!paths[openApiPath]) paths[openApiPath] = {};
|
|
paths[openApiPath][ep.method.toLowerCase()] = buildOperation(ep, tag);
|
|
}
|
|
}
|
|
|
|
const tags = sections.map((s) => ({
|
|
name: s.title,
|
|
description: s.description || '',
|
|
}));
|
|
|
|
return {
|
|
openapi: '3.0.3',
|
|
info: {
|
|
title: '3X-UI Panel API',
|
|
version: PANEL_VERSION,
|
|
description:
|
|
'Programmatic interface to a 3X-UI panel. Authenticate either by logging in (cookie) or with an API token from Settings → Security → API Token (Bearer). All endpoints under /panel/api/* honour both modes — an API token is a full-admin credential, so treat it like the panel password.',
|
|
},
|
|
servers: [
|
|
{ url: '/', description: 'Current panel (basePath aware)' },
|
|
],
|
|
components: {
|
|
securitySchemes: SECURITY_SCHEMES,
|
|
schemas: SCHEMAS,
|
|
},
|
|
security: [{ bearerAuth: [] }, { cookieAuth: [] }],
|
|
tags,
|
|
paths,
|
|
};
|
|
}
|
|
|
|
const spec = buildSpec();
|
|
writeFileSync(outPath, JSON.stringify(spec, null, 2) + '\n');
|
|
|
|
const pathCount = Object.keys(spec.paths).length;
|
|
let opCount = 0;
|
|
for (const ops of Object.values(spec.paths)) opCount += Object.keys(ops).length;
|
|
console.log(`[openapi] wrote ${outPath}`);
|
|
console.log(`[openapi] paths: ${pathCount}, operations: ${opCount}, tags: ${spec.tags.length}`);
|
|
|
|
void pathToFileURL;
|