From 21c2bb95f078aa7dc1623cc73726c0d5eb070de8 Mon Sep 17 00:00:00 2001 From: undefined Date: Sat, 26 Sep 2020 20:15:27 +0800 Subject: [PATCH] =?UTF-8?q?draft:=20=E4=BB=8Ehustoj=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/hydrooj/package.json | 2 +- packages/hydrooj/src/handler/home.ts | 2 +- packages/hydrooj/src/interface.ts | 4 +- packages/hydrooj/src/lib/crypto.ts | 13 +++ packages/hydrooj/src/lib/md5.ts | 138 --------------------------- packages/hydrooj/src/lib/misc.ts | 2 +- packages/hydrooj/src/lib/sha1.ts | 10 -- packages/migrate-hustoj/README.md | 12 +++ packages/migrate-hustoj/lib.ts | 12 +++ packages/migrate-hustoj/package.json | 21 ++++ packages/migrate-hustoj/script.ts | 105 ++++++++++++++++++++ 11 files changed, 168 insertions(+), 153 deletions(-) create mode 100644 packages/hydrooj/src/lib/crypto.ts delete mode 100644 packages/hydrooj/src/lib/md5.ts delete mode 100644 packages/hydrooj/src/lib/sha1.ts create mode 100644 packages/migrate-hustoj/README.md create mode 100644 packages/migrate-hustoj/lib.ts create mode 100644 packages/migrate-hustoj/package.json create mode 100644 packages/migrate-hustoj/script.ts diff --git a/packages/hydrooj/package.json b/packages/hydrooj/package.json index a6b696efc..f1451b40b 100644 --- a/packages/hydrooj/package.json +++ b/packages/hydrooj/package.json @@ -1,6 +1,6 @@ { "name": "hydrooj", - "version": "2.13.18", + "version": "2.13.19", "bin": "bin/hydrooj.js", "main": "dist/loader.js", "typings": "dist/loader.d.ts", diff --git a/packages/hydrooj/src/handler/home.ts b/packages/hydrooj/src/handler/home.ts index 90b0bec20..40418d222 100644 --- a/packages/hydrooj/src/handler/home.ts +++ b/packages/hydrooj/src/handler/home.ts @@ -8,7 +8,7 @@ import { Route, Connection, Handler, ConnectionHandler, param, Types, } from '../service/server'; import * as misc from '../lib/misc'; -import md5 from '../lib/md5'; +import { md5 } from '../lib/crypto'; import * as mail from '../lib/mail'; import * as contest from '../model/contest'; import * as message from '../model/message'; diff --git a/packages/hydrooj/src/interface.ts b/packages/hydrooj/src/interface.ts index c42c8271c..5b315315e 100644 --- a/packages/hydrooj/src/interface.ts +++ b/packages/hydrooj/src/interface.ts @@ -500,13 +500,13 @@ export interface Lib { jwt: typeof import('./lib/jwt'), logger: typeof import('./lib/logger'), mail: typeof import('./lib/mail'), - md5: typeof import('./lib/md5'), + md5: typeof import('./lib/crypto').md5, + sha1: typeof import('./lib/crypto').sha1, misc: typeof import('./lib/misc'), paginate: typeof import('./lib/paginate'), rank: typeof import('./lib/rank'), rating: typeof import('./lib/rating'), testdataConfig: typeof import('./lib/testdataConfig'), - sha1: typeof import('./lib/sha1'), sysinfo: typeof import('./lib/sysinfo'), 'testdata.convert.ini': typeof import('./lib/testdata.convert.ini'), useragent: typeof import('./lib/useragent'), diff --git a/packages/hydrooj/src/lib/crypto.ts b/packages/hydrooj/src/lib/crypto.ts new file mode 100644 index 000000000..1cd820365 --- /dev/null +++ b/packages/hydrooj/src/lib/crypto.ts @@ -0,0 +1,13 @@ +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; diff --git a/packages/hydrooj/src/lib/md5.ts b/packages/hydrooj/src/lib/md5.ts deleted file mode 100644 index 4155b2455..000000000 --- a/packages/hydrooj/src/lib/md5.ts +++ /dev/null @@ -1,138 +0,0 @@ -// From https://github.com/pvorb/node-md5/blob/master/md5.js -// BSD 3-Clause "New" or "Revised" License - -/* eslint-disable max-len */ -function safeAdd(x, y) { - const lsw = (x & 0xffff) + (y & 0xffff); - const msw = (x >> 16) + (y >> 16) + (lsw >> 16); - return (msw << 16) | (lsw & 0xffff); -} -const bitRotateLeft = (num, cnt) => (num << cnt) | (num >>> (32 - cnt)); -const md5cmn = (q, a, b, x, s, t) => safeAdd(bitRotateLeft(safeAdd(safeAdd(a, q), safeAdd(x, t)), s), b); -const md5ff = (a, b, c, d, x, s, t) => md5cmn((b & c) | (~b & d), a, b, x, s, t); -const md5gg = (a, b, c, d, x, s, t) => md5cmn((b & d) | (c & ~d), a, b, x, s, t); -const md5hh = (a, b, c, d, x, s, t) => md5cmn(b ^ c ^ d, a, b, x, s, t); -const md5ii = (a, b, c, d, x, s, t) => md5cmn(c ^ (b | ~d), a, b, x, s, t); -function binlMD5(x, len) { - x[len >> 5] |= 0x80 << len % 32; - x[(((len + 64) >>> 9) << 4) + 14] = len; - let i; - let olda; - let oldb; - let oldc; - let oldd; - let a = 1732584193; - let b = -271733879; - let c = -1732584194; - let d = 271733878; - for (i = 0; i < x.length; i += 16) { - olda = a; - oldb = b; - oldc = c; - oldd = d; - a = md5ff(a, b, c, d, x[i], 7, -680876936); - d = md5ff(d, a, b, c, x[i + 1], 12, -389564586); - c = md5ff(c, d, a, b, x[i + 2], 17, 606105819); - b = md5ff(b, c, d, a, x[i + 3], 22, -1044525330); - a = md5ff(a, b, c, d, x[i + 4], 7, -176418897); - d = md5ff(d, a, b, c, x[i + 5], 12, 1200080426); - c = md5ff(c, d, a, b, x[i + 6], 17, -1473231341); - b = md5ff(b, c, d, a, x[i + 7], 22, -45705983); - a = md5ff(a, b, c, d, x[i + 8], 7, 1770035416); - d = md5ff(d, a, b, c, x[i + 9], 12, -1958414417); - c = md5ff(c, d, a, b, x[i + 10], 17, -42063); - b = md5ff(b, c, d, a, x[i + 11], 22, -1990404162); - a = md5ff(a, b, c, d, x[i + 12], 7, 1804603682); - d = md5ff(d, a, b, c, x[i + 13], 12, -40341101); - c = md5ff(c, d, a, b, x[i + 14], 17, -1502002290); - b = md5ff(b, c, d, a, x[i + 15], 22, 1236535329); - a = md5gg(a, b, c, d, x[i + 1], 5, -165796510); - d = md5gg(d, a, b, c, x[i + 6], 9, -1069501632); - c = md5gg(c, d, a, b, x[i + 11], 14, 643717713); - b = md5gg(b, c, d, a, x[i], 20, -373897302); - a = md5gg(a, b, c, d, x[i + 5], 5, -701558691); - d = md5gg(d, a, b, c, x[i + 10], 9, 38016083); - c = md5gg(c, d, a, b, x[i + 15], 14, -660478335); - b = md5gg(b, c, d, a, x[i + 4], 20, -405537848); - a = md5gg(a, b, c, d, x[i + 9], 5, 568446438); - d = md5gg(d, a, b, c, x[i + 14], 9, -1019803690); - c = md5gg(c, d, a, b, x[i + 3], 14, -187363961); - b = md5gg(b, c, d, a, x[i + 8], 20, 1163531501); - a = md5gg(a, b, c, d, x[i + 13], 5, -1444681467); - d = md5gg(d, a, b, c, x[i + 2], 9, -51403784); - c = md5gg(c, d, a, b, x[i + 7], 14, 1735328473); - b = md5gg(b, c, d, a, x[i + 12], 20, -1926607734); - a = md5hh(a, b, c, d, x[i + 5], 4, -378558); - d = md5hh(d, a, b, c, x[i + 8], 11, -2022574463); - c = md5hh(c, d, a, b, x[i + 11], 16, 1839030562); - b = md5hh(b, c, d, a, x[i + 14], 23, -35309556); - a = md5hh(a, b, c, d, x[i + 1], 4, -1530992060); - d = md5hh(d, a, b, c, x[i + 4], 11, 1272893353); - c = md5hh(c, d, a, b, x[i + 7], 16, -155497632); - b = md5hh(b, c, d, a, x[i + 10], 23, -1094730640); - a = md5hh(a, b, c, d, x[i + 13], 4, 681279174); - d = md5hh(d, a, b, c, x[i], 11, -358537222); - c = md5hh(c, d, a, b, x[i + 3], 16, -722521979); - b = md5hh(b, c, d, a, x[i + 6], 23, 76029189); - a = md5hh(a, b, c, d, x[i + 9], 4, -640364487); - d = md5hh(d, a, b, c, x[i + 12], 11, -421815835); - c = md5hh(c, d, a, b, x[i + 15], 16, 530742520); - b = md5hh(b, c, d, a, x[i + 2], 23, -995338651); - a = md5ii(a, b, c, d, x[i], 6, -198630844); - d = md5ii(d, a, b, c, x[i + 7], 10, 1126891415); - c = md5ii(c, d, a, b, x[i + 14], 15, -1416354905); - b = md5ii(b, c, d, a, x[i + 5], 21, -57434055); - a = md5ii(a, b, c, d, x[i + 12], 6, 1700485571); - d = md5ii(d, a, b, c, x[i + 3], 10, -1894986606); - c = md5ii(c, d, a, b, x[i + 10], 15, -1051523); - b = md5ii(b, c, d, a, x[i + 1], 21, -2054922799); - a = md5ii(a, b, c, d, x[i + 8], 6, 1873313359); - d = md5ii(d, a, b, c, x[i + 15], 10, -30611744); - c = md5ii(c, d, a, b, x[i + 6], 15, -1560198380); - b = md5ii(b, c, d, a, x[i + 13], 21, 1309151649); - a = md5ii(a, b, c, d, x[i + 4], 6, -145523070); - d = md5ii(d, a, b, c, x[i + 11], 10, -1120210379); - c = md5ii(c, d, a, b, x[i + 2], 15, 718787259); - b = md5ii(b, c, d, a, x[i + 9], 21, -343485551); - a = safeAdd(a, olda); - b = safeAdd(b, oldb); - c = safeAdd(c, oldc); - d = safeAdd(d, oldd); - } - return [a, b, c, d]; -} -function binl2rstr(input) { - let i; - let output = ''; - const length32 = input.length * 32; - for (i = 0; i < length32; i += 8) { output += String.fromCharCode((input[i >> 5] >>> i % 32) & 0xff); } - return output; -} -function rstr2binl(input) { - let i; - const output = []; - output[(input.length >> 2) - 1] = undefined; - for (i = 0; i < output.length; i += 1) { output[i] = 0; } - const length8 = input.length * 8; - for (i = 0; i < length8; i += 8) output[i >> 5] |= (input.charCodeAt(i / 8) & 0xff) << i % 32; - return output; -} -const rstrMD5 = (s) => binl2rstr(binlMD5(rstr2binl(s), s.length * 8)); -function rstr2hex(input) { - const hexTab = '0123456789abcdef'; - let output = ''; - let x; - let i; - for (i = 0; i < input.length; i += 1) { - x = input.charCodeAt(i); - output += hexTab.charAt((x >>> 4) & 0x0f) + hexTab.charAt(x & 0x0f); - } - return output; -} - -function md5(s: string) { - return rstr2hex(rstrMD5(unescape(encodeURIComponent(s)))); -} - -export = md5; -global.Hydro.lib.md5 = md5; diff --git a/packages/hydrooj/src/lib/misc.ts b/packages/hydrooj/src/lib/misc.ts index c6c29cc2c..8e15338df 100644 --- a/packages/hydrooj/src/lib/misc.ts +++ b/packages/hydrooj/src/lib/misc.ts @@ -1,6 +1,6 @@ import AnsiUp from 'ansi_up'; import { ObjectID } from 'mongodb'; -import md5 from './md5'; +import { md5 } from './crypto'; const AU = new AnsiUp(); diff --git a/packages/hydrooj/src/lib/sha1.ts b/packages/hydrooj/src/lib/sha1.ts deleted file mode 100644 index 1d4cf8b34..000000000 --- a/packages/hydrooj/src/lib/sha1.ts +++ /dev/null @@ -1,10 +0,0 @@ -import crypto from 'crypto'; - -function sha1(content: string) { - const hash = crypto.createHash('sha1'); - hash.update(content); - return hash.digest('hex'); -} - -global.Hydro.lib.sha1 = sha1; -export = sha1; diff --git a/packages/migrate-hustoj/README.md b/packages/migrate-hustoj/README.md new file mode 100644 index 000000000..7fea86811 --- /dev/null +++ b/packages/migrate-hustoj/README.md @@ -0,0 +1,12 @@ +# migrate-hustoj + +这个模块用于从HustOJ的数据库导入数据。 + +## 迁移须知 + +迁移前,您需要指定数据来源。 +迁移过程中,Hydro 的内容会被**清空**,以下内容将被迁移: + +题目与测试数据,用户列表。 + +迁移过程中请保持网络畅通。 diff --git a/packages/migrate-hustoj/lib.ts b/packages/migrate-hustoj/lib.ts new file mode 100644 index 000000000..fff43163c --- /dev/null +++ b/packages/migrate-hustoj/lib.ts @@ -0,0 +1,12 @@ +import { } from 'hydrooj'; + +const { md5, sha1 } = global.Hydro.lib; +const RE_MD5 = /^[\da-fA-F]{32}$/; + +function hash(password: string, stored: string) { + if (RE_MD5.test(stored)) return md5(password) === stored; + const salt = Buffer.from(stored, 'base64').toString().substr(0, 20); + return Buffer.from(sha1(password + salt).substr(0, 20) + salt).toString('base64') === stored; +} + +global.Hydro.lib['hash.hust'] = hash; diff --git a/packages/migrate-hustoj/package.json b/packages/migrate-hustoj/package.json new file mode 100644 index 000000000..4fe7041c9 --- /dev/null +++ b/packages/migrate-hustoj/package.json @@ -0,0 +1,21 @@ +{ + "name": "@hydrooj/migrate-hustoj", + "version": "0.0.1", + "main": "package.json", + "repository": "git@github.com:hydro-dev/Hydro.git", + "author": "undefined ", + "license": "MIT", + "scripts": { + "lint": "eslint lib.ts script.ts --fix", + "build": "tsc" + }, + "devDependencies": { + "@types/mysql": "^2.15.15" + }, + "peerDependencies": { + "hydrooj": "^2.13.5" + }, + "dependencies": { + "mysql": "^2.18.1" + } +} diff --git a/packages/migrate-hustoj/script.ts b/packages/migrate-hustoj/script.ts new file mode 100644 index 000000000..7cd206569 --- /dev/null +++ b/packages/migrate-hustoj/script.ts @@ -0,0 +1,105 @@ +/* eslint-disable no-tabs */ +/* eslint-disable no-await-in-loop */ +import mysql from 'mysql'; +import 'hydrooj'; + +const { + user, problem, builtin: { PRIV }, +} = global.Hydro.model; + +export async function run({ + host = 'localhost', port = 27017, name = 'vijos4', username, password, +}, report: Function) { + const src = mysql.createConnection({ + host, + port, + user: username, + password, + database: name, + }); + await new Promise((resolve) => src.connect(resolve)); + const query = (q: string | mysql.Query): Promise<[values: any[], fields: mysql.FieldInfo[]]> => new Promise((res, rej) => { + src.query(q, (err, val, fields) => { + if (err) rej(err); + res([val, fields]); + }); + }); + report({ message: 'Connected to database' }); + /* + user_id varchar 20 N 用户id(主键) + email varchar 100 Y 用户E-mail + submit int 11 Y 用户提交次数 + solved int 11 Y 成功次数 + defunct char 1 N 是否屏蔽(Y/N) + ip varchar 20 N 用户注册ip + accesstime datetime Y 用户注册时间 + volume int 11 N 页码(表示用户上次看到第几页) + language int 11 N 语言 + password varchar 32 Y 密码(加密) + reg_time datetime Y 用户注册时间 + nick varchar 100 N 昵称 + school varchar 100 N 用户所在学校 + */ + const uidMap: Record = {}; + + const [udocs] = await query('SELECT * FROM `users`'); + for (const udoc of udocs) { + const uid = await user.create(udoc.email, udoc.nick, '', null, udoc.ip, udoc.defunct === 'Y' ? 0 : PRIV.PRIV_DEFAULT); + uidMap[udoc.uid] = uid; + await user.setById(uid, { + nSubmit: udoc.submit, + nSolve: udoc.solved, + loginat: udoc.accesstime, + regat: udoc.reg_time, + hash: udoc.password, + salt: udoc.password, + hashType: 'hust', + }); + } + + const [admins] = await query("SELECT * FROM 'privilege' WHERE rightstr = 'administrator'"); + for (const admin of admins) await user.setSuperAdmin(uidMap[admin.user_id]); + + /* + problem_id int 11 N 题目编号,主键 + title varchar 200 N 标题 + description text Y 题目描述 + inupt text Y 输入说明 + output text Y 输出说明 + sample_input text Y 输入参照 + sample_output text Y 输出参照 + spj char 1 N 是否为特别题目 + hint text Y 暗示 + source varchar 100 Y 来源 + in_date datetime Y 加入时间 + time_limit int 11 N 限时(秒) + memory_limit int 11 N 空间限制(MByte) + defunct char 1 N 是否屏蔽(Y/N) + accepted int 11 Y 总ac次数 + submit int 11 Y 总提交次数 + solved int 11 Y 解答(未用) + */ + const [pdocs] = await query('SELECT * FROM `problem`'); + for (const pdoc of pdocs) { + const pid = await problem.add( + 'system', `P${pdoc.problem_id}`, + pdoc.title, pdoc.description + pdoc.input + pdoc.output + pdoc.sample_input + pdoc.sample_output + pdoc.hint, + 1, pdoc.source.split(' '), undefined, undefined, pdoc.defunct === 'Y', + ); + const [cdoc] = await query(`SELECT * FROM 'privilege' WHERE rightstr = 'p${pdoc.problem_id}'`); + await problem.edit('system', pid, { + nAccept: pdoc.accepted, + nSubmit: pdoc.submit, + config: { time: `${pdoc.time_limit}s`, memory: `${pdoc.memory_limit}m` }, + owner: uidMap[cdoc[0].user_id], + }); + } + return true; +} + +export const description = 'migrate from hustoj'; +export const validate = { + host: 'string', port: 'number', name: 'string', username: 'string', password: 'string', +}; + +global.Hydro.script.migrateHustoj = { run, description, validate };