文章总结: 本文介绍Token中转站通过Node.js代理服务器封装第三方API的原理,以火山平台codingplan接口为例演示内网访问方案。核心实现包括注入固定模型参数、处理跨域请求、转发兼容OpenAI格式的API调用,并提供非流式/流式请求测试代码及curl示例。 综合评分: 85 文章分类: 安全工具,应用安全,技术标准,云安全,安全开发
Token 中转站的原理
原创
hyang0 hyang0
生有可恋
2026年4月28日 06:36 湖北
在小说阅读器读本章
去阅读
Token 中转站的原理是封装第三方的 API。
下面演示封装火山的 coding plan 给内网使用。测试请求使用的是兼容 OpenAI 格式的 API 请求。
封装的目的是给其它无法直连互联网的机器用,封装完测试效果如下:
非流式请求:
流式请求:
通过 curl 测试:
curl -X POST http://localhost:8080/api/coding/v3/chat/completions \ -H "Content-Type: application/json" \ -d '{"messages": [{"role": "user", "content": "你好"}]}'
使用时用 node 启动中转程序:
node proxy.js
中转程序代码:
#!/usr/bin/env nodeconst http = require('http');const https = require('https');// 配置 - 内置 Model 和 API Keyconst PROXY_PORT = 8080;const UPSTREAM_HOST = 'ark.cn-beijing.volces.com';const API_KEY = 'ark-api-key';const MODEL = 'ark-code-latest';function handleRequest(req, res) { // 预检请求 if (req.method === 'OPTIONS') { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); res.setHeader('Access-Control-Allow-Headers', 'Authorization, Content-Type'); res.writeHead(204); res.end(); return; } let body = ''; req.on('data', chunk => { body += chunk; }); req.on('end', () => { try { // 解析请求体,注入 model let json = {}; if (body) { json = JSON.parse(body); } json.model = MODEL; // 内置 model const newBody = JSON.stringify(json); // 直接连接上游(不使用代理) const options = { hostname: UPSTREAM_HOST, port: 443, path: req.url, method: req.method, headers: { ...req.headers, 'Host': UPSTREAM_HOST, 'Content-Length': Buffer.byteLength(newBody), 'Authorization': `Bearer ${API_KEY}` // 内置 API Key } }; const upstream = https.request(options, (upstreamRes) => { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); res.setHeader('Access-Control-Allow-Headers', 'Authorization, Content-Type'); res.writeHead(upstreamRes.statusCode, upstreamRes.headers); upstreamRes.on('data', chunk => res.write(chunk)); upstreamRes.on('end', () => res.end()); }); upstream.on('error', (err) => { console.error('Upstream error:', err.message); res.writeHead(502); res.end(JSON.stringify({ error: 'Bad Gateway', message: err.message })); }); upstream.write(newBody); upstream.end(); } catch (e) { res.writeHead(400); res.end(JSON.stringify({ error: 'Invalid JSON', message: e.message })); } });}const server = http.createServer(handleRequest);server.listen(PROXY_PORT, () => { console.log(`🚀 Proxy running at http://localhost:${PROXY_PORT}`); console.log(`📡 Forwarding to https://${UPSTREAM_HOST}`); console.log(`🔑 Model: ${MODEL}`); console.log(`🔐 API Key: ${API_KEY.substring(0, 10)}...`);});%
这里使用的是火山平台的 api 做测试:
Model : ark-code-latestAPI Key : ark-api-keyUpStream : https://ark.cn-beijing.volces.com
请求的是火山的 coding plan 的 API:
https://ark.cn-beijing.volces.com/api/coding/v3
免责声明:
本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。
任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。
本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我。
本文转载自:生有可恋 hyang0 hyang0《Token 中转站的原理》
版权声明
本站仅做备份收录,仅供研究与教学参考之用。
读者将信息用于其他用途的,全部法律及连带责任由读者自行承担,本站不承担任何责任。







![暗网泄露:[2026]CARGURUS数据库1200万信息](/images/random/titlepic/14.jpg)


评论