ordinaryd 0.8.2

Ordinary Server
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
// Copyright (C) 2026 Ordinary Labs, LLC.
//
// SPDX-License-Identifier: AGPL-3.0-only

use crate::{ProvisionMode, traverse};
use anyhow::bail;
use getrandom::SysRng;
use getrandom::rand_core::Rng;
use hashbrown::HashMap;
use ordinary_app::server::OrdinaryAppServer;
use ordinary_auth::Auth;
use ordinary_config::{OrdinaryApiLimits, OrdinaryConfig};
use ordinary_storage::saferlmdb::EnvBuilder;
use ordinary_storage::{Storage, saferlmdb};
use ordinary_utils::{SecurityMode, shutdown_signal};
use rand_chacha::rand_core::SeedableRng;
use std::fs::File;
use std::io::Write;
use std::net::Ipv6Addr;
use std::path::Path;
use std::sync::{Arc, Mutex};
use tokio::net::TcpListener;

#[allow(
    clippy::fn_params_excessive_bools,
    clippy::too_many_arguments,
    clippy::too_many_lines,
    clippy::missing_panics_doc
)]
pub async fn run(
    project_dir: &str,
    domain_override: &Option<String>,
    data_dir: &str,
    log_size: bool,
    insecure: bool,
    insecure_cookies: bool,
    log_headers: bool,
    log_ips: bool,
    port: Option<u16>,
    redirect_port: Option<u16>,
    provision: &ProvisionMode,
    danger_dns_no_verify: bool,
) -> anyhow::Result<()> {
    tracing::debug!("installing standalone project...");

    let project_path = Path::new(&project_dir);

    tracing::debug!("reading config...");
    let mut config = OrdinaryConfig::get(project_dir)?;
    config.validate()?;

    if !danger_dns_no_verify {
        OrdinaryAppServer::verify_custom_domains(&config).await?;
    }

    if let Some(domain_override) = &domain_override {
        config.domain = domain_override.clone();
    }

    let data_dir = Path::new(data_dir).join("apps").join(&config.domain);

    tracing::debug!("setting up DB environment...");
    let store_path = data_dir.join("store");
    std::fs::create_dir_all(&store_path)?;

    let ps = page_size::get() as u64;

    let storage_size = match &config.storage_size {
        Some(s) => *s,
        None => OrdinaryConfig::default_storage_size().unwrap_or_default(),
    };

    // round up to full OS page
    let remainder = storage_size % ps;
    let mapsize = (storage_size - remainder) + ps;

    tracing::info!(mapsize = %bytesize::ByteSize(mapsize).display().si_short());

    let env = Arc::new(unsafe {
        let mut env_builder = EnvBuilder::new()?;
        env_builder.set_maxreaders(126)?;
        env_builder.set_mapsize(usize::try_from(mapsize)?)?;
        env_builder.set_maxdbs(13)?;
        env_builder.open(
            store_path.to_str().expect("store_path not a str"),
            &saferlmdb::open::Flags::empty(),
            0o600,
        )?
    });

    tracing::debug!("setting up auth...");

    let keys_dir = data_dir.join("keys");
    std::fs::create_dir_all(&keys_dir)?;

    let auth_key_path = keys_dir.join("auth");

    let auth_key: [u8; 32] = if auth_key_path.exists() && auth_key_path.is_file() {
        let auth_key = std::fs::read(&auth_key_path)?;
        let auth_key: [u8; 32] = auth_key[..].try_into()?;
        auth_key
    } else {
        let mut auth_key = [0u8; 32];
        let mut rng = rand_chacha::ChaCha20Rng::try_from_rng(&mut SysRng)?;

        rng.fill_bytes(&mut auth_key[..]);

        let mut auth_key_file = File::create(auth_key_path)?;
        auth_key_file.write_all(&auth_key)?;
        auth_key_file.flush()?;

        auth_key
    };

    tracing::debug!("initializing auth...");

    let auth = Arc::new(Auth::new(
        config.domain.clone(),
        config.auth.clone(),
        auth_key,
        env.clone(),
    )?);

    tracing::debug!("initializing storage...");

    let storage_key_path = keys_dir.join("storage");

    let storage_key: [u8; 32] = if storage_key_path.exists() && storage_key_path.is_file() {
        let storage_key = std::fs::read(&storage_key_path).expect("failed to read storage key");
        let storage_key: [u8; 32] = storage_key[..]
            .try_into()
            .expect("failed to get fixed storage key");
        storage_key
    } else {
        let mut storage_key = [0u8; 32];
        let mut rng = rand_chacha::ChaCha20Rng::try_from_rng(&mut SysRng)?;

        rng.fill_bytes(&mut storage_key[..]);

        let mut storage_key_file =
            File::create(storage_key_path).expect("failed to create storage key file");
        storage_key_file
            .write_all(&storage_key)
            .expect("failed to write storage key");
        storage_key_file
            .flush()
            .expect("failed to flush storage key");

        storage_key
    };

    let limits = OrdinaryApiLimits::default();

    let storage = Arc::new(Storage::new(
        limits.storage.clone(),
        if let Some(models) = &config.models {
            models.clone()
        } else {
            vec![]
        },
        if let Some(content) = &config.content {
            content.definitions.clone()
        } else {
            vec![]
        },
        storage_key,
        &env,
        store_path.join("search"),
        log_size,
    )?);

    tracing::debug!("instantiating app...");
    let app = Arc::new(
        OrdinaryAppServer::new(
            config.for_send()?,
            limits,
            auth,
            storage,
            !insecure,
            !insecure_cookies,
            log_headers,
            log_ips,
            None,
            None,
            None,
            None,
        )
        .await?,
    );

    let app_clone = app.clone();

    tracing::debug!("copying content...");
    if let Some(content) = config.content {
        let content_path = Path::new(&content.file_path);
        let objects_json = std::fs::read_to_string(project_path.join(content_path))?;

        let objects: Vec<ordinary_types::ContentObject> = serde_json::from_str(&objects_json)?;

        app.update_content(&objects).await?;
    }

    tracing::debug!("copying assets...");
    if let Some(assets) = config.assets {
        let Some(assets_dir_path) = &assets.dir_path else {
            bail!("assets.dir_path cannot be unset");
        };

        let parent_path = &project_path.join(assets_dir_path);

        let assets_path_content = Arc::new(Mutex::new(Vec::new()));

        traverse(parent_path, &|entry| {
            let path = entry.path();

            if path.ends_with(".DS_Store") {
                tracing::warn!("ignoring .DS_Store");
            } else if let Ok(content) = std::fs::read(&path)
                && let Ok(path) = path.strip_prefix(parent_path)
                && let Some(path) = path.to_str()
                && let Ok(lock) = assets_path_content.lock()
            {
                let mut lock = lock;
                lock.push((path.to_string(), content));
            }
        })?;

        let mut assets = vec![];

        if let Ok(lock) = assets_path_content.lock() {
            for (path, content) in lock.iter() {
                assets.push((path.clone(), content.clone()));
            }
        }

        for (path, content) in &assets {
            app_clone.put_asset(path, content).await?;
        }
    }

    if config.auth.is_some() {
        let core_js =
            std::fs::read_to_string(project_path.join("./.ordinary/gen/client/js/core.js"))?;

        app.put_asset(
            &format!("{}/js/core.js", config.version),
            core_js.replace("{{ version }}", &config.version).as_bytes(),
        )
        .await?;
    }

    if config.auth.is_some() {
        let js_client_js =
            std::fs::read_to_string(project_path.join("./.ordinary/gen/client/js/client.js"))?;

        app.put_asset(
            &format!("{}/js/client.js", config.version),
            js_client_js
                .replace("{{ version }}", &config.version)
                .as_bytes(),
        )
        .await?;
    }

    if config.auth.is_some()
        || config.obfuscation == Some(true)
        || config.client_rendering == Some(true)
    {
        let client_js =
            std::fs::read_to_string(project_path.join("./.ordinary/gen/client/wasm/client.js"))?;
        let client_wasm = std::fs::read(
            Path::new(project_path).join(".ordinary/gen/client/wasm/client_bg_opt.wasm"),
        )
        .or_else(|_| {
            std::fs::read(Path::new(project_path).join(".ordinary/gen/client/wasm/client_bg.wasm"))
        })?;

        app.put_asset(
            &format!("{}/wasm/client.js", config.version),
            client_js
                .replace("{{ version }}", &config.version)
                .as_bytes(),
        )
        .await?;
        app.put_asset(
            &format!("{}/wasm/client_bg.wasm", config.version),
            &client_wasm,
        )
        .await?;
    }

    if let Some(actions) = config.actions {
        if !actions.is_empty() {
            tracing::debug!("installing actions...");
        }

        for config in actions {
            let Some(dir_path) = &config.dir_path else {
                tracing::error!("no dir_path provided for action");
                bail!("no dir_path provided for action");
            };

            let path = project_path
                .join(dir_path)
                .join("target/wasm32-wasip1/release/action.wasm");
            let action_bytes = std::fs::read(path)?;

            app.set_action(config.idx, action_bytes.as_slice()).await?;
        }
    }

    if let Some(templates) = config.templates {
        if !templates.is_empty() {
            tracing::debug!("installing templates...");
        }

        for config in templates {
            let path = project_path
                .join(".ordinary")
                .join("gen")
                .join("server")
                .join(&config.name)
                .join("target/wasm32-wasip1/release/template.wasm");
            let template_bytes = std::fs::read(path)?;

            let path = project_path
                .join(".ordinary")
                .join("gen")
                .join("hashes")
                .join(&config.name);

            let style_path = path.join("style-src.json");
            let script_path = path.join("script-src.json");

            let csp_style_hashes = if style_path.exists() {
                let csp_style_hashes: Vec<String> =
                    serde_json::from_str(&std::fs::read_to_string(style_path)?)?;
                Some(csp_style_hashes)
            } else {
                None
            };
            let csp_script_hashes = if script_path.exists() {
                let csp_script_hashes: Vec<String> =
                    serde_json::from_str(&std::fs::read_to_string(script_path)?)?;
                Some(csp_script_hashes)
            } else {
                None
            };

            app.set_template(
                config.idx,
                template_bytes.as_slice(),
                csp_style_hashes,
                csp_script_hashes,
                !insecure,
            )
            .await?;
        }
    }

    let port = if insecure {
        port.unwrap_or(config.port.unwrap_or(80))
    } else {
        port.unwrap_or(config.port.unwrap_or(443))
    };

    let mut proxy_listeners = HashMap::new();

    for (domain, (_, proxy_port)) in &*app.proxies {
        if let Some(proxy_port) = proxy_port {
            let listener = TcpListener::bind((Ipv6Addr::UNSPECIFIED, *proxy_port)).await?;
            proxy_listeners.insert(domain.clone(), listener);
        } else {
            let listener = TcpListener::bind((Ipv6Addr::UNSPECIFIED, 0)).await?;
            proxy_listeners.insert(domain.clone(), listener);
        }
    }

    let redirect_listener = if insecure {
        None
    } else {
        let redirect_port = redirect_port.unwrap_or(config.redirect_port.unwrap_or(80));

        Some(TcpListener::bind((Ipv6Addr::UNSPECIFIED, redirect_port)).await?)
    };

    let listener = TcpListener::bind((Ipv6Addr::UNSPECIFIED, port)).await?;

    app.start(
        listener,
        redirect_listener,
        Some(proxy_listeners),
        if insecure {
            SecurityMode::Insecure
        } else {
            SecurityMode::Secure(
                Path::new(&data_dir).join(&config.domain).join("certs"),
                match provision {
                    ProvisionMode::Localhost => ordinary_utils::ProvisionMode::Localhost,
                    ProvisionMode::Staging => ordinary_utils::ProvisionMode::Staging,
                    ProvisionMode::Production => ordinary_utils::ProvisionMode::Production,
                },
            )
        },
        None,
        shutdown_signal,
    )
    .await
}