Skip to content

Commit 04636b7

Browse files
committed
add feature: set default server
1 parent 1353937 commit 04636b7

File tree

16 files changed

+428
-302
lines changed

16 files changed

+428
-302
lines changed

backend/internal/host.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,32 @@ const internalHost = {
228228
}
229229

230230
return response;
231-
}
231+
},
232232

233+
/**
234+
* Internal use only, checks to see if the there is another default server record
235+
*
236+
* @param {String} hostname
237+
* @param {String} [ignore_type] 'proxy', 'redirection', 'dead'
238+
* @param {Integer} [ignore_id] Must be supplied if type was also supplied
239+
* @returns {Promise}
240+
*/
241+
checkDefaultServerNotExist: function (hostname) {
242+
let promises = proxyHostModel
243+
.query()
244+
.where('default_server', true)
245+
.andWhere('domain_names', 'not like', '%' + hostname + '%');
246+
247+
248+
return Promise.resolve(promises)
249+
.then((promises_results) => {
250+
if (promises_results.length > 0){
251+
return false;
252+
}
253+
return true;
254+
});
255+
256+
}
233257
};
234258

235259
module.exports = internalHost;

backend/internal/nginx.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ const internalNginx = {
185185
// Prevent modifying the original object:
186186
let host = JSON.parse(JSON.stringify(host_row));
187187
const nice_host_type = internalNginx.getFileFriendlyHostType(host_type);
188-
188+
189189
if (config.debug()) {
190190
logger.info('Generating ' + nice_host_type + ' Config:', JSON.stringify(host, null, 2));
191191
}

backend/internal/proxy-host.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ const internalProxyHost = {
4343
});
4444
});
4545
})
46+
.then(() => {
47+
// Get a list of the domain names and check each of them against default records
48+
if (data.default_server){
49+
if (data.domain_names.length > 1) {
50+
throw new error.ValidationError('Default server cant be set for multiple domain!');
51+
}
52+
53+
return internalHost
54+
.checkDefaultServerNotExist(data.domain_names[0])
55+
.then((result) => {
56+
if (!result){
57+
throw new error.ValidationError('One default server already exists');
58+
}
59+
});
60+
}
61+
})
4662
.then(() => {
4763
// At this point the domains should have been checked
4864
data.owner_user_id = access.token.getUserId(1);
@@ -140,6 +156,22 @@ const internalProxyHost = {
140156
});
141157
}
142158
})
159+
.then(() => {
160+
// Get a list of the domain names and check each of them against default records
161+
if (data.default_server){
162+
if (data.domain_names.length > 1) {
163+
throw new error.ValidationError('Default server cant be set for multiple domain!');
164+
}
165+
166+
return internalHost
167+
.checkDefaultServerNotExist(data.domain_names[0])
168+
.then((result) => {
169+
if (!result){
170+
throw new error.ValidationError('One default server already exists');
171+
}
172+
});
173+
}
174+
})
143175
.then(() => {
144176
return internalProxyHost.get(access, {id: data.id});
145177
})
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const migrate_name = 'identifier_for_migrate';
2+
const logger = require('../logger').migrate;
3+
4+
/**
5+
* Migrate Up
6+
*
7+
* @param {Object} knex
8+
* @param {Promise} Promise
9+
* @returns {Promise}
10+
*/
11+
exports.up = function (knex) {
12+
logger.info(`[${migrate_name}] Migrating Up...`);
13+
14+
// Add default_server column to proxy_host table
15+
return knex.schema.table('proxy_host', (table) => {
16+
table.boolean('default_server').notNullable().defaultTo(false);
17+
})
18+
.then(() => {
19+
logger.info(`[${migrate_name}] Column 'default_server' added to 'proxy_host' table`);
20+
});
21+
};
22+
23+
/**
24+
* Migrate Down
25+
*
26+
* @param {Object} knex
27+
* @param {Promise} Promise
28+
* @returns {Promise}
29+
*/
30+
exports.down = function (knex) {
31+
logger.info(`[${migrate_name}] Migrating Down...`);
32+
33+
// Remove default_server column from proxy_host table
34+
return knex.schema.table('proxy_host', (table) => {
35+
table.dropColumn('default_server');
36+
})
37+
.then(() => {
38+
logger.info(`[${migrate_name}] Column 'default_server' removed from 'proxy_host' table`);
39+
});
40+
};

backend/models/proxy_host.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const boolFields = [
2121
'enabled',
2222
'hsts_enabled',
2323
'hsts_subdomains',
24+
'default_server',
2425
];
2526

2627
class ProxyHost extends Model {

backend/schema/components/certificate-object.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
"enum": ["ecdsa", "rsa"],
4747
"description": "Type of SSL key (either ecdsa or rsa)"
4848
},
49+
"default_server": {
50+
"type": "boolean",
51+
"description": "Defines if the server is the default for unmatched requests"
52+
},
4953
"meta": {
5054
"type": "object",
5155
"additionalProperties": false,

backend/schema/components/proxy-host-object.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"hsts_enabled",
2525
"hsts_subdomains",
2626
"ssl_key_type",
27+
"default_server",
2728
"certificate"
2829
],
2930
"additionalProperties": false,
@@ -155,6 +156,10 @@
155156
"type": "string",
156157
"enum": ["ecdsa", "rsa"],
157158
"description": "Type of SSL key (either ecdsa or rsa)"
159+
},
160+
"default_server": {
161+
"type": "boolean",
162+
"description": "Defines if the server is the default for unmatched requests"
158163
}
159164
}
160165
}

backend/schema/paths/nginx/proxy-hosts/hostID/put.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@
8282
},
8383
"ssl_key_type": {
8484
"$ref": "../../../../components/proxy-host-object.json#/properties/ssl_key_type"
85+
},
86+
"default_server": {
87+
"$ref": "../../../../components/proxy-host-object.json#/properties/default_server"
8588
}
8689
}
8790
}

backend/schema/paths/nginx/proxy-hosts/post.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@
7070
},
7171
"ssl_key_type": {
7272
"$ref": "../../../components/proxy-host-object.json#/properties/ssl_key_type"
73+
},
74+
"default_server": {
75+
"$ref": "../../../components/proxy-host-object.json#/properties/default_server"
7376
}
7477
}
7578
}

backend/templates/_listen.conf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
listen 80;
1+
listen 80{% if default_server == true %} default_server{% endif %};
22
{% if ipv6 -%}
3-
listen [::]:80;
3+
listen [::]:80{% if default_server == true %} default_server{% endif %};
44
{% else -%}
55
#listen [::]:80;
66
{% endif %}
77
{% if certificate -%}
8-
listen 443 ssl;
8+
listen 443 ssl{% if default_server == true %} default_server{% endif %};
99
{% if ipv6 -%}
10-
listen [::]:443 ssl;
10+
listen [::]:443 ssl{% if default_server == true %} default_server{% endif %};
1111
{% else -%}
1212
#listen [::]:443;
1313
{% endif %}

0 commit comments

Comments
 (0)