🗝
summary refs log tree commit diff
path: root/nginx/nginx.js
blob: 4557167810fc4f9da9698409c11520265dff39b9 (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
/** @type {import('./nginx.d.ts')} */

/** @param {NginxHTTPRequest} request */
async function validate(request) {
    if (request.status !== 0) return;

    const token = request.variables.cookie___proxy_token;

    if (token == undefined) {
        // missing token
        request.return(401);
        return;
    }

    const cache = ngx.shared.auth_token_cache;
    if (cache === undefined) throw "missing shared js cache";

    const requiredScope = request.variables.required_scope;
    if (requiredScope === undefined) throw "missing required scope variable";

    let scopes = cache.get(token);

    if (scopes === undefined) {
        const subrequest = await request.subrequest(`/.nginx/scopes`, {
            args: `token=${token}`
        });

        if (subrequest.status !== 200) {
            // invalid token
            return request.return(401);
        }

        scopes = subrequest.responseText.split("\n");

        cache.set(token, scopes.join(","));
    } else {
        scopes = scopes.split(",");
    }

    if (scopes.indexOf(requiredScope) === -1) {
        return request.return(403);
    }

    return request.return(200);
}

const cat_sounds = ["meow", "mrow", "mew", "nya", "purr", "miau"];
/** @param {NginxHTTPRequest} request */
async function cat(request) {
    let buffer = "";
    buffer += "\x1b]0;meow\x07"; // set title
    buffer += "\x1b[s"; // save cursor position
    let verbosity = 50;
    if (request.uri.length > 1) {
        const supplied = parseInt(request.uri.slice(1));
        if (supplied !== NaN) verbosity = supplied;
        if (supplied > 1000) verbosity = 1000;
    }
    for (let i = 0; i < verbosity; i++) {
        const sound = cat_sounds[Math.floor(Math.random() * cat_sounds.length)];
        const col = Math.floor(Math.random() * (120 - sound.length + 2)) + 1;
        const row = Math.floor(Math.random() * 40) + 1;
        buffer += `\x1b[${row};${col}H ${sound} `; // write sound at position
    }
    buffer += '\x1b[u' // restore cursor position
    request.return(200, buffer);
}

export default {
    validate,
    cat,
}