Add captcha logger to captcha/audio

This commit is contained in:
Jerko Steiner 2019-11-04 11:10:06 -04:00
parent 91c02c9b66
commit 9ddbad9bfb
3 changed files with 15 additions and 18 deletions

View File

@ -1,9 +1,12 @@
import loggerFactory from '@rondo.dev/logger'
import { Request, Response } from 'express'
import SVGCaptcha from 'svg-captcha'
import { Command, ReadableProcess, ReadableWritable, run } from './run'
import { TextStream } from './TextStream'
import { createCaptcha } from './Captcha'
const logger = loggerFactory.getLogger('captcha')
export interface AudioConfig {
commands: Command[]
size: number
@ -18,14 +21,22 @@ export const audio = (config: AudioConfig) => async (
req.session!.captcha = createCaptcha(captcha, 'audio')
let speech: ReadableProcess
try {
speech = await speak('test', commands)
speech = await speak(captcha, commands)
} catch (err) {
res.status(500)
logger.error('Error generating speech: %s', err.stack)
res.status(200)
res.send('Internal server error')
return
}
res.status(200)
res.header('Cache-control', 'no-cache')
res.type(speech.contentType)
speech.stdout.pipe(res)
logger.info('piping output')
speech.stdout.pipe(res, { end: false })
speech.stdout.on('end', () => {
logger.info('end')
res.end()
})
}
export async function speak(

View File

@ -1,15 +0,0 @@
export function espeak() {
return {
cmd: 'espeak',
args: ['-k', '2', '-s', '90', '--stdin', '--stdout'],
contentType: 'audio/wav',
}
}
export function opus() {
return {
cmd: 'opusenc',
args: ['-', '-'],
contentType: 'audio/opus',
}
}

View File

@ -11,6 +11,7 @@ export const image = (config: ImageConfig) => (req: Request, res: Response) => {
size: config.size,
})
req.session!.captcha = createCaptcha(text, 'image')
res.header('Cache-control', 'no-cache')
res.type('svg')
res.status(200)
res.send(data)