open-chess-server 0.1.5

A Lichess-compatible chess server: the Board, Bot, Challenge and OAuth APIs on Postgres, with post-game fair-play screening
Documentation
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
//! The server's commands, as the `open-chess` program runs them (the
//! `open-chess` crate; built with its `server` feature):
//!
//!   open-chess serve      the Lichess-compatible API
//!   open-chess analyse    the fair-play worker (needs Stockfish)
//!   open-chess migrate    create or update the chess schema, then exit
//!   open-chess bot create --name esl-stockfish-1800   a bot account and its token
//!   open-chess admin grant mark            make an account an admin (or --manager)
//!   open-chess partner create --name esports-lab --scopes read,site   a partner token
//!   open-chess healthcheck   for container health checks

use std::time::Duration;

use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use tokio::sync::watch;
use tracing::info;

use crate::app::{self, Ctx};
use crate::config::{AnalyseConfig, Config};
use crate::db::Db;
use crate::{analyse, auth, stopped};

/// `open-chess` with no subcommand: serve, configured from the environment.
#[derive(Parser)]
struct ServeOnly {
    #[command(flatten)]
    cfg: Config,
}

/// The server's commands.
#[derive(Subcommand)]
pub enum Cmd {
    /// Serve the API (and sweep clocks and challenges).
    Serve(Config),
    /// Screen finished games for fair play with Stockfish.
    Analyse {
        #[command(flatten)]
        cfg: Config,
        #[command(flatten)]
        analyse: AnalyseConfig,
    },
    /// Create or update the chess schema and exit.
    Migrate(Config),
    /// Board photos kept for review.
    Photos {
        #[command(subcommand)]
        cmd: PhotosCmd,
    },
    /// Bot accounts.
    Bot {
        #[command(subcommand)]
        cmd: BotCmd,
    },
    /// Admins and managers here.
    Admin {
        #[command(subcommand)]
        cmd: AdminCmd,
    },
    /// Partners: other services' API access.
    Partner {
        #[command(subcommand)]
        cmd: PartnerCmd,
    },
    /// Exit 0 if the local server answers /healthz.
    Healthcheck {
        #[arg(long, env = "CHESS_METRICS_BIND", default_value = "0.0.0.0:9465")]
        metrics_bind: String,
    },
}

#[derive(Subcommand)]
pub enum BotCmd {
    /// Make a bot: a site player, its bot account here and an API token
    /// (printed once).
    Create {
        #[command(flatten)]
        cfg: Config,
        /// Username, e.g. esl-stockfish-1800.
        #[arg(long)]
        name: String,
        /// Token lifetime.
        #[arg(long, default_value_t = 365)]
        days: i64,
    },
    /// List bot accounts.
    List(Config),
}

#[derive(Subcommand)]
pub enum AdminCmd {
    /// Make an account an admin (or, with --manager, a manager).
    Grant {
        #[command(flatten)]
        cfg: Config,
        username: String,
        #[arg(long)]
        manager: bool,
    },
    /// Take an admin's (or, with --manager, a manager's) role away.
    Revoke {
        #[command(flatten)]
        cfg: Config,
        username: String,
        #[arg(long)]
        manager: bool,
    },
    /// List admins and managers.
    List(Config),
}

#[derive(Subcommand)]
pub enum PartnerCmd {
    /// Make a partner and its token (printed once).
    Create {
        #[command(flatten)]
        cfg: Config,
        #[arg(long)]
        name: String,
        /// What it may do: read (games, players, the feed), site (act as
        /// the host site: its players' tokens and links).
        #[arg(long, value_delimiter = ',', default_value = "read")]
        scopes: Vec<String>,
    },
    /// List partners.
    List(Config),
    /// Stop a partner's token working.
    Revoke {
        #[command(flatten)]
        cfg: Config,
        id: i64,
    },
}

