aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: aa7f24fb47beb3228937c142cb92f7e4b69cf92c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# Acron

Acron meas *another rcon*. It is a WebSocket based rcon replacement with advanced features.

## Problems with rcon

* [Security] No authorization: All rcon clients are hardcoded with OP level 4 in the Minecraft source code. There are also no permission control, giving any faulty or even malicious client full control over the server.
* [Security] Simple authentication: All clients are sharing the same secret, making the secret easy to leak and granting attackers unlimited access to the server.
* [Efficiency] Rcon executes commands in a blocking manner. The server joins the main thread and waits for the command to complete before reading more from the client.
* [Limit] Rcon does not support pushing server messages to the client. This includes player messages, death messages, server logs, etc. A lot of use cases need such information.
* [Limit] Rcon has a fixed command length. Although it is not likely for a command to exceed this limit, it still restricts the use cases of rcon.
* [Limit] Rcon commands are hard coded to run at the spawn point of Overworld. It is impossible to execute commands in other positions or dimensions if the command does not support so itself.
* [Limit] No Unix domain socket support. Unix domain socket is a great way to do localhost IPC and controlling access using Unix user and groups. However, rcon is forced to listen on a TCP address and port.
* [Performance] Minecraft creates a new thread per connection accepted, and it blocks for input. Using a thread pool or async IO is much more performant.
* [Security] Rcon does not support TLS. It is just using plain TCP.

To solve these problems, a better approach is to rewrite rcon.

## Problems Acron solved

* [Security] Authentication and Authorization: With Acron, administrators are able to specify unique tokens for each client, and it is also possible to easily define the commands clients are permitted to execute using regex rules.
* [Efficiency] Acron uses a command queue to schedule commands. Clients need to specify an ID, and Acron will return the result with the same ID once the command is done. In the meantime, clients can enqueue more commands.
* [Limit] Server push: Acron will send player messages, death messages of living entities, player join / leave messages, and server lag warnings to the client. Acron also classifies the messages, so clients do not need to parse them manually.
* [Limit] Command length: Acron does not limit command length.
* [Limit] Locations and other configurations: Acron clients can specify the world, position, rotation, and name for each command they execute, or they can set a per-connection default.
* [Limit] **Unix domain socket: Sorry, currently Acron does not support Unix domain socket either. Unix domain sockets will be available in later versions.**
* [Performance] Acron uses Netty, which is built-in in Minecraft, to performance async IO using thread pools.
* [Security] TLS: Although Acron does not support TLS itself, it is using WebSocket, which gives the choice of adding a reverse proxy with TLS support.

## Technical Specification

Acron is based on:

1. WebSocket: Instead of designing a Layer 5 protocol, Acron chooses WebSocket to make the implementation of server and client easier. Moreover, WebSocket has a wide range of support compared to plain TCP sockets.
2. JSON: Although JSON is slow and schema-less, it comes with no addition dependencies as a Minecraft mod because Minecraft depends on GSON internally.
3. Netty: The WebSocket server is based on Netty because it is built-in in the Minecraft server.
4. GSON: Acron uses GSON to deserialize / serialize JSON since GSON is also a Minecraft dependency.

## Documentation Notes

For each request JSON parameter, the format is:

`(JSON path)` (type, limit, default value or required): Description.

For each response JSON parameter, the format is:

`(JSON path)` (type, limit, always present or conditions): Description.

## Installation

To build this mod, you need to run `gradle build`, and the output JAR will be at `build/libs/acron-x.x.jar`.

Then, copy it to the `mods/` folder in your Minecraft server working directory.

Finally, edit `<Minecraft server working directory>/config/acron.json` as follows:

```json
{
  "port": 25575,
  "address": "127.0.0.1",
  "native_transport": false,
  "clients": [
    {
      "id": "client1",
      "token": "61fe277334300860dbcf8320ad866788e08b7dd930f9f04a3dc4db5e7f6521e2",
      "policy_mode": "deny",
      "rules": [
        {
          "regex": "^list$",
          "action": "allow",
          "display": false
        },
        {
          "regex": "^kick .*$",
          "action": "allow",
          "display": true
        },
        {
          "regex": "^stop .*$",
          "action": "deny"
        }
      ]
    }
  ]
}
```

