You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/packages/migrate/index.ts

72 lines
2.7 KiB
TypeScript

import {
Context, md5, Schema, sha1,
} from 'hydrooj';
const RE_MD5 = /^[\da-f]{32}$/;
export function apply(ctx: Context) {
ctx.addScript(
'migrateHustoj', 'migrate from hustoj',
Schema.object({
host: Schema.string().required(),
port: Schema.number().required(),
name: Schema.string().required(),
username: Schema.string().required(),
password: Schema.string().required(),
domainId: Schema.string().required(),
contestType: Schema.string().required(),
dataDir: Schema.string().required(),
}),
(...args) => require('./scripts/hustoj').run(...args),
);
ctx.addScript(
'migrateSyzoj', 'migrate from syzoj',
Schema.object({
host: Schema.string().required(),
port: Schema.number().required(),
name: Schema.string().required(),
username: Schema.string().required(),
password: Schema.string().required(),
domainId: Schema.string().default('system'),
dataDir: Schema.string().default('/opt/syzoj/web/uploads'),
}),
(...args) => require('./scripts/syzoj').run(...args),
);
ctx.addScript(
'migrateVijos', 'migrate from vijos',
Schema.object({
host: Schema.string().required(),
port: Schema.number().required(),
name: Schema.string().required(),
username: Schema.string().required(),
password: Schema.string().required(),
}),
(...args) => require('./scripts/vijos').run(...args),
);
ctx.provideModule('hash', 'hust', ($password, $saved) => {
$password = md5($password);
if (RE_MD5.test($saved)) return $password === $saved;
const $svd = Buffer.from($saved, 'base64').toString('hex');
const $salt = Buffer.from($svd.substr(40), 'hex').toString();
const $hash = Buffer.concat([
Buffer.from(sha1($password + $salt), 'hex'),
Buffer.from($salt),
]).toString('base64');
if ($hash.trim() === $saved.trim()) return true;
return false;
});
ctx.provideModule('hash', 'vj2', (password, salt, { uname }) => {
const pmd5 = md5(password);
const mixedSha1 = sha1(md5(uname.toLowerCase() + pmd5) + salt + sha1(pmd5 + salt));
return `${Buffer.from(uname).toString('base64')}|${mixedSha1}`;
});
ctx.provideModule('hash', 'syzoj', (password: string) => md5(`${password}syzoj2_xxx`));
ctx.i18n.load('zh', {
'migrate from hustoj': '从 HustOJ 导入',
'migrate from vijos': '从 Vijos 导入',
'migrate from syzoj': '从 SYZOJ 导入',
});
}