Skip to content

Commit ba288bc

Browse files
committed
feat: v1.2.1
1 parent b5918d5 commit ba288bc

6 files changed

Lines changed: 112 additions & 17 deletions

File tree

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2021 Jovan Jovanovic
1+
Copyright 2022 Jovan Jovanovic
22

33
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:
44

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ cd /etc/reverseproxy
5252
node dist/migrate/addMetadata.js
5353
```
5454

55+
## Rebuilding nginx configurations
56+
57+
```sh
58+
node /etc/reverseproxy
59+
node dist/migrate/rebuild.js
60+
nginx -t && nginx -s reload
61+
```
62+
5563
# How To Use
5664

5765
## Authorization
@@ -149,6 +157,13 @@ Responses
149157
}
150158
```
151159

160+
```json
161+
{
162+
"error": "Nginx configuration failed",
163+
"statusCode": 500
164+
}
165+
```
166+
152167
### DELETE /proxies/:domain
153168

154169
Delete the proxy with the domain.
@@ -245,6 +260,13 @@ Responses
245260
}
246261
```
247262

263+
```json
264+
{
265+
"error": "Nginx configuration failed",
266+
"statusCode": 500
267+
}
268+
```
269+
248270
### DELETE /streams/:name
249271

250272
Delete the stream with the name.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "reverseproxy",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "Reverse proxy API with nginx backend",
55
"main": "dist/index.js",
66
"scripts": {

src/index.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,25 @@ app.post("/proxies", async (req, res) => {
7979
});
8080
}
8181

82+
if (!(await Nginx.test())) {
83+
Logger.error(
84+
`Could not reload nginx, deleting proxy: ${Proxy.resolveURL(domain, ssl)}`
85+
);
86+
await Proxy.delete(domain);
87+
88+
return res.status(500).json({
89+
error: "Nginx configuration failed",
90+
statusCode: 500,
91+
});
92+
}
93+
8294
Logger.info(`Proxy created: ${Proxy.resolveURL(domain, ssl)} -> ${target}`);
8395
res.json({
8496
message: "Proxy created",
8597
statusCode: 200,
8698
});
8799

88-
if (await Nginx.test()) {
89-
return await Nginx.reload();
90-
} else {
91-
Logger.info(
92-
`Could not reload nginx, deleting proxy: ${Proxy.resolveURL(domain, ssl)}`
93-
);
94-
return await Proxy.delete(domain);
95-
}
100+
return await Nginx.reload();
96101
});
97102

98103
app.delete("/proxies/:domain", async (req, res) => {
@@ -174,18 +179,23 @@ app.post("/streams", async (req, res) => {
174179
});
175180
}
176181

182+
if (!(await Nginx.test())) {
183+
Logger.error(`Could not reload nginx, deleting stream: ${name}`);
184+
await Stream.delete(name);
185+
186+
return res.status(500).json({
187+
error: "Nginx configuration failed",
188+
statusCode: 500,
189+
});
190+
}
191+
177192
Logger.info(`Stream created: ${name} - ${listen} -> ${target}`);
178193
res.json({
179194
message: "Stream created",
180195
statusCode: 200,
181196
});
182197

183-
if (await Nginx.test()) {
184-
return await Nginx.reload();
185-
} else {
186-
Logger.info(`Could not reload nginx, deleting stream: ${name}`);
187-
return await Stream.delete(name);
188-
}
198+
return await Nginx.reload();
189199
});
190200

191201
app.delete("/streams/:name", async (req, res) => {

src/migrate/rebuild.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import config from "../config";
2+
import fs from "fs";
3+
import path from "path";
4+
import Templates from "../templates";
5+
6+
async function proxies() {
7+
await Templates.load();
8+
9+
const files = await fs.promises
10+
.readdir(config.nginxSitesEnabled)
11+
.then((files) => files.filter((file) => file.startsWith("proxy-")));
12+
13+
for (const file of files) {
14+
const filePath = path.resolve(config.nginxSitesEnabled, file);
15+
const content = await fs.promises.readFile(filePath, "utf-8");
16+
17+
const existingMetadata = await Templates.getMetadata(filePath);
18+
if (!existingMetadata) {
19+
console.log(`${file} has no metadata`);
20+
continue;
21+
}
22+
23+
const templateName = content.includes("listen 443")
24+
? "proxy-ssl.conf"
25+
: "proxy.conf";
26+
27+
const template = Templates.build(templateName, existingMetadata);
28+
29+
await fs.promises.writeFile(filePath, template);
30+
console.log(`${file} updated`);
31+
}
32+
}
33+
34+
async function streams() {
35+
await Templates.load();
36+
37+
const files = await fs.promises
38+
.readdir(config.nginxStreams)
39+
.then((files) => files.filter((file) => file.startsWith("stream-")));
40+
41+
for (const file of files) {
42+
const filePath = path.resolve(config.nginxStreams, file);
43+
44+
const existingMetadata = await Templates.getMetadata(filePath);
45+
if (!existingMetadata) {
46+
console.log(`${file} has no metadata`);
47+
continue;
48+
}
49+
50+
const template = Templates.build("stream.conf", existingMetadata);
51+
52+
await fs.promises.writeFile(filePath, template);
53+
console.log(`${file} updated`);
54+
}
55+
}
56+
57+
proxies();
58+
streams();

templates/proxy-ssl.conf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
server {
22
listen 80;
33
server_name <domain>;
4-
4+
55
# ACME-challenge
66
location ^~ /.well-known/acme-challenge/ {
77
root <letsencryptDir>;
@@ -39,6 +39,11 @@ server {
3939
add_header Permissions-Policy "interest-cohort=()" always;
4040
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
4141

42+
# ACME-challenge
43+
location ^~ /.well-known/acme-challenge/ {
44+
root <letsencryptDir>;
45+
}
46+
4247
location / {
4348
proxy_pass <target>;
4449
proxy_http_version 1.1;

0 commit comments

Comments
 (0)