feat(wip): 支持 JWT

This commit is contained in:
xream 2024-10-30 22:27:39 +08:00
parent 07e50175f9
commit 542957d34a
No known key found for this signature in database
GPG Key ID: 1D2C5225471789F9
4 changed files with 7433 additions and 7392 deletions

View File

@ -28,10 +28,10 @@
"http-proxy-middleware": "^2.0.6",
"ip-address": "^9.0.5",
"js-base64": "^3.7.2",
"jsonwebtoken": "^9.0.2",
"jsrsasign": "^11.1.0",
"lodash": "^4.17.21",
"request": "^2.88.2",
"requests": "^0.3.0",
"semver": "^7.3.7",
"static-js-yaml": "^1.0.0",
"uuid": "^8.3.2"

14586
backend/pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -143,7 +143,7 @@ export default function serve() {
try {
fs.accessSync(path.join(fe_abs_path, 'index.html'));
} catch (e) {
throw new Error(
$.error(
`[FRONTEND] index.html file not found in ${fe_abs_path}`,
);
}
@ -158,6 +158,7 @@ export default function serve() {
const staticFileMiddleware = express_.static(fe_path);
let be_share_rewrite = '/share/:type/:name';
let be_api_rewrite = '';
let be_download_rewrite = '';
let be_api = '/api/';
@ -174,6 +175,45 @@ export default function serve() {
be_download_rewrite = `${
fe_be_path === '/' ? '' : fe_be_path
}${be_download}`;
const jwt = eval(`require("jsonwebtoken")`);
app.use(
be_share_rewrite,
createProxyMiddleware({
target: `http://127.0.0.1:${port}`,
changeOrigin: true,
pathRewrite: (path, req) => {
if (req.method.toLowerCase() !== 'get')
throw new Error('Method not allowed');
const payload = jwt.verify(
req.query.token,
fe_be_path,
);
if (
payload.type !== req.params.type ||
payload.name !== req.params.name
)
throw new Error('Forbbiden');
if (payload.type === 'sub')
return path.replace(
'/share/sub/',
'/download/',
);
if (payload.type === 'col')
return path.replace(
'/share/col/',
'/download/collection/',
);
if (payload.type === 'file')
return path.replace(
'/share/file/',
'/api/file/',
);
throw new Error('Not Found');
},
}),
);
app.use(
be_api_rewrite,
createProxyMiddleware({
@ -220,6 +260,9 @@ export default function serve() {
$.info(
`[FRONTEND -> BACKEND] ${fe_address}:${fe_port}${be_download_rewrite} -> http://127.0.0.1:${port}${be_download}`,
);
$.info(
`[SHARE BACKEND] ${fe_address}:${fe_port}${be_share_rewrite}`,
);
}
});
}

View File

@ -20,6 +20,26 @@ export default function register($app) {
$app.get('/api/utils/env', getEnv); // get runtime environment
$app.get('/api/utils/backup', gistBackup); // gist backup actions
$app.get('/api/utils/refresh', refresh);
$app.post('/api/jwt', (req, res) => {
if (!ENV().isNode) {
return failed(
res,
new RequestInvalidError(
'INVALID_ENV',
`This endpoint is only available in Node.js environment`,
),
);
}
const { payload, options } = req.body;
const jwt = eval(`require("jsonwebtoken")`);
res.set('Content-Type', 'application/json;charset=utf-8').send({
token: jwt.sign(
payload,
eval('process.env.SUB_STORE_FRONTEND_BACKEND_PATH'),
options,
),
});
});
// Storage management
$app.route('/api/storage')