Skip to content

Commit cdb984b

Browse files
authored
Merge pull request #1360 from gnmyt/features/https
🛡️ HTTPS Support
2 parents 92dc4cd + 915a6d0 commit cdb984b

6 files changed

Lines changed: 254 additions & 1 deletion

File tree

docs/.vitepress/config/de.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const de = defineConfig({
1717
text: 'Anleitungen',
1818
items: [
1919
{ text: 'Einrichten eines Reverse Proxys', link: 'de/guides/reverse-proxy' },
20+
{ text: 'HTTPS einrichten', link: 'de/guides/https' },
2021
{ text: 'Statistiken & Diagramme', link: 'de/guides/statistics' }
2122
]
2223
},

docs/.vitepress/config/en.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const en = defineConfig({
1717
text: 'Guides',
1818
items: [
1919
{ text: 'Configuring a Reverse Proxy', link: 'guides/reverse-proxy' },
20+
{ text: 'Setting up HTTPS', link: 'guides/https' },
2021
{ text: 'Statistics & Charts', link: 'guides/statistics' }
2122
]
2223
},

docs/de/guides/https.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# HTTPS einrichten
2+
3+
::: tip Warum HTTPS verwenden?
4+
HTTPS verschlüsselt die Verbindung zwischen deinem Browser und MySpeed und schützt deine Daten vor dem Abfangen.
5+
Dies ist besonders wichtig, wenn du über ein Netzwerk oder das Internet auf MySpeed zugreifst.
6+
:::
7+
8+
## Übersicht
9+
10+
MySpeed unterstützt HTTPS nativ ohne einen Reverse Proxy zu benötigen. Lege einfach deine SSL-Zertifikate im Verzeichnis `data/certs` ab, und MySpeed startet automatisch einen HTTPS-Server.
11+
12+
## Konfiguration
13+
14+
### Umgebungsvariablen
15+
16+
| Variable | Standard | Beschreibung |
17+
|----------|----------|--------------|
18+
| `HTTPS_PORT` | `5217` | Der Port für den HTTPS-Server |
19+
20+
### Zertifikatsdateien
21+
22+
Lege deine SSL-Zertifikate im Verzeichnis `data/certs` ab:
23+
24+
- `cert.pem` - Dein SSL-Zertifikat
25+
- `key.pem` - Dein privater Schlüssel
26+
27+
Die Ordnerstruktur sollte so aussehen:
28+
29+
```
30+
MySpeed/
31+
├── data/
32+
│ ├── certs/
33+
│ │ ├── cert.pem
34+
│ │ └── key.pem
35+
│ └── ...
36+
└── ...
37+
```
38+
39+
## Eigene Zertifikate verwenden
40+
41+
Wenn du bereits SSL-Zertifikate hast (z.B. von Let's Encrypt oder einer Zertifizierungsstelle), kopiere sie in das Verzeichnis `data/certs`:
42+
43+
```sh
44+
cp /pfad/zu/deinem/zertifikat.pem /pfad/zu/myspeed/data/certs/cert.pem
45+
cp /pfad/zu/deinem/privater-schluessel.pem /pfad/zu/myspeed/data/certs/key.pem
46+
```
47+
48+
## Selbstsigniertes Zertifikat erstellen
49+
50+
Für Tests oder den internen Gebrauch kannst du ein selbstsigniertes Zertifikat erstellen:
51+
52+
```sh
53+
openssl req -x509 -newkey rsa:4096 \
54+
-keyout data/certs/key.pem \
55+
-out data/certs/cert.pem \
56+
-sha256 -days 365 -nodes \
57+
-subj "/C=DE/ST=Bundesland/L=Stadt/O=Organisation/OU=Abteilung/CN=localhost"
58+
```
59+
60+
::: warning Selbstsignierte Zertifikate
61+
Selbstsignierte Zertifikate zeigen eine Sicherheitswarnung im Browser an. Das ist bei selbstsignierten Zertifikaten normal.
62+
Für den Produktiveinsatz empfehlen wir Zertifikate von einer vertrauenswürdigen Zertifizierungsstelle wie Let's Encrypt.
63+
:::
64+
65+
## Let's Encrypt Zertifikate verwenden
66+
67+
Wenn du Let's Encrypt mit certbot verwendest, werden deine Zertifikate normalerweise in `/etc/letsencrypt/live/deine-domain.de/` gespeichert. Du kannst sie entweder kopieren oder verlinken:
68+
69+
```sh
70+
# Zertifikate kopieren
71+
sudo cp /etc/letsencrypt/live/deine-domain.de/fullchain.pem /pfad/zu/myspeed/data/certs/cert.pem
72+
sudo cp /etc/letsencrypt/live/deine-domain.de/privkey.pem /pfad/zu/myspeed/data/certs/key.pem
73+
74+
# Stelle sicher, dass MySpeed sie lesen kann
75+
sudo chown $USER:$USER /pfad/zu/myspeed/data/certs/*.pem
76+
```
77+
78+
::: tip Zertifikatserneuerung
79+
Denke daran, deine Zertifikate im Verzeichnis `data/certs` zu aktualisieren, wenn sie erneuert werden.
80+
Du kannst dies mit einem Post-Renewal-Hook in certbot automatisieren.
81+
:::
82+
83+
## Überprüfen ob HTTPS funktioniert
84+
85+
Nachdem du deine Zertifikate abgelegt und MySpeed gestartet hast, solltest du in der Konsole sehen:
86+
87+
```
88+
Server listening on port 5216
89+
HTTPS server listening on port 5217
90+
```
91+
92+
Du kannst dann auf MySpeed zugreifen über:
93+
- HTTP: `http://localhost:5216`
94+
- HTTPS: `https://localhost:5217`
95+
96+
## Docker-Konfiguration
97+
98+
Bei Verwendung von Docker mountest du das Zertifikatsverzeichnis:
99+
100+
```yaml
101+
version: "3"
102+
services:
103+
myspeed:
104+
image: germannewsmaker/myspeed
105+
ports:
106+
- "5216:5216"
107+
- "5217:5217" # HTTPS-Port
108+
volumes:
109+
- /pfad/zu/myspeed:/myspeed/data
110+
# Zertifikate befinden sich in /pfad/zu/myspeed/certs/
111+
environment:
112+
- HTTPS_PORT=5217 # Optional, 5217 ist Standard
113+
```
114+

docs/en/guides/https.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Setting up HTTPS
2+
3+
::: tip Why use HTTPS?
4+
HTTPS encrypts the connection between your browser and MySpeed, protecting your data from being intercepted.
5+
This is especially important if you access MySpeed over a network or the internet.
6+
:::
7+
8+
## Overview
9+
10+
MySpeed supports HTTPS natively without requiring a reverse proxy. Simply place your SSL certificates in the `data/certs` directory, and MySpeed will automatically start an HTTPS server.
11+
12+
## Configuration
13+
14+
### Environment Variables
15+
16+
| Variable | Default | Description |
17+
|----------|---------|-------------|
18+
| `HTTPS_PORT` | `5217` | The port for the HTTPS server |
19+
20+
### Certificate Files
21+
22+
Place your SSL certificates in the `data/certs` directory:
23+
24+
- `cert.pem` - Your SSL certificate
25+
- `key.pem` - Your private key
26+
27+
The folder structure should look like this:
28+
29+
```
30+
MySpeed/
31+
├── data/
32+
│ ├── certs/
33+
│ │ ├── cert.pem
34+
│ │ └── key.pem
35+
│ └── ...
36+
└── ...
37+
```
38+
39+
## Using Your Own Certificates
40+
41+
If you already have SSL certificates (e.g., from Let's Encrypt or a certificate authority), copy them to the `data/certs` directory:
42+
43+
```sh
44+
cp /path/to/your/certificate.pem /path/to/myspeed/data/certs/cert.pem
45+
cp /path/to/your/private-key.pem /path/to/myspeed/data/certs/key.pem
46+
```
47+
48+
## Generating a Self-Signed Certificate
49+
50+
For testing or internal use, you can generate a self-signed certificate:
51+
52+
```sh
53+
openssl req -x509 -newkey rsa:4096 \
54+
-keyout data/certs/key.pem \
55+
-out data/certs/cert.pem \
56+
-sha256 -days 365 -nodes \
57+
-subj "/C=US/ST=State/L=City/O=Organization/OU=Unit/CN=localhost"
58+
```
59+
60+
::: warning Self-Signed Certificates
61+
Self-signed certificates will show a security warning in browsers. This is normal for self-signed certificates.
62+
For production use, we recommend using certificates from a trusted certificate authority like Let's Encrypt.
63+
:::
64+
65+
## Using Let's Encrypt Certificates
66+
67+
If you're using Let's Encrypt with certbot, your certificates are typically stored in `/etc/letsencrypt/live/your-domain.com/`. You can either copy or symlink them:
68+
69+
```sh
70+
# Copy the certificates
71+
sudo cp /etc/letsencrypt/live/your-domain.com/fullchain.pem /path/to/myspeed/data/certs/cert.pem
72+
sudo cp /etc/letsencrypt/live/your-domain.com/privkey.pem /path/to/myspeed/data/certs/key.pem
73+
74+
# Make sure MySpeed can read them
75+
sudo chown $USER:$USER /path/to/myspeed/data/certs/*.pem
76+
```
77+
78+
::: tip Certificate Renewal
79+
Remember to update your certificates in the `data/certs` directory when they are renewed.
80+
You can automate this with a post-renewal hook in certbot.
81+
:::
82+
83+
## Verifying HTTPS is Working
84+
85+
After placing your certificates and starting MySpeed, you should see in the console:
86+
87+
```
88+
Server listening on port 5216
89+
HTTPS server listening on port 5217
90+
```
91+
92+
You can then access MySpeed via:
93+
- HTTP: `http://localhost:5216`
94+
- HTTPS: `https://localhost:5217`
95+
96+
## Docker Configuration
97+
98+
When using Docker, mount the certificates directory:
99+
100+
```yaml
101+
version: "3"
102+
services:
103+
myspeed:
104+
image: germannewsmaker/myspeed
105+
ports:
106+
- "5216:5216"
107+
- "5217:5217" # HTTPS port
108+
volumes:
109+
- /path/to/myspeed:/myspeed/data
110+
# Certificates will be in /path/to/myspeed/certs/
111+
environment:
112+
- HTTPS_PORT=5217 # Optional, 5217 is default
113+
```
114+

server/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import express from 'express';
22
import path from 'node:path';
33
import fs from 'node:fs';
4+
import https from 'node:https';
45
import { fileURLToPath } from 'node:url';
56
import * as timerTask from './tasks/timer.js';
67
import * as integrationTask from './tasks/integrations.js';
@@ -34,6 +35,13 @@ const app = express();
3435
app.disable('x-powered-by');
3536

3637
const port = process.env.SERVER_PORT || 5216;
38+
const httpsPort = process.env.HTTPS_PORT || 5217;
39+
40+
const certsDir = path.join(process.cwd(), 'data', 'certs');
41+
const certPath = path.join(certsDir, 'cert.pem');
42+
const keyPath = path.join(certsDir, 'key.pem');
43+
44+
const hasSSLCerts = () => fs.existsSync(certPath) && fs.existsSync(keyPath);
3745

3846
process.on('uncaughtException', err => errorHandler(err));
3947

@@ -87,6 +95,21 @@ const run = async () => {
8795
}
8896

8997
app.listen(port, () => console.log(`Server listening on port ${port}`));
98+
99+
if (hasSSLCerts()) {
100+
try {
101+
const sslOptions = {
102+
cert: fs.readFileSync(certPath),
103+
key: fs.readFileSync(keyPath)
104+
};
105+
106+
https.createServer(sslOptions, app).listen(httpsPort, () =>
107+
console.log(`HTTPS server listening on port ${httpsPort}`)
108+
);
109+
} catch (err) {
110+
console.error(`Failed to start HTTPS server: ${err.message}`);
111+
}
112+
}
90113
}
91114

92115
db.authenticate().then(() => {

server/util/createFolders.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'node:path';
33

44
const baseDir = process.cwd();
55

6-
const neededFolder = ["data", "bin", "data/logs", "data/servers"];
6+
const neededFolder = ["data", "bin", "data/logs", "data/servers", "data/certs"];
77

88
neededFolder.forEach(folder => {
99
const fullPath = path.join(baseDir, folder);

0 commit comments

Comments
 (0)