From 920de7c600e867aa42cbbb8db9d52957979f188a Mon Sep 17 00:00:00 2001 From: Zhijie He Date: Sat, 3 Aug 2024 19:35:09 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20add=20`PROXY=5FURL`=20in?= =?UTF-8?q?=20docker=20with=20proxychains-ng=20(#3362)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ✨: feat: support PROXY_URL in docker with proxychains-ng * 🔨 chore: let proxychains config more readable * 🔨 chore: add package update * ⚡️ perf: add CN mirror for Alpine * 🔨 chore: add more comments * 🔨 chore: change USE_NPM_CN_MIRROR to USE_CN_MIRROR * 🐛 fix: fix cn mirror for alpine during building * 🐛 fix: fix permission issue * 🐛 fix: fix app path error * ✨ feat: support proxy server with domain * 🔨 chore: add more comments & cleanup code * 🔨 chore: add more comments * ⚡️ perf: resolve only when host is domain * 🔨 chore: move IP_REGEX to head * 🔨 chore: replace `! -z` to `-n` * 🔨 chore: update comments * ✨ feat: add `DEFAULT_AGENT_CONFIG` and `SYSTEM_AGENT` to env, allow set default model * Add documentation for `PROXY_URL` environment variable Add documentation for `PROXY_URL` environment variable in both English and Chinese versions of the environment variables guide. * **English Documentation** - Add `PROXY_URL` section to specify the proxy URL for connecting to external services. - Include example values and a callout for Docker Desktop users. * **Chinese Documentation** - Add `PROXY_URL` section to specify the proxy URL for connecting to external services. - Include example values and a callout for Docker Desktop users. --------- Co-authored-by: Arvin Xu --- Dockerfile | 61 ++++++++++++++++--- .../environment-variables/basic.mdx | 11 ++++ .../environment-variables/basic.zh-CN.mdx | 12 ++++ package.json | 2 +- 4 files changed, 78 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1607bacda983..9e4de8373165 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,15 +1,27 @@ ## Base image for all the stages FROM node:20-alpine AS base +ARG USE_CN_MIRROR + RUN \ + # If you want to build docker in China, build with --build-arg USE_CN_MIRROR=true + if [ "${USE_CN_MIRROR:-false}" = "true" ]; then \ + sed -i "s/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g" "/etc/apk/repositories"; \ + fi \ + # Add proxychains-ng package & update base package + && apk update \ + && apk add --no-cache proxychains-ng \ + && apk upgrade --no-cache \ # Add user nextjs to run the app - addgroup --system --gid 1001 nodejs \ - && adduser --system --uid 1001 nextjs + && addgroup --system --gid 1001 nodejs \ + && adduser --system --uid 1001 nextjs \ + && chown -R nextjs:nodejs "/etc/proxychains" \ + && rm -rf /tmp/* /var/cache/apk/* ## Builder image, install all the dependencies and build the app FROM base AS builder -ARG USE_NPM_CN_MIRROR +ARG USE_CN_MIRROR ENV NEXT_PUBLIC_BASE_PATH="" @@ -37,8 +49,8 @@ COPY package.json ./ COPY .npmrc ./ RUN \ - # If you want to build docker in China, build with --build-arg USE_NPM_CN_MIRROR=true - if [ "${USE_NPM_CN_MIRROR:-false}" = "true" ]; then \ + # If you want to build docker in China, build with --build-arg USE_CN_MIRROR=true + if [ "${USE_CN_MIRROR:-false}" = "true" ]; then \ export SENTRYCLI_CDNURL="https://npmmirror.com/mirrors/sentry-cli"; \ npm config set registry "https://registry.npmmirror.com/"; \ fi \ @@ -85,7 +97,10 @@ ENV HOSTNAME="0.0.0.0" \ # General Variables ENV ACCESS_CODE="" \ API_KEY_SELECT_MODE="" \ - FEATURE_FLAGS="" + DEFAULT_AGENT_CONFIG="" \ + SYSTEM_AGENT="" \ + FEATURE_FLAGS="" \ + PROXY_URL="" # Model Variables ENV \ @@ -138,4 +153,36 @@ USER nextjs EXPOSE 3210/tcp -CMD ["node", "/app/server.js"] +CMD \ + if [ -n "$PROXY_URL" ]; then \ + # Set regex for IPv4 + IP_REGEX="^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$"; \ + # Set proxychains command + PROXYCHAINS="proxychains -q"; \ + # Parse the proxy URL + host_with_port="${PROXY_URL#*//}"; \ + host="${host_with_port%%:*}"; \ + port="${PROXY_URL##*:}"; \ + protocol="${PROXY_URL%%://*}"; \ + # Resolve to IP address if the host is a domain + if ! [[ "$host" =~ "$IP_REGEX" ]]; then \ + nslookup=$(nslookup -q="A" "$host" | tail -n +3 | grep 'Address:'); \ + if [ -n "$nslookup" ]; then \ + host=$(echo "$nslookup" | tail -n 1 | awk '{print $2}'); \ + fi; \ + fi; \ + # Generate proxychains configuration file + printf "%s\n" \ + 'localnet 127.0.0.0/255.0.0.0' \ + 'localnet ::1/128' \ + 'proxy_dns' \ + 'remote_dns_subnet 224' \ + 'strict_chain' \ + 'tcp_connect_time_out 8000' \ + 'tcp_read_time_out 15000' \ + '[ProxyList]' \ + "$protocol $host $port" \ + > "/etc/proxychains/proxychains.conf"; \ + fi; \ + # Run the server + ${PROXYCHAINS} node "/app/server.js"; diff --git a/docs/self-hosting/environment-variables/basic.mdx b/docs/self-hosting/environment-variables/basic.mdx index b6f0342e91ff..aee601389c1a 100644 --- a/docs/self-hosting/environment-variables/basic.mdx +++ b/docs/self-hosting/environment-variables/basic.mdx @@ -80,6 +80,17 @@ Further reading: For specific content, please refer to the [Feature Flags](/docs/self-hosting/advanced/feature-flags) documentation. +### `PROXY_URL` + +- Type: Optional +- Description: Used to specify the proxy URL for connecting to external services. The value of this variable should be different in different deployment environments. +- Default: - +- Example: `http://127.0.0.1:7890` or `socks5://localhost:7891` + + +If you're using Docker Desktop on Windows or macOS, it relies on a virtual machine. In this setup, `localhost` / `127.0.0.1` refers to the localhost of the container itself. In such cases, please try using `host.docker.internal` instead of `localhost`. + + ## Plugin Service ### `PLUGINS_INDEX_URL` diff --git a/docs/self-hosting/environment-variables/basic.zh-CN.mdx b/docs/self-hosting/environment-variables/basic.zh-CN.mdx index ab6812211970..41c119657b1e 100644 --- a/docs/self-hosting/environment-variables/basic.zh-CN.mdx +++ b/docs/self-hosting/environment-variables/basic.zh-CN.mdx @@ -76,6 +76,18 @@ LobeChat 在部署时提供了一些额外的配置项,你可以使用环境 具体的内容可以参见 [特性标志](/zh/docs/self-hosting/advanced/feature-flags) 中的说明。 +### `PROXY_URL` + +- 类型:可选 +- 描述:用于指定连接到外部服务的代理 URL。该变量的值在不同的部署环境中应该有所不同。 +- 默认值:- +- 示例:`http://127.0.0.1:7890` 或 `socks5://localhost:7891` + + + +`Docker Desktop` 在 `Windows `和 `macOS `上走的是虚拟机方案,如果是 `localhost` / `127.0.0.1` 是走到自身容器的 `localhost`,此时请尝试用 `host.docker.internal` 替代 `localhost` + + ## 插件服务 ### `PLUGINS_INDEX_URL` diff --git a/package.json b/package.json index aa921464b4ce..5bce8b91a094 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "pull": "git pull", "release": "semantic-release", "self-hosting:docker": "docker build -t lobe-chat:local .", - "self-hosting:docker-cn": "docker build -t lobe-chat:local --build-arg USE_NPM_CN_MIRROR=true .", + "self-hosting:docker-cn": "docker build -t lobe-chat:local --build-arg USE_CN_MIRROR=true .", "start": "next start -p 3210", "stylelint": "stylelint \"src/**/*.{js,jsx,ts,tsx}\" --fix", "test": "npm run test-app && npm run test-server",