#[derive(Subcommand)]
pub enum PhotosCmd {
    /// Write the labelled photos as a training set for chess-vision-train:
    /// `photos/<id>.<ext>` and `labels.jsonl` (datasets `open-chess-photos`,
    /// or with `--public-only` `open-chess-public`, in its datasets.toml).
    /// Only photos kept under the photo licence; each line says its licence.
    Export {
        #[command(flatten)]
        cfg: Config,
        /// Directory to write (created; existing photos are kept).
        #[arg(long)]
        out: std::path::PathBuf,
        /// Only photos two people agreed on, or an admin labelled, not ones
        /// only the player who sent them confirmed.
        #[arg(long)]
        reviewed_only: bool,
        /// Only photos their senders released as CC0 (the public dataset).
        #[arg(long)]
        public_only: bool,
    },
}

fn tracing_init() {
    let filter = tracing_subscriber::EnvFilter::try_from_default_env()
        .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info"));
    tracing_subscriber::fmt().with_env_filter(filter).with_target(false).init();
}

/// Ctrl-C or SIGTERM flips the returned watch to true.
fn shutdown_signal() -> watch::Receiver<bool> {
    let (tx, rx) = watch::channel(false);
    tokio::spawn(async move {
        let ctrl_c = tokio::signal::ctrl_c();
        #[cfg(unix)]
        {
            let mut term = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
                .expect("SIGTERM handler");
            tokio::select! {
                _ = ctrl_c => {}
                _ = term.recv() => {}
            }
        }
        #[cfg(not(unix))]
        let _ = ctrl_c.await;
        info!("shutting down");
        let _ = tx.send(true);
    });
    rx
}

/// Whether the environment configures a server (`DATABASE_URL` is set):
/// `open-chess` with no command serves then.
pub fn configured() -> bool {
    std::env::var_os("DATABASE_URL").is_some_and(|v| !v.is_empty())
}

/// Run a server command; `None` serves, configured from the environment.
pub fn run(cmd: Option<Cmd>) -> Result<()> {
    tracing_init();
    tokio::runtime::Builder::new_multi_thread().enable_all().build()?.block_on(dispatch(cmd))
}

async fn dispatch(cmd: Option<Cmd>) -> Result<()> {
    let cmd = match cmd {
        Some(c) => c,
        None => {
            Cmd::Serve(ServeOnly::try_parse_from(["open-chess"]).context("DATABASE_URL must be set")?.cfg)
        }
    };
    match cmd {
        Cmd::Serve(cfg) => serve(cfg).await,
        Cmd::Analyse { cfg, analyse: a } => {
            cfg.validate()?;
            let db = Db::connect(&cfg)?;
            db.migrate().await?;
            let ctx = Ctx::new(db.clone(), cfg.clone());
            let stop = shutdown_signal();
            let ops = tokio::net::TcpListener::bind(&cfg.metrics_bind)
                .await
                .context("binding CHESS_METRICS_BIND")?;
            let stop_ops = stop.clone();
            // The worker counts into the registry /metrics serves.
            let metrics = ctx.metrics.clone();
            tokio::spawn(async move {
                let _ = axum::serve(ops, app::ops_router(ctx)).with_graceful_shutdown(wait(stop_ops)).await;
            });
            analyse::run(db, metrics, a, stop).await
        }
        Cmd::Migrate(cfg) => {
            cfg.validate()?;
            Db::connect(&cfg)?.migrate().await?;
            info!("chess schema up to date");
            Ok(())
        }
        Cmd::Bot { cmd: BotCmd::Create { cfg, name, days } } => bot_create(cfg, &name, days).await,
        Cmd::Bot { cmd: BotCmd::List(cfg) } => bot_list(cfg).await,
        Cmd::Photos { cmd: PhotosCmd::Export { cfg, out, reviewed_only, public_only } } => {
            photos_export(cfg, &out, reviewed_only, public_only).await
        }
        Cmd::Admin { cmd } => admin(cmd).await,
        Cmd::Partner { cmd } => partner(cmd).await,
        Cmd::Healthcheck { metrics_bind } => healthcheck(&metrics_bind),
    }
}

async fn wait(mut stop: watch::Receiver<bool>) {
    stopped(&mut stop).await;
}

async fn serve(cfg: Config) -> Result<()> {
    cfg.validate()?;
    let db = Db::connect(&cfg)?;
    db.migrate().await?;
    let ctx = Ctx::new(db.clone(), cfg.clone()).with_reader()?;
    let stop = shutdown_signal();

    crate::spawn_background(&ctx, &stop);

    let ops = tokio::net::TcpListener::bind(&cfg.metrics_bind).await.context("binding CHESS_METRICS_BIND")?;
    let ops_ctx = ctx.clone();
    let stop_ops = stop.clone();
    tokio::spawn(async move {
        let _ = axum::serve(ops, app::ops_router(ops_ctx)).with_graceful_shutdown(wait(stop_ops)).await;
    });

    let listener = tokio::net::TcpListener::bind(&cfg.bind).await.context("binding CHESS_BIND")?;
    info!(bind = cfg.bind, public = cfg.public_url(), "chess server listening");
    if cfg.esports_login && cfg.esports().is_none() {
        tracing::warn!(
            "ESPORTS_LOGIN is on but ESPORTS_SECRET isn't set (32+ characters): signing in with the site is off"
        );
    }
    axum::serve(listener, app::router(&ctx)?).with_graceful_shutdown(wait(stop)).await?;
    Ok(())
}

async fn bot_create(cfg: Config, name: &str, days: i64) -> Result<()> {
    cfg.validate()?;
    let db = Db::connect(&cfg)?;
    db.migrate().await?;
    let mut c = db.conn().await?;
    let tx = c.transaction().await?;
    let player: i64 = tx
        .query_one(
            &db.sql("INSERT INTO chess.players (name, key_hash) VALUES ($1, $2) RETURNING id"),
            &[&name, &auth::hash_key(&auth::random_token(28))],
        )
        .await?
        .get(0);
    tx.execute(
        &db.sql("INSERT INTO chess.accounts (player_id, username, kind) VALUES ($1, $2, 'bot')"),
        &[&player, &name],
    )
    .await
    .context("that username is taken or invalid (letters, digits, _ and -, 2–30 characters)")?;
    let token = format!("{}{}", auth::TOKEN_PREFIX, auth::random_token(36));
    let prefix: String = token.chars().take(auth::TOKEN_PREFIX.len() + 6).collect();
    let scopes: Vec<String> =
        ["bot:play", "challenge:read", "challenge:write"].iter().map(|s| (*s).to_owned()).collect();
    let expires = chrono::Utc::now() + chrono::Duration::days(days.clamp(1, 365));
    tx.execute(
        &db.sql(
            "INSERT INTO chess.api_tokens (player_id, name, token_hash, prefix, scopes, expires_at)
             VALUES ($1, 'chess bot', $2, $3, $4, $5)",
        ),
        &[&player, &auth::hash_key(&token), &prefix, &scopes, &expires],
    )
    .await?;
    tx.commit().await?;
    println!("bot {name} created (player {player}); its token, shown once:\n{token}");
    Ok(())
}

async fn bot_list(cfg: Config) -> Result<()> {
    cfg.validate()?;
    let db = Db::connect(&cfg)?;
    let c = db.conn().await?;
    for r in c
        .query(
            &db.sql("SELECT username, player_id, seen_at FROM chess.accounts WHERE kind = 'bot' ORDER BY lower(username)"),
            &[],
        )
        .await?
    {
        let seen: Option<chrono::DateTime<chrono::Utc>> = r.get(2);
        println!(
            "{:<30} player {:<6} last seen {}",
            r.get::<_, String>(0),
            r.get::<_, i64>(1),
            seen.map_or_else(|| "never".into(), |t| t.to_rfc3339())
        );
    }
    Ok(())
}

async fn photos_export(
    cfg: Config,
    out: &std::path::Path,
    reviewed_only: bool,
    public_only: bool,
) -> Result<()> {
    cfg.validate()?;
    let db = Db::connect(&cfg)?;
    let c = db.conn().await?;
    std::fs::create_dir_all(out.join("photos")).with_context(|| format!("creating {}", out.display()))?;
    let rows = c
        .query(
            &db.sql(
                "SELECT id, player_id, created_at, image, mime, label, label_source, public
                   FROM chess.board_photos
                  WHERE label IS NOT NULL AND NOT unreadable AND terms IS NOT NULL
                    AND ($1 = false OR label_source IN ('consensus', 'admin', 'reviewer'))
                    AND ($2 = false OR public)
                  ORDER BY id",
            ),
            &[&reviewed_only, &public_only],
        )
        .await?;
    let mut lines = String::new();
    for r in &rows {
        let id: i64 = r.get(0);
        let player: Option<i64> = r.get(1);
        let at: chrono::DateTime<chrono::Utc> = r.get(2);
        let ext = match r.get::<_, String>(4).as_str() {
            "image/png" => "png",
            "image/webp" => "webp",
            _ => "jpg",
        };
        let name = format!("photos/{id}.{ext}");
        std::fs::write(out.join(&name), r.get::<_, Vec<u8>>(3))?;
        // One player's photos on one day are probably of the same board, so
        // they share a group and a split never divides them.
        let group = format!("player-{}-{}", player.unwrap_or(0), at.format("%Y-%m-%d"));
        lines.push_str(&serde_json::to_string(&serde_json::json!({
            "image": name, "placement": r.get::<_, String>(5), "group": group, "source": r.get::<_, Option<String>>(6),
            "licence": if r.get::<_, bool>(7) { "CC0-1.0" } else { PHOTO_LICENCE },
        }))?);
        lines.push('\n');
    }
    std::fs::write(out.join("labels.jsonl"), lines)?;
    info!(photos = rows.len(), out = %out.display(), "exported");
    Ok(())
}

/// Photos kept under the photo licence (docs/photo-licence.md), as the
/// trainer's datasets.toml names it.
const PHOTO_LICENCE: &str = "LicenseRef-open-chess-photo-licence-1";

async fn admin(cmd: AdminCmd) -> Result<()> {
    let (cfg, grant) = match &cmd {
        AdminCmd::Grant { cfg, .. } => (cfg, Some(true)),
        AdminCmd::Revoke { cfg, .. } => (cfg, Some(false)),
        AdminCmd::List(cfg) => (cfg, None),
    };
    cfg.validate()?;
    let db = Db::connect(cfg)?;
    db.migrate().await?;
    let c = db.conn().await?;
    if let (
        Some(on),
        AdminCmd::Grant { username, manager, .. } | AdminCmd::Revoke { username, manager, .. },
    ) = (grant, &cmd)
    {
        let sql = match (manager, on) {
            (false, true) => "UPDATE chess.players SET role = 'admin'",
            (false, false) => "UPDATE chess.players SET role = 'player'",
            (true, true) => {
                "UPDATE chess.players SET roles = array_append(array_remove(roles, 'manager'), 'manager')"
            }
            (true, false) => "UPDATE chess.players SET roles = array_remove(roles, 'manager')",
        };
        let keys: Option<i64> = c
            .query_opt(
                &db.sql(
                    "SELECT (SELECT count(*) FROM chess.passkeys k WHERE k.player_id = a.player_id)
                       FROM chess.accounts a WHERE lower(a.username) = lower($1)",
                ),
                &[username],
            )
            .await?
            .map(|r| r.get(0));
        let keys = keys.with_context(|| format!("no account here is called {username}"))?;
        anyhow::ensure!(
            !on || keys > 0,
            "{username} needs a passkey first (their rights will need it): they add one on their account page, then grant again"
        );
        c.execute(
            &db.sql(&format!(
                "{sql} WHERE id = (SELECT player_id FROM chess.accounts WHERE lower(username) = lower($1))"
            )),
            &[username],
        )
        .await?;
        let what = if *manager { "a manager" } else { "an admin" };
        println!(
            "{username} {} {what}.{}",
            if on { "is now" } else { "is no longer" },
            if on { " Their rights need a passkey check each session." } else { "" }
        );
        return Ok(());
    }
    for r in c
        .query(
            &db.sql(
                "SELECT a.username, p.role, p.roles, p.email, (SELECT count(*) FROM chess.passkeys k WHERE k.player_id = p.id)
                   FROM chess.players p JOIN chess.accounts a ON a.player_id = p.id
                  WHERE p.role = 'admin' OR 'manager' = ANY(p.roles) ORDER BY lower(a.username)",
            ),
            &[],
        )
        .await?
    {
        let (admin, manager) = auth::staff_roles(&r.get::<_, String>(1), &r.get::<_, Vec<String>>(2));
        let roles = [(admin, "admin"), (manager, "manager")].iter().filter(|x| x.0).map(|x| x.1).collect::<Vec<_>>();
        println!(
            "{:<24} {:<16} {:<32} {} passkey(s)",
            r.get::<_, String>(0),
            roles.join(","),
            r.get::<_, Option<String>>(3).unwrap_or_default(),
            r.get::<_, i64>(4)
        );
    }
    Ok(())
}

async fn partner(cmd: PartnerCmd) -> Result<()> {
    let cfg = match &cmd {
        PartnerCmd::Create { cfg, .. } | PartnerCmd::Revoke { cfg, .. } | PartnerCmd::List(cfg) => cfg,
    };
    cfg.validate()?;
    let db = Db::connect(cfg)?;
    db.migrate().await?;
    let c = db.conn().await?;
    match &cmd {
        PartnerCmd::Create { name, scopes, .. } => {
            let (id, token) = crate::http::admin::make_partner(&db, &*c, name, scopes, None).await?;
            println!("partner {name} made (id {id}); its token, shown once:\n{token}");
        }
        PartnerCmd::Revoke { id, .. } => {
            let n = c
                .execute(
                    &db.sql(
                        "UPDATE chess.partners SET revoked_at = now() WHERE id = $1 AND revoked_at IS NULL",
                    ),
                    &[id],
                )
                .await?;
            anyhow::ensure!(n == 1, "no partner {id} (or already revoked)");
            println!("partner {id} revoked");
        }
        PartnerCmd::List(_) => {
            for r in c
                .query(
                    &db.sql(
                        "SELECT id, name, prefix, scopes, last_used_at FROM chess.partners
                          WHERE revoked_at IS NULL ORDER BY id",
                    ),
                    &[],
                )
                .await?
            {
                let used: Option<chrono::DateTime<chrono::Utc>> = r.get(4);
                println!(
                    "{:<4} {:<24} {}… {:<12} last used {}",
                    r.get::<_, i64>(0),
                    r.get::<_, String>(1),
                    r.get::<_, String>(2),
                    r.get::<_, Vec<String>>(3).join(","),
                    used.map_or_else(|| "never".into(), |t| t.to_rfc3339())
                );
            }
        }
    }
    Ok(())
}

fn healthcheck(bind: &str) -> Result<()> {
    use std::io::{Read, Write};
    let port = bind.rsplit(':').next().unwrap_or("9465");
    let mut s = std::net::TcpStream::connect(format!("127.0.0.1:{port}")).context("connecting")?;
    s.set_read_timeout(Some(Duration::from_secs(5)))?;
    s.write_all(b"GET /healthz HTTP/1.0\r\nHost: localhost\r\n\r\n")?;
    let mut buf = String::new();
    s.read_to_string(&mut buf)?;
    if buf.starts_with("HTTP/1.0 200") || buf.starts_with("HTTP/1.1 200") {
        Ok(())
    } else {
        anyhow::bail!("unhealthy: {}", buf.lines().next().unwrap_or(""))
    }
}