973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 1 | # drives malice-worker's Host in a headless firefox shared worker against a throwaway |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 2 | # continuwuity, `nix develop -c nu crates/malice-worker/run.nu` |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 3 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 4 | const crate = path self . |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 5 | const root = path self ../.. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 6 | const username = "malice-harness" |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 7 | const password = "malice-harness-password" |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 8 | const room_name = "malice-harness" |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 9 | const seeded = "seeded by run.nu" |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 10 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 11 | # the shared build wrapper sets CARGO_TARGET_DIR, the nix sandbox does not |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 12 | def target [] { $env.CARGO_TARGET_DIR? | default ($root | path join target) } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 13 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 14 | def scratch [] { target | path join harness } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 15 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 16 | def start-homeserver [] { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 17 | let dir = scratch |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 18 | rm -rf $dir |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 19 | mkdir $dir |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 20 | let port = 6267 |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 21 | let config = $dir | path join homeserver.toml |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 22 | let log = $dir | path join homeserver.log |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 23 | { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 24 | global: { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 25 | server_name: "localhost" |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 26 | database_path: ($dir | path join db) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 27 | address: ["127.0.0.1"] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 28 | port: $port |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 29 | allow_federation: false |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 30 | new_user_displayname_suffix: "" |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 31 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 32 | } | to toml | save $config |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 33 | # the first account otherwise needs a one-time token that is only ever printed to the log |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 34 | let create = $"users create-user ($username) ($password)" |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 35 | let pid = ^bash -c 'conduwuit -c "$1" --execute "$2" > "$3" 2>&1 & echo $!' _ $config $create $log | str trim |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 36 | let url = $'http://127.0.0.1:($port)' |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 37 | for _ in 1..100 { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 38 | if (try { login $url | ignore; true } catch { false }) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 39 | return { pid: $pid, url: $url } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 40 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 41 | sleep 100ms |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 42 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 43 | try { stop $pid } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 44 | print (open --raw $log | lines | last 20 | str join "\n") |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 45 | error make { msg: "homeserver did not come up" } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 46 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 47 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 48 | def login [url: string] { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 49 | let body = { type: "m.login.password", identifier: { type: "m.id.user", user: $username }, password: $password } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 50 | http post --content-type application/json $'($url)/_matrix/client/v3/login' $body |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 51 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 52 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 53 | def seed [url: string] { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 54 | let auth = [Authorization $'Bearer ((login $url).access_token)'] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 55 | let room = http post --headers $auth --content-type application/json $'($url)/_matrix/client/v3/createRoom' { name: $room_name } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 56 | let room_id = $room.room_id | url encode --all |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 57 | let message = { msgtype: "m.text", body: $seeded } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 58 | http put --headers $auth --content-type application/json $'($url)/_matrix/client/v3/rooms/($room_id)/send/m.room.message/seed' $message | ignore |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 59 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 60 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 61 | def stop [pid: string] { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 62 | ^kill -9 $pid |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 63 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 64 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 65 | def main [] { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 66 | let server = start-homeserver |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 67 | let result = try { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 68 | let versions = http get $'($server.url)/_matrix/client/versions' |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 69 | if not ($versions.unstable_features | get -o 'org.matrix.simplified_msc3575' | default false) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 70 | error make { msg: "homeserver does not advertise simplified sliding sync" } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 71 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 72 | seed $server.url |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 73 | cd $crate |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 74 | with-env { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 75 | MALICE_HOMESERVER: $server.url |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 76 | MALICE_USERNAME: $username |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 77 | MALICE_PASSWORD: $password |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 78 | MALICE_ROOM_NAME: $room_name |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 79 | MALICE_SEEDED: $seeded |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 80 | GECKODRIVER: (which geckodriver | get 0.path) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 81 | WASM_BINDGEN_TEST_TIMEOUT: "180" |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 82 | } { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 83 | ^cargo test --target wasm32-unknown-unknown --test host |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 84 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 85 | null |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 86 | } catch {|error| $error } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 87 | stop $server.pid |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 88 | if $result != null { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 89 | print $'homeserver log at (scratch)/homeserver.log' |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 90 | error make { msg: $result.msg } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 91 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 92 | } |