Self-Hosted Sync Server

Anki 2.1.57+包括一个内置的同步服务器。不能或不希望使用 AnkiWeb 的高级用户可以使用此同步服务器而不是 AnkiWeb。

Anki 2.1.57+ includes a built-in sync server. Advanced users who cannot or do not wish to use AnkiWeb can use this sync server instead of AnkiWeb.

需要注意的事项:

Things to be aware of:

  • 这是一个高级功能,针对那些熟悉网络和命令行的用户。如果你使用它,你可以解决自己遇到的任何设置/网络/防火墙问题,使用它的风险完全由你自己承担。

    This is an advanced feature, targeted at users who are comfortable with networking and the command line. If you use this, the expectation is you can resolve any setup/network/firewall issues you run into yourself, and use of this is entirely at your own risk.

  • 较新的客户端可能取决于同步协议的更改,因此如果你在不更新服务器的情况下更新 Anki 客户端,同步可能会停止工作。

    Newer clients may depend on changes to the sync protocol, so syncing may stop working if you update your Anki clients without also updating the server.

  • 还存在第三方同步服务器。没有针对它们进行测试,而且当同步协议发生变化时,它们往往需要时间才能跟上,因此不建议使用它们。

    Third-party sync servers also exist. No testing is done against them, and they tend to take time to catch up when the sync protocol changes, so they are not recommended.

  • 即使配置了自定义服务器,Anki 内部的消息也会使用术语“AnkiWeb”(例如,当服务器关闭时,“无法连接到 AnkiWeb)。

    The messages inside Anki will use the term 'AnkiWeb' even if a custom server has been configured, (e.g "Cannot connect to AnkiWeb" when your server is down).

安装/运行

Installing/Running

有多种方法可以安装和运行服务器。

There are various ways you can install and run the server.

从打包的生成

From a Packaged Build

Windows cmd.exe 会话中:

On Windows in a cmd.exe session:

set SYNC_USER1=user:pass
"\Program Files\anki\anki.exe" --syncserver

MacOS 终端:

Or MacOS, in Terminal.app:

SYNC_USER1=user:pass /Applications/Anki.app/Contents/MacOS/anki --syncserver

Linux:

SYNC_USER1=user:pass anki --syncserver
With Pip

With Pip

如果你安装了 Python3.9+,你可以从 PyPI 运行,而无需下载所有 Anki 的 GUI 依赖项。

If you have Python 3.9+ installed, you can run from PyPI without downloading all Anki's GUI dependencies.

python3 -m venv ~/syncserver
~/syncserver/bin/pip install anki
SYNC_USER1=user:pass ~/syncserver/bin/python -m anki.syncserver
With Cargo

With Cargo

如果你安装了 Rustup,从 Anki 2.1.66+开始,你可以使用以下方法构建一个不需要 Python 的独立同步服务器:

If you have Rustup installed, from Anki 2.1.66+, you can build a standalone sync server that doesn't require Python, using the following:

cargo install --git https://github.com/ankitects/anki.git --tag 2.1.66 anki-sync-server

需要安装 Protobuf(protoc)。

Protobuf (protoc) will need to be installed.

运行上面命令之后,你可以运行

After running that, you can run it with

SYNC_USER1=user:pass anki-sync-server
From a source checkout

如果你已经从 GitHub 克隆了 Anki repo,你可以从那里安装:

If you've cloned the Anki repo from GitHub, you can install from there:

./ninja extract:protoc
cargo install --path rslib/sync

多用户

Multiple Users

SYNC_USER1 声明第一个用户和密码,并且必须进行设置。如果你希望设置多个帐户,你可以选择声明 SYNC_USER2、SYNC_USER3 等。

SYNC_USER1 declares the first user and password, and must be set. You can optionally declare SYNC_USER2, SYNC_USER3 and so on, if you wish to set up multiple accounts.

存储位置

Storage Location

服务器需要将收集合和媒体的副本存储在文件夹中。默认情况下,它是 ~/.syncserver;你可以通过定义 SYNC_BASE 环境变量来更改此设置。这不能与普通的 Anki 数据文件夹位于同一位置,因为服务器和客户端必须存储单独的副本。

The server needs to store a copy of your collection and media in a folder. By default it is ~/.syncserver; you can change this by defining a SYNC_BASE environmental variable. This must not be the same location as your normal Anki data folder, as the server and client must store separate copies.

