Skip to content

add custom twikoo host config for self-hosted#969

Merged
imaegoo merged 3 commits into
twikoojs:mainfrom
WingEdge777:fea.custom_host_config
May 26, 2026
Merged

add custom twikoo host config for self-hosted#969
imaegoo merged 3 commits into
twikoojs:mainfrom
WingEdge777:fea.custom_host_config

Conversation

@WingEdge777

@WingEdge777 WingEdge777 commented May 26, 2026

Copy link
Copy Markdown
Contributor

动机

添加环境变量 TWIKOO_HOST 支持指定 server binding 的 ip 地址

原因:在支持 IPV6 的vps上,禁用IPv6后(gemini对IPV6的来源流量审查特别严格),tkserver binding :: 会启动失败

修改前:

5/26/2026, 2:40:18 AM Twikoo: Connecting to database...
node:events:486
      throw er; // Unhandled 'error' event
      ^

Error: listen EAFNOSUPPORT: address family not supported :::8080
    at Server.setupListenHandle [as _listen2] (node:net:1918:21)
    at listenInCluster (node:net:1997:12)
    at node:net:2206:7
    at process.processTicksAndRejections (node:internal/process/task_queues:89:21)
Emitted 'error' event on Server instance at:
    at emitErrorNT (node:net:1976:8)
    at process.processTicksAndRejections (node:internal/process/task_queues:89:21) {
  code: 'EAFNOSUPPORT',
  errno: -97,
  syscall: 'listen',
  address: '::',
  port: 8080
}

Node.js v24.11.1

修改后:

启动命令:

export TWIKOO_HOST=0.0.0.0
nohup tkserver > tkserver.log 2>&1 &

输出:

5/26/2026, 3:17:57 AM Twikoo: Twikoo database stored at /root/workspace/twikoo/data
5/26/2026, 3:17:57 AM Twikoo: Connecting to database...
5/26/2026, 3:17:57 AM Twikoo: Twikoo is using loki database
5/26/2026, 3:17:57 AM Twikoo: Twikoo function started on host 0.0.0.0 port 8080
5/26/2026, 3:17:57 AM Twikoo: Connected to database

Summary by Sourcery

为自托管的 Twikoo 服务器新增可配置的主机绑定,并在文档中说明新的环境变量。

新特性:

  • 引入 TWIKOO_HOST 环境变量,用于自定义自托管服务器监听的主机/IP 地址;当设置该变量时,其优先级高于 TWIKOO_LOCALHOST_ONLY

文档:

  • 在后端和自托管服务器文档中补充 TWIKOO_HOST 环境变量的说明,包括其行为和使用示例。
Original summary in English

Summary by Sourcery

Add configurable host binding for the self-hosted Twikoo server and document the new environment variable.

New Features:

  • Introduce TWIKOO_HOST environment variable to customize the host/IP address the self-hosted server listens on, taking precedence over TWIKOO_LOCALHOST_ONLY when set.

Documentation:

  • Document the TWIKOO_HOST environment variable in backend and self-hosted server documentation, including its behavior and examples.
Original summary in English

Summary by Sourcery

为自托管的 Twikoo 服务器新增可配置的主机绑定,并在文档中说明新的环境变量。

新特性:

  • 引入 TWIKOO_HOST 环境变量,用于自定义自托管服务器监听的主机/IP 地址;当设置该变量时,其优先级高于 TWIKOO_LOCALHOST_ONLY

文档:

  • 在后端和自托管服务器文档中补充 TWIKOO_HOST 环境变量的说明,包括其行为和使用示例。
Original summary in English

Summary by Sourcery

Add configurable host binding for the self-hosted Twikoo server and document the new environment variable.

New Features:

  • Introduce TWIKOO_HOST environment variable to customize the host/IP address the self-hosted server listens on, taking precedence over TWIKOO_LOCALHOST_ONLY when set.

Documentation:

  • Document the TWIKOO_HOST environment variable in backend and self-hosted server documentation, including its behavior and examples.

@sourcery-ai

sourcery-ai Bot commented May 26, 2026

Copy link
Copy Markdown
Contributor
审阅者指南(在小型 PR 上折叠)

审阅者指南

