Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Feature/baidu #1

Closed
wants to merge 118 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
118 commits
Select commit Hold shift + click to select a range
c5603b5
Update sync.yml
Dogtiti Apr 5, 2023
416b29b
Merge branch 'Yidadaa:main' into master
Dogtiti Apr 5, 2023
274a7fd
Update sync.yml
Dogtiti Apr 5, 2023
3ee6da5
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 5, 2023
f391ede
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 5, 2023
0895d51
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 6, 2023
cc7de72
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 6, 2023
c4dc9e9
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 6, 2023
1cf3336
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 7, 2023
970d93b
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 7, 2023
f0db1b0
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 8, 2023
f9581f0
Merge branch 'Yidadaa:main' into main
Dogtiti Apr 8, 2023
8ce330a
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 8, 2023
b31862a
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 9, 2023
418ddbe
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 9, 2023
57e51a0
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 9, 2023
95669e4
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 9, 2023
88890ad
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 10, 2023
53ff286
Merge branch 'Yidadaa:main' into main
Dogtiti Apr 12, 2023
d1d4dfb
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 12, 2023
fb2da7f
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 12, 2023
0ccd798
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 12, 2023
0102e54
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 12, 2023
765b70d
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 12, 2023
fc9c05a
Merge branch 'Yidadaa:main' into main
Dogtiti Apr 13, 2023
814521a
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 13, 2023
f210f33
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 13, 2023
4e5940f
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 13, 2023
af79b83
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 13, 2023
b9e411d
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 14, 2023
9383506
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 14, 2023
831778a
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 15, 2023
189f6fe
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 16, 2023
217f9a4
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 16, 2023
f5e34a1
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 16, 2023
85a8f5d
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 17, 2023
af45b54
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 17, 2023
3719f0b
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 17, 2023
c01ba39
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 17, 2023
752eb93
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 18, 2023
ce0d02d
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 18, 2023
6559b3d
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 18, 2023
c166f3b
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 18, 2023
a5bc4f7
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 19, 2023
4493b32
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 19, 2023
dcdf7bb
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 19, 2023
b348e5f
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 20, 2023
1ca7618
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 20, 2023
4119334
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 21, 2023
3f9290f
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 21, 2023
a16d14b
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 21, 2023
9214cb4
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 21, 2023
3c80c67
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 22, 2023
5bad528
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 23, 2023
18a3b9d
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 24, 2023
9a36ec2
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 25, 2023
9c75776
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 25, 2023
06d523b
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 26, 2023
de4750a
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 26, 2023
3dc64fc
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 26, 2023
abfb824
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 27, 2023
4d01f33
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Apr 28, 2023
a7f2736
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 1, 2023
a21d4c5
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 1, 2023
fe3e316
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 2, 2023
6dbc3b2
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 3, 2023
52f11d2
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 3, 2023
35d7f86
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 4, 2023
35ca5bd
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 4, 2023
51e32ae
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 4, 2023
4d6f40d
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 4, 2023
fd2af42
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 5, 2023
2f47bd5
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 5, 2023
f94060e
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 6, 2023
dd9b044
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 7, 2023
bba49f9
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 7, 2023
b160bc2
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 8, 2023
fb2b472
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 8, 2023
912ac96
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 8, 2023
0e3231d
Merge branch 'Yidadaa:main' into main
Dogtiti May 17, 2023
40ed293
Merge branch 'Yidadaa:main' into main
Dogtiti May 17, 2023
d613d3a
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 18, 2023
98f4116
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 19, 2023
faa1da0
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 20, 2023
084241c
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 21, 2023
f73255d
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 22, 2023
948be7f
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 23, 2023
4be0f71
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 24, 2023
dccf33f
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 25, 2023
d4b4f9b
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 26, 2023
8de672c
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 27, 2023
e6efd28
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user May 30, 2023
ff9eb65
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Jun 1, 2023
505e1fa
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Jun 2, 2023
f2cdeaa
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Jun 3, 2023
6951c85
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Jun 6, 2023
fd281bc
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Jun 7, 2023
f64d15f
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Jun 8, 2023
f780825
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Jun 9, 2023
b4d79dc
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Jun 11, 2023
746d33b
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Jun 13, 2023
faec3b6
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Jun 14, 2023
513ca43
Merge branch 'Yidadaa:main' into main
Dogtiti Jun 21, 2023
9fd7cbe
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Jun 22, 2023
c079436
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Jun 23, 2023
3dfb7b9
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Jun 24, 2023
f99124b
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Jun 25, 2023
7b62d6a
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Jun 26, 2023
150396b
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Jun 27, 2023
e5910c0
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Jun 28, 2023
cf88827
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Jun 29, 2023
92f1e62
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Jun 30, 2023
2a2bc6c
Merge branch 'main' of https://github.com/Yidadaa/ChatGPT-Next-Web
actions-user Jul 1, 2023
bf80d36
Merge branch 'Yidadaa:main' into main
Dogtiti Oct 13, 2023
1625b26
Merge branch 'ChatGPTNextWeb:main' into main
Dogtiti Jun 20, 2024
53af85c
Merge branch 'main' of https://github.com/ChatGPTNextWeb/ChatGPT-Next…
actions-user Jun 25, 2024
591d2fa
Merge branch 'main' of https://github.com/ChatGPTNextWeb/ChatGPT-Next…
actions-user Jun 27, 2024
aa729db
wip: baidu erine
Dogtiti Jul 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions app/api/baidu/[...path]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import { getServerSideConfig } from "@/app/config/server";
import {
BAIDU_BASE_URL,
ApiPath,
DEFAULT_MODELS,
ModelProvider,
} from "@/app/constant";
import { prettyObject } from "@/app/utils/format";
import { NextRequest, NextResponse } from "next/server";
import { auth } from "../../auth";
import { collectModelTable } from "@/app/utils/model";

