|
|
const logger = require('../../utils/logger') |
|
|
const { CLIENT_DEFINITIONS } = require('../clientDefinitions') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GeminiCliValidator { |
|
|
|
|
|
|
|
|
|
|
|
static getId() { |
|
|
return CLIENT_DEFINITIONS.GEMINI_CLI.id |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static getName() { |
|
|
return CLIENT_DEFINITIONS.GEMINI_CLI.name |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static getDescription() { |
|
|
return CLIENT_DEFINITIONS.GEMINI_CLI.description |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static getIcon() { |
|
|
return CLIENT_DEFINITIONS.GEMINI_CLI.icon || '💎' |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static validate(req) { |
|
|
try { |
|
|
const userAgent = req.headers['user-agent'] || '' |
|
|
const path = req.originalUrl || '' |
|
|
|
|
|
|
|
|
if (!path.startsWith('/gemini')) { |
|
|
|
|
|
return false |
|
|
} |
|
|
|
|
|
|
|
|
if (path.includes('generateContent')) { |
|
|
|
|
|
const geminiCliPattern = /^GeminiCLI\/v?[\d\.]+/i |
|
|
if (!geminiCliPattern.test(userAgent)) { |
|
|
logger.debug( |
|
|
`Gemini CLI validation failed - UA mismatch for generateContent: ${userAgent}` |
|
|
) |
|
|
return false |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
logger.debug(`Gemini CLI validation passed for path: ${path}`) |
|
|
return true |
|
|
} catch (error) { |
|
|
logger.error('Error in GeminiCliValidator:', error) |
|
|
|
|
|
return false |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static compareVersions(v1, v2) { |
|
|
const parts1 = v1.split('.').map(Number) |
|
|
const parts2 = v2.split('.').map(Number) |
|
|
|
|
|
for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) { |
|
|
const part1 = parts1[i] || 0 |
|
|
const part2 = parts2[i] || 0 |
|
|
|
|
|
if (part1 < part2) return -1 |
|
|
if (part1 > part2) return 1 |
|
|
} |
|
|
|
|
|
return 0 |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static getInfo() { |
|
|
return { |
|
|
id: this.getId(), |
|
|
name: this.getName(), |
|
|
description: this.getDescription(), |
|
|
icon: CLIENT_DEFINITIONS.GEMINI_CLI.icon |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
module.exports = GeminiCliValidator |
|
|
|