File size: 1,560 Bytes
69b897d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const logger = require('../../utils/logger')
const { CLIENT_DEFINITIONS } = require('../clientDefinitions')

/**
 * Droid CLI 验证器
 * 检查请求是否来自 Factory Droid CLI
 */
class DroidCliValidator {
  static getId() {
    return CLIENT_DEFINITIONS.DROID_CLI.id
  }

  static getName() {
    return CLIENT_DEFINITIONS.DROID_CLI.name
  }

  static getDescription() {
    return CLIENT_DEFINITIONS.DROID_CLI.description
  }

  static validate(req) {
    try {
      const userAgent = req.headers['user-agent'] || ''
      const factoryClientHeader = (req.headers['x-factory-client'] || '').toString().toLowerCase()

      const uaMatch = /factory-cli\/(\d+\.\d+\.\d+)/i.exec(userAgent)
      const hasFactoryClientHeader =
        typeof factoryClientHeader === 'string' &&
        (factoryClientHeader.includes('droid') || factoryClientHeader.includes('factory-cli'))

      if (!uaMatch && !hasFactoryClientHeader) {
        logger.debug(`Droid CLI validation failed - UA mismatch: ${userAgent}`)
        return false
      }

      // 允许,通过基础验证
      logger.debug(
        `Droid CLI validation passed (UA: ${userAgent || 'N/A'}, header: ${factoryClientHeader || 'N/A'})`
      )
      return true
    } catch (error) {
      logger.error('Error in DroidCliValidator:', error)
      return false
    }
  }

  static getInfo() {
    return {
      id: this.getId(),
      name: this.getName(),
      description: this.getDescription(),
      icon: CLIENT_DEFINITIONS.DROID_CLI.icon
    }
  }
}

module.exports = DroidCliValidator