Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit 42ebc61

Browse files
authored
fix: Check HTTPS_ENABLED env var before HTTPS redirection (#7)
* fix: Use environment variable to determine https redirection * fix: Pass HTTPS_ENABLED in routing * fix: Use existing import to pass https var * fix: Pass env to news routes * fix: Pass env to detail, username * fix: Pass env var to creation routes * fix: Pass env var to about * fix: Remove redundant else block * feat(infra): Attempt to run a javascript world configuration via composer * feat(infra): Attempt to pass worlds from server repository to composer, to website container * feat(infra): Attempt to fix path conflict with composer * feat(infra): Fix file name from server repo * feat(infra): Add doc string for the javascript
1 parent 5775d60 commit 42ebc61

8 files changed

Lines changed: 69 additions & 14 deletions

File tree

src/lostcity/routes/about/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1+
import Environment from '#lostcity/util/Environment.js';
2+
13
export default function (f, opts, next) {
24
f.get('/', async (req, res) => {
3-
return res.view('about/index');
5+
return res.view('about/index', {HTTPS_ENABLED: Environment.HTTPS_ENABLED});
46
});
57

68
f.get('/getstart', async (req, res) => {
7-
return res.view('about/getstart');
9+
return res.view('about/getstart', {HTTPS_ENABLED: Environment.HTTPS_ENABLED});
810
});
911

1012
f.get('/virtual', async (req, res) => {
11-
return res.view('about/virtual');
13+
return res.view('about/virtual', {HTTPS_ENABLED: Environment.HTTPS_ENABLED});
1214
});
1315

1416
f.get('/whatisrs', async (req, res) => {
15-
return res.view('about/whatisrs');
17+
return res.view('about/whatisrs', {HTTPS_ENABLED: Environment.HTTPS_ENABLED});
1618
});
1719

1820
next();

src/lostcity/routes/create/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { toDisplayName, toSafeName } from '#jagex2/jstring/JString.js';
44

55
import { db } from '#lostcity/db/query.js';
66

7+
import Environment from '#lostcity/util/Environment.js';
8+
79
enum CreateStep {
810
USERNAME,
911
TERMS,
@@ -37,22 +39,25 @@ export default function (f: any, opts: any, next: any) {
3739
delete req.session.createUsername;
3840

3941
return res.view('create/username', {
42+
HTTPS_ENABLED: Environment.HTTPS_ENABLED,
4043
error: createError
4144
});
4245
} else if (createStep === CreateStep.TERMS) {
4346
return res.view('create/terms', {
47+
HTTPS_ENABLED: Environment.HTTPS_ENABLED,
4448
username: createUsername
4549
});
4650
} else if (createStep === CreateStep.PASSWORD) {
4751
return res.view('create/password', {
52+
HTTPS_ENABLED: Environment.HTTPS_ENABLED,
4853
username: createUsername,
4954
error: createError
5055
});
5156
} else if (createStep === CreateStep.FINISH) {
5257
delete req.session.createStep;
5358
delete req.session.createUsername;
5459

55-
return res.view('create/finish');
60+
return res.view('create/finish', {HTTPS_ENABLED: Environment.HTTPS_ENABLED});
5661
}
5762
});
5863

src/lostcity/routes/mod/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { db } from '#lostcity/db/query.js';
2+
import Environment from '#lostcity/util/Environment.js';
23

34
export default function (f: any, opts: any, next: any) {
45
f.get('/session/:username', async (req: any, res: any) => {
@@ -8,6 +9,7 @@ export default function (f: any, opts: any, next: any) {
89
const account = await db.selectFrom('account').where('username', '=', username).selectAll().executeTakeFirstOrThrow();
910

1011
return res.view('mod/session', {
12+
HTTPS_ENABLED: Environment.HTTPS_ENABLED,
1113
account,
1214
logs: await db.selectFrom('account_session').where('account_id', '=', account.id).orderBy('timestamp desc').limit(100).selectAll().execute()
1315
});

src/lostcity/routes/news/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export default function (f: any, opts: any, next: any) {
5858
newsposts = newsposts.limit(17);
5959

6060
return res.view('news/index', {
61+
HTTPS_ENABLED: Environment.HTTPS_ENABLED,
6162
category,
6263
page,
6364
more,
@@ -78,6 +79,7 @@ export default function (f: any, opts: any, next: any) {
7879
const next = await db.selectFrom('newspost').where('id', '>', req.params.id).where('category_id', '=', newspost.category_id).orderBy('id', 'asc').select('id').executeTakeFirst();
7980

8081
return res.view('news/post', {
82+
HTTPS_ENABLED: Environment.HTTPS_ENABLED,
8183
newspost,
8284
category,
8385
date: niceDate(newspost.date),
@@ -105,6 +107,7 @@ export default function (f: any, opts: any, next: any) {
105107
const newspost = await db.selectFrom('newspost').where('id', '=', post).selectAll().executeTakeFirst();
106108
if (newspost) {
107109
return res.view('news/create', {
110+
HTTPS_ENABLED: Environment.HTTPS_ENABLED,
108111
categories,
109112
date: niceDate(newspost.date),
110113
post,
@@ -114,6 +117,7 @@ export default function (f: any, opts: any, next: any) {
114117
}
115118

116119
return res.view('news/create', {
120+
HTTPS_ENABLED: Environment.HTTPS_ENABLED,
117121
categories
118122
});
119123
});
@@ -160,6 +164,7 @@ export default function (f: any, opts: any, next: any) {
160164
const categories = await db.selectFrom('newspost_category').selectAll().execute();
161165

162166
return res.view('news/create', {
167+
HTTPS_ENABLED: Environment.HTTPS_ENABLED,
163168
categories,
164169
post,
165170
date: niceDate(new Date()),
@@ -183,6 +188,7 @@ export default function (f: any, opts: any, next: any) {
183188
const newspost = await db.selectFrom('newspost').where('id', '=', post).selectAll().executeTakeFirst();
184189
if (newspost) {
185190
return res.view('news/create', {
191+
HTTPS_ENABLED: Environment.HTTPS_ENABLED,
186192
categories,
187193
post,
188194
newspost,
@@ -195,6 +201,7 @@ export default function (f: any, opts: any, next: any) {
195201
}
196202

197203
return res.view('news/create', {
204+
HTTPS_ENABLED: Environment.HTTPS_ENABLED,
198205
categories,
199206
post,
200207
date: niceDate(new Date()),

src/lostcity/routes/player/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { db } from '#lostcity/db/query.js';
2+
import Environment from '#lostcity/util/Environment.js';
23
import LoggerEventType from '#lostcity/util/LoggerEventType.js';
34

45
export default function (f: any, opts: any, next: any) {
@@ -9,6 +10,7 @@ export default function (f: any, opts: any, next: any) {
910
const account = await db.selectFrom('account').where('username', '=', username).selectAll().executeTakeFirstOrThrow();
1011

1112
return res.view('player/adventurelog', {
13+
HTTPS_ENABLED: Environment.HTTPS_ENABLED,
1214
account,
1315
logs: await db.selectFrom('account_session').where('account_id', '=', account.id).where('event_type', '=', LoggerEventType.ADVENTURE).orderBy('timestamp desc').limit(100).selectAll().execute()
1416
});

src/lostcity/routes/website.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import Environment from '#lostcity/util/Environment.js';
66

77
export default function (f, opts, next) {
88
f.get('/', async (req, res) => {
9-
return res.view('index');
9+
return res.view('index', {HTTPS_ENABLED: Environment.HTTPS_ENABLED});
1010
});
1111

1212
f.get('/disclaimer', async (req, res) => {
13-
return res.view('disclaimer');
13+
return res.view('disclaimer', {HTTPS_ENABLED: Environment.HTTPS_ENABLED});
1414
});
1515

1616
f.get('/title', async (req, res) => {
@@ -21,6 +21,7 @@ export default function (f, opts, next) {
2121

2222
const latestNews = Environment.DB_HOST ? await db.selectFrom('newspost').orderBy('id', 'desc').limit(5).selectAll().execute() : [];
2323
return res.view('title', {
24+
HTTPS_ENABLED: Environment.HTTPS_ENABLED,
2425
playerCount,
2526
newsposts: latestNews
2627
});
@@ -59,6 +60,7 @@ export default function (f, opts, next) {
5960
}
6061

6162
return res.view('serverlist', {
63+
HTTPS_ENABLED: Environment.HTTPS_ENABLED,
6264
detail: typeof req.query['hires.x'] !== 'undefined' ? 'high' : 'low',
6365
method: req.query.method,
6466
worlds: WorldList,
@@ -74,43 +76,43 @@ export default function (f, opts, next) {
7476
});
7577

7678
f.get('/cookies', async (req, res) => {
77-
return res.view('cookies');
79+
return res.view('cookies', {HTTPS_ENABLED: Environment.HTTPS_ENABLED});
7880
});
7981

8082
f.get('/copyright', async (req, res) => {
8183
return res.view('copyright');
8284
});
8385

8486
f.get('/detail', async (req, res) => {
85-
return res.view('detail');
87+
return res.view('detail', {HTTPS_ENABLED: Environment.HTTPS_ENABLED});
8688
});
8789

8890
f.get('/manual', async (req, res) => {
8991
return res.view('manual');
9092
});
9193

9294
f.get('/privacy', async (req, res) => {
93-
return res.view('privacy');
95+
return res.view('privacy', {HTTPS_ENABLED: Environment.HTTPS_ENABLED});
9496
});
9597

9698
f.get('/support', async (req, res) => {
9799
return res.view('support');
98100
});
99101

100102
f.get('/terms', async (req, res) => {
101-
return res.view('terms');
103+
return res.view('terms', {HTTPS_ENABLED: Environment.HTTPS_ENABLED});
102104
});
103105

104106
f.get('/whychoosers', async (req, res) => {
105-
return res.view('whychoosers');
107+
return res.view('whychoosers', {HTTPS_ENABLED: Environment.HTTPS_ENABLED});
106108
});
107109

108110
f.get('/worldmap', async (req, res) => {
109111
return res.view('worldmap/applet');
110112
});
111113

112114
f.get('/downloads', async (req, res) => {
113-
return res.view('downloads');
115+
return res.view('downloads', {HTTPS_ENABLED: Environment.HTTPS_ENABLED});
114116
});
115117

116118
next();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @fileoverview
3+
* This script dynamically loads a world configuration file from the Server repo.
4+
*
5+
* Purpose:
6+
* - This script loads worlds file dynamically from server repo
7+
* - Its meant to be used only with docker production build
8+
*
9+
* Why?
10+
* - You don't have to pull and modify Website repository just to configure your worlds
11+
* - Simplifies running prod setup
12+
* - Keeps the Server repo as the single source
13+
*/
14+
15+
import fs from 'fs';
16+
import path from 'path';
17+
import worldConfig from '/usr/src/prodWorldsConf.js';
18+
19+
const filePath = path.resolve('/usr/src/app/data/config/worlds.json');
20+
21+
fs.mkdirSync(path.dirname(filePath), {recursive: true});
22+
fs.writeFileSync(filePath, JSON.stringify(worldConfig, null, 2));
23+
24+
console.log('[INFO] Worlds config generated! Much wow ');
25+
console.log(`
26+
(\\(\\
27+
( -.-) ☆彡
28+
o_(")(") You're a star!\n`
29+
);

view/_partial/header.ejs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@
3838
<body bgcolor=black text="white" link=#90c040 alink=#90c040 vlink=#90c040 topmargin=0 leftmargin=0>
3939
<% // https://stackoverflow.com/a/4723302 %>
4040
<% // https://stackoverflow.com/a/6222280 %>
41-
<img style="display: none;" src="https://2004scape.org/img/blank.gif" onload="if (location.protocol !== 'https:') { location.replace(`https:${location.href.substring(location.protocol.length)}`); }">
41+
<% if (HTTPS_ENABLED) { %>
42+
<img
43+
style="display: none;"
44+
src="https://2004scape.org/img/blank.gif"
45+
onload="if (location.protocol !== 'https:') { location.replace(`https:${location.href.substring(location.protocol.length)}`); }"
46+
>
47+
<% } %>
4248
<table width=100% height=100% cellpadding=0 cellspacing=0>
4349
<tr>
4450
<td valign=middle>

0 commit comments

Comments
 (0)