aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/canaries/canaries.ts
blob: dab2e689cd9ac9d2a9f260d659af24966b53d334 (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
import { browser } from '$app/environment';
import { base } from '$app/paths';
import { validateSignature } from './keystore';

export const canaries: Canary[] = [];
export interface CanaryInterface {
  name: string;
  description: string;
  signer: string;
  url: string;
  keyIdentifier: string;
  contentType: string;
  upstream?: string;
}
export interface Canary extends CanaryInterface {}
type _PreCanaryInterfaceEntries = {
  [T in keyof CanaryInterface]: [T, CanaryInterface[T]];
};
type CanaryInterfaceEntry =
  _PreCanaryInterfaceEntries[keyof _PreCanaryInterfaceEntries];
export class Canary {
  public constructor(canary: CanaryInterface) {
    const entries = Object.entries(canary) as CanaryInterfaceEntry[];
    for (const a of entries) {
      // @ts-ignore we know the value will match in type
      this[a[0]] = a[1];
    }
    canaries.push(this);
  }
  public forceContentType = false;
  public async getRawText() {
    if (!browser) throw new Error('This should only be done in a browser.');
    const res = await fetch(this.url);
    if (res.ok) {
      if (!this.forceContentType)
        this.contentType = res.headers.get('Content-Type') || this.contentType;
      const text = await res.text();
      return text;
    } else {
      throw new Error(`Fetching canary failed with code ${res.status} (${res.statusText}):
${await res.text().catch((e) => `Unknown (Unable to get text, ${JSON.stringify(e)})`)}`);
    }
  }
  public async getValidatedText(
    rawText: string | Promise<string> = this.getRawText(),
  ) {
    return await validateSignature(await rawText, this.keyIdentifier);
  }
  /** Returns downloadable data url if signature passes, otherwise returns null */
  public async getUrl(rawText: string | Promise<string> = this.getRawText()) {
    const t = await rawText;
    let stripped: string;
    try {
      stripped = await this.getValidatedText(t);
    } catch (error) {
      console.warn('Failed to validate signature: ', error);
      return null;
    }
    return {
      stripped: URL.createObjectURL(
        new Blob([stripped], {
          type: this.contentType,
        }),
      ),
      signed: URL.createObjectURL(
        new Blob([t], {
          type: this.contentType,
        }),
      ),
    };
  }
}
new Canary({
  name: 'estrogen.zone',
  description: 'Services hosted around estrogen.zone and yuridick.gay',
  signer: 'memdmp',
  url: base + '/canaries/memdmp:estrogen.zone',
  keyIdentifier: 'memdmp',
  contentType: 'text/plain; charset=utf-8',
});

new Canary({
  name: 'kyun.host',
  description: 'The VPS provider "kyun.host"',
  signer: 'napatha',
  url: base + '/canaries/napatha:kyun.host',
  keyIdentifier: 'napatha',
  contentType: 'text/plain; charset=utf-8',
  upstream: 'https://files.kyun.host/canary.txt',
});