Menci
6 years ago
5 changed files with 103 additions and 94 deletions
@ -1,32 +1,32 @@ |
|||||||
const child_process = require('child_process'); |
const child_process = require('child_process'); |
||||||
|
|
||||||
const rendererd = child_process.fork(__dirname + '/rendererd', [syzoj.config.redis]); |
const rendererd = child_process.fork(__dirname + '/rendererd', [syzoj.config.redis, parseInt(syzoj.config.renderer_cache_size)]); |
||||||
|
|
||||||
const resolver = {}; |
const resolver = {}; |
||||||
let currentId = 0; |
let currentId = 0; |
||||||
|
|
||||||
rendererd.on('message', msg => { |
rendererd.on('message', msg => { |
||||||
resolver[msg.id](msg.result); |
resolver[msg.id](msg.result); |
||||||
delete resolver[msg.id]; |
delete resolver[msg.id]; |
||||||
}); |
}); |
||||||
|
|
||||||
exports.markdown = (markdownCode, callback) => { |
exports.markdown = (markdownCode, callback) => { |
||||||
resolver[++currentId] = callback; |
resolver[++currentId] = callback; |
||||||
rendererd.send({ |
rendererd.send({ |
||||||
id: currentId, |
id: currentId, |
||||||
type: 'markdown', |
type: 'markdown', |
||||||
source: markdownCode |
source: markdownCode |
||||||
}); |
}); |
||||||
} |
} |
||||||
|
|
||||||
exports.highlight = (code, lang, callback) => { |
exports.highlight = (code, lang, callback) => { |
||||||
resolver[++currentId] = callback; |
resolver[++currentId] = callback; |
||||||
rendererd.send({ |
rendererd.send({ |
||||||
id: currentId, |
id: currentId, |
||||||
type: 'highlight', |
type: 'highlight', |
||||||
source: { |
source: { |
||||||
code, |
code, |
||||||
lang |
lang |
||||||
} |
} |
||||||
}); |
}); |
||||||
} |
} |
||||||
|
@ -1,61 +1,63 @@ |
|||||||
const renderer = require('syzoj-renderer'); |
const renderer = require('syzoj-renderer'); |
||||||
const XSS = require('xss'); |
const XSS = require('xss'); |
||||||
const xssWhiteList = Object.assign({}, require('xss/lib/default').whiteList); |
const xssWhiteList = Object.assign({}, require('xss/lib/default').whiteList); |
||||||
delete xssWhiteList.audio; |
delete xssWhiteList.audio; |
||||||
delete xssWhiteList.video; |
delete xssWhiteList.video; |
||||||
|
|
||||||
for (const tag in xssWhiteList) { |
for (const tag in xssWhiteList) { |
||||||
xssWhiteList[tag] = xssWhiteList[tag].concat(['style', 'class']); |
xssWhiteList[tag] = xssWhiteList[tag].concat(['style', 'class']); |
||||||
} |
} |
||||||
|
|
||||||
const xss = new XSS.FilterXSS({ |
const xss = new XSS.FilterXSS({ |
||||||
whiteList: xssWhiteList, |
whiteList: xssWhiteList, |
||||||
stripIgnoreTag: true, |
stripIgnoreTag: true, |
||||||
onTagAttr: (tag, name, value, isWhiteAttr) => { |
onTagAttr: (tag, name, value, isWhiteAttr) => { |
||||||
if (tag.toLowerCase() === 'img' && name.toLowerCase() === 'src' && value.startsWith('data:image/')) { |
if (tag.toLowerCase() === 'img' && name.toLowerCase() === 'src' && value.startsWith('data:image/')) { |
||||||
return name + '="' + XSS.escapeAttrValue(value) + '"'; |
return name + '="' + XSS.escapeAttrValue(value) + '"'; |
||||||
} |
} |
||||||
} |
} |
||||||
}); |
}); |
||||||
|
|
||||||
const Redis = require('redis'); |
const Redis = require('redis'); |
||||||
const util = require('util'); |
const RedisLRU = require('redis-lru'); |
||||||
const redis = Redis.createClient(process.argv[2]); |
const util = require('util'); |
||||||
const redisCache = { |
const redis = Redis.createClient(process.argv[2]); |
||||||
get: util.promisify(redis.get).bind(redis), |
const redisLru = RedisLRU(redis, parseInt(process.argv[3])); |
||||||
set: util.promisify(redis.set).bind(redis) |
const redisCache = { |
||||||
}; |
get: redisLru.get.bind(redisLru), |
||||||
|
set: redisLru.set.bind(redisLru) |
||||||
async function highlight(code, lang) { |
}; |
||||||
return await renderer.highlight(code, lang, redisCache, { |
|
||||||
wrapper: null |
async function highlight(code, lang) { |
||||||
}); |
return await renderer.highlight(code, lang, redisCache, { |
||||||
} |
wrapper: null |
||||||
|
}); |
||||||
async function markdown(markdownCode) { |
} |
||||||
function filter(html) { |
|
||||||
html = xss.process(html); |
async function markdown(markdownCode) { |
||||||
if (html) { |
function filter(html) { |
||||||
html = `<div style="position: relative; overflow: hidden; ">${html}</div>`; |
html = xss.process(html); |
||||||
} |
if (html) { |
||||||
return html; |
html = `<div style="position: relative; overflow: hidden; ">${html}</div>`; |
||||||
}; |
} |
||||||
|
return html; |
||||||
return await renderer.markdown(markdownCode, redisCache, filter); |
}; |
||||||
} |
|
||||||
|
return await renderer.markdown(markdownCode, redisCache, filter); |
||||||
process.on('message', async msg => { |
} |
||||||
if (msg.type === 'markdown') { |
|
||||||
process.send({ |
process.on('message', async msg => { |
||||||
id: msg.id, |
if (msg.type === 'markdown') { |
||||||
result: await markdown(msg.source) |
process.send({ |
||||||
}); |
id: msg.id, |
||||||
} else if (msg.type === 'highlight') { |
result: await markdown(msg.source) |
||||||
process.send({ |
}); |
||||||
id: msg.id, |
} else if (msg.type === 'highlight') { |
||||||
result: await highlight(msg.source.code, msg.source.lang) |
process.send({ |
||||||
}); |
id: msg.id, |
||||||
} |
result: await highlight(msg.source.code, msg.source.lang) |
||||||
}); |
}); |
||||||
|
} |
||||||
process.on('disconnect', () => process.exit()); |
}); |
||||||
|
|
||||||
|
process.on('disconnect', () => process.exit()); |
||||||
|
Loading…
Reference in new issue