feat: add ALLOW-LAN; command: clashctl lan status/on/off#252
Merged
Conversation
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR: Improve LAN Proxy Support / 改进局域网代理支持
Background / 背景
This project already had a basic
allow-lan: truedefault in the bundled Clash/Mihomo template, but LAN access could still fail or be unclear in real usage.项目原本已经在内置 Clash/Mihomo 模板中提供了基础的
allow-lan: true默认值,但在实际使用中,局域网访问仍可能失败,且用户不容易判断当前是否真的生效。The original behavior had three main problems:
原行为主要有三个问题:
The runtime config respected subscription-provided
allow-lan.运行时配置会保留订阅中提供的
allow-lan。Some subscription YAML files include
allow-lan: false. During runtime config normalization, the project only filledallow-lanwhen it was missing:部分订阅 YAML 会包含
allow-lan: false。此前运行时配置规范化时,只会在字段缺失时补默认值:That meant a subscription could silently disable LAN access even though the project default expected it to be enabled.
因此,即使项目默认希望开启局域网访问,订阅也可能静默地把它关闭。
The checked-in default template exposed mixed proxy LAN access, but the controller template still used localhost.
仓库中的默认模板开启了 mixed proxy 的局域网访问,但控制器模板仍绑定在本机回环地址。
config/template.yamlhadallow-lan: true, butexternal-controllerwas bound to127.0.0.1:9090. This made local Web UI access work, while LAN Web UI access depended on runtime regeneration or other paths.config/template.yaml中有allow-lan: true,但external-controller绑定为127.0.0.1:9090。这会导致本机 Web UI 可用,但局域网 Web UI 是否可访问取决于运行时重新生成配置等其他路径。There was no explicit LAN management command.
缺少显式的局域网代理管理命令。
Users had to inspect generated YAML or infer behavior from
statusoutput. There was no simple command to confirm, enable, or disable LAN proxy access.用户需要手动检查生成后的 YAML,或者从
status输出中推断行为;没有一个简单命令用于确认、开启或关闭局域网代理。Changes / 改动
1. Make LAN access a project-level runtime setting / 将局域网访问变为项目级运行时设置
Added project-level helpers in
scripts/core/config.sh:在
scripts/core/config.sh中新增项目级辅助函数:config_allow_lanset_config_allow_lanRuntime config normalization now writes
allow-lanfrom the project template setting instead of preserving the subscription value. This prevents upstream subscriptions from unexpectedly turning LAN access off.运行时配置规范化现在会使用项目模板中的设置写入
allow-lan,不再保留订阅里的同名值。这样可以避免上游订阅意外关闭局域网访问。Before / 修改前:
After / 修改后:
Default / 默认值:
2. Bind the controller to all interfaces by default / 默认将控制器绑定到所有网卡
Updated
config/template.yaml:更新
config/template.yaml:This matches the runtime-generated template and allows LAN clients to reach the Web UI when the port is permitted by the host firewall.
这与运行时生成模板保持一致;当宿主机防火墙放行端口时,局域网设备可以访问 Web UI。
3. Add runtime status support for
allow-lan/ 增加运行时allow-lan状态读取能力Added
runtime_config_allow_laninscripts/core/common.shso command output can read the effective runtime value fromruntime/config.yaml.在
scripts/core/common.sh中新增runtime_config_allow_lan,使命令输出可以从runtime/config.yaml中读取实际生效的运行时值。4. Add
clashctl lan/ 新增clashctl lanAdded a dedicated LAN management command:
新增专门的局域网代理管理命令:
Behavior / 行为:
status: shows whether LAN proxy is enabled and prints the LAN proxy URL when available.status显示局域网代理是否开启,并在可用时输出局域网代理地址。on: writesallow-lan: true, regenerates runtime config, and applies the change.on写入allow-lan: true,重新生成运行时配置,并应用变更。off: writesallow-lan: false, regenerates runtime config, and applies the change.off写入allow-lan: false,重新生成运行时配置,并应用变更。5. Improve
clashctl status/ 改进clashctl statusclashctl statusandclashctl status --verbosenow show LAN proxy information alongside the local proxy:clashctl status和clashctl status --verbose现在会在本地代理旁展示局域网代理信息:If LAN access is disabled, status reports:
如果局域网访问已关闭,状态输出为:
6. Add shell completion / 增加 Shell 补全
Updated completion support for:
补充以下命令的补全支持:
User Impact / 用户影响
After this change, users have a clear and predictable way to expose the proxy to LAN devices or Docker containers.
此改动后,用户可以用更清晰、可预期的方式将代理暴露给局域网设备或 Docker 容器。
Typical Docker usage / 典型 Docker 用法:
Inside a Docker container using the default bridge network, the host is usually reachable at:
在使用默认 bridge 网络的 Docker 容器中,宿主机通常可通过以下地址访问:
So the proxy variables can be set as:
因此容器内可以设置以下代理变量:
Note: host firewalls such as UFW may still need to allow Docker bridge traffic to the proxy port:
如果宿主机启用了 UFW 等防火墙,仍可能需要放行 Docker bridge 到代理端口的访问:
sudo ufw allow in on docker0 to any port 7890 proto tcpVerification / 验证
Commands used for verification:
验证时使用的命令:
Expected runtime config / 预期运行时配置:
Expected listening ports / 预期监听端口:
Expected host-side proxy check / 预期宿主机侧代理检查:
Notes / 备注
*:7890but Docker or LAN clients time out, the most likely cause is firewall policy.*:7890,但 Docker 或局域网客户端访问超时,最常见原因是防火墙策略。mixed-port: 7890, so both HTTP and SOCKS proxy traffic can use port7890unless a separatesocks-portis configured.mixed-port: 7890,因此除非额外配置了独立的socks-port,HTTP 与 SOCKS 代理都可以走7890。