Skip to content

A full-featured download manager.

feat(server): allow external aria2 RPC access (#2008)

Dr_rOot78610342d63c
diff --git a/compose.named-volumes.yaml b/compose.named-volumes.yaml
index 25356509..3dc8ec25 100644
--- a/compose.named-volumes.yaml
+++ b/compose.named-volumes.yaml
@@ -18,6 +18,7 @@ services:
MOTRIX_PLUGIN_DIR: /data/plugins
MOTRIX_DEFAULT_SAVE_DIR: /downloads
MOTRIX_ALLOWED_SAVE_DIRS: /downloads
+ MOTRIX_ARIA2_RPC_LISTEN_ALL: "${MOTRIX_ARIA2_RPC_LISTEN_ALL:-false}"
MOTRIX_MDXP_HOST: 0.0.0.0
MOTRIX_MDXP_PORT: 16801
MOTRIX_PUBLIC_URL: "${MOTRIX_PUBLIC_URL:-}"
diff --git a/compose.yaml b/compose.yaml
index 21d4a839..966eff49 100644
--- a/compose.yaml
+++ b/compose.yaml
@@ -18,6 +18,7 @@ services:
MOTRIX_PLUGIN_DIR: /data/plugins
MOTRIX_DEFAULT_SAVE_DIR: /downloads
MOTRIX_ALLOWED_SAVE_DIRS: /downloads
+ MOTRIX_ARIA2_RPC_LISTEN_ALL: "${MOTRIX_ARIA2_RPC_LISTEN_ALL:-false}"
MOTRIX_MDXP_HOST: 0.0.0.0
MOTRIX_MDXP_PORT: 16801
MOTRIX_PUBLIC_URL: "${MOTRIX_PUBLIC_URL:-}"
diff --git a/docs/docker-server.md b/docs/docker-server.md
index 474e4dff..7c69e236 100644
--- a/docs/docker-server.md
+++ b/docs/docker-server.md
@@ -160,6 +160,45 @@ Web UI on a LAN address while keeping MDXP on host loopback. Do not change the
container's `MOTRIX_MDXP_HOST` to `127.0.0.1`: that loopback belongs to the
container, so Docker port forwarding and other containers cannot reach it.
+### Raw aria2 RPC opt-in
+
+The aria2 engine RPC endpoint is separate from the Motrix Web/API and MDXP
+services. It stays on loopback (`127.0.0.1`) by default. Set
+`MOTRIX_ARIA2_RPC_LISTEN_ALL=true` only when an external aria2 RPC client must
+reach the engine. The Server refuses this mode when the RPC secret is empty;
+verify the RPC port and secret in **Settings > Advanced** before restarting.
+Browser integrations should prefer the Motrix extension and MDXP.
+
+The included Compose files pass this opt-in to the container but intentionally
+do not publish port 16800. A client in the same Compose network can connect to
+`http://server:16800/jsonrpc` after the opt-in is enabled. For a client on the
+Docker host, save this explicit override as `compose.aria2-rpc.yaml`:
+
+```yaml
+services:
+ server:
+ ports:
+ - "127.0.0.1:16800:16800"
+```
+
+Then recreate the service with both the listener and publication enabled:
+
+```bash
+export MOTRIX_ARIA2_RPC_LISTEN_ALL=true
+docker compose -f compose.yaml -f compose.aria2-rpc.yaml up -d --wait
+```
+
+To reach it from a trusted LAN, replace the host-side `127.0.0.1` in the
+override with the NAS's specific LAN address and permit that port in the host
+firewall. If the RPC port was changed in Motrix settings, update both sides of
+the mapping. Configure the external client with the same RPC secret; aria2
+expects it as a `token:<secret>` authentication parameter. Raw aria2 RPC is
+not governed by Motrix's operator authentication or download-path policy;
+possession of the RPC secret effectively grants control of the download
+engine. It is also not TLS-protected, so never publish it directly to the
+public Internet. Prefer a private Docker network, host loopback, VPN, or
+another authenticated encrypted tunnel.
+
### Reverse proxy on the Docker host
The included [`compose.reverse-proxy.env`](../compose.reverse-proxy.env) binds
@@ -454,6 +493,7 @@ detection.
| `no matching manifest` | Confirm the NAS is 64-bit `amd64` or `arm64` with `docker info`; 32-bit ARM is unsupported. |
| Startup reports `EACCES`, read-only, or a path failure | Compare `docker inspect ... .Config.User` with numeric ownership of both mounts. Correct ownership/ACLs; do not run privileged or as root. |
| Port is already allocated | Change `MOTRIX_HTTP_PORT` or `MOTRIX_MDXP_PUBLIC_PORT`, and update `MOTRIX_PUBLIC_URL` if the Web port changes. |
+| External aria2 RPC client cannot connect | Confirm `MOTRIX_ARIA2_RPC_LISTEN_ALL=true`, a non-empty matching RPC secret, the correct 16800 mapping (or configured RPC port), and the host firewall. The standard Compose files do not publish this port. |
| Web UI opens but unlock fails | Read the current persistent `/data/operator-token`; do not use a token copied from another deployment. Verify the file is regular, base64url text, and mode `0600`. |
| Download directory is rejected or files are missing from the NAS share | Use an absolute container path below `MOTRIX_ALLOWED_SAVE_DIRS`; verify the intended host directory is mounted at that exact path and writable by the runtime UID/GID. |
| Plugin install fails | Inspect `/api/diagnostics` and logs, retain `/data`, confirm package/source trust and network/TLS access, and mount any `MOTRIX_PLUGIN_IMPORT_DIRS` explicitly. Do not enable unmanaged plugins for a normal `.moext`, URL, or registry install. |
@@ -475,6 +515,7 @@ detection.
| `MOTRIX_ALLOW_UNMANAGED_PLUGINS` | `false` | Allow plugin directories without install provenance |
| `MOTRIX_OPERATOR_TOKEN` | generated file | Operator control-plane credential |
| `MOTRIX_SECRETS_SEED` | generated lockbox | 64-hex-character plugin secret key |
+| `MOTRIX_ARIA2_RPC_LISTEN_ALL` | `false` | Opt in to an authenticated all-interface aria2 RPC listener; Docker port publication is still separate |
| `MOTRIX_WEB_BIND_IP` | Compose: `0.0.0.0` | Host address publishing Web port 8080; falls back through `MOTRIX_BIND_IP` |
| `MOTRIX_MDXP_BIND_IP` | Compose: `0.0.0.0` | Host address publishing MDXP port 16801; falls back through `MOTRIX_BIND_IP` |
| `MOTRIX_BIND_IP` | Compose: `0.0.0.0` | Backward-compatible shared host-publish fallback |
diff --git a/docs/docker-server.zh-CN.md b/docs/docker-server.zh-CN.md
index a15c53a2..542f7fd9 100644
--- a/docs/docker-server.zh-CN.md
+++ b/docs/docker-server.zh-CN.md
@@ -147,6 +147,40 @@ Docker 能够转发流量,但不会单独把服务变成公网服务。最终
`MOTRIX_MDXP_HOST` 改为 `127.0.0.1`:该 loopback 属于容器,Docker 端口转发和其他容器
都无法访问它。
+### 显式开启原始 aria2 RPC
+
+aria2 引擎 RPC endpoint 与 Motrix Web/API、MDXP 是相互独立的服务,默认只在
+loopback(`127.0.0.1`)监听。只有外部 aria2 RPC 客户端确实需要直连引擎时,才设置
+`MOTRIX_ARIA2_RPC_LISTEN_ALL=true`。RPC secret 为空时,Server 会拒绝开启该模式;
+重启前请先在**设置 > 高级设置**中确认 RPC 端口和 secret。
+浏览器集成应优先使用 Motrix 扩展和 MDXP。
+
+仓库自带的 Compose 文件会把该 opt-in 传入容器,但有意不发布 16800 端口。启用后,
+同一 Compose network 内的客户端可以连接 `http://server:16800/jsonrpc`。如果客户端
+运行在 Docker 宿主上,请把以下显式 override 保存为 `compose.aria2-rpc.yaml`:
+
+```yaml
+services:
+ server:
+ ports:
+ - "127.0.0.1:16800:16800"
+```
+
+然后同时启用 listener 和端口发布,并重建 service:
+
+```bash
+export MOTRIX_ARIA2_RPC_LISTEN_ALL=true
+docker compose -f compose.yaml -f compose.aria2-rpc.yaml up -d --wait
+```
+
+如果需要从可信 LAN 访问,请把 override 宿主侧的 `127.0.0.1` 换成 NAS 的具体 LAN
+地址,并在宿主 firewall 中只允许所需来源。若已在 Motrix 设置中修改 RPC 端口,
+映射两侧也要同步修改。外部客户端必须配置相同的 RPC secret;aria2 会把它作为
+`token:<secret>` 鉴权参数。原始 aria2 RPC 不受 Motrix operator 鉴权和下载路径
+策略约束;持有该 RPC secret 基本等同于获得下载引擎控制权。它也没有 TLS 保护,
+切勿直接发布到公网;优先使用私有 Docker network、宿主 loopback、VPN 或其他带
+鉴权的加密 tunnel。
+
### 运行在 Docker 宿主上的反向代理
仓库提供的 [`compose.reverse-proxy.env`](../compose.reverse-proxy.env) 会把两个已发布的
@@ -404,6 +438,7 @@ secret-store 状态和 FFmpeg 探测结果。
| `no matching manifest` | 用 `docker info` 确认 NAS 是 64 位 `amd64` 或 `arm64`;不支持 32 位 ARM。 |
| 启动报告 `EACCES`、只读或路径失败 | 对比 `docker inspect ... .Config.User` 与两个挂载的数字 owner。修正 owner/ACL;不要改用 privileged 或 root。 |
| 端口已分配 | 修改 `MOTRIX_HTTP_PORT` 或 `MOTRIX_MDXP_PUBLIC_PORT`;Web 端口变化时同步更新 `MOTRIX_PUBLIC_URL`。 |
+| 外部 aria2 RPC 客户端无法连接 | 确认 `MOTRIX_ARIA2_RPC_LISTEN_ALL=true`、RPC secret 非空且匹配、16800(或自定义 RPC 端口)映射正确,并检查宿主 firewall。标准 Compose 文件不会发布该端口。 |
| Web 能打开但无法解锁 | 读取当前持久化 `/data/operator-token`,不要使用另一套部署的 token。确认它是普通文件、内容为 base64url 文本且权限为 `0600`。 |
| 下载目录被拒绝或 NAS 共享目录中没有文件 | 使用 `MOTRIX_ALLOWED_SAVE_DIRS` 下的绝对容器路径;确认目标宿主目录正好挂载到该路径,并允许 runtime UID/GID 写入。 |
| 插件安装失败 | 检查 `/api/diagnostics` 与日志,保留 `/data`,核实包/来源可信且网络/TLS 可达;显式挂载所有 `MOTRIX_PLUGIN_IMPORT_DIRS`。正常 `.moext`、URL 或 registry 安装不要开启 unmanaged plugins。 |
@@ -425,6 +460,7 @@ secret-store 状态和 FFmpeg 探测结果。
| `MOTRIX_ALLOW_UNMANAGED_PLUGINS` | `false` | 允许没有安装来源记录的插件目录 |
| `MOTRIX_OPERATOR_TOKEN` | 自动生成文件 | operator 控制面凭据 |
| `MOTRIX_SECRETS_SEED` | 自动生成 lockbox | 64 位十六进制插件 secret 密钥 |
+| `MOTRIX_ARIA2_RPC_LISTEN_ALL` | `false` | 显式开启带鉴权、面向所有接口的 aria2 RPC listener;Docker 端口发布仍需单独配置 |
| `MOTRIX_WEB_BIND_IP` | Compose:`0.0.0.0` | 发布 Web 8080 端口的宿主地址;通过 `MOTRIX_BIND_IP` fallback |
| `MOTRIX_MDXP_BIND_IP` | Compose:`0.0.0.0` | 发布 MDXP 16801 端口的宿主地址;通过 `MOTRIX_BIND_IP` fallback |
| `MOTRIX_BIND_IP` | Compose:`0.0.0.0` | 向后兼容的共享宿主发布 fallback |
diff --git a/scripts/smoke-server-image.mjs b/scripts/smoke-server-image.mjs
index 729fec36..6ca7ea7d 100644
--- a/scripts/smoke-server-image.mjs
+++ b/scripts/smoke-server-image.mjs
@@ -605,6 +605,7 @@ async function assertRuntimeContract(name, url, token, identity, timeoutMs) {
"aria2_pid=$(for pid in $(pidof aria2c); do tr '\\0' '\\n' </proc/$pid/cmdline | grep -Fxq -- '--conf-path=/data/aria2.conf' && { echo $pid; break; }; done)",
'test -n "$aria2_pid"',
'test "$(readlink /proc/$aria2_pid/exe)" = "/app/bin/aria2c"',
+ "tr '\\0' '\\n' </proc/$aria2_pid/cmdline | grep -Fx -- '--rpc-listen-all=false'",
"tr '\\0' '\\n' </proc/$aria2_pid/cmdline | grep -Fx -- '--dht-file-path=/data/dht.dat'",
"tr '\\0' '\\n' </proc/$aria2_pid/cmdline | grep -Fx -- '--dht-file-path6=/data/dht6.dat'",
].join('; '),
diff --git a/src/core/engine/aria2/aria2-config-builder.test.ts b/src/core/engine/aria2/aria2-config-builder.test.ts
index 24484046..44896494 100644
--- a/src/core/engine/aria2/aria2-config-builder.test.ts
+++ b/src/core/engine/aria2/aria2-config-builder.test.ts
@@ -229,6 +229,46 @@ describe('Aria2ConfigBuilder', () => {
expect(args).toContain('--rpc-listen-all=false')
})
+ it('allows an explicit all-interface RPC listener with authentication', () => {
+ const externalBuilder = new Aria2ConfigBuilder(
+ '/app/extra/aria2.conf',
+ '/home/user/.config/motrix',
+ { rpcListenAll: true }
+ )
+
+ const args = externalBuilder.buildArgs(
+ makeEngineSettings(),
+ true,
+ null,
+ { download: 0, upload: 0 },
+ DEFAULT_SAVE_DIR
+ )
+
+ expect(args).toContain('--rpc-listen-all=true')
+ expect(args).not.toContain('--rpc-listen-all=false')
+ })
+
+ it.each(['', ' '])(
+ 'refuses an all-interface RPC listener with a blank secret',
+ (rpcSecret) => {
+ const externalBuilder = new Aria2ConfigBuilder(
+ '/app/extra/aria2.conf',
+ '/home/user/.config/motrix',
+ { rpcListenAll: true }
+ )
+
+ expect(() =>
+ externalBuilder.buildArgs(
+ { ...makeEngineSettings(), rpcSecret },
+ true,
+ null,
+ { download: 0, upload: 0 },
+ DEFAULT_SAVE_DIR
+ )
+ ).toThrow('External aria2 RPC access requires a non-empty RPC secret')
+ }
+ )
+
it('includes save-session path in userConfigDir', () => {
const args = buildArgs(DEFAULT_ENGINE_SETTINGS, true, null, {
download: 0,
diff --git a/src/core/engine/aria2/aria2-config-builder.ts b/src/core/engine/aria2/aria2-config-builder.ts
index 4919e378..0343c0b4 100644
--- a/src/core/engine/aria2/aria2-config-builder.ts
+++ b/src/core/engine/aria2/aria2-config-builder.ts
@@ -5,22 +5,29 @@ import type { Aria2ProxyOptions } from '@core/proxy/serializers'
import type { EngineSettings } from '@shared/types/settings'
import { dnsModeToAsyncDns } from './dns-fallback'
+export interface Aria2ConfigBuilderOptions {
+ rpcListenAll?: boolean
+}
+
export class Aria2ConfigBuilder {
private readonly userConfPath: string
private readonly defaultDbPath: string
private readonly saveSessionPath: string
private readonly dhtFilePath: string
private readonly dht6FilePath: string
+ private readonly rpcListenAll: boolean
constructor(
private templatePath: string,
- private userConfigDir: string
+ private userConfigDir: string,
+ options: Aria2ConfigBuilderOptions = {}
) {
this.userConfPath = path.join(userConfigDir, 'aria2.conf')
this.defaultDbPath = path.join(userConfigDir, 'aria2.db')
this.saveSessionPath = path.join(userConfigDir, 'aria2.session')
this.dhtFilePath = path.join(userConfigDir, 'dht.dat')
this.dht6FilePath = path.join(userConfigDir, 'dht6.dat')
+ this.rpcListenAll = options.rpcListenAll ?? false
}
async ensureUserConfig(): Promise<string> {
@@ -115,6 +122,12 @@ export class Aria2ConfigBuilder {
defaultSaveDir: string,
loadTextSession = false
): string[] {
+ if (this.rpcListenAll && settings.rpcSecret.trim() === '') {
+ throw new Error(
+ 'External aria2 RPC access requires a non-empty RPC secret'
+ )
+ }
+
// Layer order in the produced argv:
// L4 conf-path → L2 engine bindings → L3 user-tunable → L1 invariants
//
@@ -132,7 +145,7 @@ export class Aria2ConfigBuilder {
args.push(
'--enable-rpc=true',
`--rpc-allow-origin-all=${settings.rpcSecret === '' ? 'false' : 'true'}`,
- '--rpc-listen-all=false',
+ `--rpc-listen-all=${this.rpcListenAll}`,
`--rpc-listen-port=${settings.rpcPort}`
)
if (settings.rpcSecret !== '') {
diff --git a/src/server/environment.test.ts b/src/server/environment.test.ts
index 13f14aea..db2f0b21 100644
--- a/src/server/environment.test.ts
+++ b/src/server/environment.test.ts
@@ -1,5 +1,26 @@
import { describe, expect, it } from 'vitest'
-import { parseServerPort } from './environment'
+import { parseServerBoolean, parseServerPort } from './environment'
+
+describe('parseServerBoolean', () => {
+ it('uses the fallback when the variable is unset or blank', () => {
+ expect(parseServerBoolean(undefined, 'FEATURE_ENABLED')).toBe(false)
+ expect(parseServerBoolean('', 'FEATURE_ENABLED')).toBe(false)
+ expect(parseServerBoolean(' ', 'FEATURE_ENABLED', true)).toBe(true)
+ })
+
+ it('accepts strict boolean values', () => {
+ expect(parseServerBoolean('true', 'FEATURE_ENABLED')).toBe(true)
+ expect(parseServerBoolean(' 1 ', 'FEATURE_ENABLED')).toBe(true)
+ expect(parseServerBoolean('FALSE', 'FEATURE_ENABLED')).toBe(false)
+ expect(parseServerBoolean('0', 'FEATURE_ENABLED')).toBe(false)
+ })
+
+ it('rejects ambiguous values with the variable name', () => {
+ expect(() => parseServerBoolean('yes', 'FEATURE_ENABLED')).toThrow(
+ 'FEATURE_ENABLED must be true, false, 1, or 0'
+ )
+ })
+})
describe('parseServerPort', () => {
it('uses the fallback only when the variable is unset', () => {
diff --git a/src/server/environment.ts b/src/server/environment.ts
index 3d465685..2b1ae2df 100644
--- a/src/server/environment.ts
+++ b/src/server/environment.ts
@@ -1,3 +1,15 @@
+export function parseServerBoolean(
+ value: string | undefined,
+ name: string,
+ fallback = false
+): boolean {
+ if (value === undefined || value.trim() === '') return fallback
+ const normalized = value.trim().toLowerCase()
+ if (normalized === 'true' || normalized === '1') return true
+ if (normalized === 'false' || normalized === '0') return false
+ throw new Error(`${name} must be true, false, 1, or 0`)
+}
+
export function parseServerPort(
value: string | undefined,
name: string,
diff --git a/src/server/index.ts b/src/server/index.ts
index 43c8a462..67921bae 100644
--- a/src/server/index.ts
+++ b/src/server/index.ts
@@ -117,7 +117,7 @@ import {
createServerDownloadPathPolicy,
resolveServerDefaultSaveDir,
} from './download-path-policy'
-import { parseServerPort } from './environment'
+import { parseServerBoolean, parseServerPort } from './environment'
import { createApp } from './http/app'
import { buildServerCommandHandlers } from './ipc/commands'
import { buildServerQueryHandlers } from './ipc/queries'
@@ -394,7 +394,13 @@ async function main() {
})
const configBuilder = new Aria2ConfigBuilder(
path.join(platform.extraResourceDir, 'aria2.conf'),
- platform.userDataDir
+ platform.userDataDir,
+ {
+ rpcListenAll: parseServerBoolean(
+ process.env.MOTRIX_ARIA2_RPC_LISTEN_ALL,
+ 'MOTRIX_ARIA2_RPC_LISTEN_ALL'
+ ),
+ }
)
const trustStore = new Aria2TrustStore(platform.userDataDir)
diff --git a/tests/scripts/compose-server.test.ts b/tests/scripts/compose-server.test.ts
index 18964a70..2c5b8a7a 100644
--- a/tests/scripts/compose-server.test.ts
+++ b/tests/scripts/compose-server.test.ts
@@ -18,6 +18,7 @@ const MDXP_BIND_IP = interpolation(`MOTRIX_MDXP_BIND_IP:-${LEGACY_BIND_IP}`)
const HTTP_PORT = interpolation('MOTRIX_HTTP_PORT:-8080')
const MDXP_PORT = interpolation('MOTRIX_MDXP_PUBLIC_PORT:-16801')
const PUBLIC_URL = interpolation('MOTRIX_PUBLIC_URL:-')
+const ARIA2_RPC_LISTEN_ALL = interpolation('MOTRIX_ARIA2_RPC_LISTEN_ALL:-false')
function record(value: unknown, label: string): LooseRecord {
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
@@ -58,6 +59,7 @@ describe('NAS-importable Compose contract', () => {
])
expect(service.environment).toEqual({
MOTRIX_ALLOWED_SAVE_DIRS: '/downloads',
+ MOTRIX_ARIA2_RPC_LISTEN_ALL: ARIA2_RPC_LISTEN_ALL,
MOTRIX_DATA_DIR: '/data',
MOTRIX_DEFAULT_SAVE_DIR: '/downloads',
MOTRIX_MDXP_HOST: '0.0.0.0',
diff --git a/tests/scripts/docker-server-docs.test.ts b/tests/scripts/docker-server-docs.test.ts
index 2628b354..d7910a24 100644
--- a/tests/scripts/docker-server-docs.test.ts
+++ b/tests/scripts/docker-server-docs.test.ts
@@ -34,6 +34,8 @@ describe('public Docker Server documentation', () => {
'MOTRIX_WEB_BIND_IP',
'MOTRIX_MDXP_BIND_IP',
'MOTRIX_BIND_IP',
+ 'MOTRIX_ARIA2_RPC_LISTEN_ALL',
+ '16800',
'/mdxp/events',
'/healthz',
'/api/diagnostics',