This repository has been archived by the owner on Jun 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rewrite login & config, impl cookie helper
- Loading branch information
Showing
11 changed files
with
433 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const schoolEdgeCases = { | ||
宁波大学: { | ||
formIdx: 0, | ||
rememberMe: 'on', | ||
cookiePath: '/', | ||
checkCaptchaPath: '/needCaptcha.html', | ||
getCaptchaPath: '/captcha.html', | ||
}, | ||
} | ||
|
||
// we will using proxy to get the default properties | ||
const defaultProps = { | ||
rememberMe: true, | ||
getCaptchaPath: '/getCaptcha.htl', | ||
checkCaptchaPath: '/checkNeedCaptcha.htl', | ||
cookiePath: '/authserver', | ||
formIdx: 2, | ||
pwdEncrypt: true, | ||
} | ||
|
||
const iapDefaultProps = { | ||
lt: '/security/lt', | ||
rememberMe: true, | ||
checkCaptchaPath: '/checkNeedCaptcha', | ||
getCaptchaPath: '/generateCaptcha', | ||
} | ||
|
||
export type EdgeCasesSchools = keyof typeof schoolEdgeCases | ||
type NoIapDefaultProps = typeof defaultProps | ||
type IapDefaultProps = typeof iapDefaultProps | ||
|
||
export type DefaultProps = NoIapDefaultProps & IapDefaultProps | ||
|
||
/** | ||
* handle edge cases, proxy default properties | ||
*/ | ||
export default (schoolName: EdgeCasesSchools, isIap: boolean) => | ||
schoolName | ||
? (new Proxy(schoolEdgeCases[schoolName] || {}, { | ||
get(target, prop, receiver) { | ||
if ( | ||
target[prop as keyof NoIapDefaultProps & keyof IapDefaultProps] === | ||
undefined | ||
) { | ||
return isIap | ||
? iapDefaultProps[prop as keyof IapDefaultProps] | ||
: defaultProps[prop as keyof NoIapDefaultProps] | ||
} | ||
return Reflect.get(target, prop, receiver) | ||
}, | ||
}) as unknown as DefaultProps) | ||
: ({} as DefaultProps) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { createWorker } from 'tesseract.js' | ||
import fetch from 'node-fetch' | ||
import fs from 'fs' | ||
const tessdataPath = '/tmp/eng.traineddata.gz' | ||
|
||
async function downloadTessdata() { | ||
process.env.TESSDATA_PREFIX = '/tmp' | ||
// check folder exists | ||
if (!fs.existsSync('/tmp')) { | ||
fs.mkdirSync('/tmp') | ||
} else { | ||
// check file exists | ||
if (fs.existsSync(tessdataPath)) { | ||
return | ||
} | ||
} | ||
download( | ||
'https://beetcb.gitee.io/filetransfer/tmp/eng.traineddata.gz', | ||
tessdataPath | ||
) | ||
} | ||
|
||
async function download(url: string, filename: string): string { | ||
const stream = fs.createWriteStream(filename) | ||
const res = await fetch(url) | ||
const result = await new Promise((resolve, reject) => { | ||
res.body.pipe(stream) | ||
res.body.on('error', reject) | ||
stream.on('close', () => resolve(`Downloaded tess data as ${filename}`)) | ||
}) | ||
return result as string | ||
} | ||
|
||
async function ocr(captchaUrl: string) { | ||
await downloadTessdata() | ||
const worker = createWorker({ | ||
langPath: '/tmp', | ||
cachePath: '/tmp', | ||
}) | ||
await worker.load() | ||
await worker.loadLanguage('eng') | ||
await worker.initialize('eng') | ||
await worker.setParameters({ | ||
tessedit_char_whitelist: | ||
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', | ||
}) | ||
const { | ||
data: { text }, | ||
} = await worker.recognize(captchaUrl) | ||
await worker.terminate() | ||
return text | ||
} | ||
|
||
export default ocr |
Oops, something went wrong.