Webstone
使用 WebSocket 远程控制你的红石装置。
背景
本模组受 Fundy 的一个 YouTube 视频启发,他在视频中简要展示了一个可以通过浏览器控制的自定义红石块。该视频部分从未作为独立模组发布,因此 Webstone 应运而生。
用法
该模组会在端口 4321 上启动一个 WebSocket 服务器,允许你通过任何 WebSocket 客户端(如 此客户端 - 或托管在 的客户端)连接,以切换已注册方块的状态或更改其输出信号强度。
你可以在创造模式的“红石块”标签页中找到 Webstone Remote Block(生存模式暂无合成配方)。要注册它,请空手右键点击该方块(如果成功,你会在快捷栏上方看到一条消息)。要本地切换它,请在潜行状态下右键点击该方块。
合成配方
从 Webstone 1.0.1 开始,Webstone Remote Block 可以在非创造模式下合成:

配置选项
Webstone 拥有自己的配置文件,位于 .minecraft/config/webstone-config.toml。你可以设置以下选项:
| 键 |
默认值 |
描述 |
Passphrase |
"" |
用于仅允许授权用户连接的密码短语。 |
WebSocketPort |
4321 |
Webstone WebSocket 服务器使用的端口。 |
SecureWebSocket |
false |
指定 WebSocket 是否应使用安全连接。 |
CertificateFilename |
cert.pem |
.minecraft/data 内证书公钥文件的文件名。 |
CertificateKeyFilename |
key.pem |
.minecraft/data 内证书私钥文件的文件名。 |
CertificateKeyPass |
"" |
用于私钥的密码短语。 |
API 参考
服务器 → 客户端
如果 WebSocket 服务器需要密码短语才能连接,客户端需要在 Sec-WebSocket-Protocol 头中以十六进制字符串形式提供该密码短语。使用 JavaScript,你可以在创建 WebSocket 实例时,在连接 URL 之后将其作为第二个参数添加到数组中:
const socket = new WebSocket('ws://<IP address or FQDN>:4321', ['<your passphrase>']);
当连接到 WebSocket 服务器时,连接的客户端会收到当前已注册方块的列表(包括其显示名称、当前通电状态、输出信号强度和分配的组 ID(如果有)),以及方块组。
{
"type": "block_list",
"data": [
{
"blockId": "00000000-0000-0000-0000-000000000000",
"name": "Example",
"power": 15,
"powered": false,
"groupId": "00000000-0000-0000-0000-000000000000"
},
// ...
]
}
{
"type": "block_groups",
"data": [
{
"groupId": "00000000-0000-0000-0000-000000000000",
"name": "Example Group",
"blockIds": [
//...
]
},
// ...
]
}
如果任何方块或组发生更新,其数据将广播给所有连接的客户端。
{
"type": "block_updated",
"data": {
"blockId": "00000000-0000-0000-0000-000000000000",
"name": "Example",
"power": 15,
"powered": false,
"groupId": "00000000-0000-0000-0000-000000000000"
}
}
{
"type": "group_updated",
"data":
"groupId": "00000000-0000-0000-0000-000000000000",
"name": "Example Group",
"blockIds": [
//...
]
}
}
客户端 → 服务器
方块管理
设置方块的通电状态:
{
"type": "block_state",
"data": {
"blockId": "00000000-0000-0000-0000-000000000000",
"powered": false // 可以是 true 或 false
}
}
设置方块的输出信号强度:
{
"type": "block_power",
"data": {
"blockId": "00000000-0000-0000-0000-000000000000",
"power": 7 // 可以是 0 到 15 之间的任意值
}
}
设置方块的显示名称:
{
"type": "rename_block",
"data": {
"blockId": "00000000-0000-0000-0000-000000000000",
"name": "My Example Block"
}
}
从 Web UI 中删除方块:
{
"type": "unregister_block",
"data": {
"blockId": "00000000-0000-0000-0000-000000000000"
}
}
将方块添加或移除出组:
{
"type": "change_group",
"data": {
"blockId": "00000000-0000-0000-0000-000000000000",
"groupId": "00000000-0000-0000-0000-000000000000"
}
}
将方块在组中移动到其他位置:
这仅对已分配到组的方块产生影响。
{
"type": "move_block",
"data": {
"blockId": "00000000-0000-0000-0000-000000000000",
"newIndex": "2"
}
}
分组管理
创建新分组:
{
"type": "create_group",
"data": {
"name": "My awesome group"
}
}
重命名分组:
{
"type": "create_group",
"data": {
"groupId": "00000000-0000-0000-0000-000000000000",
"name": "My awesome (renamed) group"
}
}
删除分组:
{
"type": "delete_group",
"data": {
"groupId": "00000000-0000-0000-0000-000000000000"
}
}
将分组移动到所有分组列表中的新位置:
{
"type": "move_group",
"data": {
"groupId": "00000000-0000-0000-0000-000000000000",
"newIndex": 2
}
}
安全性
在 Webstone 1.0.2 中,添加了两个安全选项:连接 WebSocket 服务器所需的可选密码短语,以及使用 SSL 证书支持安全 WebSocket(Secure WebSocket)。
密码短语
为了保护你的服务器免受未授权访问,你可以添加一个可选的密码短语,客户端在连接到 WebSocket 服务器之前需要提交该密码短语。此密码短语需要使用 Bcrypt 进行哈希处理,这样你就不需要以明文形式存储密码短语。
要在基于 Debian 的系统上生成 Bcrypt 哈希值,可以使用 mkpasswd(位于 whois 包中):
$ mkpasswd -m bcrypt <your password>
$2b$05$aHZ8W4AL3o.GnXN5ocSWXumD0Qlu0fU5jLGseYdLzbDW5N1d21Poa
然后将结果添加到配置中的 Passphrase 键。
如 API 参考所述,连接到 WebSocket 服务器时,需要在客户端设置 Sec-WebSocket-Protocol 头部为十六进制编码的密码短语,否则连接将被拒绝。
使用 TLS 的安全 WebSocket
默认情况下,WebSocket 服务器与客户端之间的流量是未加密的。要加密流量,你需要一个域名以及专门针对该域名的 SSL 证书,包括私钥和公钥。这两个密钥需要分别存储在 .minecraft/data 目录下,命名为 cert.pem(公钥)和 key.pem(私钥)。如果私钥有单独的密码短语,你可以在配置中设置 CertificateKeyPass 键。
要启用或禁用安全 WebSocket,请将 SecureWebSocket 键设置为 true 或 false。
支持
目前仅支持 Forge。欢迎将其移植到你喜欢的任何其他加载器,只要明确声明本项目为原始项目即可。
该模组已在 Minecraft 1.20.1 上测试过,但也应该能在更高版本中运行。它可能兼容也可能不兼容旧版本的 Minecraft。
鸣谢
Fundy - 原始创意、纹理(大概)
Kaupenjoe - 制作了一套教程系列,使这个模组的诞生成为可能
Webstone
Remote control your Redstone contraptions using WebSockets.
Background
This mod is inspired by a YouTube video by Fundy, in which he briefly shows a custom Redstone block that can be controlled via a browser. This part of the video never got released as a standalone mod, so this is where Webstone comes in.
Usage
The mod opens up a WebSocket server on port 4321, which then lets you connect with any WebSocket client (like this one - or the hosted client at ) to toggle registered blocks or change their output signal strength.
You can find the Webstone Remote Block under the "Redstone Blocks" tab in Creative Mode (no Survival recipe yet). To register it, right-click it with an empty hand (if successful, you'll see a message above your hotbar). To toggle it locally, right-click the block while sneaking.
Crafting Recipe
Starting with Webstone 1.0.1, the Webstone Remote Block can be crafted outside of Creative:

Configuration Options
Webstone has its own configuration file, located at .minecraft/config/webstone-config.toml. You can set the following options:
| Key |
Default value |
Description |
Passphrase |
"" |
Passphrase to allow only authorized users. |
WebSocketPort |
4321 |
Port used by the Webstone WebSocket Server. |
SecureWebSocket |
false |
Specifies if the WebSocket should use a secure connection. |
CertificateFilename |
cert.pem |
Filename of the certificate public key inside .minecraft/data. |
CertificateKeyFilename |
key.pem |
Filename of the certificate private key inside .minecraft/data. |
CertificateKeyPass |
"" |
Passphrase used for the private key. |
API Reference
Server → Client
If the WebSocket server needs a passphrase to connect to, clients need to supply it encoded as a hex string in the Sec-WebSocket-Protocol header. Using JavaScript, you can add it to an array as a second parameter when creating a WebSocket instance, following the connection URL:
const socket = new WebSocket('ws://<IP address or FQDN>:4321', ['<your passphrase>']);
When connecting to the WebSocket server, the connecting client receives a list of currently registered blocks (including their display name, the current powered state, the output signal strength and the assigned group's id, if any), as well as block groups.
{
"type": "block_list",
"data": [
{
"blockId": "00000000-0000-0000-0000-000000000000",
"name": "Example",
"power": 15,
"powered": false,
"groupId": "00000000-0000-0000-0000-000000000000"
},
// ...
]
}
{
"type": "block_groups",
"data": [
{
"groupId": "00000000-0000-0000-0000-000000000000",
"name": "Example Group",
"blockIds": [
//...
]
},
// ...
]
}
If any block or group is updated, their data will broadcast to every connected client.
{
"type": "block_updated",
"data": {
"blockId": "00000000-0000-0000-0000-000000000000",
"name": "Example",
"power": 15,
"powered": false,
"groupId": "00000000-0000-0000-0000-000000000000"
}
}
{
"type": "group_updated",
"data":
"groupId": "00000000-0000-0000-0000-000000000000",
"name": "Example Group",
"blockIds": [
//...
]
}
}
Client → Server
Block Management
Set a block's powered state:
{
"type": "block_state",
"data": {
"blockId": "00000000-0000-0000-0000-000000000000",
"powered": false // Can be either true or false
}
}
Set a block's output signal strength:
{
"type": "block_power",
"data": {
"blockId": "00000000-0000-0000-0000-000000000000",
"power": 7 // Can be anything between 0 and 15
}
}
Set the display name of a block:
{
"type": "rename_block",
"data": {
"blockId": "00000000-0000-0000-0000-000000000000",
"name": "My Example Block"
}
}
Delete a block from the Web UI:
{
"type": "unregister_block",
"data": {
"blockId": "00000000-0000-0000-0000-000000000000"
}
}
Add or remove a block to/from a group:
{
"type": "change_group",
"data": {
"blockId": "00000000-0000-0000-0000-000000000000",
"groupId": "00000000-0000-0000-0000-000000000000"
}
}
Move a block to a different position in a group:
This only affects a block if it has been assigned to a group.
{
"type": "move_block",
"data": {
"blockId": "00000000-0000-0000-0000-000000000000",
"newIndex": "2"
}
}
Group Management
Create a new group:
{
"type": "create_group",
"data": {
"name": "My awesome group"
}
}
Rename a group:
{
"type": "create_group",
"data": {
"groupId": "00000000-0000-0000-0000-000000000000",
"name": "My awesome (renamed) group"
}
}
Delete a group:
{
"type": "delete_group",
"data": {
"groupId": "00000000-0000-0000-0000-000000000000"
}
}
Move a group to a new position in the list of all groups:
{
"type": "move_group",
"data": {
"groupId": "00000000-0000-0000-0000-000000000000",
"newIndex": 2
}
}
Security
In Webstone 1.0.2, two security options have been added: an optional passphrase required to connect to the WebSocket server, as well as support for Secure WebSockets using SSL certificates.
Passphrase
In order to protect your server from unauthorized access, you can add an optional passphrase that clients need to submit before connecting to the WebSocket Server. This passphrase needs to be hashed using Bcrypt, so you don't have to store your passphrase in plain text.
To generate a Bcrypt hash, you can use mkpasswd on Debian-based systems (available in the whois package):
$ mkpasswd -m bcrypt <your password>
$2b$05$aHZ8W4AL3o.GnXN5ocSWXumD0Qlu0fU5jLGseYdLzbDW5N1d21Poa
The result then needs to be added to the configuration using the Passphrase key.
As described in the API Reference, connecting to the WebSocket server then needs the Sec-WebSocket-Protocol header to be set client-side to the hex-encoded passphrase in order for the connection to not be rejected.
Secure WebSocket using TLS
By default, the traffic between the WebSocket server and clients is unencrypted. To encrypt the traffic, you'll need a domain name and a SSL certificate for that domain specifically, including the private and the public key. Both keys need to be stored in .minecraft/data as cert.pem (public key) and key.pem (private key) respectively. If the private key requires its own passphrase, you can set the CertificateKeyPass key in the config.
To enable or disable Secure WebSocket, set the SecureWebSocket key to either true or false.
Support
Currently, only Forge is supported. Feel free to port this over to any other mod loader you like, as long as you clearly state this project as the original one.
The mod has been tested on Minecraft 1.20.1, but should also work on newer versions. It also may or may not work on older versions of Minecraft.
Credits
Fundy - Original idea, textures (probably)
Kaupenjoe - Creating a tutorial series that made this mod possible in the first place
评论 0
登录后参与评分
还没有评论,来抢沙发吧