🔨 Add option to accept self signed certificates

This commit is contained in:
SebClem 2020-10-12 13:51:57 +02:00
parent 4129f0cbf5
commit 7af0263b6c
3 changed files with 46 additions and 11 deletions

View File

@ -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 => {

View File

@ -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");

View File

@ -15,7 +15,7 @@
</div>
</div>
<div class="row" style="margin-bottom: 10px;">
<div class="col s12" style="margin-bottom: 10px;">
<div class="col m4 s12 " style="margin-bottom: 10px;">
<div style="color: #9e9e9e; display: inline;">SSL</div>
<div class="switch" style="display: inline;">
<label>
@ -23,7 +23,15 @@
<span class="lever"></span>
</label>
</div>
</div>
<div class="col m8 s12 hide" style="margin-bottom: 10px;">
<div style="color: #9e9e9e; display: inline;">Accept Self-signed certificate</div>
<div class="switch" style="display: inline;">
<label>
<input id="self_signed" type="checkbox">
<span class="lever"></span>
</label>
</div>
</div>
</div>
<div class="row">
@ -34,11 +42,11 @@
</div>
</div>
<div class="row">
<div class="input-field col s6">
<div class="input-field col m6 s12">
<input id="username" type="text" class="white-text">
<label for="username">Username</label>
</div>
<div class="input-field col s6">
<div class="input-field col m6 s12">
<input id="password" type="password" class="white-text">
<label for="password">Password</label>
<span class="helper-text">!!! Use App Password !!! See <a target="_blank"