File size: 3,134 Bytes
330c0d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import lodash from 'lodash'

let tmp = {}

export class GTest extends plugin {
  constructor () {
    super({
      name: 'GTest',
      dsc: '手动过码',
      route: '/GTest',
      rule: [
        {
          method: 'get',
          path: '/:key',
          fnc: 'index'
        },
        {
          method: 'get',
          path: '/validate/:key',
          fnc: 'result'
        },
        {
          method: 'get',
          path: '/register/:key',
          fnc: 'get_register'
        },
        {
          method: 'post',
          path: '/register',
          fnc: 'register'
        },
        {
          method: 'post',
          path: '/validate/:key',
          fnc: 'validate'
        }
      ]
    })
  }

  index () {
    let { key } = this.params
    if (!key || !tmp[key]) return this.error('验证信息不存在或已失效。')
    this.render('GTest/main', { key, copyright: this.cfg.Copyright })
  }

  register () {
    if (this.cfg.Key && this.query.key !== this.cfg.Key) return this._send(null, 'please enter the correct key')
    let { gt, challenge } = this.body || {}
    if (!gt || !challenge) return this.error()
    let key = this.randomKey(6, tmp, { register: this.params })
    setTimeout(() => delete tmp[key], 150000)
    this._send({
      link: `${this.address}/${key}`,
      result: `${this.address}/validate/${key}`
    })
  }

  async result () {
    let { key } = this.params
    if (!key) return this.error()

    let data = null
    if (tmp[key]) {
      for (let i = 0; i < 240; i++) {
        let result = tmp[key]?.result
        if (result) {
          data = result
          break
        }
        await this.sleep(500)
      }
      if (!data) data = {}
      delete tmp[key]
    }
    this._send(data)
  }

  /** 浏览器返回Validate */
  validate () {
    let { key } = this.params
    if (!key || !tmp[key]) return this.error()
    tmp[key].result = this.body
    setTimeout(() => delete tmp[key], 30000)
    this._send({})
    logger.info(`[GTest] 验证成功, KEY: ${key}`)
  }

  /** 浏览器获取gt参数 */
  get_register () {
    let { key } = this.params
    if (!key || !tmp[key]) return this.error()
    let info = tmp[key].register
    if (!info) return this._send(null, '该验证信息已被使用,若非本人操作请重新获取')
    this.send(info)
    delete tmp[key].register
  }

  _send (data, message = 'OK') {
    return this.send({
      status: Number(!data),
      message,
      data
    })
  }

  get cfg () {
    return Server.cfg.getConfig('GTest')
  }

  get address () {
    let { Host, Key } = this.cfg
    let protocol = Server.cfg.http.HTTPS ? 'https' : 'http'
    let port = Server.cfg.http_listen[0]
    if (![80, 443,7860].includes(port)) Host += `:${port}`
    return `${protocol}://${Host}${this.route}`
  }

  randomKey (length, checkObj, data) {
    let letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
    let key = lodash.sampleSize(letters, length).join('')
    while (checkObj[key]) {
      key = lodash.sampleSize(letters, length).join('')
    }
    checkObj[key] = data || {}
    return key
  }
}