区间救国之在国内使用grok2图片生成功能教程


前言

个人用的是FastGPT,之前按照隔壁论坛大佬的配置配置好了grok2生成图片的功能,但现在问题在于生成的图片返回的urlhttps://imgen.x.ai/xai-imgen/这个链接开头的,国内无法访问(我说为啥我正常用,给我朋友用都说加载不出来图片😭)。
因而想了个曲线救国的方案(由于都基于cloudflare workers,因而受其请求数量限制,但感觉仅个人使用应该是够了):

逻辑

1. 先新建一个a.example.com使用cloudflare workers反代api.x.ai,然后生成图片的请求都走这个a.example.com;
2. 再新建一个b.example.com使用cloudflare workers反代imgen.x.ai;
3. 再在A的代码中添加将响应头中的imgen.x.ai全部修改为b.example.com的逻辑即可

代码

这是a.example.com的代码

// 全局变量,定义反代图片的自定义域名
const CUSTOM_DOMAIN = 'b.example.com';

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  // 目标 URL
  const targetUrl = new URL('https://api.x.ai');

  // 创建一个新的 URL 对象
  const url = new URL(request.url);

  // 修改路径以匹配目标 URL
  const newUrl = `https://api.x.ai${url.pathname}${url.search}`;

  // 创建一个新的请求对象,保持原始请求的头部和方法
  const modifiedRequest = new Request(newUrl, {
    method: request.method,
    headers: request.headers,
    body: request.body,
    redirect: 'follow'
  });

  // 发送请求到目标 URL
  const response = await fetch(modifiedRequest);

  // 克隆响应以便修改
  const newResponse = new Response(response.body, response);

  // 获取响应头部
  const headers = new Headers(newResponse.headers);

  // 修改响应头部的 URL
  const location = headers.get('Location');
  if (location && location.includes('imgen.x.ai')) {
    const newLocation = location.replace('imgen.x.ai', CUSTOM_DOMAIN);
    headers.set('Location', newLocation);
  }

  // 修改响应中的 URL
  const contentType = headers.get('Content-Type');
  if (contentType && contentType.includes('application/json')) {
    const json = await newResponse.json();
    if (json.data && Array.isArray(json.data)) {
      json.data.forEach(item => {
        if (item.url && item.url.includes('imgen.x.ai')) {
          item.url = item.url.replace('imgen.x.ai', CUSTOM_DOMAIN);
        }
      });
    }
    return new Response(JSON.stringify(json), {
      status: newResponse.status,
      statusText: newResponse.statusText,
      headers: {
        ...headers,
        'Content-Type': 'application/json'
      }
    });
  } else if (contentType && contentType.includes('text/html')) {
    const text = await newResponse.text();
    const modifiedText = text.replace(/(href|src)=["'](.*?)imgen\.x\.ai(.*?)["']/g, (match, attr, prefix, suffix) => {
      return `${attr}="${prefix}${CUSTOM_DOMAIN}${suffix}"`;
    }).replace(/(href|src)=["']\/\/(.*?)imgen\.x\.ai(.*?)["']/g, (match, attr, prefix, suffix) => {
      return `${attr}="//${CUSTOM_DOMAIN}${suffix}"`;
    });
    return new Response(modifiedText, {
      status: newResponse.status,
      statusText: newResponse.statusText,
      headers: headers
    });
  }

  // 返回修改后的响应
  return new Response(newResponse.body, {
    status: newResponse.status,
    statusText: newResponse.statusText,
    headers: headers
  });
}

这是b.example.com的代码:

addEventListener(
  "fetch",event => {
     let url=new URL(event.request.url);
     url.hostname="imgen.x.ai";  //你需要反代的域名
     let request=new Request(url,event.request);
     event. respondWith(
       fetch(request)
     )
  }
)

两个cloudflare workers都部署之后将生成图片api中的api.x.ai修改为自定义的a.example.com即可成功。

change_api

效果展示

可以看到,FastGPT中的响应请求url已经被修改。

响应体

国内也能正常看到生成的图片,说明成功😋
result


文章作者: xieshang
版权声明: 本博客所有文章除特別声明外,均采用 CC BY-NC 4.0 许可协议。转载请注明来源 xieshang !
评论
  目录