From 7af0263b6c75c6f5ccbf94b0637257dc8d5adc7e Mon Sep 17 00:00:00 2001 From: SebClem Date: Mon, 12 Oct 2020 13:51:57 +0200 Subject: [PATCH] :hammer: Add option to accept self signed certificates --- .../opt/nextcloud_backup/tools/webdavTools.js | 24 ++++++++++++++----- .../opt/nextcloud_backup/views/index.ejs | 17 ++++++++++++- .../views/modals/nextcloud-settings-modal.ejs | 16 +++++++++---- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/webdavTools.js b/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/webdavTools.js index b7de3d1..66c55a2 100644 --- a/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/webdavTools.js +++ b/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/webdavTools.js @@ -1,6 +1,7 @@ const { createClient } = require("webdav"); const fs = require("fs"); const moment = require('moment'); +const https = require('https') const statusTools = require('./status'); const endpoint = "/remote.php/webdav"; @@ -21,15 +22,16 @@ class WebdavTools { this.password = null; } - init(ssl, host, username, password) { + init(ssl, host, username, password, accept_selfsigned_cert) { return new Promise((resolve, reject) => { let status = statusTools.getStatus(); logger.info("Initilizing and checking webdav client..."); this.baseUrl = (ssl === "true" ? "https" : "http") + "://" + host + endpoint; this.username = username; this.password = password; + let agent_option = ssl === "true" ? { rejectUnauthorized: accept_selfsigned_cert === "false" } : {} try { - this.client = createClient(this.baseUrl, { username: username, password: password }); + this.client = createClient(this.baseUrl, { username: username, password: password }, new https.Agent(agent_option)); this.client.getDirectoryContents("/").then(() => { if (status.error_code == 3) { @@ -96,7 +98,9 @@ class WebdavTools { }); } - + /** + * Check if theh webdav config is valid, if yes, start init of webdav client + */ confIsValid() { return new Promise((resolve, reject) => { let status = statusTools.getStatus(); @@ -109,7 +113,12 @@ class WebdavTools { status.error_code = null; statusTools.setStatus(status); } - this.init(conf.ssl, conf.host, conf.username, conf.password).then(() => { + // Check if self_signed option exist + if( conf.self_signed == null || conf.self_signed == ''){ + conf.self_signed = "false"; + this.setConf(conf); + } + this.init(conf.ssl, conf.host, conf.username, conf.password, conf.self_signed).then(() => { resolve(); }).catch((err) => { reject(err); @@ -194,11 +203,14 @@ class WebdavTools { logger.info('Uploading snap...'); let tmpFile = `./temp/${id}.tar` let stream = fs.createReadStream(tmpFile); - + let conf = this.getConf() let options = { body: stream, username: this.username, - password: this.password + password: this.password, + } + if(conf.ssl === 'true'){ + options["https"] = { rejectUnauthorized: conf.self_signed === "false" } } got.stream.put(this.baseUrl + encodeURI(path), options).on('uploadProgress', e => { diff --git a/nextcloud_backup/rootfs/opt/nextcloud_backup/views/index.ejs b/nextcloud_backup/rootfs/opt/nextcloud_backup/views/index.ejs index 850997f..f7409f6 100644 --- a/nextcloud_backup/rootfs/opt/nextcloud_backup/views/index.ejs +++ b/nextcloud_backup/rootfs/opt/nextcloud_backup/views/index.ejs @@ -431,6 +431,15 @@ $('#backup-snap-keep-read').val($(this).val()); }); + $('#ssl').change(function(){ + let div = $('#self_signed').parent().parent().parent(); + + if($('#ssl').is(':checked')) + div.removeClass("hide") + else + div.addClass("hide"); + }) + } @@ -439,11 +448,12 @@ function sendNextcloudSettings() { loadingModal.open(); let ssl = $('#ssl').is(':checked') + let self_signed = $('#self_signed').is(':checked') let hostname = $('#hostname').val(); let username = $('#username').val(); let password = $('#password').val(); let back_dir = $('#back-dir').val(); - $.post('./api/nextcloud-settings', { ssl: ssl, host: hostname, username: username, password: password, back_dir: back_dir }) + $.post('./api/nextcloud-settings', { ssl: ssl, host: hostname, username: username, password: password, back_dir: back_dir, self_signed: self_signed }) .done((data) => { console.log('Saved'); $('#nextcloud_settings_message').parent().addClass("hide"); @@ -479,6 +489,11 @@ loadingModal.open(); $.get('./api/nextcloud-settings', (data) => { $('#ssl').prop("checked", data.ssl == "true"); + if(data.ssl == "true"){ + let div = $('#self_signed').parent().parent().parent(); + div.removeClass("hide"); + } + $('#self_signed').prop('checked', data.self_signed == "true") $('#hostname').val(data.host); $('#hostname + label').removeClass("active"); $('#hostname + label').addClass("active"); diff --git a/nextcloud_backup/rootfs/opt/nextcloud_backup/views/modals/nextcloud-settings-modal.ejs b/nextcloud_backup/rootfs/opt/nextcloud_backup/views/modals/nextcloud-settings-modal.ejs index d288160..2de3057 100644 --- a/nextcloud_backup/rootfs/opt/nextcloud_backup/views/modals/nextcloud-settings-modal.ejs +++ b/nextcloud_backup/rootfs/opt/nextcloud_backup/views/modals/nextcloud-settings-modal.ejs @@ -15,7 +15,7 @@
-
+
SSL
- +
+
+
Accept Self-signed certificate
+
+ +
@@ -34,11 +42,11 @@
-
+
-
+
!!! Use App Password !!! See