新增一个 TWIKOO_HOST 环境变量,用于控制自托管服务器的绑定主机(bind host),将其接入 Node HTTP 服务器的 listen 调用中,并在现有环境变量文档旁说明其行为。

文件级变更

变更 详情 文件
允许通过 TWIKOO_HOST 配置自托管服务器的绑定主机,对 TWIKOO_LOCALHOST_ONLY 以及之前的 IPv6 默认值具有优先级。
  • 修改主机解析逻辑,先检查 TWIKOO_HOST,如果未设置则回退到基于 TWIKOO_LOCALHOST_ONLY 的选择,或 IPv6 任意地址。
  • 当未设置 TWIKOO_HOST 时,保持现有行为,包括仅本地主机和 :: 的默认值。
src/server/self-hosted/server.js
为后端和自托管部署文档化新的 TWIKOO_HOST 环境变量。
  • 在后端环境变量表中添加 TWIKOO_HOST 一行,解释其对 TWIKOO_LOCALHOST_ONLY 的优先级以及默认行为。
  • 在自托管 README 的环境变量表中添加 TWIKOO_HOST 一行,说明相同语义,并给出如 0.0.0.0127.0.0.1 等示例。
docs/backend.md
src/server/self-hosted/README.md

技巧与命令

与 Sourcery 交互

  • 触发新的审阅: 在 Pull Request 上评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审阅评论。
  • 从审阅评论生成 GitHub issue: 在某条审阅评论下回复,请 Sourcery 根据该评论创建 issue。你也可以直接回复 @sourcery-ai issue,从该审阅评论创建一个 issue。
  • 生成 Pull Request 标题: 在 Pull Request 标题的任意位置写上 @sourcery-ai 即可随时生成标题。你也可以在 Pull Request 中评论 @sourcery-ai title 来(重新)生成标题。
  • 生成 Pull Request 摘要: 在 Pull Request 正文的任意位置写上 @sourcery-ai summary,即可在指定位置生成 PR 摘要。你也可以在 Pull Request 中评论 @sourcery-ai summary 来(重新)生成摘要。
  • 生成审阅者指南: 在 Pull Request 中评论 @sourcery-ai guide,即可随时(重新)生成审阅者指南。
  • 一次性解决所有 Sourcery 评论: 在 Pull Request 中评论 @sourcery-ai resolve,即可将所有 Sourcery 评论标记为已解决。如果你已经处理了所有评论且不想再看到它们,这会很有用。
  • 一次性忽略所有 Sourcery 审阅: 在 Pull Request 中评论 @sourcery-ai dismiss,即可忽略所有现有的 Sourcery 审阅。若你希望以一次全新的审阅开始,这尤其有用 —— 别忘了再评论 @sourcery-ai review 以触发新的审阅!

自定义你的体验

访问你的 控制面板 来:

  • 启用或禁用审阅功能,例如 Sourcery 生成的 Pull Request 摘要、审阅者指南等。
  • 更改审阅语言。
  • 添加、移除或编辑自定义审阅说明。
  • 调整其他审阅设置。

获取帮助

Original review guide in English
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds a new TWIKOO_HOST environment variable to control the bind host of the self-hosted server, wiring it into the Node HTTP server listen call and documenting its behavior alongside existing env vars.

File-Level Changes

Change Details Files
Allow configuring the self-hosted server bind host via TWIKOO_HOST, with precedence over TWIKOO_LOCALHOST_ONLY and the previous IPv6 default.
  • Change host resolution logic to first check TWIKOO_HOST and fall back to TWIKOO_LOCALHOST_ONLY-based selection or the IPv6-any address.
  • Preserve existing behavior when TWIKOO_HOST is not set, including the localhost-only and :: defaults.
src/server/self-hosted/server.js
Document the new TWIKOO_HOST environment variable for backend and self-hosted deployments.
  • Add TWIKOO_HOST row to the backend environment variable table, explaining precedence over TWIKOO_LOCALHOST_ONLY and the default behavior.
  • Add TWIKOO_HOST row to the self-hosted README env table with the same semantics and examples like 0.0.0.0 or 127.0.0.1.
