mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: skip sid allocation for guest
This commit is contained in:
2 files changed
+14
-18
No files matched your search
@@ -1,6 +1,7 @@
|
|||||||
import { PassThrough } from 'stream';
|
import { PassThrough } from 'stream';
|
||||||
import type { Next } from 'koa';
|
import type { Next } from 'koa';
|
||||||
import { cloneDeep, pick } from 'lodash';
|
import { cloneDeep, pick } from 'lodash';
|
||||||
|
import { PERM } from '../../model/builtin';
|
||||||
import * as system from '../../model/system';
|
import * as system from '../../model/system';
|
||||||
import token from '../../model/token';
|
import token from '../../model/token';
|
||||||
import type { HydroRequest, HydroResponse, KoaContext } from '../server';
|
import type { HydroRequest, HydroResponse, KoaContext } from '../server';
|
||||||
@@ -65,12 +66,19 @@ export default async (ctx: KoaContext, next: Next) => {
|
|||||||
ctx.HydroContext = {
|
ctx.HydroContext = {
|
||||||
request, response, UiContext, domain: domainInfo, user: null, args,
|
request, response, UiContext, domain: domainInfo, user: null, args,
|
||||||
};
|
};
|
||||||
|
const header = ctx.request.headers.authorization;
|
||||||
|
const sid = header
|
||||||
|
? header.split(' ')[1] // Accept bearer token
|
||||||
|
: ctx.cookies.get('sid') || ctx.query.sid; // FIXME maybe a better way for shared conn?
|
||||||
|
const session = sid ? await token.get(sid instanceof Array ? sid[0] : sid, token.TYPE_SESSION) : null;
|
||||||
|
ctx.session = Object.create(session || { uid: 0, scope: PERM.PERM_ALL.toString() });
|
||||||
await next();
|
await next();
|
||||||
const ua = request.headers['user-agent'] || '';
|
const ua = request.headers['user-agent'] || '';
|
||||||
if (!ctx.session.uid && system.get('server.ignoreUA').replace(/\r/g, '').split('\n').filter((i) => i && ua.includes(i)).length) return;
|
if (!ctx.session.uid && system.get('server.ignoreUA').replace(/\r/g, '').split('\n').filter((i) => i && ua.includes(i)).length) return;
|
||||||
const expireSeconds = ctx.session.save
|
const expireSeconds = ctx.session.save
|
||||||
? system.get('session.saved_expire_seconds')
|
? system.get('session.saved_expire_seconds')
|
||||||
: system.get('session.unsaved_expire_seconds');
|
: system.get('session.unsaved_expire_seconds');
|
||||||
|
if (!ctx.session._id && !Object.getOwnPropertyNames(ctx.session).length) return;
|
||||||
Object.assign(ctx.session, { updateIp: request.ip, updateUa: ua });
|
Object.assign(ctx.session, { updateIp: request.ip, updateUa: ua });
|
||||||
if (ctx.session._id) {
|
if (ctx.session._id) {
|
||||||
await token.update(ctx.session._id, token.TYPE_SESSION, expireSeconds, ctx.session);
|
await token.update(ctx.session._id, token.TYPE_SESSION, expireSeconds, ctx.session);
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import BlackListModel from '../../model/blacklist';
|
import BlackListModel from '../../model/blacklist';
|
||||||
import DomainModel from '../../model/domain';
|
import DomainModel from '../../model/domain';
|
||||||
import * as system from '../../model/system';
|
import * as system from '../../model/system';
|
||||||
import token from '../../model/token';
|
|
||||||
import { KoaContext } from '../server';
|
import { KoaContext } from '../server';
|
||||||
|
|
||||||
export default async (ctx: KoaContext, next) => {
|
export default async (ctx: KoaContext, next) => {
|
||||||
@@ -12,29 +11,18 @@ export default async (ctx: KoaContext, next) => {
|
|||||||
let ip = ctx.request.headers[xff?.toLowerCase() || ''] as string || ctx.request.ip;
|
let ip = ctx.request.headers[xff?.toLowerCase() || ''] as string || ctx.request.ip;
|
||||||
ip = ip.split(',')[0].trim();
|
ip = ip.split(',')[0].trim();
|
||||||
const host = ctx.request.headers[xhost?.toLowerCase() || ''] as string || ctx.request.host;
|
const host = ctx.request.headers[xhost?.toLowerCase() || ''] as string || ctx.request.host;
|
||||||
const header = ctx.request.headers.authorization;
|
const domainId = forceDomain?.[1] || 'system';
|
||||||
const sid = header
|
const [absoluteDomain, inferDomain, bdoc] = await Promise.all([
|
||||||
? header.split(' ')[1] // Accept bearer token
|
|
||||||
: ctx.cookies.get('sid') || ctx.query.sid; // FIXME maybe a better way for shared conn?
|
|
||||||
let domainId = forceDomain?.[1] || 'system';
|
|
||||||
const [absoluteDomain, inferDomain, bdoc, session] = await Promise.all([
|
|
||||||
DomainModel.get(domainId),
|
DomainModel.get(domainId),
|
||||||
forceDomain ? Promise.resolve() : DomainModel.getByHost(host),
|
forceDomain ? Promise.resolve(null) : DomainModel.getByHost(host),
|
||||||
BlackListModel.get(`ip::${ip}`),
|
BlackListModel.get(`ip::${ip}`),
|
||||||
token.get(sid instanceof Array ? sid[0] : sid, token.TYPE_SESSION),
|
|
||||||
]);
|
]);
|
||||||
if (bdoc) {
|
if (bdoc) {
|
||||||
ctx.body = 'blacklisted'; // Just return 404 if blacklisted
|
ctx.body = 'blacklisted'; // Just return 404 if blacklisted
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (inferDomain) domainId = inferDomain._id;
|
ctx.domainId = inferDomain?._id || domainId;
|
||||||
ctx.domainId = domainId;
|
|
||||||
ctx.domainInfo = inferDomain || absoluteDomain;
|
ctx.domainInfo = inferDomain || absoluteDomain;
|
||||||
if (domainId !== ctx.domainInfo._id) {
|
if (domainId !== ctx.domainInfo._id) ctx.redirect(ctx.originalPath.replace(/^\/d\/[^/]+\//, `/d/${ctx.domainInfo._id}/`));
|
||||||
// Case sensitive
|
else await next();
|
||||||
ctx.redirect(ctx.originalPath.replace(/^\/d\/[^/]+\//, `/d/${ctx.domainInfo._id}/`));
|
|
||||||
} else {
|
|
||||||
ctx.session = session || { uid: 0 };
|
|
||||||
await next();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user