Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Apr 10, 2024
2 parents 0c2f424 + 13db64f commit b44670e
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 18 deletions.
14 changes: 14 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,17 @@ ENABLE_BALANCE_QUERY=
# If you want to disable parse settings from url, set this value to 1.
DISABLE_FAST_LINK=


# anthropic claude Api Key.(optional)
ANTHROPIC_API_KEY=

### anthropic claude Api version. (optional)
ANTHROPIC_API_VERSION=



### anthropic claude Api url (optional)
ANTHROPIC_URL=

### (optional)
WHITE_WEBDEV_ENDPOINTS=
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,18 @@ Google Gemini Pro Api Key.

Google Gemini Pro Api Url.

### `ANTHROPIC_API_KEY` (optional)

anthropic claude Api Key.

### `ANTHROPIC_API_VERSION` (optional)

anthropic claude Api version.

### `ANTHROPIC_URL` (optional)

anthropic claude Api Url.

### `HIDE_USER_API_KEY` (optional)

> Default: Empty
Expand Down Expand Up @@ -233,6 +245,13 @@ To control custom models, use `+` to add a custom model, use `-` to hide a model

User `-all` to disable all default models, `+all` to enable all default models.

### `WHITE_WEBDEV_ENDPOINTS` (可选)

You can use this option if you want to increase the number of webdav service addresses you are allowed to access, as required by the format:
- Each address must be a complete endpoint
> `https://xxxx/yyy`
- Multiple addresses are connected by ', '

## Requirements

NodeJS >= 18, Docker >= 20
Expand Down
19 changes: 19 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ Google Gemini Pro 密钥.

Google Gemini Pro Api Url.

### `ANTHROPIC_API_KEY` (optional)

anthropic claude Api Key.

### `ANTHROPIC_API_VERSION` (optional)

anthropic claude Api version.

### `ANTHROPIC_URL` (optional)

anthropic claude Api Url.

### `HIDE_USER_API_KEY` (可选)

如果你不想让用户自行填入 API Key,将此环境变量设置为 1 即可。
Expand All @@ -130,6 +142,13 @@ Google Gemini Pro Api Url.

如果你想禁用从链接解析预制设置,将此环境变量设置为 1 即可。

### `WHITE_WEBDEV_ENDPOINTS` (可选)

如果你想增加允许访问的webdav服务地址,可以使用该选项,格式要求:
- 每一个地址必须是一个完整的 endpoint
> `https://xxxx/xxx`
- 多个地址以`,`相连

### `CUSTOM_MODELS` (可选)

> 示例:`+qwen-7b-chat,+glm-6b,-gpt-3.5-turbo,gpt-4-1106-preview=gpt-4-turbo` 表示增加 `qwen-7b-chat``glm-6b` 到模型列表,而从列表中删除 `gpt-3.5-turbo`,并将 `gpt-4-1106-preview` 模型名字展示为 `gpt-4-turbo`
Expand Down
39 changes: 23 additions & 16 deletions app/api/webdav/[...path]/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { NextRequest, NextResponse } from "next/server";
import { STORAGE_KEY } from "../../../constant";
import { STORAGE_KEY, internalWhiteWebDavEndpoints } from "../../../constant";
import { getServerSideConfig } from "@/app/config/server";

const config = getServerSideConfig();

const mergedWhiteWebDavEndpoints = [
...internalWhiteWebDavEndpoints,
...config.whiteWebDevEndpoints,
].filter((domain) => Boolean(domain.trim()));

async function handle(
req: NextRequest,
{ params }: { params: { path: string[] } },
Expand All @@ -14,7 +23,9 @@ async function handle(
let endpoint = requestUrl.searchParams.get("endpoint");

// Validate the endpoint to prevent potential SSRF attacks
if (!endpoint || !endpoint.startsWith("/")) {
if (
!mergedWhiteWebDavEndpoints.some((white) => endpoint?.startsWith(white))
) {
return NextResponse.json(
{
error: true,
Expand All @@ -25,6 +36,11 @@ async function handle(
},
);
}

if (!endpoint?.endsWith("/")) {
endpoint += "/";
}

const endpointPath = params.path.join("/");
const targetPath = `${endpoint}/${endpointPath}`;

Expand All @@ -42,10 +58,7 @@ async function handle(
}

// for MKCOL request, only allow request ${folder}
if (
req.method === "MKCOL" &&
!targetPath.endsWith(folder)
) {
if (req.method === "MKCOL" && !targetPath.endsWith(folder)) {
return NextResponse.json(
{
error: true,
Expand All @@ -58,10 +71,7 @@ async function handle(
}

// for GET request, only allow request ending with fileName
if (
req.method === "GET" &&
!targetPath.endsWith(fileName)
) {
if (req.method === "GET" && !targetPath.endsWith(fileName)) {
return NextResponse.json(
{
error: true,
Expand All @@ -74,10 +84,7 @@ async function handle(
}

// for PUT request, only allow request ending with fileName
if (
req.method === "PUT" &&
!targetPath.endsWith(fileName)
) {
if (req.method === "PUT" && !targetPath.endsWith(fileName)) {
return NextResponse.json(
{
error: true,
Expand All @@ -101,7 +108,7 @@ async function handle(
authorization: req.headers.get("authorization") ?? "",
},
body: shouldNotHaveBody ? null : req.body,
redirect: 'manual',
redirect: "manual",
method,
// @ts-ignore
duplex: "half",
Expand All @@ -117,7 +124,7 @@ async function handle(
return fetchResult;
}

export const POST = handle;
export const PUT = handle;
export const GET = handle;
export const OPTIONS = handle;

Expand Down
1 change: 0 additions & 1 deletion app/components/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ function escapeBrackets(text: string) {

function _MarkDownContent(props: { content: string }) {
const escapedContent = useMemo(() => {
console.log("================", props.content);
return escapeBrackets(escapeDollarNumber(props.content));
}, [props.content]);

Expand Down
5 changes: 5 additions & 0 deletions app/config/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ export const getServerSideConfig = () => {
`[Server Config] using ${randomIndex + 1} of ${apiKeys.length} api key`,
);

const whiteWebDevEndpoints = (process.env.WHITE_WEBDEV_ENDPOINTS ?? "").split(
",",
);

return {
baseUrl: process.env.BASE_URL,
apiKey,
Expand Down Expand Up @@ -112,5 +116,6 @@ export const getServerSideConfig = () => {
hideBalanceQuery: !process.env.ENABLE_BALANCE_QUERY,
disableFastLink: !!process.env.DISABLE_FAST_LINK,
customModels,
whiteWebDevEndpoints,
};
};
4 changes: 3 additions & 1 deletion app/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ You are ChatGPT, a large language model trained by {{ServiceProvider}}.
Knowledge cutoff: {{cutoff}}
Current model: {{model}}
Current time: {{time}}
Latex inline: \(x^2\)
Latex inline: \\(x^2\\)
Latex block: $$e=mc^2$$
`;

Expand Down Expand Up @@ -366,3 +366,5 @@ export const DEFAULT_MODELS = [

export const CHAT_PAGE_SIZE = 15;
export const MAX_RENDER_MSG_COUNT = 45;

export const internalWhiteWebDavEndpoints = ["https://dav.jianguoyun.com"];

0 comments on commit b44670e

Please sign in to comment.