docs/backend.md
src/server/self-hosted/README.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - 我在这里给出了一些总体反馈:

  • 由于现在 TWIKOO_HOST 会覆盖 TWIKOO_LOCALHOST_ONLY,建议在环境变量表中明确记录这一优先级,这样当两个变量同时设置时,用户能清楚知道是哪一个生效。
  • 文档中将 TWIKOO_HOST 的默认值写为 null,但实际运行时会回退到 localhost::;为避免混淆,可能更清晰的做法是描述真实的回退行为,而不是写成 null
给 AI 助手的提示
Please address the comments from this code review:

## Overall Comments
- Since `TWIKOO_HOST` now overrides `TWIKOO_LOCALHOST_ONLY`, consider explicitly documenting this precedence in the env var table so users understand which setting wins when both are set.
- The docs list `TWIKOO_HOST` default as `null`, but the runtime behavior falls back to `localhost` or `::`; it may be clearer to describe the actual fallback behavior rather than `null` to avoid confusion.

Sourcery 对开源项目是免费的——如果你觉得我们的评审有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进以后的评审。
Original comment in English

Hey - I've left some high level feedback:

  • Since TWIKOO_HOST now overrides TWIKOO_LOCALHOST_ONLY, consider explicitly documenting this precedence in the env var table so users understand which setting wins when both are set.
  • The docs list TWIKOO_HOST default as null, but the runtime behavior falls back to localhost or ::; it may be clearer to describe the actual fallback behavior rather than null to avoid confusion.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Since `TWIKOO_HOST` now overrides `TWIKOO_LOCALHOST_ONLY`, consider explicitly documenting this precedence in the env var table so users understand which setting wins when both are set.
- The docs list `TWIKOO_HOST` default as `null`, but the runtime behavior falls back to `localhost` or `::`; it may be clearer to describe the actual fallback behavior rather than `null` to avoid confusion.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@WingEdge777

Copy link
Copy Markdown
Contributor Author

@sourcery-ai review

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - 我在这里给出一些高层次的反馈:

  • 建议把显式为空的 TWIKOO_HOST(例如 TWIKOO_HOST='')视为无效配置,而不是静默地回退到 ::。比如,可以在传入 server.listen 之前先对该值进行 trim 和校验,这样更容易发现配置错误。
  • 文档中写的是 TWIKOO_HOST 的默认值为 null,但运行时行为实际上会回退到 ::;为了避免概念默认值和实际默认行为之间的混淆,或许可以直接用“最终监听地址”的角度来描述默认值(例如“未设置 → 行为等同于 ::”)。
给 AI 代理的提示
Please address the comments from this code review:

## Overall Comments
- Consider treating an explicitly empty `TWIKOO_HOST` (e.g. `TWIKOO_HOST=''`) as invalid rather than silently falling back to `::`, for example by trimming and validating the value before passing it to `server.listen` so misconfigurations are easier to spot.
- The docs say `TWIKOO_HOST` default is `null` but runtime behavior is a fallback to `::`; it may be clearer to describe the default directly in terms of the effective listen address (e.g. “unset → behaves as `::`”) to avoid confusion between conceptual and actual defaults.

Sourcery 对开源项目免费——如果你觉得我们的评审有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点击 👍 或 👎,我会根据你的反馈改进后续评审。
Original comment in English

Hey - I've left some high level feedback:

  • Consider treating an explicitly empty TWIKOO_HOST (e.g. TWIKOO_HOST='') as invalid rather than silently falling back to ::, for example by trimming and validating the value before passing it to server.listen so misconfigurations are easier to spot.
  • The docs say TWIKOO_HOST default is null but runtime behavior is a fallback to ::; it may be clearer to describe the default directly in terms of the effective listen address (e.g. “unset → behaves as ::”) to avoid confusion between conceptual and actual defaults.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider treating an explicitly empty `TWIKOO_HOST` (e.g. `TWIKOO_HOST=''`) as invalid rather than silently falling back to `::`, for example by trimming and validating the value before passing it to `server.listen` so misconfigurations are easier to spot.
- The docs say `TWIKOO_HOST` default is `null` but runtime behavior is a fallback to `::`; it may be clearer to describe the default directly in terms of the effective listen address (e.g. “unset → behaves as `::`”) to avoid confusion between conceptual and actual defaults.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@imaegoo imaegoo merged commit 600442b into twikoojs:main May 26, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants