summary refs log tree commit diff
path: root/README.adoc
blob: 28bfdd8f0d52a937ff48defec1d4e2fb1f1e6add (plain) (blame)
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
flipdot-gschichtler
===================
sterni
November 2020

`flipdot-gschichtler` allows to display text on the OpenLab's
flipdot panel in an user- and machine-friendly way without
fear of interfering with others and without requiring direct
connectivity to the panel.

Components
----------

`flipdot-gschichtler` consists of the following three components:

- `warteraum` manages a queue of text snippets which is accessible and
  manipulable via a public REST API. Users will directly or indirectly
  use it to send text to the flipdot panel.
- `anzeigetafel` is a daemon which runs on a machine with direct
  connectivity to the flipdot panel. It monitors the queue of
  `warteraum` and displays new incoming text on the panel and
  deletes it from the queue afterwards.
- `bahnhofshalle` is a web fronted which allows users to view and
  manipulate the queue of `warteraum` from their browser.

API Documentation
-----------------

Caveats
~~~~~~~

* The `v2` API may support `application/json` request bodies in
  the future. This is not yet the case.
* Any endpoint may also return different status codes than the
  ones listed here in unusual cases. These would typically
  be codes like 500 or 502.
* ids are not guaranteed to never be reassigned. However,
  ids only start to get reassigned after the queue has been
  fully emptied.

API `v2`
~~~~~~~~

GET `/api/v2/queue`
^^^^^^^^^^^^^^^^^^^

|=============================================
| Request Content-Type  | none
| Response Content-Type | `application/json`
| Authentication        | no
|=============================================

This endpoint returns the current queue in the
following JSON format. `length` corresponds with
the length of the queue array. `queue` is ordered
by time of each item's addition, i. e. the first
item is next up to be displayed. The supplied `id`
may be used to delete the entries.

-------------------------
{
  "queue": [
    {
      "id": 0,
      "text": "hello"
    },
    {
      "id": 1,
      "text": "world"
    }
  ],
  "length": 2
}
-------------------------

|====================================
| HTTP Status  | Meaning
| 200          | Success
|====================================

POST `/api/v2/queue/add`
^^^^^^^^^^^^^^^^^^^^^^^^

|=============================================
| Request Content-Type  | `application/x-www-form-urlencoded`
| Response Content-Type | `application/json`
| Authentication        | no
|=============================================

This endpoints allows you to add text to the queue,
it requires no authentication as it is a feature
available to the public. There is also no rate
limiting as of yet, but it may be implemented should
it become necessary (hopefully not).

The endpoint expects an urlencoded form as request
body with the following field.

|=============================================
| `text` | text to be added to the queue
|=============================================

The response contains the queue entry consisting
of its `text` and `id`:

----------------------
{
  "id": 3,
  "text": "hello"
}
----------------------

|=============================================
| HTTP Status  | Meaning
| 200          | Success, text added
| 400          | Illegal method or malformed request
| 415          | Request body too big or text field longer allowed (usually 512 bytes)
| 503          | The queue is full, i. e. the max id has been reached
|=============================================

DELETE `/api/v2/queue/<id>`
^^^^^^^^^^^^^^^^^^^^^^^^^^^

|=============================================
| Request Content-Type  | `application/x-www-form-urlencoded`
| Response Content-Type | none
| Authentication        | yes (see below)
|=============================================

This endpoint can be used to delete queue entries, e. g. after
they have been displayed on the panel. The request should send
a form with the following fields as request body in order to
authenticate itself:

|=============================================
| `token` | API token of the application
|=============================================

|=============================================
| HTTP Status  | Meaning
| 204          | Success, queue entry deleted
| 400          | Illegal method or malformed request
| 401          | Unauthorized API token
| 404          | No queue entry with given `id`
| 415          | Request body too big
|=============================================

API `v1`
~~~~~~~

GET `/api/v1/queue`
^^^^^^^^^^^^^^^^^^^

Same as `/api/v2/queue`, since v2 didn't introduce any changes.

POST `/api/v1/queue/add`
^^^^^^^^^^^^^^^^^^^^^^^^

|=============================================
| Request Content-Type  | `application/x-www-form-urlencoded`
| Response Content-Type | `text/html`
| Authentication        | no
|=============================================

This is the legacy endpoint to add text to the queue. It enabled
interacting with it via a `<form>` in the old web app. The form
sent as part of the request should have the following fields:

|=============================================
| `text` | text to be added to the queue
|=============================================

The response format has been changed since the previous implementation.
I sincerly hope that nobody scraped the resulting page.

|=============================================
| HTTP Status  | Meaning
| 200          | Success, text added
| 400          | Illegal method or malformed request
| 415          | Request body too big or text field longer allowed (usually 512 bytes)
|=============================================

DELETE `/api/v1/queue/del/<id>`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Same as `/api/v2/queue/<id>`, `v2` only changed the endpoint URL.

Bug Bounty
----------

https://github.com/sternenseemann[I]'ll gift anyone who finds serious
bugs or exploits in `flipdot-gschichtler` a crate of _Mate_ (or another
beverage if you don't like it) as a reward. Such issues would be remotely
triggering crashes or segfaults, bypassing authentication, remote code
execution etc. I won't bother making an exhaustive list or a precise
list of criteria in the hopes it doesn't come back to haunt me…

Contributing
------------

Help is welcome! Some things that remain to be done:

* More “funny” bits for the web frontend
  (hint: see `const subjects` in `main.es6`)
* Important: Documentation. Annoying sterni into doing it is also helping.
* Make `warteraum` accept `application/json` request bodies for the `v2`
  API using http://www.catb.org/~esr/microjson/[microjson] (?).
* Polish the web frontend, test across browsers
* Refresh queue regularly in the web frontend
* Write more tests
* Full Unicode support by using Unifont on the flipdots
* A completely new feature you thought of

Building
--------

warteraum
~~~~~~~~~

Requirements:

* a C99 compiler
* GNU make
* https://www.tarsnap.com/scrypt.html[`libscrypt-kdf`]

------------------------
cd warteraum
make
------------------------

bahnhofshalle
~~~~~~~~~~~~~

To build `bahnhofshalle` you need GNU make and https://esbuild.github.io/[esbuild].
One way to obtain both is to run `nix-shell -A bahnhofshalle` from the repository's
root. To build, use the following commands:

------------------------
cd bahnhofshalle
make
firefox index.html # for local development where only js needs to be rebuilt

make dist
firefox dist/index.html # properly minified distribution
------------------------

Note that all requests are sent using a `same-origin` policy,
so you need to configure a reverse proxy to serve the web
frontend and API simuntaneously for testing. You may take
inspiration from the `nginx` configuration in
`nixos/flipdot-gschichtler.nix`.

A note on vendoring
~~~~~~~~~~~~~~~~~~~

To ease the submodule hassle, dependencies that are inconvenient to
handle via a package manager are vendored or added as a
https://www.atlassian.com/git/tutorials/git-subtree[git subtree].
To avoid confusion these are located under `third_party` exclusively.

Also be aware that different licensing terms may apply to code under
this directory.

Nix packages
~~~~~~~~~~~~

`default.nix` provides the following nix derivations which are
ready to be installed:

* `warteraum`: standard clang/glibc build of warteraum
* `warteraum-static`: statically linked build of warteraum
  using gcc and musl (used for the systemd service so we
  can restrict file system access)
* `bahnhofshalle`
* `anzeigetafel`

Configuration
~~~~~~~~~~~~~

`nixos/flipdot-gschichtler.nix` provides a NixOS module which
defines `services.flipdot-gschichtler` to conveniently set up
the server side with `warteraum` and `bahnhofshalle` behind
a nginx reverse proxy. A minimal `configuration.nix` utilizing
it could look like this:

---------------
{ pkgs, ... }:

{
  imports = [
    /path/to/flipdot-gschichtler/nixos/flipdot-gschichtler.nix
  ];

  services.flipdot-gschichtler = {
    enable = true;
    virtualHost = "flipdot.openlab-augsburg.de";
    tokensFile = "/var/secrets/flipdot-gschichtler/tokens";
    saltFile = "/var/secrets/flipdot-gschichtler/salt";
    # if you want to change the derivations to use
    # packages = {
    #   warteraum = …;
    #   bahnhofshalle = …;
    # };
  };

  services.nginx.enable = true;
  security.acme = {
    ....
  };
}
---------------

warteraum
^^^^^^^^^

`warteraum` is configured via environment variables (which the NixOS
module utilizes):

* `WARTERAUM_SALT_FILE`: A file containing random data to use as salt
* `WARTERAUM_TOKENS_FILE`: API tokens hashed using `scrypt`

To generate the tokens file, `warteraum` ships a utility tool.
Setting up auth works like this:

-------------------
$ head -c 512 /dev/urandom > $WARTERAUM_SALT_FILE
$ hashtoken $WARTERAUM_SALT_FILE token1 >> $WARTERAUM_TOKENS_FILE
$ hashtoken $WARTERAUM_SALT_FILE token2 >> $WARTERAUM_TOKENS_FILE
-------------------

Now `warteraum` would accept “token1” and “token2” when authenticating.
Note that `hashtoken` only supports appending tokens in a
convenient fashion at the moment. Removing tokens is quite cumbersome
and only possible with a knowledge of `warteraum` internals.

Changelog
---------

2.1.0 (WIP)
~~~~~~~~~~~

* `warteraum`
** Limit size of request bodies to prevent DoS attacks
** Trim whitespace on input text
** Instead of compiling in salt and tokens, read them from the
   files specified via the `WARTERAUM_SALT_FILE` and
   `WARTERAUM_TOKENS_FILE` environment variables.
* NixOS module
** Reflect change to `warteraum` by using `saltFile` and
   `tokensFile` respectively over the previous `salt` and
   `tokens`.
** Fix sandboxing in `nixos/flipdot-gschichtler.nix`: Now only
   the secret files and the nix store will be readable to the
   `warteraum` process.
** Allow changing `bahnhofshalle` and `warteraum` derivation
   to use via `packages`.
** Don't require `flipdot-gschichtler` to be passed as module
   argument, instead import directly from file system (unless
   a non-default derivation is configured).
* `bahnhofshalle`
** Switch to `esbuild`, requiring ES6 support in the browser as a result.

2.0.0
~~~~~

https://github.com/openlab-aux/flipdot-gschichtler/tree/2.0.0[Browse code]

* Replace `admin` and `web` frontends with pure EcmaScript
  frontend `bahnhofshalle`
* Replace `web` API implementation with `warteraum`
* Rename `flipper` to `anzeigetafel`, port to Python 3
* API:
** Move endpoints from `/` to `/api/v1/`
** `/api/v1/queue/add` HTML response changes, since no longer
   used by the frontend (except when no JavaScript is available)
** Add cleaned up version of the API as `/api/v2`. This one is used
   by `bahnhofshalle` and `anzeigetafel` and should be utilized by
   clients going forward.
* Deployment:
** Implement API/Frontend deployment as a NixOS service

1.0.0
~~~~~

https://github.com/openlab-aux/flipdot-gschichtler/tree/1.0.0[Browse code]

Initial Version: Flask and Python 2.7 based web interface.