1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| ```
## secrets_of_admin
```js
const readFile = promisify(fs.readFile)
const getCheckSum = (filename: string): Promise<string> => { return new Promise((resolve, reject) => { const shasum = crypto.createHash('md5'); try { const s = fs.createReadStream(path.join(__dirname , "../files/", filename)); s.on('data', (data) => { shasum.update(data) }) s.on('end', () => { return resolve(shasum.digest('hex')); }) } catch (err) { reject(err) } }) }
const checkAuth = (req: Request, res:Response, next:NextFunction) => { let token = req.signedCookies['token'] if (token && token["username"]) { if (token.username === 'superuser'){ next(createError(404)) } if (token.isAdmin === true) { next(); } else { return res.redirect('/') } } else { next(createError(404)); } }
const router = express.Router();
router.get('/', (_, res) => res.render('index', { message: `Only admin's function is implemented. 😖 `}))
router.post('/', async (req, res) => { let { username, password } = req.body; if ( username && password) { if ( username == '' || typeof(username) !== "string" || password == '' || typeof(password) !== "string" ) { return res.render('index', { error: 'Parameters error 👻'}); } let data = await DB.Login(username, password) if(!data) { return res.render('index', { error : 'You are not admin 😤'}); } res.cookie('token', { username: username, isAdmin: true }, { signed: true }) res.redirect('/admin'); } else { return res.render('index', { error : 'Parameters cannot be blank 😒'}); } })
router.get('/admin', checkAuth, async (req, res) => { let token = req.signedCookies['token']; try { const files = await DB.listFile(token.username); if (files) { res.cookie('token', {username: token.username, files: files, isAdmin: true }, { signed: true }) } } catch (err) { return res.render('admin', { error: 'Something wrong ... 👻'}) } return res.render('admin'); });
router.post('/admin', checkAuth, (req, res, next) => { let { content } = req.body; if ( content == '' || content.includes('<') || content.includes('>') || content.includes('/') || content.includes('script') || content.includes('on')){ return res.render('admin', { error: 'Forbidden word 🤬'}); } else { let template = ` <html> <meta charset="utf8"> <title>Create your own pdfs</title> <body> <h3>${content}</h3> </body> </html> ` try { const filename = `${uuid()}.pdf` pdf.create(template, { "format": "Letter", "orientation": "portrait", "border": "0", "type": "pdf", "renderDelay": 3000, "timeout": 5000 }).toFile(`./files/${filename}`, async (err, _) => { if (err) next(createError(500)); const checksum = await getCheckSum(filename); await DB.Create('superuser', filename, checksum) return res.render('admin', { message : `Your pdf is successfully saved 🤑 You know how to download it right?`}); }); } catch (err) { return res.render('admin', { error : 'Failed to generate pdf 😥'}) } } });
router.get('/api/files', async (req, res, next) => { if (req.socket.remoteAddress.replace(/^.*:/, '') != '127.0.0.1') { return next(createError(401)); } let { username , filename, checksum } = req.query; if (typeof(username) == "string" && typeof(filename) == "string" && typeof(checksum) == "string") { try { await DB.Create(username, filename, checksum) return res.send('Done') } catch (err) { return res.send('Error!') } } else { return res.send('Parameters error') } });
router.get('/api/files/:id', async (req, res) => { let token = req.signedCookies['token'] if (token && token['username']) { if (token.username == 'superuser') { return res.send('Superuser is disabled now'); } try { let filename = await DB.getFile(token.username, req.params.id) if (fs.existsSync(path.join(__dirname , "../files/", filename))){ return res.send(await readFile(path.join(__dirname , "../files/", filename))); } else { return res.send('No such file!'); } } catch (err) { return res.send('Error!'); } } else { return res.redirect('/'); } });
export default router;
|