公共访问

Public Access

服务器监听未加密的 HTTP 连接,因此将其直接暴露在互联网上不是一个好主意。你可能希望将使用限制在本地网络上,或者在服务器前放置某种形式的加密,例如 VPN(Tailscale 显然很容易)或 HTTPS 反向代理。

The server listens on an unencrypted HTTP connection, so it's not a good idea to expose it directly to the internet. You'll want to either restrict usage to your local network, or place some form of encryption in front of the server, such as a VPN (Tailscale is apparently easy), or a HTTPS reverse proxy.

你可以定义 SYNC_HOST 和 SYNC_PORT 来更改服务器绑定到的主机和端口。

You can define SYNC_HOST and SYNC_PORT to change the host and port that the server binds to.

客户端设置

Client Setup

你需要确定计算机的网络 IP 地址,然后将每个 Anki 客户端指向该地址,例如 http://192.168.1.200:8080/。URL 可以在首选项中进行配置。

You'll need to determine your computer's network IP address, and then point each of your Anki clients to the address, e.g something like http://192.168.1.200:8080/. The URL can be configured in the preferences.

如果你正在使用 AnkiMobile,并且无法连接到本地网络上的服务器,请进入 iOS 设置,在底部附近找到 Anki,然后关闭并再次打开“允许 Anki 访问本地网络”。

If you're using AnkiMobile and are unable to connect to a server on your local network, please go into the iOS settings, locate Anki near the bottom, and toggle "Allow Anki to access local network" off and then on again.

较旧的桌面客户端要求你定义 SYNC_ENDPOINT 和 SYNC_ENDPONT_MEDIA。如果使用较旧的客户端,你可以将其设置为例如。http://192.168.1.200:8080/sync/http://192.168.1.200:8080/msync/ 分别地 2.16 之前的 AnkiDroid 客户端需要对两个端点进行单独配置。

Older desktop clients required you to define SYNC_ENDPOINT and SYNC_ENDPOINT_MEDIA. If using an older client, you'd put it as e.g. http://192.168.1.200:8080/sync/ and http://192.168.1.200:8080/msync/ respectively. AnkiDroid clients before 2.16 require separate configuration for the two endpoints.

反向代理

Reverse Proxies

如果使用反向代理提供 HTTPS 访问(例如 nginx),并绑定到子路径(例如。http://example.com/custom/->http://localhost:8080/),你必须确保在配置 Anki 时包含尾部斜杠。如果你使用http://example.com/custom ,它不会起作用。

If using a reverse proxy to provide HTTPS access (e.g. nginx), and binding to a subpath (e.g. http://example.com/custom/ -> http://localhost:8080/), you must make sure to including a trailing slash when configuring Anki. If you put http://example.com/custom instead, it will not work.

在 iOS 上,TLS 1.3 不受支持,因此你的反向代理需要启用 TLS 1.2,否则你将收到“错误代码-9836”。

On iOS, TLS 1.3 is not supported, so your reverse proxy will need to have TLS 1.2 enabled, or you'll get an "error code -9836".

大量请求

Large Requests

默认情况下,对上传应用标准的 AnkiWeb 限制。如果希望增加限制,你可以选择将 MAX_SYNC_PAYLOAD_MEGS 设置为大于 100。请记住,如果你使用反向代理,你可能也需要调整限制。

The standard AnkiWeb limit on uploads is applied by default. You can optionally set MAX_SYNC_PAYLOAD_MEGS to something greater than 100 if you wish to increase the limit. Bear in mind that if you're using a reverse proxy, you may need to adjust the limit there as well.

贡献改变

Contributing Changes

由于此服务器与 Anki 捆绑在一起,因此简单性是设计目标-它针对个人/家庭使用,而添加 REST API 或外部数据库等内容的 PR 目前不太可能被接受。如果有疑问,请在开始 PR 工作之前联系。

Because this server is bundled with Anki, simplicity is a design goal - it is targeted at individual/family use, and PRs that add things like a REST API or external databases are unlikely to be accepted at this time. If in doubt, please reach out before starting work on a PR.

如果你正在寻找现有的 API 解决方案,AnkiConnect 插件可能会满足你的需求。

If you're looking for an existing API solution, the AnkiConnect add-on may meet your needs.