@@ -44,19 +44,31 @@ const internalHost = {
4444 */
4545 create : payload => {
4646 return new Promise ( ( resolve , reject ) => {
47- // Enforce lowercase hostnames
48- payload . hostname = payload . hostname . toLowerCase ( ) ;
47+ let existing_host = false ;
4948
50- // 1. Check that the hostname doesn't already exist
51- let existing_host = db . hosts . findOne ( { hostname : payload . hostname } ) ;
49+ if ( payload . type === 'stream' ) {
50+ // Check that the incoming port doesn't already exist
51+ existing_host = db . hosts . findOne ( { incoming_port : payload . incoming_port } ) ;
52+
53+ if ( payload . incoming_port === 80 || payload . incoming_port === 81 || payload . incoming_port === 443 ) {
54+ reject ( new error . ConfigurationError ( 'Port ' + payload . incoming_port + ' is reserved' ) ) ;
55+ return ;
56+ }
57+
58+ } else {
59+ payload . hostname = payload . hostname . toLowerCase ( ) ;
60+
61+ // Check that the hostname doesn't already exist
62+ existing_host = db . hosts . findOne ( { hostname : payload . hostname } ) ;
63+ }
5264
5365 if ( existing_host ) {
5466 reject ( new error . ValidationError ( 'Hostname already exists' ) ) ;
5567 } else {
56- // 2. Add host to db
68+ // Add host to db
5769 let host = db . hosts . save ( payload ) ;
5870
59- // 3. Fire the config generation for this host
71+ // Fire the config generation for this host
6072 internalHost . configure ( host , true )
6173 . then ( ( /*result*/ ) => {
6274 resolve ( host ) ;
@@ -98,10 +110,16 @@ const internalHost = {
98110 }
99111
100112 // Check that the hostname doesn't already exist
101- let other_host = db . hosts . findOne ( { hostname : payload . hostname } ) ;
113+ let other_host = false ;
114+
115+ if ( typeof payload . incoming_port !== 'undefined' ) {
116+ other_host = db . hosts . findOne ( { incoming_port : payload . incoming_port } ) ;
117+ } else {
118+ other_host = db . hosts . findOne ( { hostname : payload . hostname } ) ;
119+ }
102120
103121 if ( other_host && other_host . _id !== id ) {
104- reject ( new error . ValidationError ( ' Hostname already exists') ) ;
122+ reject ( new error . ValidationError ( ( other_host . type === 'stream' ? 'Source Stream Port' : ' Hostname' ) + ' already exists') ) ;
105123 } else {
106124 // 2. Update host
107125 db . hosts . update ( { _id : id } , payload , { multi : false , upsert : false } ) ;
@@ -126,17 +144,22 @@ const internalHost = {
126144 return data ;
127145 } )
128146 . then ( data => {
129- if (
130- ( data . original . ssl && ! data . updated . ssl ) || // ssl was enabled and is now disabled
131- ( data . original . ssl && data . original . hostname !== data . updated . hostname ) // hostname was changed for a previously ssl-enabled host
132- ) {
133- // SSL was turned off or hostname for ssl has changed so we should remove certs for the original
134- return internalSsl . deleteCerts ( data . original )
135- . then ( ( ) => {
136- db . hosts . update ( { _id : data . updated . _id } , { ssl_expires : 0 } , { multi : false , upsert : false } ) ;
137- data . updated . ssl_expires = 0 ;
138- return data ;
139- } ) ;
147+ if ( data . updated . type !== 'stream' ) {
148+ if (
149+ ( data . original . ssl && ! data . updated . ssl ) || // ssl was enabled and is now disabled
150+ ( data . original . ssl && data . original . hostname !== data . updated . hostname ) // hostname was changed for a previously ssl-enabled host
151+ ) {
152+ // SSL was turned off or hostname for ssl has changed so we should remove certs for the original
153+ return internalSsl . deleteCerts ( data . original )
154+ . then ( ( ) => {
155+ db . hosts . update ( { _id : data . updated . _id } , { ssl_expires : 0 } , {
156+ multi : false ,
157+ upsert : false
158+ } ) ;
159+ data . updated . ssl_expires = 0 ;
160+ return data ;
161+ } ) ;
162+ }
140163 }
141164
142165 return data ;
0 commit comments