Finally, start the server.

> **Notes**
> 
> JSON is not the first choice for configuration files because it takes too much manual labor to write it correctly.
> However, since Minecraft server bundles GSON, it is redundant for this mod to depend on another configuration parsing library
> for the sole purpose of loading configurations.
> 
> To save users' time, we are planning to release a online GUI configuration editor.

## Configuration

### Server configuration

JSON Path: `.`

* `port` (int, [0, 65535], 25575): Port to listen.
* `address` (string, IPv4 or IPv6 address, "127.0.0.1"): Address to listen.
* `native_transport` (boolean, true / false, false): Use Epoll when available.

### Client configuration

JSON Path: `.clients.[]`

* `id` (string, any, required): The ID of the client. The client needs to specify it in the connection string.
* `token` (string, SHA256, required): The SHA256 of the token. The token is generated by the administrator.
* `policy_mode` (enum, deny / allow, deny): The default rule if its command does not mach any rules in the `rules` array.
* `rules.[]regex` (string, regex, required): The regex to match the command.
* `rules.[]action` (enum, deny / allow, required): The action for this rule.
* `rules.[]display` (boolean, true / false, false): Display the output of the command on chat.

## Client Management

Each client has a unique ID (like a username), and it has a token used to authenticate itself. The administrator needs to add the client to the configuration with an ID (administrator chosen) and a token (administrator generated).

When the client connects, it needs to supply the ID - token pair, or Acron will return HTTP 401 in the WebSocket handshake request.

Each client has some rules and a default policy mode. When it executes a command, Acron will match the command string against the rules, 
from the first to the last, until a match is found, and the corresponding action in the rule is taken.
It Acron cannot match any rules, it will take the default policy mode.

Auditing is also available. Users may specify the `display` parameter in rules to make the command output to both server logs and chat.

> **Note**
> 
> Internally, the command will run at OP level 4 (the highest level) after
> passing rules check.

> **Note**
> 
> Minecraft accepts commands both starting with `/` or not (but
> not commands starting with two or more `/`). However, Acron will remove 
> the leading slash if present when matching against rules.

> **Note**
> 
> If the format of `.port`, `.listen` or `.native_transport` is wrong, Acron will prevent
> Minecraft server from starting up.
> 
> However, if the format of anything in `.clients` is wrong, it will print a warning and skip
> that part because administrators can reload clients during runtime.

### Configuration reloading

Any administrator with OP level 4 can execute the command `/acron rule update`.
It will instantly read the configuration file
and apply the changes to clients and rules.

However, this does not affect existing connections since authentication happens
during WebSocket handshaking.

Note, listen port and address cannot be changed during runtime.

> **Note**
> 
> Similarly, if Acron finds an error in `.clients` after running `/acron rule update`,
> it will print a warning and skip the whole new configuration file until the
> error is fixed.

## Client API

Acron uses polymorphic JSONs when communicating with clients. Therefore, each JSON
has to contain a valid `type` parameter indicating its type:

```json
{
  "type": "cmd",
  "id": 1,
  "cmd": "list"
}
```

### Request ordering

To work in a full-duplex environment, each command can specify a `id` parameter. Acron will
return any results or errors with the same ID.

Sample request:

```json
{
  "type": "cmd",
  "id": 1,
  "cmd": "list"
}
```

The parameter `id` can be any integer, but it is the client developer's responsibility to
make it a unique value, so he or she can identify it.

Parameter `id` defaults to -1.

In response, any non-server-push responses (i. e. messages) will include the same `id` parameter:

```json
{
  "type": "cmd_result",
  "id": 1,
  "result": 0,
  "success": true
}
```

If the server fails to parse the request and returns an error, it will report the default ID `-2`.

### Error Handling

Error handling: Besides from the handshake request, which will send errors using HTTP status
codes, all faulty WebSocket requests will receive error in the following format:

```json
{
  "type": "error",
  "id": 1,
  "code": 500,
  "message": "Error message. Not machine-readable."
}
```

Parameters:

* `.code` (int, HTTP status codes, always present): The machine-readable error code (e. g. 400 for Bad Request).
* `.message` (string, any, always present): The human-readable error message.

