Download server.js from Norsec93/Zydit1: direct link, hf CLI and curl.
- Browser
- Download file 4.39 kB
-
https://huggingface.co/spaces/Norsec93/Zydit1/resolve/main/server.js
- Command line
-
hf download hf://spaces/Norsec93/Zydit1/server.js
-
curl -L -o server.js https://huggingface.co/spaces/Norsec93/Zydit1/resolve/main/server.js
4.39 kB
| const express = require('express'); | |
| const axios = require('axios'); | |
| const cors = require('cors'); | |
| const app = express(); | |
| const PORT = 3000; | |
| // Zydit V1 completions route | |
| const ZYDIT_V1_URL = 'https://api.zydit.in/v1/chat/completions'; | |
| const MODEL_MAPPING = { | |
| 'gpt-4': 'z-ai/glm5.1', | |
| 'gpt-4o': 'z-ai/glm5.1', | |
| 'gpt-3.5-turbo': 'z-ai/glm5.1' | |
| }; | |
| app.use(cors()); | |
| app.use(express.json({ limit: '50mb' })); | |
| app.use(express.urlencoded({ limit: '50mb', extended: true })); | |
| app.get('*', (req, res) => { | |
| res.status(200).json({ status: "alive", message: "GLM 5.1 Shield Active on Port 3000." }); | |
| }); | |
| app.post('*', async (req, res) => { | |
| if (!req.body || !req.body.messages) { | |
| return res.status(200).json({ status: "alive", message: "Endpoint reached." }); | |
| } | |
| if (!process.env.ZYDIT_LIVE_KEY) { | |
| return res.status(500).json({ | |
| error: { message: "Proxy Configuration Error: ZYDIT_LIVE_KEY secret is missing." } | |
| }); | |
| } | |
| try { | |
| const inboundModel = req.body.model || 'gpt-4'; | |
| const targetModel = MODEL_MAPPING[inboundModel] || 'z-ai/glm5.1'; | |
| const sanitizedMessages = []; | |
| let combinedSystemContent = ""; | |
| (req.body.messages || []).forEach(msg => { | |
| let role = msg.role; | |
| if (role === 'example_user') role = 'user'; | |
| if (role === 'example_assistant') role = 'assistant'; | |
| const content = typeof msg.content === 'string' ? msg.content.trim() : ""; | |
| if (!content) return; | |
| if (role === 'system') { | |
| combinedSystemContent += (combinedSystemContent ? "\n" : "") + content; | |
| } else if (role === 'user' || role === 'assistant') { | |
| if (sanitizedMessages.length > 0 && sanitizedMessages[sanitizedMessages.length - 1].role === role) { | |
| sanitizedMessages[sanitizedMessages.length - 1].content += "\n" + content; | |
| } else { | |
| sanitizedMessages.push({ role, content }); | |
| } | |
| } | |
| }); | |
| if (combinedSystemContent) { | |
| sanitizedMessages.unshift({ role: 'system', content: combinedSystemContent }); | |
| } | |
| const pristineBody = { | |
| model: targetModel, | |
| messages: sanitizedMessages, | |
| stream: req.body.stream ?? true | |
| }; | |
| if (typeof req.body.temperature === 'number') pristineBody.temperature = req.body.temperature; | |
| if (typeof req.body.top_p === 'number') pristineBody.top_p = req.body.top_p; | |
| if (typeof req.body.max_tokens === 'number') pristineBody.max_tokens = req.body.max_tokens; | |
| console.log(`[Proxy 3000] Routing request to GLM 5.1 via masked headers...`); | |
| const response = await axios({ | |
| method: 'post', | |
| url: ZYDIT_V1_URL, | |
| data: pristineBody, | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'Authorization': `Bearer ${process.env.ZYDIT_LIVE_KEY.trim()}`, | |
| // Spoofing a standard desktop browser to bypass Cloudflare anti-bot firewalls | |
| 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36', | |
| 'Accept': '*/*' | |
| }, | |
| responseType: 'stream' | |
| }); | |
| res.setHeader('Content-Type', response.headers['content-type'] || 'text/event-stream'); | |
| response.data.pipe(res); | |
| } catch (error) { | |
| const statusCode = error.response ? error.response.status : 500; | |
| console.error(`[Zydit V1 Error] Status code: ${statusCode}`); | |
| // Log the exact error body from Zydit to help us debug authorization quirks | |
| if (error.response && error.response.data) { | |
| console.error("[Zydit Raw Response Failure Details]:", error.response.data); | |
| } | |
| if (!res.headersSent) { | |
| return res.status(statusCode).json({ | |
| error: { | |
| message: `Zydit V1 API rejected the request with status ${statusCode}.`, | |
| type: "proxy_forward_failure", | |
| code: statusCode | |
| } | |
| }); | |
| } | |
| } | |
| }); | |
| app.listen(PORT, '0.0.0.0', () => { | |
| console.log(`GLM 5.1 browser-masked proxy bridge running on port ${PORT}`); | |
| }); |