Skip to content

Commit

Permalink
feat: allow to force disable SSL (#2562)
Browse files Browse the repository at this point in the history
* feat: allow to force disable SSL

* fix: remove unused import
  • Loading branch information
robertsLando committed Aug 2, 2022
1 parent e003e10 commit 44d86a1
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 30 deletions.
78 changes: 51 additions & 27 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ const apisLimiter = rateLimit({
},
})

function sslDisabled() {
return process.env.FORCE_DISABLE_SSL === 'true'
}

// apis response codes
enum RESPONSE_CODES {
OK = 'OK',
Expand Down Expand Up @@ -177,17 +181,32 @@ export async function startServer(host: string, port: number | string) {
const httpsEnabled = process.env.HTTPS || settings?.gateway?.https

if (httpsEnabled) {
logger.info('HTTPS is enabled. Loading cert and keys')
const { cert, key } = await loadCertKey()
server = createHttpsServer(
{
key,
cert,
rejectUnauthorized: false,
},
app
)
} else {
if (!sslDisabled()) {
logger.info('HTTPS is enabled. Loading cert and keys')
const { cert, key } = await loadCertKey()

if (cert && key) {
server = createHttpsServer(
{
key,
cert,
rejectUnauthorized: false,
},
app
)
} else {
logger.warn(
'HTTPS is enabled but cert or key cannot be generated. Falling back to HTTP'
)
}
} else {
logger.warn(
'HTTPS enabled but FORCE_DISABLE_SSL env var is set. Falling back to HTTP'
)
}
}

if (!server) {
server = createHttpServer(app)
}

Expand Down Expand Up @@ -282,23 +301,27 @@ async function loadCertKey(): Promise<{
'Cert and key not found in store, generating fresh new ones...'
)

const result = await createCertificate({
days: 99999,
selfSigned: true,
})

key = result.serviceKey
cert = result.certificate
try {
const result = await createCertificate({
days: 99999,
selfSigned: true,
})

await fs.writeFile(
utils.joinPath(storeDir, 'key.pem'),
result.serviceKey
)
await fs.writeFile(
utils.joinPath(storeDir, 'cert.pem'),
result.certificate
)
logger.info('New cert and key created')
key = result.serviceKey
cert = result.certificate

await fs.writeFile(
utils.joinPath(storeDir, 'key.pem'),
result.serviceKey
)
await fs.writeFile(
utils.joinPath(storeDir, 'cert.pem'),
result.certificate
)
logger.info('New cert and key created')
} catch (error) {
logger.error('Error creating cert and key for HTTPS', error)
}
}

return { cert, key }
Expand Down Expand Up @@ -905,6 +928,7 @@ app.get(
devices: gw?.zwave?.devices ?? {},
serial_ports: [],
scales: scales,
sslDisabled: sslDisabled(),
}

if (process.platform !== 'sunos') {
Expand Down
1 change: 1 addition & 0 deletions docs/guide/env-vars.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This is the list of the supported environment variables:
- `Z2M_LOG_MAXFILES`: The maximum number of files to keep in the log directory, if you add `d` suffix this will set the number of days to keep logs. Default is `7d`
- `Z2M_LOG_MAXSIZE`: The maximum size of a single log file. Default is `50m` (50MB)
- `NO_LOG_COLORS`: Set this env var to `'true'` to disable application log colors also in the console.
- `FORCE_DISABLE_SSL`: Set this env var to `'true'` to disable SSL.

These variables can be used when running the webpack dev server with HMR (most users will not need them):

Expand Down
2 changes: 1 addition & 1 deletion docs/usage/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ To configure ZWavejs2Mqtt, you must access it via your web browser at <http://lo
- **Auth**: Enable this to password protect your application. Default credentials are:
- Username:`admin`
- Password: `zwave`
- **HTTPS**: Enable this to serve the UI over HTTPS (Requires app reload).
- **HTTPS**: Enable this to serve the UI over HTTPS (Requires app reload). **Requires openssl to be installed on the machine.**
- **Plugins**: List of plugins to use. If the plugin you want to use is not listed just write the name or the path to it and press enter. More about plugins [here](/guide/plugins)
- **Log enabled**: Enable logging for Zwavejs2Mqtt
- **Log level**: Set the log level (Error, Warn, Info, Verbose, Debug, Silly)
Expand Down
1 change: 0 additions & 1 deletion lib/ZwaveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ import {
ZWaveNodeEvents,
SerialAPISetupCommand,
FirmwareUpdateFileInfo,
FirmwareUpdateInfo,
} from 'zwave-js'
import { getEnumMemberName, parseQRCodeString } from 'zwave-js/Utils'
import { nvmBackupsDir, storeDir } from '../config/app'
Expand Down
9 changes: 8 additions & 1 deletion src/components/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@
v-model="newGateway.authEnabled"
></v-switch>
</v-col>
<v-col cols="12" sm="6" md="4">
<v-col
v-if="!sslDisabled"
cols="12"
sm="6"
md="4"
>
<v-switch
hint="Enable this to serve page using HTTPS. REQUIRES APP RELOAD"
persistent-hint
Expand Down Expand Up @@ -1274,6 +1279,7 @@ export default {
return {
valid_zwave: true,
dialogValue: false,
sslDisabled: false,
newGateway: {},
newMqtt: {},
newZwave: {},
Expand Down Expand Up @@ -1563,6 +1569,7 @@ export default {
console.log(data)
} else {
this.$store.dispatch('init', data)
this.sslDisabled = data.sslDisabled
this.resetConfig()
}
} catch (error) {
Expand Down

0 comments on commit 44d86a1

Please sign in to comment.