This repository has been archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
/
rammerhead.tsx
97 lines (78 loc) · 2.73 KB
/
rammerhead.tsx
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
import type { HolyPage } from '../../App';
import { getDestination } from '../../CompatLayout';
import { RammerheadAPI, StrShuffler } from '../../RammerheadAPI';
import { RH_API } from '../../consts';
import Cookies from 'js-cookie';
import { useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { useLocation } from 'react-router-dom';
function patch(url: string) {
// url = _rhsEPrcb://bqhQko.tHR/
// remove slash
return url.replace(/(^.*?:\/)\//, '$1');
}
const Rammerhead: HolyPage = ({ compatLayout }) => {
const { t } = useTranslation('compat');
const location = useLocation();
useEffect(() => {
(async function () {
let errorCause: string | undefined;
try {
const api = new RammerheadAPI(RH_API);
// according to our NGINX config
if (import.meta.env.NODE_ENV === 'production') {
Cookies.set('auth_proxy', '1', {
domain: `.${globalThis.location.host}`,
expires: 1000 * 60 * 60 * 24 * 7, // 1 week
secure: globalThis.location.protocol === 'https:',
sameSite: 'lax',
});
Cookies.set('origin_proxy', globalThis.location.origin, {
expires: 1000 * 60 * 60 * 24 * 7, // 1 week
secure: globalThis.location.protocol === 'https:',
sameSite: 'lax',
});
}
errorCause = t('error.unreachable', { what: 'Rammerhead' });
await fetch(RH_API);
errorCause = undefined;
errorCause = t('error.rammerheadSavedSession');
if (
localStorage.rammerhead_session &&
(await api.sessionExists(localStorage.rammerhead_session))
) {
const test = await fetch(
new URL(localStorage.rammerhead_session, RH_API),
);
await api.deleteSession(localStorage.rammerhead_session);
// 404 = good, 403 = Sessions must come from the same IP
if (test.status === 403) delete localStorage.rammerhead_session;
} else {
delete localStorage.rammerhead_session;
}
errorCause = t('error.rammerheadNewSession');
const session =
localStorage.rammerhead_session || (await api.newSession());
errorCause = undefined;
errorCause = undefined;
errorCause = t('error.rammerheadEditSession');
await api.editSession(session, false, true);
errorCause = undefined;
errorCause = t('error.rammerheadDict');
const dict = await api.shuffleDict(session);
errorCause = undefined;
const shuffler = new StrShuffler(dict);
globalThis.location.replace(
new URL(
`${session}/${patch(shuffler.shuffle(getDestination(location)))}`,
RH_API,
),
);
} catch (err) {
compatLayout.current!.report(err, errorCause, 'Rammerhead');
}
})();
}, [compatLayout, location, t]);
return <main>{t('loading', { what: 'Rammerhead' })}</main>;
};
export default Rammerhead;