Skip to content

Commit b19c9c9

Browse files
committed
server-ssl.js
improve how the local certificates are generated remove start-windows.bat remove --autoRestart flag temporarily remove --skipNodeUpdate flag remove nodeUpdate /root │ ├── /error │ ├── 404.html │ └── 500.html │ ├── /ssl │ ├── /openssl │ ├── /production │ │ │ │ │ ├── ... │ │ ├── private-key.pem │ │ └── certificate.pem │ │ │ ├── /staging │ │ │ │ │ ├── ... │ │ ├── private-key.pem │ │ └── certificate.pem │ │ │ └── state.js │ ├── /wwwroot │ └── index.html <---- Your website goes here │ └── server-ssl.js
1 parent 6c768b6 commit b19c9c9

File tree

5 files changed

+35
-200
lines changed

5 files changed

+35
-200
lines changed

README.md

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,25 @@ The easiest usage would be to serve a website:
1515
1. [`Clone`](https://github.com/FirstTimeEZ/server-ssl.git) or [`Download`](https://github.com/FirstTimeEZ/server-ssl/archive/refs/heads/main.zip) the main branch and extract it somewhere
1616
2. Put your website in the `website` folder
1717
3. Open the `server-ssl` folder in `Command Prompt` or `Git Bash`
18-
4. Run `start-windows.bat`
18+
4. Run `node server-ssl.js`
1919
5. View your website at `https://localhost`
2020

21-
[![](https://i.imgur.com/w2UUgO4.gif)](https://github.com/FirstTimeEZ/server-ssl/archive/refs/heads/main.zip)
21+
[![](https://i.imgur.com/VQGGVp0.gif)](https://github.com/FirstTimeEZ/server-ssl/archive/refs/heads/main.zip)
2222

2323
The default page/config is a simple [`API`](https://github.com/FirstTimeEZ/simple-api-router) that serves and displays the `time`
2424

2525
[![](https://i.imgur.com/DEbJVUq.png)](https://github.com/FirstTimeEZ/server-ssl/archive/refs/heads/main.zip)
2626

2727
### Advanced/Production Usage
2828

29-
`start-windows.bat` takes your arguments and starts the server
29+
`node server-ssl.js` takes your arguments and starts the server
3030

3131
```
3232
# Start for production (Lets Encrypt!) with SAN Extension
33-
./start-windows.bat --letsEncrypt --domains=['www.ssl.boats','ssl.boats']
34-
35-
# Start for production (BYO)
36-
./start-windows.bat --cert="your-certificate.pem" --pk="your-private-key.pem"
33+
node server-ssl.js --letsEncrypt --domains=['www.ssl.boats','ssl.boats']
3734
```
3835

39-
[![](https://i.imgur.com/2bMobPI.gif)](https://github.com/FirstTimeEZ/server-ssl/archive/refs/heads/main.zip)
36+
[![](https://i.imgur.com/XP8pa8a.gif)](https://github.com/FirstTimeEZ/server-ssl/archive/refs/heads/main.zip)
4037

4138
### Optional Arguments
4239

@@ -51,7 +48,6 @@ The default page/config is a simple [`API`](https://github.com/FirstTimeEZ/simpl
5148
| `--site=` | The directory for the website files | `"wwwroot"` |
5249
| `--error=` | The directory for error messages (404,500) | `"error"` |
5350
| `--entry=` | The page to use for the entry point | `"index.html"` |
54-
| `--skipNodeUpdate` | Do not update `NPM` packages before starting the server | `not present` |
5551

5652
All Arguments are case sensitive.
5753

@@ -69,12 +65,9 @@ The certificates will be changed automatically when they are updated, you don't
6965
| `--domains=` | Domains to generate certificates for, this can not include wild cards, this should be an array. eg. `--domains=['www.ssl.boats','ssl.boats']` |
7066
| `--generateAnyway` | Certificates should always be generated when the server starts, this could get you rate limited, maybe use `--staging` |
7167
| `--staging` | The `Lets Encrypt!` staging server should be used instead of production |
72-
| `--autoRestart` | Restart the server after cert generation, firing a callback to notify of the restart (not required) |
73-
74-
`start-windows.bat` is required to use `--autoRestart`
7568

7669
```
77-
./start-windows.bat --letsEncrypt --domains=['www.ssl.boats','ssl.boats']
70+
node server-ssl.js --letsEncrypt --domains=['www.ssl.boats','ssl.boats']
7871
```
7972
--------
8073

@@ -132,8 +125,7 @@ This layout keeps the project organized and maintainable, separating error handl
132125
├── /wwwroot
133126
│ └── index.html <---- Your website goes here
134127
135-
├── server-ssl.js <--- server config
136-
└── start-windows.bat
128+
└── server-ssl.js
137129
```
138130

139131
--------

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "server-ssl",
33
"author": "FirstTimeEZ",
4-
"version": "35.0.2",
4+
"version": "40.0.1",
55
"description": "Configurable SSL Server that runs on Node.js which can be used for development or production and can create and renew Lets Encrypt Certificates automatically using ACME",
66
"main": "template; do not import; read the Getting Started of the README, maybe you want to use: lets-encrypt-acme-client",
77
"type": "module",

ssl/state.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
import { join, extname as _extname } from 'path';
1919
import { createServer as createServerHTTP } from 'http';
20-
import { readFile, existsSync, readFileSync } from 'fs';
20+
import { readFile, existsSync, readFileSync, mkdirSync } from 'fs';
2121
import { checkChallengesMixin, startLetsEncryptDaemon } from 'lets-encrypt-acme-client';
2222
import { fetchAndRetryUntilOk } from 'base-acme-client';
23+
import { execSync } from 'child_process';
24+
import { platform } from 'os';
2325

2426
/**
2527
* **SSL-Server** configuration state
@@ -97,12 +99,9 @@ export const STATE = {
9799
arg.includes("--domains=") && (STATE.optDomains = rightSide);
98100
arg.includes("--letsEncrypt") && (STATE.optLetsEncrypt = true);
99101
arg.includes("--generateAnyway") && (STATE.optGenerateAnyway = true);
100-
arg.includes("--autoRestart") && (STATE.optAutoRestart = true);
101102
arg.includes("--staging") && (STATE.optStaging = true);
102103
// Internal
103104
arg.includes("--notAfter=") && (STATE.expireDate = rightSide);
104-
arg.includes("--arAvailable") && (STATE.isRestartAvailable = true);
105-
arg.includes("--ok") && (STATE.override = true);
106105
});
107106

108107
if (STATE.optLetsEncrypt === true) {
@@ -123,8 +122,26 @@ export const STATE = {
123122
STATE.expireDate && STATE.timeUntilRenew(STATE.expireDate);
124123

125124
const SSL = join(__rootDir, STATE.SSL, STATE.optStaging ? "staging" : "production");
126-
const PK = join(SSL, STATE.optPk);
127-
const CERT = join(SSL, STATE.optCert);
125+
126+
let PK = join(SSL, STATE.optPk);
127+
let CERT = join(SSL, STATE.optCert);
128+
129+
if (!existsSync(PK) || !existsSync(CERT)) {
130+
let create_local_cert;
131+
132+
if (platform() === 'win32') {
133+
create_local_cert = '"ssl/openssl/bin/openssl" req -x509 -newkey rsa:2048 -nodes -sha256 -keyout ' + SSL + '/private-key.pem -out ' + SSL + '/certificate.pem -days 365 -subj "/CN=localhost"';
134+
}
135+
else {
136+
// todo: detect if openssl is installed on linux
137+
create_local_cert = 'openssl req -x509 -newkey rsa:2048 -nodes -sha256 -keyout ' + SSL + '/private-key.pem -out ' + SSL + '/certificate.pem -days 365 -subj "/CN=localhost"';
138+
}
139+
140+
!existsSync(SSL) && mkdirSync(SSL);
141+
execSync(create_local_cert, { stdio: 'inherit' });
142+
PK = join(SSL, "private-key.pem");
143+
CERT = join(SSL, "certificate.pem");
144+
}
128145

129146
!existsSync(PK) && STATE.certNotExist();
130147
!existsSync(CERT) && STATE.certNotExist();
@@ -153,7 +170,8 @@ export const STATE = {
153170
console.log("You need to generate or provide an SSL Certificate and Private Key in PEM format");
154171
console.log("You can use the following command from git bash or run start-windows.bat with no arguments");
155172
console.log(" ");
156-
console.log('openssl req -x509 -newkey rsa:2048 -nodes -sha256 -keyout ssl/private-key.pem -out ssl/certificate.pem -days 365 -subj "//CN=localhost"');
173+
console.log('openssl req -x509 -newkey rsa:2048 -nodes -sha256 -keyout ssl/production/private-key.pem -out ssl/production/certificate.pem -days 365 -subj "/CN=localhost"');
174+
console.log('openssl req -x509 -newkey rsa:2048 -nodes -sha256 -keyout ssl/staging/private-key.pem -out ssl/staging/certificate.pem -days 365 -subj "/CN=localhost"');
157175
process.exit(1);
158176
},
159177
getErrorPage: (res, err) => {

start-windows.bat

Lines changed: 0 additions & 175 deletions
This file was deleted.

0 commit comments

Comments
 (0)