roster-server 1.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitattributes +2 -0
- package/README.md +128 -0
- package/demo/roster/server.js +9 -0
- package/demo/www/eample.com/index.js +28 -0
- package/index.js +219 -0
- package/package.json +34 -0
package/.gitattributes
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# πΎ RosterServer
|
|
2
|
+
|
|
3
|
+
**Because hosting multiple HTTPS sites has never been easier!**
|
|
4
|
+
|
|
5
|
+
Welcome to **RosterServer**, the ultimate domain host router with automatic HTTPS and virtual hosting. Why juggle multiple servers when you can have one server to rule them all? π
|
|
6
|
+
|
|
7
|
+
## β¨ Features
|
|
8
|
+
|
|
9
|
+
- **Automatic HTTPS** with Let's Encrypt via Greenlock.
|
|
10
|
+
- **Dynamic Site Loading**: Just drop your Node.js apps in the `www` folder.
|
|
11
|
+
- **Virtual Hosting**: Serve multiple domains from a single server.
|
|
12
|
+
- **Automatic Redirects**: Redirect `www` subdomains to the root domain.
|
|
13
|
+
- **Zero Configuration**: Well, almost zero. Just a tiny bit of setup.
|
|
14
|
+
|
|
15
|
+
## π¦ Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install roster-server
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## π οΈ Usage
|
|
22
|
+
|
|
23
|
+
### Directory Structure
|
|
24
|
+
|
|
25
|
+
Your project should look something like this:
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
/srv/
|
|
29
|
+
βββ roster/server.js
|
|
30
|
+
βββ www/
|
|
31
|
+
βββ example.com/
|
|
32
|
+
β βββ index.js
|
|
33
|
+
βββ anotherdomain.com/
|
|
34
|
+
βββ index.js
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Setting Up Your Server
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
// /srv/roster/server.js
|
|
41
|
+
const Roster = require('roster-express');
|
|
42
|
+
|
|
43
|
+
const options = {
|
|
44
|
+
maintainerEmail: 'admin@example.com',
|
|
45
|
+
wwwPath: '/srv/www', // Path to your 'www' directory (default: '../www')
|
|
46
|
+
staging: false // Set to true for Let's Encrypt staging environment
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const server = new Roster(options);
|
|
50
|
+
server.start();
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Your Site Handlers
|
|
54
|
+
|
|
55
|
+
Each domain should have its own folder under `www`, containing an `index.js` that exports a request handler function.
|
|
56
|
+
|
|
57
|
+
For example, `www/example.com/index.js`:
|
|
58
|
+
|
|
59
|
+
```javascript
|
|
60
|
+
module.exports = (req, res) => {
|
|
61
|
+
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
62
|
+
res.end('Hello from example.com!');
|
|
63
|
+
};
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Running the Server
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
# /srv/roster/server.js
|
|
70
|
+
node server.js
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
And that's it! Your server is now hosting multiple HTTPS-enabled sites. π
|
|
74
|
+
|
|
75
|
+
## π€― But Wait, There's More!
|
|
76
|
+
|
|
77
|
+
### Automatic SSL Certificate Management
|
|
78
|
+
|
|
79
|
+
RosterExpress uses [greenlock-express](https://www.npmjs.com/package/greenlock-express) to automatically obtain and renew SSL certificates from Let's Encrypt. No need to manually manage certificates ever again. Unless you enjoy that sort of thing. π§
|
|
80
|
+
|
|
81
|
+
### Redirects from `www`
|
|
82
|
+
|
|
83
|
+
All requests to `www.yourdomain.com` are automatically redirected to `yourdomain.com`. Because who needs the extra three characters? π
|
|
84
|
+
|
|
85
|
+
### Dynamic Site Loading
|
|
86
|
+
|
|
87
|
+
Add a new site? Just drop it into the `www` folder with an `index.js` file, and RosterExpress will handle the rest. No need to restart the server. Well, you might need to restart the server. But that's what `nodemon` is for, right? π
|
|
88
|
+
|
|
89
|
+
## βοΈ Configuration Options
|
|
90
|
+
|
|
91
|
+
When creating a new `RosterExpress` instance, you can pass the following options:
|
|
92
|
+
|
|
93
|
+
- `maintainerEmail` (string): Your email for Let's Encrypt notifications.
|
|
94
|
+
- `wwwPath` (string): Path to your `www` directory containing your sites.
|
|
95
|
+
- `greenlockConfigDir` (string): Directory for Greenlock configuration.
|
|
96
|
+
- `staging` (boolean): Set to `true` to use Let's Encrypt's staging environment (for testing).
|
|
97
|
+
|
|
98
|
+
## π§ A Touch of Magic
|
|
99
|
+
|
|
100
|
+
You might be thinking, "But setting up HTTPS and virtual hosts is supposed to be complicated and time-consuming!" Well, not anymore. With RosterExpress, you can get back to writing code that matters, like defending Earth from alien invaders! πΎπΎπΎ
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
## π€ Contributing
|
|
104
|
+
|
|
105
|
+
Feel free to submit issues or pull requests. Or don't. I'm not your boss. π
|
|
106
|
+
|
|
107
|
+
If you find any issues or have suggestions for improvement, please open an issue or submit a pull request on the [GitHub repository](https://github.com/clasen/RosterExpress).
|
|
108
|
+
|
|
109
|
+
## π Acknowledgments
|
|
110
|
+
|
|
111
|
+
- [Node.js](https://nodejs.org/) - JavaScript runtime
|
|
112
|
+
- [Greenlock](https://git.coolaj86.com/coolaj86/greenlock.js) - Fully-featured ACME client
|
|
113
|
+
|
|
114
|
+
## π License
|
|
115
|
+
|
|
116
|
+
The MIT License (MIT)
|
|
117
|
+
|
|
118
|
+
Copyright (c) Martin Clasen
|
|
119
|
+
|
|
120
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
121
|
+
|
|
122
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
123
|
+
|
|
124
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
Happy hosting! π
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module.exports = (req, res) => {
|
|
2
|
+
// Send a simple response
|
|
3
|
+
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
4
|
+
res.end(`
|
|
5
|
+
<!DOCTYPE html>
|
|
6
|
+
<html>
|
|
7
|
+
<head>
|
|
8
|
+
<title>Example Site</title>
|
|
9
|
+
<style>
|
|
10
|
+
body {
|
|
11
|
+
font-family: Arial, sans-serif;
|
|
12
|
+
max-width: 800px;
|
|
13
|
+
margin: 40px auto;
|
|
14
|
+
padding: 0 20px;
|
|
15
|
+
line-height: 1.6;
|
|
16
|
+
}
|
|
17
|
+
h1 { color: #333; }
|
|
18
|
+
</style>
|
|
19
|
+
</head>
|
|
20
|
+
<body>
|
|
21
|
+
<h1>Welcome to Example.com</h1>
|
|
22
|
+
<p>This is a sample page served by the Roster server.</p>
|
|
23
|
+
<p>Request received from: ${req.headers.host}</p>
|
|
24
|
+
<p>URL path: ${req.url}</p>
|
|
25
|
+
</body>
|
|
26
|
+
</html>
|
|
27
|
+
`);
|
|
28
|
+
};
|
package/index.js
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const Greenlock = require('greenlock-express');
|
|
4
|
+
|
|
5
|
+
class Roster {
|
|
6
|
+
constructor(options = {}) {
|
|
7
|
+
this.maintainerEmail = options.maintainerEmail || 'admin@example.com';
|
|
8
|
+
this.wwwPath = options.wwwPath || path.join(__dirname, '..', '..', '..', 'www');
|
|
9
|
+
this.greenlockConfigDir = options.greenlockConfigDir || path.join(__dirname, '..', '..', 'greenlock.d');
|
|
10
|
+
this.staging = options.staging || false; // Set to true for testing
|
|
11
|
+
this.domains = [];
|
|
12
|
+
this.sites = {};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Function to dynamically load domain applications
|
|
16
|
+
loadSites() {
|
|
17
|
+
fs.readdirSync(this.wwwPath, { withFileTypes: true })
|
|
18
|
+
.filter(dirent => dirent.isDirectory())
|
|
19
|
+
.forEach((dirent) => {
|
|
20
|
+
const domain = dirent.name;
|
|
21
|
+
const domainPath = path.join(this.wwwPath, domain);
|
|
22
|
+
|
|
23
|
+
// Check for different module file extensions
|
|
24
|
+
const possibleIndexFiles = ['index.js', 'index.mjs', 'index.cjs'];
|
|
25
|
+
let siteApp;
|
|
26
|
+
let loadedFile;
|
|
27
|
+
|
|
28
|
+
for (const indexFile of possibleIndexFiles) {
|
|
29
|
+
const indexPath = path.join(domainPath, indexFile);
|
|
30
|
+
if (fs.existsSync(indexPath)) {
|
|
31
|
+
siteApp = require(indexPath);
|
|
32
|
+
loadedFile = indexFile;
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (siteApp) {
|
|
38
|
+
// Add the main domain and 'www' subdomain by default
|
|
39
|
+
const domainEntries = [domain, `www.${domain}`];
|
|
40
|
+
this.domains.push(...domainEntries);
|
|
41
|
+
domainEntries.forEach(d => {
|
|
42
|
+
this.sites[d] = siteApp;
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
console.log(`β
Loaded site: ${domain} (using ${loadedFile})`);
|
|
46
|
+
} else {
|
|
47
|
+
console.warn(`β οΈ No index file (js/mjs/cjs) found in ${domainPath}`);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
generateConfigJson() {
|
|
53
|
+
const configDir = this.greenlockConfigDir;
|
|
54
|
+
const configPath = path.join(configDir, 'config.json');
|
|
55
|
+
|
|
56
|
+
// Create the directory if it does not exist
|
|
57
|
+
if (!fs.existsSync(configDir)) {
|
|
58
|
+
fs.mkdirSync(configDir, { recursive: true });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const sitesConfig = [];
|
|
62
|
+
const uniqueDomains = new Set();
|
|
63
|
+
|
|
64
|
+
this.domains.forEach(domain => {
|
|
65
|
+
const rootDomain = domain.replace(/^www\./, '');
|
|
66
|
+
uniqueDomains.add(rootDomain);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Read the existing config.json if it exists
|
|
70
|
+
let existingConfig = {};
|
|
71
|
+
if (fs.existsSync(configPath)) {
|
|
72
|
+
// Read the current content
|
|
73
|
+
const currentConfigContent = fs.readFileSync(configPath, 'utf8');
|
|
74
|
+
existingConfig = JSON.parse(currentConfigContent);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
uniqueDomains.forEach(domain => {
|
|
78
|
+
const altnames = [domain];
|
|
79
|
+
if ((domain.match(/\./g) || []).length < 2) {
|
|
80
|
+
altnames.push(`www.${domain}`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Find the existing site to preserve renewAt
|
|
84
|
+
let existingSite = null;
|
|
85
|
+
if (existingConfig.sites) {
|
|
86
|
+
existingSite = existingConfig.sites.find(site => site.subject === domain);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const siteConfig = {
|
|
90
|
+
subject: domain,
|
|
91
|
+
altnames: altnames
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// Preserve renewAt if it exists
|
|
95
|
+
if (existingSite && existingSite.renewAt) {
|
|
96
|
+
siteConfig.renewAt = existingSite.renewAt;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
sitesConfig.push(siteConfig);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const newConfig = {
|
|
103
|
+
defaults: {
|
|
104
|
+
store: {
|
|
105
|
+
module: "greenlock-store-fs",
|
|
106
|
+
basePath: this.greenlockConfigDir
|
|
107
|
+
},
|
|
108
|
+
challenges: {
|
|
109
|
+
"http-01": {
|
|
110
|
+
module: "acme-http-01-standalone"
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
renewOffset: "-45d",
|
|
114
|
+
renewStagger: "3d",
|
|
115
|
+
accountKeyType: "EC-P256",
|
|
116
|
+
serverKeyType: "RSA-2048",
|
|
117
|
+
subscriberEmail: this.maintainerEmail
|
|
118
|
+
},
|
|
119
|
+
sites: sitesConfig
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// Check if config.json already exists and compare
|
|
123
|
+
if (fs.existsSync(configPath)) {
|
|
124
|
+
// Read the current content
|
|
125
|
+
const currentConfigContent = fs.readFileSync(configPath, 'utf8');
|
|
126
|
+
const currentConfig = JSON.parse(currentConfigContent);
|
|
127
|
+
|
|
128
|
+
// Compare the entire configurations
|
|
129
|
+
const newConfigContent = JSON.stringify(newConfig, null, 2);
|
|
130
|
+
const currentConfigContentFormatted = JSON.stringify(currentConfig, null, 2);
|
|
131
|
+
|
|
132
|
+
if (newConfigContent === currentConfigContentFormatted) {
|
|
133
|
+
console.log('βΉοΈ Configuration has not changed. config.json will not be overwritten.');
|
|
134
|
+
return; // Exit the function without overwriting
|
|
135
|
+
} else {
|
|
136
|
+
console.log('π Configuration has changed. config.json will be updated.');
|
|
137
|
+
}
|
|
138
|
+
} else {
|
|
139
|
+
console.log('π config.json does not exist. A new one will be created.');
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Write the new config.json
|
|
143
|
+
fs.writeFileSync(configPath, JSON.stringify(newConfig, null, 2));
|
|
144
|
+
console.log(`π config.json generated at ${configPath}`);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
handleRequest(req, res) {
|
|
148
|
+
const host = req.headers.host || '';
|
|
149
|
+
|
|
150
|
+
// Handle www redirect
|
|
151
|
+
if (host.startsWith('www.')) {
|
|
152
|
+
const newHost = host.slice(4);
|
|
153
|
+
res.writeHead(301, { Location: `https://${newHost}${req.url}` });
|
|
154
|
+
res.end();
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Find and execute the appropriate site handler
|
|
159
|
+
const siteApp = this.sites[host];
|
|
160
|
+
if (siteApp) {
|
|
161
|
+
siteApp(req, res);
|
|
162
|
+
} else {
|
|
163
|
+
res.writeHead(404);
|
|
164
|
+
res.end('Site not found');
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
initGreenlock() {
|
|
169
|
+
Greenlock.init({
|
|
170
|
+
packageRoot: __dirname,
|
|
171
|
+
configDir: this.greenlockConfigDir,
|
|
172
|
+
maintainerEmail: this.maintainerEmail,
|
|
173
|
+
cluster: false,
|
|
174
|
+
staging: this.staging,
|
|
175
|
+
manager: { module: "@greenlock/manager" },
|
|
176
|
+
approveDomains: (opts, certs, cb) => {
|
|
177
|
+
// If certs is defined, we already have a certificate and are renewing it
|
|
178
|
+
if (certs) {
|
|
179
|
+
opts.domains = certs.altnames;
|
|
180
|
+
} else {
|
|
181
|
+
// If it's a new request, verify if the domain is in our list
|
|
182
|
+
if (this.domains.includes(opts.domain)) {
|
|
183
|
+
opts.email = this.maintainerEmail;
|
|
184
|
+
opts.agreeTos = true;
|
|
185
|
+
opts.domains = [opts.domain];
|
|
186
|
+
} else {
|
|
187
|
+
console.warn(`β οΈ Domain not approved: ${opts.domain}`);
|
|
188
|
+
return cb(new Error(`Domain not approved: ${opts.domain}`));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
cb(null, { options: opts, certs });
|
|
192
|
+
}
|
|
193
|
+
}).ready((glx) => {
|
|
194
|
+
// Setup HTTPS server
|
|
195
|
+
const httpsServer = glx.httpsServer(null, (req, res) => {
|
|
196
|
+
this.handleRequest(req, res);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
httpsServer.listen(443, "0.0.0.0", () => {
|
|
200
|
+
console.info("HTTPS Listening on", httpsServer.address());
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// Setup HTTP server for ACME challenges
|
|
204
|
+
const httpServer = glx.httpServer();
|
|
205
|
+
|
|
206
|
+
httpServer.listen(80, "0.0.0.0", () => {
|
|
207
|
+
console.info("HTTP Listening on", httpServer.address());
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
start() {
|
|
213
|
+
this.loadSites();
|
|
214
|
+
this.generateConfigJson();
|
|
215
|
+
this.initGreenlock();
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
module.exports = Roster;
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "roster-server",
|
|
3
|
+
"version": "1.1.8",
|
|
4
|
+
"description": "πΎ RosterServer - A domain host router to host multiple HTTPS.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/clasen/RosterExpress.git"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"greenlock",
|
|
15
|
+
"https",
|
|
16
|
+
"domain",
|
|
17
|
+
"multi-domain",
|
|
18
|
+
"letsencrypt",
|
|
19
|
+
"hosting",
|
|
20
|
+
"ssl",
|
|
21
|
+
"host",
|
|
22
|
+
"greenlock-express",
|
|
23
|
+
"clasen"
|
|
24
|
+
],
|
|
25
|
+
"author": "Martin Clasen",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/clasen/RosterExpress/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/clasen/RosterExpress#readme",
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"greenlock-express": "^4.0.3"
|
|
33
|
+
}
|
|
34
|
+
}
|