@@ -5,111 +5,145 @@ const logger = require('./logger').setup;
5
5
const userModel = require ( './models/user' ) ;
6
6
const userPermissionModel = require ( './models/user_permission' ) ;
7
7
const authModel = require ( './models/auth' ) ;
8
+ const settingModel = require ( './models/setting' ) ;
8
9
const debug_mode = process . env . NODE_ENV !== 'production' || ! ! process . env . DEBUG ;
9
10
10
- module . exports = function ( ) {
11
- return new Promise ( ( resolve , reject ) => {
12
- // Now go and check if the jwt gpg keys have been created and if not, create them
13
- if ( ! config . has ( 'jwt' ) || ! config . has ( 'jwt.key' ) || ! config . has ( 'jwt.pub' ) ) {
14
- logger . info ( 'Creating a new JWT key pair...' ) ;
11
+ function setupJwt ( resolve , reject ) {
12
+ // Now go and check if the jwt gpg keys have been created and if not, create them
13
+ if ( ! config . has ( 'jwt' ) || ! config . has ( 'jwt.key' ) || ! config . has ( 'jwt.pub' ) ) {
14
+ logger . info ( 'Creating a new JWT key pair...' ) ;
15
15
16
- // jwt keys are not configured properly
17
- const filename = config . util . getEnv ( 'NODE_CONFIG_DIR' ) + '/' + ( config . util . getEnv ( 'NODE_ENV' ) || 'default' ) + '.json' ;
18
- let config_data = { } ;
16
+ // jwt keys are not configured properly
17
+ const filename = config . util . getEnv ( 'NODE_CONFIG_DIR' ) + '/' + ( config . util . getEnv ( 'NODE_ENV' ) || 'default' ) + '.json' ;
18
+ let config_data = { } ;
19
19
20
- try {
21
- config_data = require ( filename ) ;
22
- } catch ( err ) {
23
- // do nothing
24
- if ( debug_mode ) {
25
- logger . debug ( filename + ' config file could not be required' ) ;
26
- }
20
+ try {
21
+ config_data = require ( filename ) ;
22
+ } catch ( err ) {
23
+ // do nothing
24
+ if ( debug_mode ) {
25
+ logger . debug ( filename + ' config file could not be required' ) ;
27
26
}
27
+ }
28
28
29
- // Now create the keys and save them in the config.
30
- let key = new NodeRSA ( { b : 2048 } ) ;
31
- key . generateKeyPair ( ) ;
32
-
33
- config_data . jwt = {
34
- key : key . exportKey ( 'private' ) . toString ( ) ,
35
- pub : key . exportKey ( 'public' ) . toString ( )
36
- } ;
29
+ // Now create the keys and save them in the config.
30
+ let key = new NodeRSA ( { b : 2048 } ) ;
31
+ key . generateKeyPair ( ) ;
37
32
38
- // Write config
39
- fs . writeFile ( filename , JSON . stringify ( config_data , null , 2 ) , ( err ) => {
40
- if ( err ) {
41
- logger . error ( 'Could not write JWT key pair to config file: ' + filename ) ;
42
- reject ( err ) ;
43
- } else {
44
- logger . info ( 'Wrote JWT key pair to config file: ' + filename ) ;
33
+ config_data . jwt = {
34
+ key : key . exportKey ( 'private' ) . toString ( ) ,
35
+ pub : key . exportKey ( 'public' ) . toString ( )
36
+ } ;
45
37
46
- logger . warn ( 'Restarting interface to apply new configuration' ) ;
47
- process . exit ( 0 ) ;
48
- }
49
- } ) ;
38
+ // Write config
39
+ fs . writeFile ( filename , JSON . stringify ( config_data , null , 2 ) , ( err ) => {
40
+ if ( err ) {
41
+ logger . error ( 'Could not write JWT key pair to config file: ' + filename ) ;
42
+ reject ( err ) ;
43
+ } else {
44
+ logger . info ( 'Wrote JWT key pair to config file: ' + filename ) ;
50
45
51
- } else {
52
- // JWT key pair exists
53
- if ( debug_mode ) {
54
- logger . debug ( 'JWT Keypair already exists' ) ;
46
+ logger . warn ( 'Restarting interface to apply new configuration' ) ;
47
+ process . exit ( 0 ) ;
55
48
}
49
+ } ) ;
56
50
57
- resolve ( ) ;
51
+ } else {
52
+ // JWT key pair exists
53
+ if ( debug_mode ) {
54
+ logger . debug ( 'JWT Keypair already exists' ) ;
58
55
}
59
- } )
60
- . then ( ( ) => {
56
+
57
+ resolve ( ) ;
58
+ }
59
+ }
60
+
61
+ function setupDefaultUser ( ) {
62
+ ( userModel
63
+ . query ( )
64
+ . select ( userModel . raw ( 'COUNT(`id`) as `count`' ) )
65
+ . where ( 'is_deleted' , 0 )
66
+ . first ( )
67
+ ) . then ( ( row ) => {
68
+ if ( ! row . count ) {
69
+ // Create a new user and set password
70
+ logger . info ( 'Creating a new user: [email protected] with password: changeme' ) ;
71
+
72
+ let data = {
73
+ is_deleted : 0 ,
74
+
75
+ name : 'Administrator' ,
76
+ nickname : 'Admin' ,
77
+ avatar : '' ,
78
+ roles : [ 'admin' ]
79
+ } ;
80
+
61
81
return userModel
62
82
. query ( )
63
- . select ( userModel . raw ( 'COUNT(`id`) as `count`' ) )
64
- . where ( 'is_deleted' , 0 )
65
- . first ( ) ;
66
- } )
67
- . then ( ( row ) => {
68
- if ( ! row . count ) {
69
- // Create a new user and set password
70
- logger . info ( 'Creating a new user: [email protected] with password: changeme' ) ;
71
-
72
- let data = {
73
- is_deleted : 0 ,
74
-
75
- name : 'Administrator' ,
76
- nickname : 'Admin' ,
77
- avatar : '' ,
78
- roles : [ 'admin' ]
79
- } ;
83
+ . insertAndFetch ( data )
84
+ . then ( ( user ) => {
85
+ return authModel
86
+ . query ( )
87
+ . insert ( {
88
+ user_id : user . id ,
89
+ type : 'password' ,
90
+ secret : 'changeme' ,
91
+ meta : { }
92
+ } )
93
+ . then ( ( ) => {
94
+ return userPermissionModel
95
+ . query ( )
96
+ . insert ( {
97
+ user_id : user . id ,
98
+ visibility : 'all' ,
99
+ proxy_hosts : 'manage' ,
100
+ redirection_hosts : 'manage' ,
101
+ dead_hosts : 'manage' ,
102
+ streams : 'manage' ,
103
+ access_lists : 'manage' ,
104
+ certificates : 'manage'
105
+ } ) ;
106
+ } ) ;
107
+ } )
108
+ . then ( ( ) => {
109
+ logger . info ( 'Initial admin setup completed' ) ;
110
+ } ) ;
111
+ } else if ( debug_mode ) {
112
+ logger . debug ( 'Admin user setup not required' ) ;
113
+ }
114
+ } ) ;
115
+ }
80
116
81
- return userModel
117
+ function setupDefaultSettings ( ) {
118
+ return settingModel
119
+ . query ( )
120
+ . select ( userModel . raw ( 'COUNT(`id`) as `count`' ) )
121
+ . first ( )
122
+ . then ( ( row ) => {
123
+ if ( ! row . count ) {
124
+ settingModel
82
125
. query ( )
83
- . insertAndFetch ( data )
84
- . then ( ( user ) => {
85
- return authModel
86
- . query ( )
87
- . insert ( {
88
- user_id : user . id ,
89
- type : 'password' ,
90
- secret : 'changeme' ,
91
- meta : { }
92
- } )
93
- . then ( ( ) => {
94
- return userPermissionModel
95
- . query ( )
96
- . insert ( {
97
- user_id : user . id ,
98
- visibility : 'all' ,
99
- proxy_hosts : 'manage' ,
100
- redirection_hosts : 'manage' ,
101
- dead_hosts : 'manage' ,
102
- streams : 'manage' ,
103
- access_lists : 'manage' ,
104
- certificates : 'manage'
105
- } ) ;
106
- } ) ;
107
- } )
108
- . then ( ( ) => {
109
- logger . info ( 'Initial setup completed' ) ;
126
+ . insert ( {
127
+ id : 'default-site' ,
128
+ name : 'Default Site' ,
129
+ description : 'What to show when Nginx is hit with an unknown Host' ,
130
+ value : 'congratulations' ,
131
+ meta : { }
132
+ } ) . then ( ( ) => {
133
+ logger . info ( 'Default settings added' ) ;
110
134
} ) ;
111
- } else if ( debug_mode ) {
112
- logger . debug ( 'Admin user setup not required' ) ;
135
+ } if ( debug_mode ) {
136
+ logger . debug ( 'Default setting setup not required' ) ;
113
137
}
114
138
} ) ;
139
+ }
140
+
141
+ module . exports = function ( ) {
142
+ return new Promise ( ( resolve , reject ) => {
143
+ return setupJwt ( resolve , reject ) ;
144
+ } ) . then ( ( ) => {
145
+ return setupDefaultUser ( ) ;
146
+ } ) . then ( ( ) => {
147
+ return setupDefaultSettings ( ) ;
148
+ } ) ;
115
149
} ;
0 commit comments