1
- const _ = require ( 'lodash' ) ;
2
- const fs = require ( 'fs' ) ;
3
- const logger = require ( '../logger' ) . nginx ;
4
- const utils = require ( '../lib/utils' ) ;
5
- const error = require ( '../lib/error' ) ;
6
- const { Liquid } = require ( 'liquidjs' ) ;
7
- const passthroughHostModel = require ( '../models/ssl_passthrough_host' ) ;
8
- const debug_mode = process . env . NODE_ENV !== 'production' || ! ! process . env . DEBUG ;
1
+ const _ = require ( 'lodash' ) ;
2
+ const fs = require ( 'fs' ) ;
3
+ const logger = require ( '../logger' ) . nginx ;
4
+ const utils = require ( '../lib/utils' ) ;
5
+ const error = require ( '../lib/error' ) ;
6
+ const { Liquid } = require ( 'liquidjs' ) ;
7
+ const passthroughHostModel = require ( '../models/ssl_passthrough_host' ) ;
8
+ const debug_mode = process . env . NODE_ENV !== 'production' || ! ! process . env . DEBUG ;
9
9
10
10
const internalNginx = {
11
11
@@ -24,7 +24,7 @@ const internalNginx = {
24
24
* @returns {Promise }
25
25
*/
26
26
configure : ( model , host_type , host ) => {
27
- let combined_meta = { } ;
27
+ let combined_meta = { } ;
28
28
const sslPassthroughEnabled = internalNginx . sslPassthroughEnabled ( ) ;
29
29
30
30
return internalNginx . test ( )
@@ -34,7 +34,7 @@ const internalNginx = {
34
34
return internalNginx . deleteConfig ( host_type , host ) ; // Don't throw errors, as the file may not exist at all
35
35
} )
36
36
. then ( ( ) => {
37
- if ( host_type === 'ssl_passthrough_host' && ! sslPassthroughEnabled ) {
37
+ if ( host_type === 'ssl_passthrough_host' && ! sslPassthroughEnabled ) {
38
38
// ssl passthrough is disabled
39
39
const meta = {
40
40
nginx_online : false ,
@@ -64,7 +64,7 @@ const internalNginx = {
64
64
nginx_err : null
65
65
} ) ;
66
66
67
- if ( host_type === 'ssl_passthrough_host' ) {
67
+ if ( host_type === 'ssl_passthrough_host' ) {
68
68
// If passthrough is disabled we have already marked the hosts as offline
69
69
if ( sslPassthroughEnabled ) {
70
70
return passthroughHostModel
@@ -109,7 +109,7 @@ const internalNginx = {
109
109
nginx_err : valid_lines . join ( '\n' )
110
110
} ) ;
111
111
112
- if ( host_type === 'ssl_passthrough_host' ) {
112
+ if ( host_type === 'ssl_passthrough_host' ) {
113
113
return passthroughHostModel
114
114
. query ( )
115
115
. where ( 'is_deleted' , 0 )
@@ -235,7 +235,7 @@ const internalNginx = {
235
235
* @param {Object } host
236
236
* @returns {Promise }
237
237
*/
238
- generateConfig : ( host_type , host ) => {
238
+ generateConfig : async ( host_type , host ) => {
239
239
host_type = host_type . replace ( new RegExp ( '-' , 'g' ) , '_' ) ;
240
240
241
241
if ( debug_mode ) {
@@ -248,90 +248,87 @@ const internalNginx = {
248
248
root : __dirname + '/../templates/'
249
249
} ) ;
250
250
251
- return new Promise ( async ( resolve , reject ) => {
252
- let template = null ;
253
- let filename = internalNginx . getConfigName ( host_type , host . id ) ;
254
-
255
- try {
256
- template = fs . readFileSync ( __dirname + '/../templates/' + host_type + '.conf' , { encoding : 'utf8' } ) ;
257
- } catch ( err ) {
258
- reject ( new error . ConfigurationError ( err . message ) ) ;
259
- return ;
260
- }
251
+ let template = null ;
252
+ let filename = internalNginx . getConfigName ( host_type , host . id ) ;
261
253
262
- let locationsPromise ;
263
- let origLocations ;
254
+ try {
255
+ template = fs . readFileSync ( __dirname + '/../templates/' + host_type + '.conf' , { encoding : 'utf8' } ) ;
256
+ } catch ( err ) {
257
+ throw new error . ConfigurationError ( err . message ) ;
258
+ }
264
259
265
- // Manipulate the data a bit before sending it to the template
266
- if ( host_type === 'ssl_passthrough_host' ) {
267
- if ( internalNginx . sslPassthroughEnabled ( ) ) {
268
- const allHosts = await passthroughHostModel
269
- . query ( )
270
- . where ( 'is_deleted' , 0 )
271
- . groupBy ( 'id' )
272
- . omit ( [ 'is_deleted' ] ) ;
273
- host = {
274
- all_passthrough_hosts : allHosts . map ( ( host ) => {
275
- // Replace dots in ___domain
276
- host . forwarding_host = internalNginx . addIpv6Brackets ( host . forwarding_host ) ;
277
- return host ;
278
- } ) ,
279
- }
280
- } else {
281
- internalNginx . deleteConfig ( host_type , host , false )
282
- }
283
-
284
- } else if ( host_type !== 'default' ) {
285
- host . use_default_location = true ;
286
- if ( typeof host . advanced_config !== 'undefined' && host . advanced_config ) {
287
- host . use_default_location = ! internalNginx . advancedConfigHasDefaultLocation ( host . advanced_config ) ;
288
- }
260
+ let locationsPromise ;
261
+ let origLocations ;
262
+
263
+ // Manipulate the data a bit before sending it to the template
264
+ if ( host_type === 'ssl_passthrough_host' ) {
265
+ if ( internalNginx . sslPassthroughEnabled ( ) ) {
266
+ const allHosts = await passthroughHostModel
267
+ . query ( )
268
+ . where ( 'is_deleted' , 0 )
269
+ . groupBy ( 'id' )
270
+ . omit ( [ 'is_deleted' ] ) ;
271
+ host = {
272
+ all_passthrough_hosts : allHosts . map ( ( host ) => {
273
+ // Replace dots in ___domain
274
+ host . forwarding_host = internalNginx . addIpv6Brackets ( host . forwarding_host ) ;
275
+ return host ;
276
+ } ) ,
277
+ } ;
278
+ } else {
279
+ internalNginx . deleteConfig ( host_type , host , false ) ;
289
280
}
281
+
282
+ } else if ( host_type !== 'default' ) {
283
+ host . use_default_location = true ;
284
+ if ( typeof host . advanced_config !== 'undefined' && host . advanced_config ) {
285
+ host . use_default_location = ! internalNginx . advancedConfigHasDefaultLocation ( host . advanced_config ) ;
286
+ }
287
+ }
290
288
291
- if ( host . locations ) {
292
- //logger.info ('host.locations = ' + JSON.stringify(host.locations, null, 2));
293
- origLocations = [ ] . concat ( host . locations ) ;
294
- locationsPromise = internalNginx . renderLocations ( host ) . then ( ( renderedLocations ) => {
295
- host . locations = renderedLocations ;
296
- } ) ;
289
+ if ( host . locations ) {
290
+ //logger.info ('host.locations = ' + JSON.stringify(host.locations, null, 2));
291
+ origLocations = [ ] . concat ( host . locations ) ;
292
+ locationsPromise = internalNginx . renderLocations ( host ) . then ( ( renderedLocations ) => {
293
+ host . locations = renderedLocations ;
294
+ } ) ;
297
295
298
- // Allow someone who is using / custom ___location path to use it, and skip the default / ___location
299
- _ . map ( host . locations , ( ___location ) => {
300
- if ( ___location . path === '/' ) {
301
- host . use_default_location = false ;
302
- }
303
- } ) ;
296
+ // Allow someone who is using / custom ___location path to use it, and skip the default / ___location
297
+ _ . map ( host . locations , ( ___location ) => {
298
+ if ( ___location . path === '/' ) {
299
+ host . use_default_location = false ;
300
+ }
301
+ } ) ;
304
302
305
- } else {
306
- locationsPromise = Promise . resolve ( ) ;
307
- }
303
+ } else {
304
+ locationsPromise = Promise . resolve ( ) ;
305
+ }
308
306
309
- // Set the IPv6 setting for the host
310
- host . ipv6 = internalNginx . ipv6Enabled ( ) ;
307
+ // Set the IPv6 setting for the host
308
+ host . ipv6 = internalNginx . ipv6Enabled ( ) ;
311
309
312
- locationsPromise . then ( ( ) => {
313
- renderEngine
314
- . parseAndRender ( template , host )
315
- . then ( ( config_text ) => {
316
- fs . writeFileSync ( filename , config_text , { encoding : 'utf8' } ) ;
310
+ return locationsPromise . then ( ( ) => {
311
+ renderEngine
312
+ . parseAndRender ( template , host )
313
+ . then ( ( config_text ) => {
314
+ fs . writeFileSync ( filename , config_text , { encoding : 'utf8' } ) ;
317
315
318
- if ( debug_mode ) {
319
- logger . success ( 'Wrote config:' , filename , config_text ) ;
320
- }
316
+ if ( debug_mode ) {
317
+ logger . success ( 'Wrote config:' , filename , config_text ) ;
318
+ }
321
319
322
- // Restore locations array
323
- host . locations = origLocations ;
320
+ // Restore locations array
321
+ host . locations = origLocations ;
324
322
325
- resolve ( true ) ;
326
- } )
327
- . catch ( ( err ) => {
328
- if ( debug_mode ) {
329
- logger . warn ( 'Could not write ' + filename + ':' , err . message ) ;
330
- }
323
+ return true ;
324
+ } )
325
+ . catch ( ( err ) => {
326
+ if ( debug_mode ) {
327
+ logger . warn ( 'Could not write ' + filename + ':' , err . message ) ;
328
+ }
331
329
332
- reject ( new error . ConfigurationError ( err . message ) ) ;
333
- } ) ;
334
- } ) ;
330
+ throw new error . ConfigurationError ( err . message ) ;
331
+ } ) ;
335
332
} ) ;
336
333
} ,
337
334
@@ -514,12 +511,12 @@ const internalNginx = {
514
511
* Helper function to add brackets to an IP if it is IPv6
515
512
* @returns {string }
516
513
*/
517
- addIpv6Brackets : function ( ip ) {
514
+ addIpv6Brackets : function ( ip ) {
518
515
// Only run check if ipv6 is enabled
519
516
if ( internalNginx . ipv6Enabled ( ) ) {
520
517
const ipv6Regex = / ^ ( ( [ 0 - 9 a - f A - F ] { 1 , 4 } : ) { 7 , 7 } [ 0 - 9 a - f A - F ] { 1 , 4 } | ( [ 0 - 9 a - f A - F ] { 1 , 4 } : ) { 1 , 7 } : | ( [ 0 - 9 a - f A - F ] { 1 , 4 } : ) { 1 , 6 } : [ 0 - 9 a - f A - F ] { 1 , 4 } | ( [ 0 - 9 a - f A - F ] { 1 , 4 } : ) { 1 , 5 } ( : [ 0 - 9 a - f A - F ] { 1 , 4 } ) { 1 , 2 } | ( [ 0 - 9 a - f A - F ] { 1 , 4 } : ) { 1 , 4 } ( : [ 0 - 9 a - f A - F ] { 1 , 4 } ) { 1 , 3 } | ( [ 0 - 9 a - f A - F ] { 1 , 4 } : ) { 1 , 3 } ( : [ 0 - 9 a - f A - F ] { 1 , 4 } ) { 1 , 4 } | ( [ 0 - 9 a - f A - F ] { 1 , 4 } : ) { 1 , 2 } ( : [ 0 - 9 a - f A - F ] { 1 , 4 } ) { 1 , 5 } | [ 0 - 9 a - f A - F ] { 1 , 4 } : ( ( : [ 0 - 9 a - f A - F ] { 1 , 4 } ) { 1 , 6 } ) | : ( ( : [ 0 - 9 a - f A - F ] { 1 , 4 } ) { 1 , 7 } | : ) | f e 8 0 : ( : [ 0 - 9 a - f A - F ] { 0 , 4 } ) { 0 , 4 } % [ 0 - 9 a - z A - Z ] { 1 , } | : : ( f f f f ( : 0 { 1 , 4 } ) { 0 , 1 } : ) { 0 , 1 } ( ( 2 5 [ 0 - 5 ] | ( 2 [ 0 - 4 ] | 1 { 0 , 1 } [ 0 - 9 ] ) { 0 , 1 } [ 0 - 9 ] ) \. ) { 3 , 3 } ( 2 5 [ 0 - 5 ] | ( 2 [ 0 - 4 ] | 1 { 0 , 1 } [ 0 - 9 ] ) { 0 , 1 } [ 0 - 9 ] ) | ( [ 0 - 9 a - f A - F ] { 1 , 4 } : ) { 1 , 4 } : ( ( 2 5 [ 0 - 5 ] | ( 2 [ 0 - 4 ] | 1 { 0 , 1 } [ 0 - 9 ] ) { 0 , 1 } [ 0 - 9 ] ) \. ) { 3 , 3 } ( 2 5 [ 0 - 5 ] | ( 2 [ 0 - 4 ] | 1 { 0 , 1 } [ 0 - 9 ] ) { 0 , 1 } [ 0 - 9 ] ) ) $ / gi;
521
- if ( ipv6Regex . test ( ip ) ) {
522
- return `[${ ip } ]`
518
+ if ( ipv6Regex . test ( ip ) ) {
519
+ return `[${ ip } ]` ;
523
520
}
524
521
}
525
522
return ip ;
0 commit comments