core&utils: remove outdated code

pull/420/head
undefined 2 years ago
parent 5ae8258b8a
commit 7903d012da

@ -1,6 +1,6 @@
{ {
"name": "hydrooj", "name": "hydrooj",
"version": "3.16.1", "version": "3.16.2",
"bin": "bin/hydrooj.js", "bin": "bin/hydrooj.js",
"main": "src/loader", "main": "src/loader",
"module": "src/loader", "module": "src/loader",

@ -23,9 +23,15 @@ function parseParameters(fn: Function) {
} }
async function runScript(name: string, arg: any) { async function runScript(name: string, arg: any) {
if (!global.Hydro.script[name]) return console.error('Script %s not found.', name); const s = global.Hydro.script[name];
validate(global.Hydro.script[name].validate, arg); if (!s) return console.error('Script %s not found.', name);
return await global.Hydro.script[name].run(arg, console.info); if (typeof s.validate === 'function') {
arg = s.validate(arg);
} else {
console.warn('You are using the legacy script validation API, which will be dropped in the future.');
validate(s.validate, arg);
}
return await s.run(arg, console.info);
} }
async function cli() { async function cli() {

@ -8,7 +8,6 @@ import {
} from '../error'; } from '../error';
import { DomainDoc, MessageDoc, Setting } from '../interface'; import { DomainDoc, MessageDoc, Setting } from '../interface';
import avatar from '../lib/avatar'; import avatar from '../lib/avatar';
import { md5 } from '../lib/crypto';
import * as mail from '../lib/mail'; import * as mail from '../lib/mail';
import { isDomainId, isEmail, isPassword } from '../lib/validator'; import { isDomainId, isEmail, isPassword } from '../lib/validator';
import BlackListModel from '../model/blacklist'; import BlackListModel from '../model/blacklist';
@ -27,6 +26,7 @@ import * as bus from '../service/bus';
import { import {
Connection, ConnectionHandler, Handler, param, query, Route, Types, Connection, ConnectionHandler, Handler, param, query, Route, Types,
} from '../service/server'; } from '../service/server';
import { md5 } from '../utils';
const { geoip, useragent } = global.Hydro.lib; const { geoip, useragent } = global.Hydro.lib;

@ -5,7 +5,6 @@ import { lookup } from 'mime-types';
import { import {
BadRequestError, ForbiddenError, ValidationError, BadRequestError, ForbiddenError, ValidationError,
} from '../error'; } from '../error';
import { md5 } from '../lib/crypto';
import { PRIV } from '../model/builtin'; import { PRIV } from '../model/builtin';
import * as oplog from '../model/oplog'; import * as oplog from '../model/oplog';
import storage from '../model/storage'; import storage from '../model/storage';
@ -16,7 +15,7 @@ import {
} from '../service/server'; } from '../service/server';
import { encodeRFC5987ValueChars } from '../service/storage'; import { encodeRFC5987ValueChars } from '../service/storage';
import { builtinConfig } from '../settings'; import { builtinConfig } from '../settings';
import { sortFiles } from '../utils'; import { md5, sortFiles } from '../utils';
class SwitchLanguageHandler extends Handler { class SwitchLanguageHandler extends Handler {
@param('lang', Types.Name) @param('lang', Types.Name)

@ -12,14 +12,6 @@ export interface System {
} }
export interface SystemKeys { export interface SystemKeys {
'file.endPoint': string,
'file.accessKey': string,
'file.secretKey': string,
'file.bucket': string,
'file.region': string,
'file.pathStyle': boolean,
'file.endPointForUser': string,
'file.endPointForJudge': string,
'smtp.user': string, 'smtp.user': string,
'smtp.from': string, 'smtp.from': string,
'smtp.pass': string, 'smtp.pass': string,
@ -657,15 +649,11 @@ export interface ProblemSearchOptions {
export type ProblemSearch = (domainId: string, q: string, options?: ProblemSearchOptions) => Promise<ProblemSearchResponse>; export type ProblemSearch = (domainId: string, q: string, options?: ProblemSearchOptions) => Promise<ProblemSearchResponse>;
export interface Lib extends Record<string, any> { export interface Lib extends Record<string, any> {
download: typeof import('./lib/download'),
difficulty: typeof import('./lib/difficulty'), difficulty: typeof import('./lib/difficulty'),
buildContent: typeof import('./lib/content').buildContent, buildContent: typeof import('./lib/content').buildContent,
'hash.hydro': typeof import('./lib/hash.hydro'), 'hash.hydro': typeof import('./lib/hash.hydro'),
i18n: typeof import('./lib/i18n'), i18n: typeof import('./lib/i18n'),
jwt: typeof import('./lib/jwt'),
mail: typeof import('./lib/mail'), mail: typeof import('./lib/mail'),
md5: typeof import('./lib/crypto').md5,
sha1: typeof import('./lib/crypto').sha1,
paginate: typeof import('./lib/paginate'), paginate: typeof import('./lib/paginate'),
rank: typeof import('./lib/rank'), rank: typeof import('./lib/rank'),
rating: typeof import('./lib/rating'), rating: typeof import('./lib/rating'),

@ -1,4 +1,4 @@
import { md5 } from './crypto'; import { md5 } from '../utils';
type AvatarProvider = (src: string, size: number) => string; type AvatarProvider = (src: string, size: number) => string;

@ -1,13 +0,0 @@
import crypto from 'crypto';
const encrypt = (algorithm, content) => {
const hash = crypto.createHash(algorithm);
hash.update(content);
return hash.digest('hex');
};
export const sha1 = (content: string) => encrypt('sha1', content);
export const md5 = (content: string) => encrypt('md5', content);
global.Hydro.lib.md5 = md5;
global.Hydro.lib.sha1 = sha1;

@ -1,22 +0,0 @@
import fs from 'fs';
import superagent, { SuperAgentRequest } from 'superagent';
async function _download(url: string, path: string, retry: number) {
const w = fs.createWriteStream(path);
superagent.get(url).retry(retry).pipe(w);
await new Promise((resolve, reject) => {
w.on('finish', resolve);
w.on('error', reject);
});
return path;
}
function download(url: string): SuperAgentRequest;
function download(url: string, path?: string, retry = 3) {
if (path) return _download(url, path, retry);
return superagent.get(url).retry(retry);
}
export = download;
global.Hydro.lib.download = download;

@ -1,9 +1,6 @@
import './jwt';
import './download';
import './i18n'; import './i18n';
import './mail'; import './mail';
import './useragent'; import './useragent';
import './crypto';
import './paginate'; import './paginate';
import './hash.hydro'; import './hash.hydro';
import './rank'; import './rank';

@ -1,35 +0,0 @@
function decodeBase64WithUriEncoding(encodedText: string) {
return Buffer.from(encodedText, 'base64').toString('utf8');
}
function unescapedString(escapedString: string) {
escapedString += new Array(5 - (escapedString.length % 4)).join('=');
return escapedString.replace(/-/g, '+').replace(/_/g, '/');
}
function decodeJWT(idToken: string) {
const token = idToken.split('.');
if (token.length !== 3) {
throw new Error('Invalid idToken');
}
try {
const headerSegment = JSON.parse(decodeBase64WithUriEncoding(token[0]));
const payloadSegment = JSON.parse(decodeBase64WithUriEncoding(token[1]));
const signature = unescapedString(token[2]);
return {
dataToSign: [token[0], token[1]].join('.'),
header: headerSegment,
payload: payloadSegment,
signature,
};
} catch (e) {
throw new Error('Invalid payload');
}
}
export function decode(idToken: string) {
const decodedJWT = decodeJWT(idToken);
return decodedJWT.payload;
}
global.Hydro.lib.jwt = { decode };

@ -1,33 +1,22 @@
import { Dictionary, isNull } from 'lodash';
import { PERM, PRIV } from '../model/builtin'; import { PERM, PRIV } from '../model/builtin';
const trueChecker = () => true; const trueChecker = () => true;
const Checker = (perm: bigint, priv: number, checker: Function = trueChecker) => (handler) => ( const Checker = (perm: bigint | bigint[], priv: number | number[], checker: Function = trueChecker) => (handler) => (
checker(handler) checker(handler)
&& (perm ? handler.user.hasPerm(perm) : true) && (perm ? handler.user.hasPerm(perm) : true)
&& (priv ? handler.user.hasPriv(priv) : true) && (priv ? handler.user.hasPriv(priv) : true)
); );
const buildChecker = (...permPrivChecker: Array<number | bigint | Function>) => { const buildChecker = (...permPrivChecker: Array<number | bigint | Function | number[] | bigint[]>) => {
let _priv: number; let _priv: number | number[];
let _perm: bigint; let _perm: bigint | bigint[];
let checker: Function = trueChecker; let checker: Function = trueChecker;
for (const item of permPrivChecker) { for (const item of permPrivChecker) {
if (item instanceof Object && !isNull(item)) { if (typeof item === 'function') checker = item;
if (item instanceof Array) { else if (typeof item === 'number') _priv = item;
if (typeof item[0] === 'number') { else if (typeof item === 'bigint') _perm = item;
// @ts-ignore else if (item instanceof Array) {
_priv = item; if (typeof item[0] === 'number') _priv = item as number[];
} else if (typeof item[0] === 'bigint') { else _perm = item as bigint[];
// @ts-ignore
_perm = item;
}
} else if (typeof item.call !== 'undefined') {
checker = item;
}
} else if (typeof item === 'number') {
_priv = item;
} else if (typeof item === 'bigint') {
_perm = item;
} }
} }
return Checker(_perm, _priv, checker); return Checker(_perm, _priv, checker);
@ -51,7 +40,7 @@ export const Nav = (
}; };
export const ProblemAdd = ( export const ProblemAdd = (
name: string, args: Dictionary<any> = {}, icon = 'add', text = 'Create Problem', name: string, args: Record<string, any> = {}, icon = 'add', text = 'Create Problem',
) => { ) => {
global.Hydro.ui.nodes.problem_add.push({ global.Hydro.ui.nodes.problem_add.push({
name, args, icon, text, name, args, icon, text,
@ -59,7 +48,7 @@ export const ProblemAdd = (
}; };
export const UserDropdown = ( export const UserDropdown = (
name: string, args: Dictionary<any> = {}, ...permPrivChecker: Array<number | bigint | Function> name: string, args: Record<string, any> = {}, ...permPrivChecker: Array<number | bigint | Function>
) => { ) => {
global.Hydro.ui.nodes.user_dropdown.push({ global.Hydro.ui.nodes.user_dropdown.push({
name, args: args || {}, checker: buildChecker(...permPrivChecker), name, args: args || {}, checker: buildChecker(...permPrivChecker),

@ -14,6 +14,7 @@ import './ui';
// This is the main entry. So let's re-export some modules. // This is the main entry. So let's re-export some modules.
export * from './interface'; export * from './interface';
export { Schema, Logger }; export { Schema, Logger };
export { requestConfig } from './settings';
const argv = cac().parse(); const argv = cac().parse();
const logger = new Logger('loader'); const logger = new Logger('loader');

@ -7,10 +7,10 @@ import {
} from 'fs-extra'; } from 'fs-extra';
import { lookup } from 'mime-types'; import { lookup } from 'mime-types';
import { BucketItem, Client, ItemBucketMetadata } from 'minio'; import { BucketItem, Client, ItemBucketMetadata } from 'minio';
import { md5 } from '../lib/crypto';
import { Logger } from '../logger'; import { Logger } from '../logger';
import { builtinConfig } from '../settings'; import { builtinConfig } from '../settings';
import { MaybeArray } from '../typeutils'; import { MaybeArray } from '../typeutils';
import { md5 } from '../utils';
const logger = new Logger('storage'); const logger = new Logger('storage');

@ -810,7 +810,7 @@ const scripts: UpgradeScript[] = [
] = system.getMany([ ] = system.getMany([
'file.endPoint', 'file.accessKey', 'file.secretKey', 'file.bucket', 'file.region', 'file.endPoint', 'file.accessKey', 'file.secretKey', 'file.bucket', 'file.region',
'file.pathStyle', 'file.endPointForUser', 'file.endPointForJudge', 'file.pathStyle', 'file.endPointForUser', 'file.endPointForJudge',
]); ] as any[]) as any;
if ((endPoint && accessKey) || process.env.MINIO_ACCESS_KEY) { if ((endPoint && accessKey) || process.env.MINIO_ACCESS_KEY) {
await setBuiltinConfig('file', { await setBuiltinConfig('file', {
type: 's3', type: 's3',

@ -1,5 +1,3 @@
import type { Logger } from './logger';
export function buildProjection(fields: string[]): Record<string, 1> { export function buildProjection(fields: string[]): Record<string, 1> {
const o = {}; const o = {};
for (const k of fields) o[k] = 1; for (const k of fields) o[k] = 1;
@ -18,21 +16,4 @@ export function ArgMethod(target: any, funcName: string, obj: any) {
return obj; return obj;
} }
export function logAndReturn(logger: Logger) {
return function cb(err: Error) {
logger.error(err);
return err;
};
}
const RE_USER = /(?:^|\s)(@)([^ ]{3,255}?)(?=\s|$)/g;
export function getRelatedUsers(content: string) {
const results = [];
let res: string[];
// eslint-disable-next-line no-cond-assign
while (res = RE_USER.exec(content)) results.push(res[2]);
return results;
}
export * from '@hydrooj/utils/lib/utils'; export * from '@hydrooj/utils/lib/utils';

@ -23,6 +23,29 @@ async function get() {
this.response.redirect = `https://accounts.google.com/o/oauth2/v2/auth?client_id=${appid}&response_type=code&redirect_uri=${url}oauth/google/callback&scope=https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile&state=${state}`; this.response.redirect = `https://accounts.google.com/o/oauth2/v2/auth?client_id=${appid}&response_type=code&redirect_uri=${url}oauth/google/callback&scope=https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile&state=${state}`;
} }
function unescapedString(escapedString: string) {
escapedString += new Array(5 - (escapedString.length % 4)).join('=');
return escapedString.replace(/-/g, '+').replace(/_/g, '/');
}
function decodeJWT(idToken: string) {
const token = idToken.split('.');
if (token.length !== 3) throw new Error('Invalid idToken');
try {
const headerSegment = JSON.parse(Buffer.from(token[0], 'base64').toString('utf8'));
const payloadSegment = JSON.parse(Buffer.from(token[1], 'base64').toString('utf8'));
const signature = unescapedString(token[2]);
return {
dataToSign: [token[0], token[1]].join('.'),
header: headerSegment,
payload: payloadSegment,
signature,
};
} catch (e) {
throw new Error('Invalid payload');
}
}
async function callback({ async function callback({
state, code, error, state, code, error,
}) { }) {
@ -46,7 +69,7 @@ async function callback({
grant_type: 'authorization_code', grant_type: 'authorization_code',
redirect_uri: `${url}oauth/google/callback`, redirect_uri: `${url}oauth/google/callback`,
}); });
const payload = global.Hydro.lib.jwt.decode(res.body.id_token); const payload = decodeJWT(res.body.id_token).payload;
await token.del(state, token.TYPE_OAUTH); await token.del(state, token.TYPE_OAUTH);
this.response.redirect = s.redirect; this.response.redirect = s.redirect;
return { return {

@ -1,6 +1,6 @@
{ {
"name": "@hydrooj/login-with-google", "name": "@hydrooj/login-with-google",
"version": "0.1.0", "version": "0.1.1",
"main": "package.json", "main": "package.json",
"repository": "git@github.com:hydro-dev/Hydro.git", "repository": "git@github.com:hydro-dev/Hydro.git",
"author": "undefined <i@undefined.moe>", "author": "undefined <i@undefined.moe>",

@ -1,4 +1,4 @@
import { md5, sha1 } from 'hydrooj/src/lib/crypto'; import { md5, sha1 } from '@hydrooj/utils/lib/utils';
const RE_MD5 = /^[\da-f]{32}$/; const RE_MD5 = /^[\da-f]{32}$/;

@ -1,6 +1,6 @@
{ {
"name": "@hydrooj/migrate", "name": "@hydrooj/migrate",
"version": "0.1.4", "version": "0.1.5",
"main": "package.json", "main": "package.json",
"repository": "git@github.com:hydro-dev/Hydro.git", "repository": "git@github.com:hydro-dev/Hydro.git",
"author": "undefined <i@undefined.moe>", "author": "undefined <i@undefined.moe>",

@ -2,7 +2,6 @@
import { size } from '@hydrooj/utils/lib/utils'; import { size } from '@hydrooj/utils/lib/utils';
import cac from 'cac'; import cac from 'cac';
import chalk from 'chalk'; import chalk from 'chalk';
import { build } from 'esbuild';
import log from 'fancy-log'; import log from 'fancy-log';
import fs from 'fs-extra'; import fs from 'fs-extra';
import gulp from 'gulp'; import gulp from 'gulp';

@ -2,7 +2,6 @@ const base = 'https://hydro.ac';
const target = [ const target = [
'https://hydro.ac', 'https://hydro.ac',
'https://us.hydro.ac', 'https://us.hydro.ac',
'https://proxy1.hydro.workers.dev',
]; ];
this.addEventListener('install', () => { this.addEventListener('install', () => {

@ -1,3 +1,4 @@
import crypto from 'crypto';
import os from 'os'; import os from 'os';
import path from 'path'; import path from 'path';
import { Duplex } from 'stream'; import { Duplex } from 'stream';
@ -7,6 +8,10 @@ import type { Moment } from 'moment-timezone';
import { isMoment } from 'moment-timezone'; import { isMoment } from 'moment-timezone';
import { ObjectID } from 'mongodb'; import { ObjectID } from 'mongodb';
const encrypt = (algorithm, content) => crypto.createHash(algorithm).update(content).digest('hex');
export const sha1 = (content: string) => encrypt('sha1', content);
export const md5 = (content: string) => encrypt('md5', content);
export function folderSize(folderPath: string) { export function folderSize(folderPath: string) {
// eslint-disable-next-line @typescript-eslint/no-shadow // eslint-disable-next-line @typescript-eslint/no-shadow
let size = 0; let size = 0;

@ -1,6 +1,6 @@
{ {
"name": "@hydrooj/utils", "name": "@hydrooj/utils",
"version": "1.3.5", "version": "1.3.6",
"description": "hydrooj utils", "description": "hydrooj utils",
"main": "package.json", "main": "package.json",
"repository": "https://github.com/hydro-dev/Hydro.git", "repository": "https://github.com/hydro-dev/Hydro.git",

Loading…
Cancel
Save