#98 proxy support for sync setup

This commit is contained in:
azivner 2018-07-25 08:30:41 +02:00
parent 0ece9bd1be
commit c8253caae9
7 changed files with 40 additions and 25 deletions

View File

@ -16,7 +16,8 @@ function SetupModel() {
this.password1 = ko.observable();
this.password2 = ko.observable();
this.serverAddress = ko.observable();
this.syncServerHost = ko.observable();
this.syncProxy = ko.observable();
this.instanceType = utils.isElectron() ? "desktop" : "server";
@ -68,11 +69,12 @@ function SetupModel() {
});
}
else if (this.setupSyncFromServer()) {
const serverAddress = this.serverAddress();
const syncServerHost = this.syncServerHost();
const syncProxy = this.syncProxy();
const username = this.username();
const password = this.password1();
if (!serverAddress) {
if (!syncServerHost) {
showAlert("Trilium server address can't be empty");
return;
}
@ -89,7 +91,8 @@ function SetupModel() {
// not using server.js because it loads too many dependencies
const resp = await $.post('/api/setup/sync-from-server', {
serverAddress: serverAddress,
syncServerHost: syncServerHost,
syncProxy: syncProxy,
username: username,
password: password
});

View File

@ -10,9 +10,9 @@ async function setupNewDocument(req) {
}
async function setupSyncFromServer(req) {
const { serverAddress, username, password } = req.body;
const { syncServerHost, syncProxy, username, password } = req.body;
return await setupService.setupSyncFromSyncServer(serverAddress, username, password);
return await setupService.setupSyncFromSyncServer(syncServerHost, syncProxy, username, password);
}
async function setupSyncFromClient(req) {

View File

@ -115,19 +115,23 @@ async function getDocument() {
async function syncToServer() {
log.info("Initiating sync to server");
// FIXME: add proxy support
const syncServerHost = await optionService.getOption('syncServerHost');
const syncProxy = await optionService.getOption('syncProxy');
const payload = {
options: await getDocumentOptions()
};
await rp({
const rpOpts = {
uri: syncServerHost + '/api/setup/sync-from-client',
method: 'POST',
json: true,
body: payload
});
body: {
options: await getDocumentOptions()
}
};
if (syncProxy) {
rpOpts.proxy = syncProxy;
}
await rp(rpOpts);
// this is completely new sync, need to reset counters. If this would not be new sync,
// the previous request would have failed.

View File

@ -29,7 +29,7 @@ async function initSyncedOptions(username, password) {
await passwordEncryptionService.setDataKey(password, utils.randomSecureToken(16));
}
async function initNotSyncedOptions(initialized, startNotePath = '', syncServerHost = '') {
async function initNotSyncedOptions(initialized, startNotePath = '', syncServerHost = '', syncProxy = '') {
await optionService.createOption('startNotePath', startNotePath, false);
await optionService.createOption('lastBackupDate', dateUtils.nowDate(), false);
await optionService.createOption('dbVersion', appInfo.dbVersion, false);
@ -42,7 +42,7 @@ async function initNotSyncedOptions(initialized, startNotePath = '', syncServerH
await optionService.createOption('syncServerHost', syncServerHost, false);
await optionService.createOption('syncServerTimeout', 5000, false);
await optionService.createOption('syncProxy', '', false);
await optionService.createOption('syncProxy', syncProxy, false);
await optionService.createOption('initialized', initialized ? 'true' : 'false', false);
}

View File

@ -14,7 +14,7 @@ function triggerSync() {
});
}
async function setupSyncFromSyncServer(serverAddress, username, password) {
async function setupSyncFromSyncServer(syncServerHost, syncProxy, username, password) {
if (await sqlInit.isDbInitialized()) {
return {
result: 'failure',
@ -27,7 +27,7 @@ async function setupSyncFromSyncServer(serverAddress, username, password) {
// response is expected to contain documentId and documentSecret options
const options = await rp.get({
uri: serverAddress + '/api/sync/document',
uri: syncServerHost + '/api/sync/document',
auth: {
'user': username,
'pass': password
@ -35,7 +35,11 @@ async function setupSyncFromSyncServer(serverAddress, username, password) {
json: true
});
await sqlInit.createDatabaseForSync(options, serverAddress);
if (syncProxy) {
options.proxy = syncProxy;
}
await sqlInit.createDatabaseForSync(options, syncServerHost, syncProxy);
triggerSync();

View File

@ -99,7 +99,7 @@ async function createInitialDatabase(username, password) {
await initDbConnection();
}
async function createDatabaseForSync(options, syncServerHost = '') {
async function createDatabaseForSync(options, syncServerHost = '', syncProxy = '') {
log.info("Creating database for sync");
if (await isDbInitialized()) {
@ -111,7 +111,7 @@ async function createDatabaseForSync(options, syncServerHost = '') {
await sql.transactional(async () => {
await sql.executeScript(schema);
await require('./options_init').initNotSyncedOptions(false, '', syncServerHost);
await require('./options_init').initNotSyncedOptions(false, '', syncServerHost, syncProxy);
// document options required for sync to kick off
for (const opt of options) {

View File

@ -68,16 +68,20 @@
<p>Please enter Trilium server address and credentials below. This will download the whole Trilium document from server and setup sync to it. Depending on the document size and your connection speed, this may take a while.</p>
<div class="form-group">
<label for="username">Trilium server address</label>
<input type="text" class="form-control" data-bind="value: serverAddress" placeholder="https://<hostname>:<port>">
<label for="sync-server-host">Trilium server address</label>
<input type="text" id="syncServerHost" class="form-control" data-bind="value: syncServerHost" placeholder="https://<hostname>:<port>">
</div>
<div class="form-group">
<label for="sync-proxy">Proxy server (optional)</label>
<input type="text" id="sync-proxy" class="form-control" data-bind="value: syncProxy" placeholder="https://<hostname>:<port>">
</div>
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" data-bind="value: username" placeholder="Username">
<input type="text" id="username" class="form-control" data-bind="value: username" placeholder="Username">
</div>
<div class="form-group">
<label for="password1">Password</label>
<input type="password" class="form-control" data-bind="value: password1" placeholder="Password">
<input type="password" id="password1" class="form-control" data-bind="value: password1" placeholder="Password">
</div>
<button type="button" data-bind="click: back" class="btn btn-default">Back</button>