dsh-mo-image 1.0.3 → 1.0.4
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/CHANGELOG.md +4 -0
- package/README.md +1 -1
- package/lib/config-store.js +2 -4
- package/lib/index.js +42 -8
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -217,7 +217,7 @@ $DSH_HOME/mo-image/out/
|
|
|
217
217
|
|
|
218
218
|
聊天里新生成的图片使用本地文件绝对路径,支持本地图片菜单的 DSH 客户端可以通过宿主读取原图、复制图片并在桌面文件夹中定位。找不到会话目录,或图片超过宿主的本地预览上限时,使用签名预览。
|
|
219
219
|
|
|
220
|
-
|
|
220
|
+
登录 DSH 后,旧聊天中的 `/api/mo-image/preview/<basename>` 和签名预览链接都可用。HMAC 签名 key 保存在 `$DSH_HOME/mo-image/preview.key`,因此签名链接在宿主重启后仍然有效。
|
|
221
221
|
|
|
222
222
|
建议把 `generated-images/` 加进项目的 `.gitignore`,避免把生成图误提交。
|
|
223
223
|
|
package/lib/config-store.js
CHANGED
|
@@ -1033,10 +1033,8 @@ function requestHostName(req) {
|
|
|
1033
1033
|
return withoutPort;
|
|
1034
1034
|
}
|
|
1035
1035
|
/**
|
|
1036
|
-
*
|
|
1037
|
-
*
|
|
1038
|
-
* DSH_WEB_URL 公布的域名;Origin 必须同源;POST 必须是 application/json。
|
|
1039
|
-
* 同一 OS 用户的进程本来就能读凭据文件,这里不是对抗同 UID 进程的边界。
|
|
1036
|
+
* DSH 完成访问校验后,设置相关接口仍要求同源浏览器请求。
|
|
1037
|
+
* Host 支持回环、IP 字面量及 DSH 公布的域名,写请求必须使用 application/json。
|
|
1040
1038
|
*/
|
|
1041
1039
|
export function assertLocalRequest(store, req, write) {
|
|
1042
1040
|
const hostName = requestHostName(req);
|
package/lib/index.js
CHANGED
|
@@ -175,6 +175,40 @@ function sendJson(res, status, value) {
|
|
|
175
175
|
res.writeHead(status, { 'Content-Type': 'application/json; charset=utf-8', 'Cache-Control': 'no-store' });
|
|
176
176
|
res.end(JSON.stringify(value));
|
|
177
177
|
}
|
|
178
|
+
|
|
179
|
+
function routeAccessRejection(ctx, req, peer) {
|
|
180
|
+
// New hosts admit requests before dispatch and pass the authenticated Peer.
|
|
181
|
+
if (peer !== undefined) {
|
|
182
|
+
if (peer?.signal?.aborted) return 401;
|
|
183
|
+
return peer?.principal?.kind === 'owner' ? undefined : 403;
|
|
184
|
+
}
|
|
185
|
+
// On older hosts the handler itself must apply Connection's access check.
|
|
186
|
+
try {
|
|
187
|
+
const connection = ctx.get?.('connection');
|
|
188
|
+
if (typeof connection?.requestRejection === 'function') {
|
|
189
|
+
const rejection = connection.requestRejection(req);
|
|
190
|
+
if (rejection === undefined || rejection === 401 || rejection === 403) return rejection;
|
|
191
|
+
}
|
|
192
|
+
} catch (_error) {
|
|
193
|
+
// A missing or unavailable authentication service cannot grant access.
|
|
194
|
+
}
|
|
195
|
+
return 503;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function registerPrivateRoute(ctx, route) {
|
|
199
|
+
return ctx.webServer.register({
|
|
200
|
+
...route,
|
|
201
|
+
auth: 'required',
|
|
202
|
+
async handler(req, res, peer) {
|
|
203
|
+
const rejection = routeAccessRejection(ctx, req, peer);
|
|
204
|
+
if (rejection === undefined) return route.handler(req, res);
|
|
205
|
+
const error = rejection === 401 ? 'unauthorized'
|
|
206
|
+
: rejection === 403 ? 'forbidden' : 'DSH authentication unavailable';
|
|
207
|
+
return sendJson(res, rejection, { ok: false, error });
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
178
212
|
async function readBody(req) {
|
|
179
213
|
const chunks = [];
|
|
180
214
|
let total = 0;
|
|
@@ -1164,7 +1198,7 @@ export function apply(ctx, options = {}) {
|
|
|
1164
1198
|
|
|
1165
1199
|
ctx.effect(() => {
|
|
1166
1200
|
try {
|
|
1167
|
-
return ctx
|
|
1201
|
+
return registerPrivateRoute(ctx, {
|
|
1168
1202
|
kind: 'exact', path: '/api/mo-image/config',
|
|
1169
1203
|
handler: async (req, res) => {
|
|
1170
1204
|
try {
|
|
@@ -1189,7 +1223,7 @@ export function apply(ctx, options = {}) {
|
|
|
1189
1223
|
|
|
1190
1224
|
ctx.effect(() => {
|
|
1191
1225
|
try {
|
|
1192
|
-
return ctx
|
|
1226
|
+
return registerPrivateRoute(ctx, {
|
|
1193
1227
|
kind: 'exact', path: '/api/mo-image/models',
|
|
1194
1228
|
handler: async (req, res) => {
|
|
1195
1229
|
try {
|
|
@@ -1216,7 +1250,7 @@ export function apply(ctx, options = {}) {
|
|
|
1216
1250
|
|
|
1217
1251
|
ctx.effect(() => {
|
|
1218
1252
|
try {
|
|
1219
|
-
return ctx
|
|
1253
|
+
return registerPrivateRoute(ctx, {
|
|
1220
1254
|
kind: 'exact', path: '/api/mo-image/detect-protocol',
|
|
1221
1255
|
handler: async (req, res) => {
|
|
1222
1256
|
try {
|
|
@@ -1276,7 +1310,7 @@ export function apply(ctx, options = {}) {
|
|
|
1276
1310
|
|
|
1277
1311
|
ctx.effect(() => {
|
|
1278
1312
|
try {
|
|
1279
|
-
return ctx
|
|
1313
|
+
return registerPrivateRoute(ctx, {
|
|
1280
1314
|
kind: 'exact', path: '/api/mo-image/detect-model-rule',
|
|
1281
1315
|
handler: async (req, res) => {
|
|
1282
1316
|
try {
|
|
@@ -1333,7 +1367,7 @@ export function apply(ctx, options = {}) {
|
|
|
1333
1367
|
|
|
1334
1368
|
ctx.effect(() => {
|
|
1335
1369
|
try {
|
|
1336
|
-
return ctx
|
|
1370
|
+
return registerPrivateRoute(ctx, {
|
|
1337
1371
|
kind: 'exact', path: '/api/mo-image/comfy/analyze',
|
|
1338
1372
|
handler: async (req, res) => {
|
|
1339
1373
|
try {
|
|
@@ -1361,7 +1395,7 @@ export function apply(ctx, options = {}) {
|
|
|
1361
1395
|
*/
|
|
1362
1396
|
ctx.effect(() => {
|
|
1363
1397
|
try {
|
|
1364
|
-
return ctx
|
|
1398
|
+
return registerPrivateRoute(ctx, {
|
|
1365
1399
|
kind: 'exact', path: '/api/mo-image/comfy/params',
|
|
1366
1400
|
handler: async (req, res) => {
|
|
1367
1401
|
try {
|
|
@@ -1386,7 +1420,7 @@ export function apply(ctx, options = {}) {
|
|
|
1386
1420
|
|
|
1387
1421
|
ctx.effect(() => {
|
|
1388
1422
|
try {
|
|
1389
|
-
return ctx
|
|
1423
|
+
return registerPrivateRoute(ctx, {
|
|
1390
1424
|
kind: 'prefix', path: '/api/mo-image/preview',
|
|
1391
1425
|
handler: async (req, res) => {
|
|
1392
1426
|
let handle;
|
|
@@ -1431,7 +1465,7 @@ export function apply(ctx, options = {}) {
|
|
|
1431
1465
|
|
|
1432
1466
|
ctx.effect(() => {
|
|
1433
1467
|
try {
|
|
1434
|
-
return ctx
|
|
1468
|
+
return registerPrivateRoute(ctx, {
|
|
1435
1469
|
kind: 'exact', path: '/api/mo-image/update',
|
|
1436
1470
|
handler: async (req, res) => {
|
|
1437
1471
|
try {
|
package/package.json
CHANGED