const serverConfig = getServerSideConfig();

const ALLOWD_PATH = new Set([""]);

async function handle(
req: NextRequest,
{ params }: { params: { path: string[] } },
) {
console.log("[Baidu Route] params ", params);

if (req.method === "OPTIONS") {
return NextResponse.json({ body: "OK" }, { status: 200 });
}

const subpath = params.path.join("/");

if (!ALLOWD_PATH.has(subpath)) {
console.log("[Baidu Route] forbidden path ", subpath);
return NextResponse.json(
{
error: true,
msg: "you are not allowed to request " + subpath,
},
{
status: 403,
},
);
}

const authResult = auth(req, ModelProvider.Claude);
if (authResult.error) {
return NextResponse.json(authResult, {
status: 401,
});
}

try {
const response = await request(req);
return response;
} catch (e) {
console.error("[Baidu] ", e);
return NextResponse.json(prettyObject(e));
}
}

export const GET = handle;
export const POST = handle;

export const runtime = "edge";
export const preferredRegion = [
"arn1",
"bom1",
"cdg1",
"cle1",
"cpt1",
"dub1",
"fra1",
"gru1",
"hnd1",
"iad1",
"icn1",
"kix1",
"lhr1",
"pdx1",
"sfo1",
"sin1",
"syd1",
];

async function request(req: NextRequest) {
const controller = new AbortController();

let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.Baidu, "");

let baseUrl = serverConfig.baiduUrl || serverConfig.baseUrl || BAIDU_BASE_URL;

if (!baseUrl.startsWith("http")) {
baseUrl = `https://${baseUrl}`;
}

if (baseUrl.endsWith("/")) {
baseUrl = baseUrl.slice(0, -1);
}

console.log("[Proxy] ", path);
console.log("[Base Url]", baseUrl);

const timeoutId = setTimeout(
() => {
controller.abort();
},
10 * 60 * 1000,
);

const fetchUrl = `${baseUrl}${path}`;

const fetchOptions: RequestInit = {
headers: {
"Content-Type": "application/json",
},
method: req.method,
body: req.body,
redirect: "manual",
// @ts-ignore
duplex: "half",
signal: controller.signal,
};

// #1815 try to refuse some request to some models
if (serverConfig.customModels && req.body) {
try {
const modelTable = collectModelTable(
DEFAULT_MODELS,
serverConfig.customModels,
);
const clonedBody = await req.text();
fetchOptions.body = clonedBody;

const jsonBody = JSON.parse(clonedBody) as { model?: string };

// not undefined and is false
if (modelTable[jsonBody?.model ?? ""].available === false) {
return NextResponse.json(
{
error: true,
message: `you are not allowed to use ${jsonBody?.model} model`,
},
{
status: 403,
},
);
}
} catch (e) {
console.error(`[Baidu] filter`, e);
}
}
console.log("[Baidu request]", fetchOptions.headers, req.method);
try {
const res = await fetch(fetchUrl, fetchOptions);

console.log("[Baidu response]", res.status, " ", res.headers, res.url);
// to prevent browser prompt for credentials
const newHeaders = new Headers(res.headers);
newHeaders.delete("www-authenticate");
// to disable nginx buffering
newHeaders.set("X-Accel-Buffering", "no");

return new Response(res.body, {
status: res.status,
statusText: res.statusText,
headers: newHeaders,
});
} finally {
clearTimeout(timeoutId);
}
}
Loading
Loading