Upload 11 files
Browse files- .dockerignore +5 -0
- .env.example +6 -0
- .github/workflows/build_docker.yml +62 -0
- .github/workflows/release.yml +60 -0
- .gitignore +1 -0
- Dockerfile +41 -0
- LICENSE +21 -0
- docker-compose.yml +13 -0
- go.mod +34 -0
- go.sum +79 -0
- main.go +535 -0
.dockerignore
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Ignore the local environment files
|
| 2 |
+
.env
|
| 3 |
+
|
| 4 |
+
# Ignore Git files
|
| 5 |
+
.git
|
.env.example
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
API_PREFIX=/
|
| 2 |
+
MAX_RETRY_COUNT=3
|
| 3 |
+
RETRY_DELAY=5000
|
| 4 |
+
PORT=8787
|
| 5 |
+
APIKEY=
|
| 6 |
+
PROXY_URL=http://127.0.0.1:7897
|
.github/workflows/build_docker.yml
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: Build and Push Docker Image
|
| 2 |
+
|
| 3 |
+
on:
|
| 4 |
+
release:
|
| 5 |
+
types: [created] # 表示在创建新的 Release 时触发
|
| 6 |
+
workflow_dispatch: # 手动触发(可选)
|
| 7 |
+
|
| 8 |
+
jobs:
|
| 9 |
+
build:
|
| 10 |
+
runs-on: ubuntu-latest
|
| 11 |
+
|
| 12 |
+
steps:
|
| 13 |
+
- name: Checkout repository
|
| 14 |
+
uses: actions/checkout@v3
|
| 15 |
+
|
| 16 |
+
- name: List files in the repository
|
| 17 |
+
run: ls -la # 列出文件以确保必要的文件存在
|
| 18 |
+
|
| 19 |
+
- name: Disable Docker BuildKit
|
| 20 |
+
run: echo "DOCKER_BUILDKIT=0" >> $GITHUB_ENV
|
| 21 |
+
|
| 22 |
+
- name: Set up Docker Buildx
|
| 23 |
+
uses: docker/setup-buildx-action@v2
|
| 24 |
+
|
| 25 |
+
- name: Log in to Docker Hub
|
| 26 |
+
uses: docker/login-action@v2
|
| 27 |
+
with:
|
| 28 |
+
username: ${{ secrets.DOCKER_HUB_USERNAME }}
|
| 29 |
+
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
|
| 30 |
+
|
| 31 |
+
- name: Log in to GitHub Container Registry
|
| 32 |
+
uses: docker/login-action@v2
|
| 33 |
+
with:
|
| 34 |
+
registry: ghcr.io
|
| 35 |
+
username: ${{ github.actor }}
|
| 36 |
+
password: ${{ secrets.GITHUB_TOKEN }}
|
| 37 |
+
|
| 38 |
+
- name: Convert GitHub username to lowercase
|
| 39 |
+
id: lowercase_username
|
| 40 |
+
run: echo "username=$(echo ${{ github.actor }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
|
| 41 |
+
|
| 42 |
+
- name: Build and push to Docker Hub
|
| 43 |
+
uses: docker/build-push-action@v4
|
| 44 |
+
with:
|
| 45 |
+
context: .
|
| 46 |
+
push: true
|
| 47 |
+
tags: ${{ secrets.DOCKER_HUB_USERNAME }}/ddg-chat-go:latest
|
| 48 |
+
platforms: linux/amd64,linux/arm64
|
| 49 |
+
labels: |
|
| 50 |
+
version=latest
|
| 51 |
+
maintainers=https://blog.lmyself.top
|
| 52 |
+
|
| 53 |
+
- name: Build and push to GitHub Packages
|
| 54 |
+
uses: docker/build-push-action@v4
|
| 55 |
+
with:
|
| 56 |
+
context: .
|
| 57 |
+
push: true
|
| 58 |
+
tags: ghcr.io/${{ env.username }}/ddg-chat-go:latest
|
| 59 |
+
platforms: linux/amd64,linux/arm64
|
| 60 |
+
labels: |
|
| 61 |
+
version=latest
|
| 62 |
+
maintainers=https://blog.lmyself.top
|
.github/workflows/release.yml
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: Go Build
|
| 2 |
+
|
| 3 |
+
on:
|
| 4 |
+
release: # 只在Release创建时触发
|
| 5 |
+
types:
|
| 6 |
+
- created # 当新Release创建时触发
|
| 7 |
+
workflow_dispatch: # 手动触发(可选)
|
| 8 |
+
|
| 9 |
+
jobs:
|
| 10 |
+
build:
|
| 11 |
+
name: Build
|
| 12 |
+
runs-on: ubuntu-latest
|
| 13 |
+
strategy:
|
| 14 |
+
fail-fast: false
|
| 15 |
+
matrix:
|
| 16 |
+
include:
|
| 17 |
+
- goos: windows
|
| 18 |
+
goarch: 386
|
| 19 |
+
- goos: windows
|
| 20 |
+
goarch: amd64
|
| 21 |
+
- goos: windows
|
| 22 |
+
goarch: arm64
|
| 23 |
+
- goos: linux
|
| 24 |
+
goarch: 386
|
| 25 |
+
- goos: linux
|
| 26 |
+
goarch: amd64
|
| 27 |
+
- goos: linux
|
| 28 |
+
goarch: arm64
|
| 29 |
+
- goos: freebsd
|
| 30 |
+
goarch: 386
|
| 31 |
+
- goos: freebsd
|
| 32 |
+
goarch: amd64
|
| 33 |
+
- goos: freebsd
|
| 34 |
+
goarch: arm64
|
| 35 |
+
steps:
|
| 36 |
+
- name: Checkout code
|
| 37 |
+
uses: actions/checkout@v3
|
| 38 |
+
|
| 39 |
+
- name: Setup Go
|
| 40 |
+
uses: actions/setup-go@v4
|
| 41 |
+
with:
|
| 42 |
+
go-version: '1.22'
|
| 43 |
+
|
| 44 |
+
- name: Build
|
| 45 |
+
run: |
|
| 46 |
+
echo "Building for ${{ matrix.goos }} ${{ matrix.goarch }}"
|
| 47 |
+
suffix=""
|
| 48 |
+
if [ "${{ matrix.goos }}" == "windows" ]; then
|
| 49 |
+
suffix=".exe"
|
| 50 |
+
fi
|
| 51 |
+
GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -o ./build/ddgchatgo-${{ matrix.goos }}-${{ matrix.goarch }}$suffix
|
| 52 |
+
|
| 53 |
+
- name: Upload release artifact
|
| 54 |
+
uses: svenstaro/upload-release-action@v2 # 使用此插件将文件上传到Release
|
| 55 |
+
with:
|
| 56 |
+
repo_token: ${{ secrets.GITHUB_TOKEN }} # 使用 GitHub 令牌授权
|
| 57 |
+
file: ./build/ddgchatgo-${{ matrix.goos }}-${{ matrix.goarch }}* # 上传构建产物
|
| 58 |
+
file_glob: true
|
| 59 |
+
tag: ${{ github.ref_name }} # 使用Release的标签作为标识
|
| 60 |
+
name: ddgchatgo-${{ matrix.goos }}-${{ matrix.goarch }}
|
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.env
|
Dockerfile
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 使用官方的 Golang 镜像进行构建
|
| 2 |
+
FROM golang:1.22 AS builder
|
| 3 |
+
|
| 4 |
+
# 在容器中设置工作目录
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# 将 go.mod 和 go.sum 复制到工作目录
|
| 8 |
+
COPY go.mod go.sum ./
|
| 9 |
+
|
| 10 |
+
# 下载所有依赖(如果 go.mod 没有变化则会使用缓存)
|
| 11 |
+
RUN go mod download
|
| 12 |
+
|
| 13 |
+
# 将源代码复制到工作目录
|
| 14 |
+
COPY . .
|
| 15 |
+
|
| 16 |
+
# 静态编译 Go 应用程序,确保兼容 Alpine
|
| 17 |
+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ddg-chat-go .
|
| 18 |
+
|
| 19 |
+
# 使用更小的 Alpine 镜像以减小最终镜像体积
|
| 20 |
+
FROM alpine:latest
|
| 21 |
+
|
| 22 |
+
# 在容器中设置工作目录
|
| 23 |
+
WORKDIR /root/
|
| 24 |
+
|
| 25 |
+
# 从构建阶段复制编译好的二进制文件
|
| 26 |
+
COPY --from=builder /app/ddg-chat-go .
|
| 27 |
+
|
| 28 |
+
# 设置环境变量
|
| 29 |
+
ENV API_PREFIX="/"
|
| 30 |
+
ENV MAX_RETRY_COUNT="3"
|
| 31 |
+
ENV RETRY_DELAY="5000"
|
| 32 |
+
ENV PORT="8787"
|
| 33 |
+
|
| 34 |
+
# 暴露应用运行的端口
|
| 35 |
+
EXPOSE 8787
|
| 36 |
+
|
| 37 |
+
# 确保二进制文件可执行
|
| 38 |
+
RUN chmod +x ddg-chat-go
|
| 39 |
+
|
| 40 |
+
# 启动二进制文件
|
| 41 |
+
CMD ["./ddg-chat-go"]
|
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2025 Shadownc
|
| 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.
|
docker-compose.yml
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version: '3.8'
|
| 2 |
+
|
| 3 |
+
services:
|
| 4 |
+
ddg-chat-go:
|
| 5 |
+
container_name: ddg-chat-go
|
| 6 |
+
image: lmyself/ddg-chat-go:latest
|
| 7 |
+
ports:
|
| 8 |
+
- "8787:8787"
|
| 9 |
+
environment:
|
| 10 |
+
API_PREFIX: "/"
|
| 11 |
+
MAX_RETRY_COUNT: 3
|
| 12 |
+
RETRY_DELAY: 5000
|
| 13 |
+
restart: unless-stopped
|
go.mod
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module ddg
|
| 2 |
+
|
| 3 |
+
go 1.22.5
|
| 4 |
+
|
| 5 |
+
require (
|
| 6 |
+
github.com/bytedance/sonic v1.12.3 // indirect
|
| 7 |
+
github.com/bytedance/sonic/loader v0.2.0 // indirect
|
| 8 |
+
github.com/cloudwego/base64x v0.1.4 // indirect
|
| 9 |
+
github.com/cloudwego/iasm v0.2.0 // indirect
|
| 10 |
+
github.com/gabriel-vasile/mimetype v1.4.6 // indirect
|
| 11 |
+
github.com/gin-contrib/sse v0.1.0 // indirect
|
| 12 |
+
github.com/gin-gonic/gin v1.10.0 // indirect
|
| 13 |
+
github.com/go-playground/locales v0.14.1 // indirect
|
| 14 |
+
github.com/go-playground/universal-translator v0.18.1 // indirect
|
| 15 |
+
github.com/go-playground/validator/v10 v10.22.1 // indirect
|
| 16 |
+
github.com/goccy/go-json v0.10.3 // indirect
|
| 17 |
+
github.com/joho/godotenv v1.5.1 // indirect
|
| 18 |
+
github.com/json-iterator/go v1.1.12 // indirect
|
| 19 |
+
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
|
| 20 |
+
github.com/leodido/go-urn v1.4.0 // indirect
|
| 21 |
+
github.com/mattn/go-isatty v0.0.20 // indirect
|
| 22 |
+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
| 23 |
+
github.com/modern-go/reflect2 v1.0.2 // indirect
|
| 24 |
+
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
|
| 25 |
+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
| 26 |
+
github.com/ugorji/go/codec v1.2.12 // indirect
|
| 27 |
+
golang.org/x/arch v0.11.0 // indirect
|
| 28 |
+
golang.org/x/crypto v0.28.0 // indirect
|
| 29 |
+
golang.org/x/net v0.30.0 // indirect
|
| 30 |
+
golang.org/x/sys v0.26.0 // indirect
|
| 31 |
+
golang.org/x/text v0.19.0 // indirect
|
| 32 |
+
google.golang.org/protobuf v1.35.1 // indirect
|
| 33 |
+
gopkg.in/yaml.v3 v3.0.1 // indirect
|
| 34 |
+
)
|
go.sum
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
github.com/bytedance/sonic v1.12.3 h1:W2MGa7RCU1QTeYRTPE3+88mVC0yXmsRQRChiyVocVjU=
|
| 2 |
+
github.com/bytedance/sonic v1.12.3/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk=
|
| 3 |
+
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
| 4 |
+
github.com/bytedance/sonic/loader v0.2.0 h1:zNprn+lsIP06C/IqCHs3gPQIvnvpKbbxyXQP1iU4kWM=
|
| 5 |
+
github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
| 6 |
+
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
| 7 |
+
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
| 8 |
+
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
|
| 9 |
+
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
| 10 |
+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
| 11 |
+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
| 12 |
+
github.com/gabriel-vasile/mimetype v1.4.6 h1:3+PzJTKLkvgjeTbts6msPJt4DixhT4YtFNf1gtGe3zc=
|
| 13 |
+
github.com/gabriel-vasile/mimetype v1.4.6/go.mod h1:JX1qVKqZd40hUPpAfiNTe0Sne7hdfKSbOqqmkq8GCXc=
|
| 14 |
+
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
| 15 |
+
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
| 16 |
+
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
|
| 17 |
+
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
|
| 18 |
+
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
| 19 |
+
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
| 20 |
+
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
| 21 |
+
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
| 22 |
+
github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27HYW8P9FDk5PbgA=
|
| 23 |
+
github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
| 24 |
+
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
|
| 25 |
+
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
| 26 |
+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
| 27 |
+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
| 28 |
+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
| 29 |
+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
| 30 |
+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
| 31 |
+
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
| 32 |
+
github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM=
|
| 33 |
+
github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
| 34 |
+
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
| 35 |
+
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
| 36 |
+
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
| 37 |
+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
| 38 |
+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
| 39 |
+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
| 40 |
+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
| 41 |
+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
| 42 |
+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
| 43 |
+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
| 44 |
+
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
|
| 45 |
+
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
|
| 46 |
+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
| 47 |
+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
| 48 |
+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
| 49 |
+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
| 50 |
+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
| 51 |
+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
| 52 |
+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
| 53 |
+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
| 54 |
+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
| 55 |
+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
| 56 |
+
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
| 57 |
+
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
| 58 |
+
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
| 59 |
+
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
| 60 |
+
golang.org/x/arch v0.11.0 h1:KXV8WWKCXm6tRpLirl2szsO5j/oOODwZf4hATmGVNs4=
|
| 61 |
+
golang.org/x/arch v0.11.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
| 62 |
+
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
|
| 63 |
+
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
|
| 64 |
+
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
|
| 65 |
+
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
|
| 66 |
+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
| 67 |
+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
| 68 |
+
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
|
| 69 |
+
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
| 70 |
+
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
|
| 71 |
+
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
| 72 |
+
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
|
| 73 |
+
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
| 74 |
+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
| 75 |
+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
| 76 |
+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
| 77 |
+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
| 78 |
+
nullprogram.com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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package main
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"bufio"
|
| 5 |
+
"encoding/json"
|
| 6 |
+
"errors"
|
| 7 |
+
"fmt"
|
| 8 |
+
"io"
|
| 9 |
+
"log"
|
| 10 |
+
"net/http"
|
| 11 |
+
"net/url"
|
| 12 |
+
"os"
|
| 13 |
+
"strings"
|
| 14 |
+
"time"
|
| 15 |
+
|
| 16 |
+
"github.com/gin-gonic/gin"
|
| 17 |
+
"github.com/joho/godotenv"
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
type Config struct {
|
| 21 |
+
APIPrefix string
|
| 22 |
+
MaxRetryCount int
|
| 23 |
+
RetryDelay time.Duration
|
| 24 |
+
FakeHeaders map[string]string
|
| 25 |
+
ProxyURL string
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
var config Config
|
| 29 |
+
|
| 30 |
+
func init() {
|
| 31 |
+
godotenv.Load()
|
| 32 |
+
config = Config{
|
| 33 |
+
APIPrefix: getEnv("API_PREFIX", "/"),
|
| 34 |
+
MaxRetryCount: getIntEnv("MAX_RETRY_COUNT", 3),
|
| 35 |
+
RetryDelay: getDurationEnv("RETRY_DELAY", 5000),
|
| 36 |
+
ProxyURL: getEnv("PROXY_URL", ""),
|
| 37 |
+
FakeHeaders: map[string]string{
|
| 38 |
+
"Accept": "*/*",
|
| 39 |
+
"Accept-Encoding": "gzip, deflate, br, zstd",
|
| 40 |
+
"Accept-Language": "zh-CN,zh;q=0.9",
|
| 41 |
+
"Origin": "https://duckduckgo.com/",
|
| 42 |
+
"Cookie": "dcm=3; dcs=1",
|
| 43 |
+
"Priority": "u=1, i",
|
| 44 |
+
"Referer": "https://duckduckgo.com/",
|
| 45 |
+
"Sec-Ch-Ua": `"Chromium";v="134", "Not:A-Brand";v="24", "Google Chrome";v="134"`,
|
| 46 |
+
"Sec-Ch-Ua-Mobile": "?0",
|
| 47 |
+
"Sec-Ch-Ua-Platform": `"Windows"`,
|
| 48 |
+
"Sec-Fetch-Dest": "empty",
|
| 49 |
+
"Sec-Fetch-Mode": "cors",
|
| 50 |
+
"Sec-Fetch-Site": "same-origin",
|
| 51 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36",
|
| 52 |
+
},
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
func main() {
|
| 57 |
+
r := gin.Default()
|
| 58 |
+
|
| 59 |
+
// Try to disable trusting proxies if the version supports it
|
| 60 |
+
// Uncomment this if your Gin version supports it
|
| 61 |
+
// r.ForwardedByClientIP = false
|
| 62 |
+
|
| 63 |
+
r.Use(corsMiddleware())
|
| 64 |
+
|
| 65 |
+
r.GET("/", func(c *gin.Context) {
|
| 66 |
+
c.JSON(http.StatusOK, gin.H{"message": "API 服务运行中~"})
|
| 67 |
+
})
|
| 68 |
+
|
| 69 |
+
r.GET("/ping", func(c *gin.Context) {
|
| 70 |
+
c.JSON(http.StatusOK, gin.H{"message": "pong"})
|
| 71 |
+
})
|
| 72 |
+
|
| 73 |
+
r.GET(config.APIPrefix+"/v1/models", func(c *gin.Context) {
|
| 74 |
+
models := []gin.H{
|
| 75 |
+
{"id": "gpt-4o-mini", "object": "model", "owned_by": "ddg"},
|
| 76 |
+
{"id": "claude-3-haiku", "object": "model", "owned_by": "ddg"},
|
| 77 |
+
{"id": "llama-3.3-70b", "object": "model", "owned_by": "ddg"},
|
| 78 |
+
{"id": "mistral-small", "object": "model", "owned_by": "ddg"},
|
| 79 |
+
{"id": "o3-mini", "object": "model", "owned_by": "ddg"},
|
| 80 |
+
}
|
| 81 |
+
c.JSON(http.StatusOK, gin.H{"object": "list", "data": models})
|
| 82 |
+
})
|
| 83 |
+
|
| 84 |
+
r.POST(config.APIPrefix+"/v1/chat/completions", handleCompletion)
|
| 85 |
+
|
| 86 |
+
port := os.Getenv("PORT")
|
| 87 |
+
if port == "" {
|
| 88 |
+
port = "8787"
|
| 89 |
+
}
|
| 90 |
+
r.Run(":" + port)
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
func handleCompletion(c *gin.Context) {
|
| 94 |
+
apiKey := os.Getenv("APIKEY")
|
| 95 |
+
authorizationHeader := c.GetHeader("Authorization")
|
| 96 |
+
|
| 97 |
+
if apiKey != "" {
|
| 98 |
+
if authorizationHeader == "" {
|
| 99 |
+
c.JSON(http.StatusUnauthorized, gin.H{"error": "未提供 APIKEY"})
|
| 100 |
+
return
|
| 101 |
+
} else if !strings.HasPrefix(authorizationHeader, "Bearer ") {
|
| 102 |
+
c.JSON(http.StatusUnauthorized, gin.H{"error": "APIKEY 格式错误"})
|
| 103 |
+
return
|
| 104 |
+
} else {
|
| 105 |
+
providedToken := strings.TrimPrefix(authorizationHeader, "Bearer ")
|
| 106 |
+
if providedToken != apiKey {
|
| 107 |
+
c.JSON(http.StatusUnauthorized, gin.H{"error": "APIKEY无效"})
|
| 108 |
+
return
|
| 109 |
+
}
|
| 110 |
+
}
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
var req struct {
|
| 114 |
+
Model string `json:"model"`
|
| 115 |
+
Messages []struct {
|
| 116 |
+
Role string `json:"role"`
|
| 117 |
+
Content interface{} `json:"content"`
|
| 118 |
+
} `json:"messages"`
|
| 119 |
+
Stream bool `json:"stream"`
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
if err := c.ShouldBindJSON(&req); err != nil {
|
| 123 |
+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
| 124 |
+
return
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
model := convertModel(req.Model)
|
| 128 |
+
content := prepareMessages(req.Messages)
|
| 129 |
+
|
| 130 |
+
var retryCount = 0
|
| 131 |
+
var lastError error
|
| 132 |
+
|
| 133 |
+
for retryCount <= config.MaxRetryCount {
|
| 134 |
+
if retryCount > 0 {
|
| 135 |
+
log.Printf("重试中... 次数: %d", retryCount)
|
| 136 |
+
time.Sleep(config.RetryDelay)
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
token, vqdHash, err := requestTokenAndHash()
|
| 140 |
+
if err != nil {
|
| 141 |
+
lastError = fmt.Errorf("无法获取token: %v", err)
|
| 142 |
+
retryCount++
|
| 143 |
+
continue
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
reqBody := map[string]interface{}{
|
| 147 |
+
"model": model,
|
| 148 |
+
"messages": []map[string]interface{}{
|
| 149 |
+
{
|
| 150 |
+
"role": "user",
|
| 151 |
+
"content": content,
|
| 152 |
+
},
|
| 153 |
+
},
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
body, err := json.Marshal(reqBody)
|
| 157 |
+
if err != nil {
|
| 158 |
+
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("请求体序列化失败: %v", err)})
|
| 159 |
+
return
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
upstreamReq, err := http.NewRequest("POST", "https://duckduckgo.com/duckchat/v1/chat", strings.NewReader(string(body)))
|
| 163 |
+
if err != nil {
|
| 164 |
+
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("创建请求失败: %v", err)})
|
| 165 |
+
return
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
for k, v := range config.FakeHeaders {
|
| 169 |
+
upstreamReq.Header.Set(k, v)
|
| 170 |
+
}
|
| 171 |
+
upstreamReq.Header.Set("x-vqd-4", token)
|
| 172 |
+
if vqdHash != "" {
|
| 173 |
+
upstreamReq.Header.Set("x-vqd-hash-1", vqdHash)
|
| 174 |
+
}
|
| 175 |
+
upstreamReq.Header.Set("Content-Type", "application/json")
|
| 176 |
+
upstreamReq.Header.Set("Accept", "text/event-stream")
|
| 177 |
+
|
| 178 |
+
client := createHTTPClient(30 * time.Second)
|
| 179 |
+
resp, err := client.Do(upstreamReq)
|
| 180 |
+
if err != nil {
|
| 181 |
+
lastError = fmt.Errorf("请求失败: %v", err)
|
| 182 |
+
retryCount++
|
| 183 |
+
continue
|
| 184 |
+
}
|
| 185 |
+
defer resp.Body.Close()
|
| 186 |
+
|
| 187 |
+
if resp.StatusCode != http.StatusOK {
|
| 188 |
+
bodyBytes, _ := io.ReadAll(resp.Body)
|
| 189 |
+
lastError = fmt.Errorf("非200响应: %d, 内容: %s", resp.StatusCode, string(bodyBytes))
|
| 190 |
+
retryCount++
|
| 191 |
+
continue
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
// 处理响应
|
| 195 |
+
if err := handleResponse(c, resp, req.Stream, model); err != nil {
|
| 196 |
+
lastError = err
|
| 197 |
+
retryCount++
|
| 198 |
+
continue
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
return
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
// 如果所有重试都失败了
|
| 205 |
+
c.JSON(http.StatusInternalServerError, gin.H{"error": lastError.Error()})
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
func handleResponse(c *gin.Context, resp *http.Response, isStream bool, model string) error {
|
| 209 |
+
if isStream {
|
| 210 |
+
return handleStreamResponse(c, resp, model)
|
| 211 |
+
}
|
| 212 |
+
return handleNonStreamResponse(c, resp, model)
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
func handleStreamResponse(c *gin.Context, resp *http.Response, model string) error {
|
| 216 |
+
c.Writer.Header().Set("Content-Type", "text/event-stream")
|
| 217 |
+
c.Writer.Header().Set("Cache-Control", "no-cache")
|
| 218 |
+
c.Writer.Header().Set("Connection", "keep-alive")
|
| 219 |
+
|
| 220 |
+
flusher, ok := c.Writer.(http.Flusher)
|
| 221 |
+
if !ok {
|
| 222 |
+
return errors.New("Streaming not supported")
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
reader := bufio.NewReader(resp.Body)
|
| 226 |
+
for {
|
| 227 |
+
line, err := reader.ReadString('\n')
|
| 228 |
+
if err != nil {
|
| 229 |
+
if err != io.EOF {
|
| 230 |
+
log.Printf("读取流式响应失败: %v", err)
|
| 231 |
+
}
|
| 232 |
+
break
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
if strings.HasPrefix(line, "data: ") {
|
| 236 |
+
line = strings.TrimPrefix(line, "data: ")
|
| 237 |
+
line = strings.TrimSpace(line)
|
| 238 |
+
|
| 239 |
+
if line == "[DONE]" {
|
| 240 |
+
response := map[string]interface{}{
|
| 241 |
+
"id": "chatcmpl-QXlha2FBbmROaXhpZUFyZUF3ZXNvbWUK",
|
| 242 |
+
"object": "chat.completion.chunk",
|
| 243 |
+
"created": time.Now().Unix(),
|
| 244 |
+
"model": model,
|
| 245 |
+
"choices": []map[string]interface{}{
|
| 246 |
+
{
|
| 247 |
+
"index": 0,
|
| 248 |
+
"finish_reason": "stop",
|
| 249 |
+
},
|
| 250 |
+
},
|
| 251 |
+
}
|
| 252 |
+
sseData, _ := json.Marshal(response)
|
| 253 |
+
sseMessage := fmt.Sprintf("data: %s\n\n", sseData)
|
| 254 |
+
if _, err := c.Writer.Write([]byte(sseMessage)); err != nil {
|
| 255 |
+
return fmt.Errorf("写入响应失败: %v", err)
|
| 256 |
+
}
|
| 257 |
+
flusher.Flush()
|
| 258 |
+
break
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
var chunk map[string]interface{}
|
| 262 |
+
if err := json.Unmarshal([]byte(line), &chunk); err != nil {
|
| 263 |
+
log.Printf("解析响应行失败: %v", err)
|
| 264 |
+
continue
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
if chunk["action"] != "success" {
|
| 268 |
+
continue
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
if msg, exists := chunk["message"]; exists && msg != nil {
|
| 272 |
+
if msgStr, ok := msg.(string); ok {
|
| 273 |
+
response := map[string]interface{}{
|
| 274 |
+
"id": "chatcmpl-QXlha2FBbmROaXhpZUFyZUF3ZXNvbWUK",
|
| 275 |
+
"object": "chat.completion.chunk",
|
| 276 |
+
"created": time.Now().Unix(),
|
| 277 |
+
"model": model,
|
| 278 |
+
"choices": []map[string]interface{}{
|
| 279 |
+
{
|
| 280 |
+
"index": 0,
|
| 281 |
+
"delta": map[string]string{
|
| 282 |
+
"content": msgStr,
|
| 283 |
+
},
|
| 284 |
+
"finish_reason": nil,
|
| 285 |
+
},
|
| 286 |
+
},
|
| 287 |
+
}
|
| 288 |
+
sseData, _ := json.Marshal(response)
|
| 289 |
+
sseMessage := fmt.Sprintf("data: %s\n\n", sseData)
|
| 290 |
+
|
| 291 |
+
if _, err := c.Writer.Write([]byte(sseMessage)); err != nil {
|
| 292 |
+
return fmt.Errorf("写入响应失败: %v", err)
|
| 293 |
+
}
|
| 294 |
+
flusher.Flush()
|
| 295 |
+
}
|
| 296 |
+
}
|
| 297 |
+
}
|
| 298 |
+
}
|
| 299 |
+
return nil
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
func handleNonStreamResponse(c *gin.Context, resp *http.Response, model string) error {
|
| 303 |
+
var fullResponse strings.Builder
|
| 304 |
+
reader := bufio.NewReader(resp.Body)
|
| 305 |
+
|
| 306 |
+
for {
|
| 307 |
+
line, err := reader.ReadString('\n')
|
| 308 |
+
if err == io.EOF {
|
| 309 |
+
break
|
| 310 |
+
} else if err != nil {
|
| 311 |
+
return fmt.Errorf("读取响应失败: %v", err)
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
+
if strings.HasPrefix(line, "data: ") {
|
| 315 |
+
line = strings.TrimPrefix(line, "data: ")
|
| 316 |
+
line = strings.TrimSpace(line)
|
| 317 |
+
|
| 318 |
+
if line == "[DONE]" {
|
| 319 |
+
break
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
var chunk map[string]interface{}
|
| 323 |
+
if err := json.Unmarshal([]byte(line), &chunk); err != nil {
|
| 324 |
+
log.Printf("解析响应行失败: %v", err)
|
| 325 |
+
continue
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
if chunk["action"] != "success" {
|
| 329 |
+
continue
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
if msg, exists := chunk["message"]; exists && msg != nil {
|
| 333 |
+
if msgStr, ok := msg.(string); ok {
|
| 334 |
+
fullResponse.WriteString(msgStr)
|
| 335 |
+
}
|
| 336 |
+
}
|
| 337 |
+
}
|
| 338 |
+
}
|
| 339 |
+
|
| 340 |
+
response := map[string]interface{}{
|
| 341 |
+
"id": "chatcmpl-QXlha2FBbmROaXhpZUFyZUF3ZXNvbWUK",
|
| 342 |
+
"object": "chat.completion",
|
| 343 |
+
"created": time.Now().Unix(),
|
| 344 |
+
"model": model,
|
| 345 |
+
"usage": map[string]int{
|
| 346 |
+
"prompt_tokens": 0,
|
| 347 |
+
"completion_tokens": 0,
|
| 348 |
+
"total_tokens": 0,
|
| 349 |
+
},
|
| 350 |
+
"choices": []map[string]interface{}{
|
| 351 |
+
{
|
| 352 |
+
"message": map[string]string{
|
| 353 |
+
"role": "assistant",
|
| 354 |
+
"content": fullResponse.String(),
|
| 355 |
+
},
|
| 356 |
+
"index": 0,
|
| 357 |
+
},
|
| 358 |
+
},
|
| 359 |
+
}
|
| 360 |
+
|
| 361 |
+
c.JSON(http.StatusOK, response)
|
| 362 |
+
return nil
|
| 363 |
+
}
|
| 364 |
+
|
| 365 |
+
func requestToken() (string, error) {
|
| 366 |
+
token, _, err := requestTokenAndHash()
|
| 367 |
+
return token, err
|
| 368 |
+
}
|
| 369 |
+
|
| 370 |
+
func requestTokenAndHash() (string, string, error) {
|
| 371 |
+
req, err := http.NewRequest("GET", "https://duckduckgo.com/duckchat/v1/status", nil)
|
| 372 |
+
if err != nil {
|
| 373 |
+
return "", "", fmt.Errorf("创建请求失败: %v", err)
|
| 374 |
+
}
|
| 375 |
+
for k, v := range config.FakeHeaders {
|
| 376 |
+
req.Header.Set(k, v)
|
| 377 |
+
}
|
| 378 |
+
req.Header.Set("x-vqd-accept", "1")
|
| 379 |
+
|
| 380 |
+
client := createHTTPClient(10 * time.Second)
|
| 381 |
+
|
| 382 |
+
log.Println("发送 token 请求")
|
| 383 |
+
resp, err := client.Do(req)
|
| 384 |
+
if err != nil {
|
| 385 |
+
return "", "", fmt.Errorf("请求失败: %v", err)
|
| 386 |
+
}
|
| 387 |
+
defer resp.Body.Close()
|
| 388 |
+
|
| 389 |
+
if resp.StatusCode != http.StatusOK {
|
| 390 |
+
bodyBytes, _ := io.ReadAll(resp.Body)
|
| 391 |
+
bodyString := string(bodyBytes)
|
| 392 |
+
log.Printf("requestToken: 非200响应: %d, 内容: %s\n", resp.StatusCode, bodyString)
|
| 393 |
+
return "", "", fmt.Errorf("非200响应: %d, 内容: %s", resp.StatusCode, bodyString)
|
| 394 |
+
}
|
| 395 |
+
|
| 396 |
+
// 从响应头中获取令牌
|
| 397 |
+
token := resp.Header.Get("x-vqd-4")
|
| 398 |
+
if token == "" {
|
| 399 |
+
return "", "", errors.New("响应中未包含x-vqd-4头")
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
// 从响应头中获取x-vqd-hash-1
|
| 403 |
+
vqdHash := resp.Header.Get("x-vqd-hash-1")
|
| 404 |
+
if vqdHash == "" {
|
| 405 |
+
// 尝试从响应体中获取
|
| 406 |
+
bodyBytes, err := io.ReadAll(resp.Body)
|
| 407 |
+
if err != nil {
|
| 408 |
+
log.Printf("读取响应体失败: %v", err)
|
| 409 |
+
} else {
|
| 410 |
+
// 记录响应体以便调试
|
| 411 |
+
log.Printf("响应体: %s", string(bodyBytes))
|
| 412 |
+
|
| 413 |
+
// 尝试从JSON响应中解析可能包含的哈希值
|
| 414 |
+
var respObj map[string]interface{}
|
| 415 |
+
if err := json.Unmarshal(bodyBytes, &respObj); err == nil {
|
| 416 |
+
if hashValue, ok := respObj["x-vqd-hash-1"].(string); ok {
|
| 417 |
+
vqdHash = hashValue
|
| 418 |
+
}
|
| 419 |
+
}
|
| 420 |
+
}
|
| 421 |
+
}
|
| 422 |
+
|
| 423 |
+
log.Printf("获取到的 token: %s", token)
|
| 424 |
+
if vqdHash != "" {
|
| 425 |
+
log.Printf("获取到的 hash: %s", vqdHash)
|
| 426 |
+
} else {
|
| 427 |
+
log.Println("未获取到 hash 值")
|
| 428 |
+
}
|
| 429 |
+
|
| 430 |
+
return token, vqdHash, nil
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
func prepareMessages(messages []struct {
|
| 434 |
+
Role string `json:"role"`
|
| 435 |
+
Content interface{} `json:"content"`
|
| 436 |
+
}) string {
|
| 437 |
+
var contentBuilder strings.Builder
|
| 438 |
+
|
| 439 |
+
for _, msg := range messages {
|
| 440 |
+
// Determine the role - 'system' becomes 'user'
|
| 441 |
+
role := msg.Role
|
| 442 |
+
if role == "system" {
|
| 443 |
+
role = "user"
|
| 444 |
+
}
|
| 445 |
+
|
| 446 |
+
// Process the content as string
|
| 447 |
+
contentStr := ""
|
| 448 |
+
switch v := msg.Content.(type) {
|
| 449 |
+
case string:
|
| 450 |
+
contentStr = v
|
| 451 |
+
case []interface{}:
|
| 452 |
+
for _, item := range v {
|
| 453 |
+
if itemMap, ok := item.(map[string]interface{}); ok {
|
| 454 |
+
if text, exists := itemMap["text"].(string); exists {
|
| 455 |
+
contentStr += text
|
| 456 |
+
}
|
| 457 |
+
}
|
| 458 |
+
}
|
| 459 |
+
default:
|
| 460 |
+
contentStr = fmt.Sprintf("%v", msg.Content)
|
| 461 |
+
}
|
| 462 |
+
|
| 463 |
+
// Append the role and content to the builder
|
| 464 |
+
contentBuilder.WriteString(fmt.Sprintf("%s:%s;\r\n", role, contentStr))
|
| 465 |
+
}
|
| 466 |
+
|
| 467 |
+
return contentBuilder.String()
|
| 468 |
+
}
|
| 469 |
+
|
| 470 |
+
func convertModel(inputModel string) string {
|
| 471 |
+
switch strings.ToLower(inputModel) {
|
| 472 |
+
case "claude-3-haiku":
|
| 473 |
+
return "claude-3-haiku-20240307"
|
| 474 |
+
case "llama-3.3-70b":
|
| 475 |
+
return "meta-llama/Llama-3.3-70B-Instruct-Turbo"
|
| 476 |
+
case "mistral-small":
|
| 477 |
+
return "mistralai/Mistral-Small-24B-Instruct-2501"
|
| 478 |
+
case "o3-mini":
|
| 479 |
+
return "o3-mini"
|
| 480 |
+
default:
|
| 481 |
+
return "gpt-4o-mini"
|
| 482 |
+
}
|
| 483 |
+
}
|
| 484 |
+
|
| 485 |
+
func corsMiddleware() gin.HandlerFunc {
|
| 486 |
+
return func(c *gin.Context) {
|
| 487 |
+
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
| 488 |
+
c.Writer.Header().Set("Access-Control-Allow-Methods", "*")
|
| 489 |
+
c.Writer.Header().Set("Access-Control-Allow-Headers", "*")
|
| 490 |
+
if c.Request.Method == http.MethodOptions {
|
| 491 |
+
c.AbortWithStatus(http.StatusNoContent)
|
| 492 |
+
return
|
| 493 |
+
}
|
| 494 |
+
c.Next()
|
| 495 |
+
}
|
| 496 |
+
}
|
| 497 |
+
|
| 498 |
+
func getEnv(key, fallback string) string {
|
| 499 |
+
if value, exists := os.LookupEnv(key); exists {
|
| 500 |
+
return value
|
| 501 |
+
}
|
| 502 |
+
return fallback
|
| 503 |
+
}
|
| 504 |
+
|
| 505 |
+
func getIntEnv(key string, fallback int) int {
|
| 506 |
+
if value, exists := os.LookupEnv(key); exists {
|
| 507 |
+
var intValue int
|
| 508 |
+
fmt.Sscanf(value, "%d", &intValue)
|
| 509 |
+
return intValue
|
| 510 |
+
}
|
| 511 |
+
return fallback
|
| 512 |
+
}
|
| 513 |
+
|
| 514 |
+
func getDurationEnv(key string, fallback int) time.Duration {
|
| 515 |
+
return time.Duration(getIntEnv(key, fallback)) * time.Millisecond
|
| 516 |
+
}
|
| 517 |
+
|
| 518 |
+
func createHTTPClient(timeout time.Duration) *http.Client {
|
| 519 |
+
client := &http.Client{
|
| 520 |
+
Timeout: timeout,
|
| 521 |
+
}
|
| 522 |
+
|
| 523 |
+
if config.ProxyURL != "" {
|
| 524 |
+
proxyURL, err := url.Parse(config.ProxyURL)
|
| 525 |
+
if err != nil {
|
| 526 |
+
log.Printf("代理URL解析失败: %v", err)
|
| 527 |
+
return client
|
| 528 |
+
}
|
| 529 |
+
client.Transport = &http.Transport{
|
| 530 |
+
Proxy: http.ProxyURL(proxyURL),
|
| 531 |
+
}
|
| 532 |
+
}
|
| 533 |
+
|
| 534 |
+
return client
|
| 535 |
+
}
|