Global error codes:

* 400: The request is invalid.
* 500: The server encountered an unknown error.

**`.type` and `.id` are included in every request / response, except for further noticed. Thus,
this document excludes them from the parameter lists.**

### Handshaking

Clients need to use the following connection string when connecting to the Acron server:

```
ws://host:port/ws?id=client_id&token=client_token&version=0
```

*A better approach for specifying the authentication parameters is using HTTP headers, 
but the JavaScript client does not allow so. To extend compatibility, Acron forces
all users to use HTTP query parameters to supply information.*

Parameters:

* `id` (required): Client ID set by the administrator.
* `token` (required): Client token set by the administrator.
* `version` (default: 0): API version. Only 0 is accepted at this time.

Responses:

* HTTP 400 (Bad Request): If either `id` or `token` is missing, or `version` is not 0.
* HTTP 401 (Unauthorized): If either `id` is not found or `token` does not match the record.
* HTTP 101 (Switching Protocols): The handshake is complete, and the server is upgrading to
WebSocket.

### Setting Configuration

This allows clients to set a per-connection default configuration to execute commands.

Clients can override the configuration temporarily when executing commands.

Request:
```json
{
  "type": "set_config",
  "id": 1,
  "world": "overworld",
  "pos": {
    "x": 0.0,
    "y": 0.0,
    "z": 0.0
  },
  "rot": {
    "x": 0.0,
    "y": 0.0
  },
  "name": ""
}
```

Parameters:

* `.world` (enum, overworld / nether / end, overworld): The world to run commands in.
* `.pos` (vec3d, *see below*, spawn point of `.world`): The position to run commands at.
  * `.x` (double, any within border limit, 0.0): X
  * `.y` (double, any within border limit, 0.0): Y
  * `.z` (double, any within border limit, 0.0): Z
* `.rot` (vec2f, *see below*, `0.0 0.0`): Rotation.
  * `.x` (float, ?, 0.0): X
  * `.z` (float, ?, 0.0): Z
* `.name` (string, any, random): Name when running commands.

When the client connects, Acron will set the configuration to default values.

Successful response:

```json
{
  "type": "ok"
}
```

This shows that the configuration update is successful.

### Executing Commands

The main goal of Acron is to allow clients to run commands. A client can send
any commands, and Acron will schedule them in the background.

Request:

```json
{
  "type": "cmd",
  "id": 1,
  "cmd": "list",
  "config": {
    
  }
}
```

Parameters:

* `.cmd` (string, any valid command, required): The command to execute. It may or may not begin with `/`.
* `.config` (set_config, *see above*, current connection default configuration): Temporary configuration 
when running this command. It is the same `set_config` object in the above section, but `type` and `id` 
must not be supplied.

Successful response:

```json
{
  "type": "ok"
}
```

This shows that the command is scheduled.

If the connection breaks before it is done, it is still executed without any output to the connection.

Possible failures:

* 403: This client is not allowed to execute this command. (Configured by rules)

**Command output:**

When the command prints a line, Acron will send the following response:

```json
{
  "type": "cmd_out",
  "id": 1,
  "sender": "UUID",
  "out": "..."
}
```

Parameters:

* `.sender` (UUID, any UUID, always present): Sender UUID.
* `.out` (string, any, always present): Output.

**Command result:**

When the command finishes without issues (?), Acron will send the following response:

```json
{
  "type": "cmd_result",
  "id": 1,
  "result": 0,
  "success": true
}
```

All parameters always present.

> **Note**
> 
> The result completely depends on Minecraft server's response.
> It may not be reliable, and the values of `.result` and `.success` are
> undocumented.

### Receiving Messages

Another major part of Acron is to allow clients receive events from the server.

Every event will have a pre-defined `type` with other custom parameters. Parameter `id` will not
present in events.

> **Contributor Guide**
> 
> Internally, all message Acron sends to clients are called events, including
> command results.

#### Player joined

Response:

```json
{
  "type": "join",
  "player": {
    "name": "",
    "uuid": "",
    "pos": {
      "x": 0.0,
      "y": 0.0,
      "z": 0.0
    },
    "world": "end"
  }
}
```

Parameters:

