| import express from 'express'; |
| import fetch from 'node-fetch'; |
| import dotenv from 'dotenv'; |
| import cors from 'cors'; |
| import { launch } from 'puppeteer'; |
| import { v4 as uuidv4 } from 'uuid'; |
| import path from 'path'; |
| import fs from 'fs'; |
|
|
| dotenv.config(); |
| |
| const CONFIG = { |
| MODELS: { |
| 'grok-latest': 'grok-latest', |
| 'grok-latest-image': 'grok-latest' |
| }, |
| API: { |
| BASE_URL: "https://grok.com", |
| API_KEY: process.env.API_KEY || "sk-123456", |
| SSO_TOKEN: null, |
| SIGNATURE_COOKIE: null |
| }, |
| SERVER: { |
| PORT: process.env.PORT || 3000, |
| BODY_LIMIT: '5mb' |
| }, |
| RETRY: { |
| MAX_ATTEMPTS: 3, |
| DELAY_BASE: 1000 |
| }, |
| CHROME_PATH: process.env.CHROME_PATH || 'C:/Program Files/Google/Chrome/Application/chrome.exe' |
| }; |
|
|
|
|
| |
| const DEFAULT_HEADERS = { |
| 'accept': '*/*', |
| 'accept-language': 'zh-CN,zh;q=0.9', |
| 'accept-encoding': 'gzip, deflate, br, zstd', |
| 'content-type': 'text/plain;charset=UTF-8', |
| 'Connection': 'keep-alive', |
| 'origin': 'https://grok.com', |
| 'priority': 'u=1, i', |
| 'sec-ch-ua': '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"', |
| 'sec-ch-ua-mobile': '?0', |
| 'sec-ch-ua-platform': '"Windows"', |
| 'sec-fetch-dest': 'empty', |
| 'sec-fetch-mode': 'cors', |
| 'sec-fetch-site': 'same-origin', |
| 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36', |
| 'baggage': 'sentry-public_key=b311e0f2690c81f25e2c4cf6d4f7ce1c' |
| }; |
|
|
| |
| const SIGNATURE_FILE_PATH = '/tmp/signature.json'; |
|
|
| class Utils { |
| static async extractGrokHeaders() { |
| try { |
| |
| const browser = await launch({ |
| executablePath: CONFIG.CHROME_PATH, |
| headless: true |
| }); |
|
|
| const page = await browser.newPage(); |
| await page.goto(`${CONFIG.API.BASE_URL}`, { waitUntil: 'networkidle0' }); |
|
|
| |
| const cookies = await page.cookies(); |
| const targetHeaders = ['x-anonuserid', 'x-challenge', 'x-signature']; |
| const extractedHeaders = {}; |
| |
| for (const cookie of cookies) { |
| |
| if (targetHeaders.includes(cookie.name.toLowerCase())) { |
| extractedHeaders[cookie.name.toLowerCase()] = cookie.value; |
| } |
| } |
| |
| await browser.close(); |
| |
| console.log('提取的头信息:', JSON.stringify(extractedHeaders, null, 2)); |
| return extractedHeaders; |
|
|
| } catch (error) { |
| console.error('获取头信息出错:', error); |
| return null; |
| } |
| } |
| static async get_signature() { |
| console.log("刷新认证信息"); |
| let retryCount = 0; |
| while (retryCount < CONFIG.RETRY.MAX_ATTEMPTS) { |
| let headers = await Utils.extractGrokHeaders(); |
| if (headers) { |
| console.log("获取认证信息成功"); |
| CONFIG.API.SIGNATURE_COOKIE = { cookie: `x-anonuserid=${headers["x-anonuserid"]}; x-challenge=${headers["x-challenge"]}; x-signature=${headers["x-signature"]}` }; |
| try { |
| fs.writeFileSync(SIGNATURE_FILE_PATH, JSON.stringify(CONFIG.API.SIGNATURE_COOKIE)); |
| return CONFIG.API.SIGNATURE_COOKIE; |
| } catch (error) { |
| console.error('写入签名文件失败:', error); |
| return CONFIG.API.SIGNATURE_COOKIE; |
| } |
| } |
| retryCount++; |
| if (retryCount >= CONFIG.RETRY.MAX_ATTEMPTS) { |
| throw new Error(`获取认证信息失败!`); |
| } |
| await new Promise(resolve => setTimeout(resolve, CONFIG.RETRY.DELAY_BASE * retryCount)); |
| } |
| } |
| static async retryRequestWithNewSignature(originalRequest) { |
| console.log("认证信息已过期,尝试刷新认证信息并重新请求~"); |
| try { |
| |
| await Utils.get_signature(); |
|
|
| |
| return await originalRequest(); |
| } catch (error) { |
| |
| throw new Error(`重试请求失败: ${error.message}`); |
| } |
| } |
| static async handleError(error, res, originalRequest = null) { |
| console.error('Error:', error); |
|
|
| |
| if (error.status === 500 && originalRequest) { |
| try { |
| const result = await Utils.retryRequestWithNewSignature(originalRequest); |
| return result; |
| } catch (retryError) { |
| console.error('重试失败:', retryError); |
| return res.status(500).json({ |
| error: { |
| message: retryError.message, |
| type: 'server_error', |
| param: null, |
| code: retryError.code || null |
| } |
| }); |
| } |
| } |
|
|
| |
| res.status(500).json({ |
| error: { |
| message: error.message, |
| type: 'server_error', |
| param: null, |
| code: error.code || null |
| } |
| }); |
| } |
|
|
| static async makeRequest(req, res) { |
| try { |
| if (!CONFIG.API.SIGNATURE_COOKIE) { |
| await Utils.get_signature(); |
| try { |
| CONFIG.API.SIGNATURE_COOKIE = JSON.parse(fs.readFileSync(SIGNATURE_FILE_PATH)); |
| } catch (error) { |
| console.error('读取签名文件失败:', error); |
| await Utils.get_signature(); |
| } |
| } |
| const grokClient = new GrokApiClient(req.body.model); |
| const requestPayload = await grokClient.prepareChatRequest(req.body); |
| |
| const newMessageReq = await fetch(`${CONFIG.API.BASE_URL}/api/rpc`, { |
| method: 'POST', |
| headers: { |
| ...DEFAULT_HEADERS, |
| ...CONFIG.API.SIGNATURE_COOKIE |
| }, |
| body: JSON.stringify({ |
| rpc: "createConversation", |
| req: { |
| temporary: false |
| } |
| }) |
| }); |
|
|
| if (!newMessageReq.ok) { |
| throw new Error(`上游服务请求失败! status: ${newMessageReq.status}`); |
| } |
|
|
| |
| const responseText = await newMessageReq.json(); |
| const conversationId = responseText.conversationId; |
| console.log("会话ID:conversationId", conversationId); |
| if (!conversationId) { |
| throw new Error(`创建会话失败! status: ${newMessageReq.status}`); |
| } |
| |
| const response = await fetch(`${CONFIG.API.BASE_URL}/api/conversations/${conversationId}/responses`, { |
| method: 'POST', |
| headers: { |
| "accept": "text/event-stream", |
| "baggage": "sentry-public_key=b311e0f2690c81f25e2c4cf6d4f7ce1c", |
| "content-type": "text/plain;charset=UTF-8", |
| "Connection": "keep-alive", |
| ...CONFIG.API.SIGNATURE_COOKIE |
| }, |
| body: JSON.stringify(requestPayload) |
| }); |
|
|
| if (!response.ok) { |
| throw new Error(`上游服务请求失败! status: ${response.status}`); |
| } |
|
|
| if (req.body.stream) { |
| await handleStreamResponse(response, req.body.model, res); |
| } else { |
| await handleNormalResponse(response, req.body.model, res); |
| } |
| } catch (error) { |
| throw error; |
| } |
| } |
| } |
|
|
|
|
| class GrokApiClient { |
| constructor(modelId) { |
| if (!CONFIG.MODELS[modelId]) { |
| throw new Error(`不支持的模型: ${modelId}`); |
| } |
| this.modelId = CONFIG.MODELS[modelId]; |
| } |
|
|
| processMessageContent(content) { |
| if (typeof content === 'string') return content; |
| return null; |
| } |
| |
| getImageType(base64String) { |
| let mimeType = 'image/jpeg'; |
| if (base64String.includes('data:image')) { |
| const matches = base64String.match(/data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+);base64,/); |
| if (matches) { |
| mimeType = matches[1]; |
| } |
| } |
| const extension = mimeType.split('/')[1]; |
| const fileName = `image.${extension}`; |
|
|
| return { |
| mimeType: mimeType, |
| fileName: fileName |
| }; |
| } |
|
|
| async uploadBase64Image(base64Data, url) { |
| try { |
| |
| let imageBuffer; |
| if (base64Data.includes('data:image')) { |
| imageBuffer = base64Data.split(',')[1]; |
| } else { |
| imageBuffer = base64Data |
| } |
| const { mimeType, fileName } = this.getImageType(base64Data); |
| let uploadData = { |
| rpc: "uploadFile", |
| req: { |
| fileName: fileName, |
| fileMimeType: mimeType, |
| content: imageBuffer |
| } |
| }; |
| console.log("发送图片请求"); |
| |
| const response = await fetch(url, { |
| method: 'POST', |
| headers: { |
| ...CONFIG.DEFAULT_HEADERS, |
| ...CONFIG.API.SIGNATURE_COOKIE |
| }, |
| body: JSON.stringify(uploadData) |
| }); |
|
|
| if (!response.ok) { |
| console.error(`上传图片失败,状态码:${response.status},原因:${response.error}`); |
| return ''; |
| } |
|
|
| const result = await response.json(); |
| console.log('上传图片成功:', result); |
| return result.fileMetadataId; |
|
|
| } catch (error) { |
| console.error('上传图片失败:', error); |
| return ''; |
| } |
| } |
|
|
| async prepareChatRequest(request) { |
| const processImageUrl = async (content) => { |
| if (content.type === 'image_url' && content.image_url.url.includes('data:image')) { |
| const imageResponse = await this.uploadBase64Image( |
| content.image_url.url, |
| `${CONFIG.API.BASE_URL}/api/rpc` |
| ); |
| return imageResponse; |
| } |
| return null; |
| }; |
| let fileAttachments = []; |
| let messages = ''; |
| let lastRole = null; |
| let lastContent = ''; |
|
|
| for (const current of request.messages) { |
| const role = current.role === 'assistant' ? 'assistant' : 'user'; |
| let textContent = ''; |
| |
| if (Array.isArray(current.content)) { |
| |
| for (const item of current.content) { |
| if (item.type === 'image_url') { |
| |
| if (current === request.messages[request.messages.length - 1]) { |
| const processedImage = await processImageUrl(item); |
| if (processedImage) fileAttachments.push(processedImage); |
| } |
| textContent += (textContent ? '\n' : '') + "[图片]"; |
| } else if (item.type === 'text') { |
| textContent += (textContent ? '\n' : '') + item.text; |
| } |
| } |
| } else if (typeof current.content === 'object' && current.content !== null) { |
| |
| if (current.content.type === 'image_url') { |
| |
| if (current === request.messages[request.messages.length - 1]) { |
| const processedImage = await processImageUrl(current.content); |
| if (processedImage) fileAttachments.push(processedImage); |
| } |
| textContent += (textContent ? '\n' : '') + "[图片]"; |
| } else if (current.content.type === 'text') { |
| textContent = current.content.text; |
| } |
| } else { |
| |
| textContent = this.processMessageContent(current.content); |
| } |
| |
| if (textContent) { |
| if (role === lastRole) { |
| |
| lastContent += '\n' + textContent; |
| messages = messages.substring(0, messages.lastIndexOf(`${role.toUpperCase()}: `)) + |
| `${role.toUpperCase()}: ${lastContent}\n`; |
| } else { |
| |
| messages += `${role.toUpperCase()}: ${textContent}\n`; |
| lastContent = textContent; |
| lastRole = role; |
| } |
| } else if (current === request.messages[request.messages.length - 1] && fileAttachments.length > 0) { |
| |
| messages += `${role.toUpperCase()}: [图片]\n`; |
| } |
| } |
|
|
| if (fileAttachments.length > 4) { |
| fileAttachments = fileAttachments.slice(0, 4); |
| } |
|
|
| messages = messages.trim(); |
|
|
| return { |
| message: messages, |
| modelName: this.modelId, |
| disableSearch: false, |
| imageAttachments: [], |
| returnImageBytes: false, |
| returnRawGrokInXaiRequest: false, |
| fileAttachments: fileAttachments, |
| enableImageStreaming: false, |
| imageGenerationCount: 1, |
| toolOverrides: { |
| imageGen: request.model === 'grok-latest-image', |
| webSearch: false, |
| xSearch: false, |
| xMediaSearch: false, |
| trendsSearch: false, |
| xPostAnalyze: false |
| } |
| }; |
| } |
| } |
|
|
| class MessageProcessor { |
| static createChatResponse(message, model, isStream = false) { |
| const baseResponse = { |
| id: `chatcmpl-${uuidv4()}`, |
| created: Math.floor(Date.now() / 1000), |
| model: model |
| }; |
|
|
| if (isStream) { |
| return { |
| ...baseResponse, |
| object: 'chat.completion.chunk', |
| choices: [{ |
| index: 0, |
| delta: { |
| content: message |
| } |
| }] |
| }; |
| } |
|
|
| |
| const messageContent = Array.isArray(message) ? message : message; |
|
|
| return { |
| ...baseResponse, |
| object: 'chat.completion', |
| choices: [{ |
| index: 0, |
| message: { |
| role: 'assistant', |
| content: messageContent |
| }, |
| finish_reason: 'stop' |
| }], |
| usage: null |
| }; |
| } |
| } |
|
|
| |
| const app = express(); |
| app.use(express.json({ limit: '5mb' })); |
| app.use(express.urlencoded({ extended: true, limit: '5mb' })); |
| app.use(cors({ |
| origin: '*', |
| methods: ['GET', 'POST', 'OPTIONS'], |
| allowedHeaders: ['Content-Type', 'Authorization'] |
| })); |
| |
| app.get('/hf/v1/models', (req, res) => { |
| res.json({ |
| object: "list", |
| data: Object.keys(CONFIG.MODELS).map((model, index) => ({ |
| id: model, |
| object: "model", |
| created: Math.floor(Date.now() / 1000), |
| owned_by: "xai", |
| })) |
| }); |
| }); |
|
|
| app.post('/hf/v1/chat/completions', async (req, res) => { |
| const authToken = req.headers.authorization?.replace('Bearer ', ''); |
| if (authToken !== CONFIG.API.API_KEY) { |
| return res.status(401).json({ error: 'Unauthorized' }); |
| } |
|
|
| try { |
| await Utils.makeRequest(req, res); |
| } catch (error) { |
| await Utils.handleError(error, res); |
| } |
| }); |
|
|
| async function handleStreamResponse(response, model, res) { |
| res.setHeader('Content-Type', 'text/event-stream'); |
| res.setHeader('Cache-Control', 'no-cache'); |
| res.setHeader('Connection', 'keep-alive'); |
|
|
| try { |
| |
| const responseText = await response.text(); |
| const lines = responseText.split('\n'); |
|
|
| for (const line of lines) { |
| if (!line.startsWith('data: ')) continue; |
|
|
| const data = line.slice(6); |
| if (data === '[DONE]') { |
| res.write('data: [DONE]\n\n'); |
| continue; |
| } |
| console.log("data", data); |
| let linejosn = JSON.parse(data); |
| if (linejosn.response === "token" && model == "grok-latest") { |
| const token = linejosn.token; |
| if (token && token.length > 0) { |
| const responseData = MessageProcessor.createChatResponse(token, model, true); |
| res.write(`data: ${JSON.stringify(responseData)}\n\n`); |
| } |
| } else if (linejosn.response === "modelResponse" && model === 'grok-latest-image') { |
| if (linejosn?.modelResponse?.generatedImageUrls?.length > 0) { |
| imageUrl = linejosn.modelResponse.generatedImageUrls[0]; |
| const dataImage = await handleImageResponse(imageUrl); |
| const responseData = MessageProcessor.createChatResponse(dataImage, model, true); |
| res.write(`data: ${JSON.stringify(responseData)}\n\n`); |
| } |
| } |
| } |
|
|
| res.end(); |
| } catch (error) { |
| Utils.handleError(error, res); |
| } |
| } |
|
|
| async function handleNormalResponse(response, model, res) { |
| let fullResponse = ''; |
| let imageUrl = ''; |
|
|
| try { |
| const responseText = await response.text(); |
| console.log('原始响应文本:', responseText); |
| const lines = responseText.split('\n'); |
|
|
| for (const line of lines) { |
| if (!line.startsWith('data: ')) continue; |
|
|
| const data = line.slice(6); |
| if (data === '[DONE]') continue; |
| let linejosn = JSON.parse(data); |
| console.log('解析的JSON数据:', linejosn); |
| |
| if (linejosn.response === "token") { |
| const token = linejosn.token; |
| if (token && token.length > 0) { |
| fullResponse += token; |
| } |
| } else if (linejosn.response === "modelResponse" && model === 'grok-latest-image') { |
| console.log('检测到图片响应:', linejosn.modelResponse); |
| if (linejosn?.modelResponse?.generatedImageUrls?.length > 0) { |
| imageUrl = linejosn.modelResponse.generatedImageUrls[0]; |
| console.log('获取到图片URL:', imageUrl); |
| } |
| } |
| } |
|
|
| if (imageUrl) { |
| console.log('开始处理图片URL:', imageUrl); |
| const dataImage = await handleImageResponse(imageUrl); |
| |
| const responseData = MessageProcessor.createChatResponse([dataImage], model); |
| console.log('最终的响应对象:', JSON.stringify(responseData, null, 2)); |
| res.json(responseData); |
| } else { |
| console.log('没有图片URL,返回文本响应:', fullResponse); |
| const responseData = MessageProcessor.createChatResponse(fullResponse, model); |
| res.json(responseData); |
| } |
| } catch (error) { |
| console.error('处理响应时发生错误:', error); |
| Utils.handleError(error, res); |
| } |
| } |
|
|
| async function handleImageResponse(imageUrl) { |
| try { |
| |
| const MAX_RETRIES = 3; |
| let retryCount = 0; |
| let imageBase64Response; |
|
|
| while (retryCount < MAX_RETRIES) { |
| try { |
| |
| imageBase64Response = await fetch(`https://assets.grok.com/${imageUrl}`, { |
| method: 'GET', |
| headers: { |
| ...DEFAULT_HEADERS, |
| ...CONFIG.API.SIGNATURE_COOKIE |
| } |
| }); |
|
|
| if (imageBase64Response.ok) { |
| break; |
| } |
|
|
| retryCount++; |
| if (retryCount === MAX_RETRIES) { |
| throw new Error(`上游服务请求失败! status: ${imageBase64Response.status}`); |
| } |
|
|
| |
| await new Promise(resolve => setTimeout(resolve, 500 * retryCount)); |
|
|
| } catch (error) { |
| retryCount++; |
| if (retryCount === MAX_RETRIES) { |
| throw error; |
| } |
| |
| await new Promise(resolve => setTimeout(resolve, 500 * retryCount)); |
| } |
| } |
|
|
| const arrayBuffer = await imageBase64Response.arrayBuffer(); |
| const imagebuffer = Buffer.from(arrayBuffer); |
|
|
| const base64String = imagebuffer.toString('base64'); |
|
|
| |
| const imageContentType = imageBase64Response.headers.get('content-type'); |
|
|
| |
| return { |
| type: "image_url", |
| image_url: { |
| url: `data:image/jpg;base64,${base64String}` |
| } |
| }; |
|
|
| |
| |
| } catch (error) { |
| console.error('图片处理失败:', error); |
| return '图片生成失败'; |
| } |
| } |
|
|
| |
| app.use((req, res) => { |
| res.status(404).send('请求路径不存在'); |
| }); |
|
|
| |
| app.listen(CONFIG.SERVER.PORT, () => { |
| console.log(`服务器已启动,监听端口: ${CONFIG.SERVER.PORT}`); |
| }); |