2026年3月

Github地址为 https://github.com/router-for-me/CLIProxyAPI

2. 搭建

选择一个目录,本文中使用的是/data/CLIProxyAPI

运行 mkdir -p /data/CLIProxyAPI && touch /data/CLIProxyAPI/config.yaml ,创建项目配置文件

复制以下项目配置,将 secret-key 修改为你的密码

# Server host/interface to bind to. Default is empty ("") to bind all interfaces (IPv4 + IPv6).
# Use "127.0.0.1" or "localhost" to restrict access to local machine only.
host: ""
# Server port
port: 8317
# TLS settings for HTTPS. When enabled, the server listens with the provided certificate and key.
tls:
  enable: false
  cert: ""
  key: ""
# Management API settings
remote-management:
# Whether to allow remote (non-localhost) management access.
# When false, only localhost can access management endpoints (a key is still required).
  allow-remote: true
# Management key. If a plaintext value is provided here, it will be hashed on startup.
# All management requests (even from localhost) require this key.
# Leave empty to disable the Management API entirely (404 for all /v0/management routes).
  secret-key: "登陆密码"
# Disable the bundled management control panel asset download and HTTP route when true.
  disable-control-panel: false
# GitHub repository for the management control panel. Accepts a repository URL or releases API URL.
  panel-github-repository: "https://github.com/router-for-me/Cli-Proxy-API-Management-Center"
# Authentication directory (supports ~ for home directory)
auth-dir: "~/.cli-proxy-api"

粘贴到刚刚新建的 config.yamlvi /data/CLIProxyAPI/config.yaml

然后运行 mkdir -p /data/CLIProxyAPI && touch /data/CLIProxyAPI/compose.yaml ,创建docker compose文件

复制以下docker compose 配置 ,粘贴到刚刚新建的 compose.yamlvi /data/CLIProxyAPI/compose.yaml

services:
  cli-proxy-api:
    image: eceasy/cli-proxy-api:latest
    container_name: cli-proxy-api
    ports:
      - "8317:8317"
    volumes:
      - ./config.yaml:/CLIProxyAPI/config.yaml
      - ./auths:/root/.cli-proxy-api
    restart: unless-stopped

注意:前面个端口可以改为公网端口

运行如下命令 cd /data/CLIProxyAPI && docker compose up -d ,创建容器

3. 配置

接下来访问 服务器IP:8317/management.html ,进入后台管理页(如果你修改了端口则替换为你的端口),输入你在前面配置的密码(secret-key),登录

点击 OAuth 登录、认证文件管理 认证对应的账号就行

接下来添加 API 密钥,点击配置面板,下拉到认证配置处,点击添加 API 密钥 ,可自定义或随机生成一个 API 密钥,点击添加,可以看到我们刚刚添加的 API 密钥,点击下方的对号,保存更改

4. 使用

到此已经完成,可以使用该密钥了,BaseURL 即为你的服务器IP:项目端口,本项目能提供标准的OpenAI API和Claude API

一般是:http://localhost:8317/v1
API:上面生成的API就可以使用了

5. 更新

cd /data/CLIProxyAPI && docker compose pull && docker compose up -d

代码:

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

async function handleRequest(request) {
  // ✅ 使用你的Pages默认地址
  const targetBase = 'https://your.pages.dev'

  if (request.headers.get('Upgrade') === 'websocket') {
    return fetch(request)
  }

  const url = new URL(request.url)
  const newUrl = new URL(url.pathname + url.search, targetBase)
  
  const newHeaders = new Headers(request.headers)
  newHeaders.set('Host', newUrl.hostname)
  
  const newRequest = new Request(newUrl, {
    method: request.method,
    headers: newHeaders,
    body: request.body,
    redirect: 'manual'
  })

  const response = await fetch(newRequest)
  
  if ([301, 302, 303, 307, 308].includes(response.status)) {
    const location = response.headers.get('Location')
    if (location && location.includes(targetBase)) {
      return Response.redirect(
        location.replace(targetBase, `https://${url.hostname}`),
        response.status
      )
    }
  }
  
  return response
}