* `.player` (entity, see below, always present): The player.
  * `.name` (string, any valid Minecraft username, always present): Username.
  * `.uuid` (uuid, UUID, always present): UUID.
  * `.pos` (vec3d, see below, always present): The position he or she joins.
    * `.x` (double, any within border limit, 0.0): X
    * `.y` (double, any within border limit, 0.0): Y
    * `.z` (double, any within border limit, 0.0): Z
  * `.world` (enum, overworld / nether / end, not present if Acron cannot determine the world): The dimension
he or she joins.

#### Player Disconnected

Response:

```json
{
  "type": "disconnect",
  "player": {
    "name": "",
    "uuid": "",
    "pos": {
      "x": 0.0,
      "y": 0.0,
      "z": 0.0
    },
    "world": "end"
  },
  "reason": ""
}
```

Parameters:

* `.player` (entity, see below, null only when the server cannot verify the user): The player.
  * `.name` (string, any valid Minecraft username, always present): Username.
  * `.uuid` (uuid, UUID, always present): UUID.
  * `.pos` (vec3d, see below, always present): The position he or she leaves.
    * `.x` (double, any within border limit, 0.0): X
    * `.y` (double, any within border limit, 0.0): Y
    * `.z` (double, any within border limit, 0.0): Z
  * `.world` (enum, overworld / nether / end, not present if Acron cannot determine the world): The dimension
    he or she leaves.
* `.reason` (string, any valid disconnect reason, always present): Disconnect reason.

#### Player Message

Response:

```json
{
  "type": "message",
  "player": {
    "name": "",
    "uuid": "",
    "pos": {
      "x": 0.0,
      "y": 0.0,
      "z": 0.0
    },
    "world": "end"
  },
  "text": ""
}
```

Parameters:

* `.player` (entity, see below, always present): The player.
  * `.name` (string, any valid Minecraft username, always present): Username.
  * `.uuid` (uuid, UUID, always present): UUID.
  * `.pos` (vec3d, see below, always present): The position he or she sends the message.
    * `.x` (double, any within border limit, 0.0): X
    * `.y` (double, any within border limit, 0.0): Y
    * `.z` (double, any within border limit, 0.0): Z
  * `.world` (enum, overworld / nether / end, not present if Acron cannot determine the world): The dimension
he or she sends the message.
* `.text` (string, any valid Minecraft message, always present): The message.

#### Entity Death

Response:

```json
{
  "type": "death",
  "entity": {
    "name": "",
    "uuid": "",
    "pos": {
      "x": 0.0,
      "y": 0.0,
      "z": 0.0
    },
    "world": "end"
  },
  "message": ""
}
```

Parameters:

* `.entity` (entity, see below, always present): The entity.
  * `.name` (string, any, always present): Default name or custom name of the entity.
  * `.uuid` (uuid, UUID, always present): UUID.
  * `.pos` (vec3d, see below, always present): The position of the entity when died.
    * `.x` (double, any within border limit, 0.0): X
    * `.y` (double, any within border limit, 0.0): Y
    * `.z` (double, any within border limit, 0.0): Z
  * `.world` (enum, overworld / nether / end, not present if Acron cannot determine the world): The dimension
of the entity when died.
* `.message` (string, any valid death message, always present): The user-readable death message.

> **Roadmap**
> 
> Parsing the death message and sending a more machine-readable message is on the roadmap.

#### Server Lagging

Acron will send this event when the server prints 
`Can't keep up! Is the server overloaded? Running 4313ms or 86 ticks behind` to the standard output.

Response:

```json
{
  "type": "lagging",
  "ms": 100,
  "ticks": 1000
}
```

Parameters:

* `.ms` (long, >= 0, always present): Running {}ms behind.
* `.ticks` (long, >= 0, always present): Running {} ticks behind.

## Contributing

As a community project, I highly appreciate any help to this project. If you have any suggestions or
patches, or if you find a bug or security issue, please send them to `yuuta@yuuta.moe`, and mention Acron in
the email subject. If you are sending a patch, please include `[PATCH]` in the subject as well. Thank you very much.

## License

Acron is licensed under GPL-2.0-only except libac is licensed under LGPL-2.1-only.