palapalapla/GT-Manual
0
1import lodash from 'lodash'2 3let tmp = {}4 5export class GTest extends plugin {6 constructor () {7 super({8 name: 'GTest',9 dsc: '手动过码',10 route: '/GTest',11 rule: [12 {13 method: 'get',14 path: '/:key',15 fnc: 'index'16 },17 {18 method: 'get',19 path: '/validate/:key',20 fnc: 'result'21 },22 {23 method: 'get',24 path: '/register/:key',25 fnc: 'get_register'26 },27 {28 method: 'post',29 path: '/register',30 fnc: 'register'31 },32 {33 method: 'post',34 path: '/validate/:key',35 fnc: 'validate'36 }37 ]38 })39 }40 41 index () {42 let { key } = this.params43 if (!key || !tmp[key]) return this.error('验证信息不存在或已失效。')44 this.render('GTest/main', { key, copyright: this.cfg.Copyright })45 }46 47 register () {48 if (this.cfg.Key && this.query.key !== this.cfg.Key) return this._send(null, 'please enter the correct key')49 let { gt, challenge } = this.body || {}50 if (!gt || !challenge) return this.error()51 let key = this.randomKey(6, tmp, { register: this.params })52 setTimeout(() => delete tmp[key], 150000)53 this._send({54 link: `${this.address}/${key}`,55 result: `${this.address}/validate/${key}`56 })57 }58 59 async result () {60 let { key } = this.params61 if (!key) return this.error()62 63 let data = null64 if (tmp[key]) {65 for (let i = 0; i < 240; i++) {66 let result = tmp[key]?.result67 if (result) {68 data = result69 break70 }71 await this.sleep(500)72 }73 if (!data) data = {}74 delete tmp[key]75 }76 this._send(data)77 }78 79 /** 浏览器返回Validate */80 validate () {81 let { key } = this.params82 if (!key || !tmp[key]) return this.error()83 tmp[key].result = this.body84 setTimeout(() => delete tmp[key], 30000)85 this._send({})86 logger.info(`[GTest] 验证成功, KEY: ${key}`)87 }88 89 /** 浏览器获取gt参数 */90 get_register () {91 let { key } = this.params92 if (!key || !tmp[key]) return this.error()93 let info = tmp[key].register94 if (!info) return this._send(null, '该验证信息已被使用,若非本人操作请重新获取')95 this.send(info)96 delete tmp[key].register97 }98 99 _send (data, message = 'OK') {100 return this.send({101 status: Number(!data),102 message,103 data104 })105 }106 107 get cfg () {108 return Server.cfg.getConfig('GTest')109 }110 111 get address () {112 let { Host, Key } = this.cfg113 let protocol = Server.cfg.http.HTTPS ? 'https' : 'http'114 let port = Server.cfg.http_listen[0]115 if (![80, 443].includes(port)) Host += `:${port}`116 return `${protocol}://${Host}${this.route}`117 }118 119 randomKey (length, checkObj, data) {120 let letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'121 let key = lodash.sampleSize(letters, length).join('')122 while (checkObj[key]) {123 key = lodash.sampleSize(letters, length).join('')124 }125 checkObj[key] = data || {}126 return key127 }128}