├── LICENSE ├── README.md ├── img ├── cloudflared.svg ├── cloudflared_1.png ├── cloudflared_2.png ├── deno.svg ├── deno_1.png ├── deno_2.png ├── deno_3.png └── deno_4.png ├── src └── worker.js └── wrangler.toml /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 枫莹 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Grok API 代理 2 | 3 | 一个简单的脚本,为了解决地区受限制无法使用grok的api调用,用作 x.ai API 的代理。此脚本处理用户请求并将所有其他请求转发到 x.ai API。 4 | 5 | ## 功能 6 | 7 | * 代理请求到 `https://api.x.ai` 8 | 9 | ## 部署到 Cloudflare Workers 10 | 11 | Fork 此仓库 12 | 13 | > [!TIP] 14 | > 点击下面的按钮,登陆账号,然后选择`workers-and-pages`,点击创建,选择导入储存库,选择复刻的仓库,一键部署就可以调用了 15 | 16 | [![](img/cloudflared.svg)](https://www.cloudflare.com/) 17 | 18 | ## 部署到 Deno 19 | 20 | Fork 此仓库 21 | 22 | > [!TIP] 23 | > 点击下面的按钮,登陆账号,然后授权Github,选择复刻的仓库,选择主文件为`src/worker.js`一键部署就行了 24 | 25 | [![](img/deno.svg)](https://deno.com/deploy) 26 | 27 | ## 具体操作 28 | ### Cloudflare 29 | ![alt text](img/cloudflared_1.png) 30 | ![alt text](img/cloudflared_2.png) 31 | 32 | ### Deno 33 | ![alt text](img/deno_1.png) 34 | ![alt text](img/deno_2.png) 35 | ![alt text](img/deno_3.png) 36 | ![alt text](img/deno_4.png) 37 | 38 | ## License 39 | 40 | **[MIT license](LICENSE)** -------------------------------------------------------------------------------- /img/cloudflared.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 80 | 81 | -------------------------------------------------------------------------------- /img/cloudflared_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FengYing1314/grok-api-proxy/1222f318a0fe35fb23c50c1b152669fcdd64de5c/img/cloudflared_1.png -------------------------------------------------------------------------------- /img/cloudflared_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FengYing1314/grok-api-proxy/1222f318a0fe35fb23c50c1b152669fcdd64de5c/img/cloudflared_2.png -------------------------------------------------------------------------------- /img/deno.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 77 | 78 | -------------------------------------------------------------------------------- /img/deno_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FengYing1314/grok-api-proxy/1222f318a0fe35fb23c50c1b152669fcdd64de5c/img/deno_1.png -------------------------------------------------------------------------------- /img/deno_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FengYing1314/grok-api-proxy/1222f318a0fe35fb23c50c1b152669fcdd64de5c/img/deno_2.png -------------------------------------------------------------------------------- /img/deno_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FengYing1314/grok-api-proxy/1222f318a0fe35fb23c50c1b152669fcdd64de5c/img/deno_3.png -------------------------------------------------------------------------------- /img/deno_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FengYing1314/grok-api-proxy/1222f318a0fe35fb23c50c1b152669fcdd64de5c/img/deno_4.png -------------------------------------------------------------------------------- /src/worker.js: -------------------------------------------------------------------------------- 1 | addEventListener('fetch', event => { 2 | event.respondWith(handleRequest(event.request)); 3 | }); 4 | 5 | async function handleRequest(request) { 6 | if (request.method === 'OPTIONS') { 7 | return handleOptionsRequest(); 8 | } 9 | 10 | const method = request.method; 11 | const url = new URL(request.url); 12 | const path = url.pathname + url.search; 13 | const headers = new Headers(request.headers); 14 | 15 | const authHeader = headers.get('Authorization'); 16 | if (!authHeader) { 17 | return new Response('Missing Authorization header', { status: 401 }); 18 | } 19 | 20 | const apiUrl = `https://api.x.ai${path}`; 21 | 22 | const apiRequest = new Request(apiUrl, { 23 | method: method, 24 | headers: headers, 25 | body: method !== 'GET' && method !== 'HEAD' ? await request.blob() : null, 26 | }); 27 | 28 | const apiResponse = await fetch(apiRequest); 29 | 30 | const response = new Response(apiResponse.body, apiResponse); 31 | response.headers.set('Access-Control-Allow-Origin', '*'); 32 | response.headers.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); 33 | response.headers.set('Access-Control-Allow-Headers', 'Content-Type, Authorization'); 34 | 35 | return response; 36 | } 37 | 38 | function handleOptionsRequest() { 39 | const headers = new Headers(); 40 | headers.set('Access-Control-Allow-Origin', '*'); 41 | headers.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); 42 | headers.set('Access-Control-Allow-Headers', 'Content-Type, Authorization'); 43 | headers.set('Access-Control-Max-Age', '86400'); 44 | return new Response(null, { headers }); 45 | } -------------------------------------------------------------------------------- /wrangler.toml: -------------------------------------------------------------------------------- 1 | name = "grok-api-proxy" 2 | main = "src/worker.js" 3 | compatibility_date = "2023-01-01" 4 | --------------------------------------------------------------------------------