2024-02-28 15:45:30 +01:00
|
|
|
// Copyright (C) 2024, The Duplicati Team
|
|
|
|
|
// https://duplicati.com, hello@duplicati.com
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
|
|
using System;
|
2011-11-21 22:28:20 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace Duplicati.GUI.TrayIcon
|
2011-11-28 11:05:42 +00:00
|
|
|
{
|
2011-11-21 22:28:20 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// We keep the hosted instance here to allow the TrayIcon executable to load without requiring the Server.exe
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal class HostedInstanceKeeper : IDisposable
|
|
|
|
|
{
|
2018-05-23 21:18:01 -07:00
|
|
|
private readonly System.Threading.Thread m_runner;
|
2011-11-28 11:05:42 +00:00
|
|
|
private System.Exception m_runnerException = null;
|
2014-07-02 12:05:09 +02:00
|
|
|
public event Action InstanceShutdown;
|
2011-11-21 22:28:20 +00:00
|
|
|
|
|
|
|
|
public HostedInstanceKeeper(string[] args)
|
|
|
|
|
{
|
2014-07-17 23:36:04 +02:00
|
|
|
m_runner = new System.Threading.Thread((dummy_arg) => {
|
2013-12-07 15:24:57 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//When running the hosted instance we do not really care what port we are using,
|
|
|
|
|
// so we just throw a few out there and try them
|
2017-09-18 23:23:45 -06:00
|
|
|
if (args == null || !args.Any(x => x.Trim().StartsWith("--" + Duplicati.Server.WebServer.Server.OPTION_PORT + "=", StringComparison.OrdinalIgnoreCase)))
|
2014-06-27 13:23:05 +02:00
|
|
|
args = (args ?? new string[0]).Union(new string[] { "--" + Duplicati.Server.WebServer.Server.OPTION_PORT + "=8200,8300,8400,8500,8600,8700,8800,8900,8989" }).ToArray();
|
2013-12-07 15:24:57 +01:00
|
|
|
|
|
|
|
|
Duplicati.Server.Program.Main(args);
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
m_runnerException = ex;
|
|
|
|
|
Duplicati.Server.Program.ServerStartedEvent.Set();
|
2014-07-02 12:05:09 +02:00
|
|
|
} finally {
|
|
|
|
|
if (InstanceShutdown != null)
|
2017-02-28 12:32:11 +01:00
|
|
|
try { InstanceShutdown(); }
|
|
|
|
|
catch (Exception shutex)
|
|
|
|
|
{
|
|
|
|
|
if (m_runnerException != null)
|
|
|
|
|
m_runnerException = new AggregateException(m_runnerException, shutex);
|
|
|
|
|
else
|
|
|
|
|
m_runnerException = shutex;
|
|
|
|
|
}
|
2013-12-07 15:24:57 +01:00
|
|
|
}
|
2014-07-02 12:05:09 +02:00
|
|
|
|
2013-12-07 15:24:57 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
m_runner.Start();
|
2011-11-21 22:28:20 +00:00
|
|
|
|
2012-05-14 20:15:27 +00:00
|
|
|
if (!Duplicati.Server.Program.ServerStartedEvent.WaitOne(TimeSpan.FromSeconds(100), true))
|
2011-11-28 11:05:42 +00:00
|
|
|
{
|
|
|
|
|
if (m_runnerException != null)
|
|
|
|
|
throw m_runnerException;
|
|
|
|
|
else
|
2018-03-12 14:07:11 +01:00
|
|
|
throw new Duplicati.Library.Interface.UserInformationException("Hosted server startup timed out", "HostedStartupError");
|
2011-11-28 11:05:42 +00:00
|
|
|
}
|
2013-02-27 14:23:17 +00:00
|
|
|
|
|
|
|
|
if (m_runnerException != null)
|
|
|
|
|
throw m_runnerException;
|
2011-11-21 22:28:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2014-07-08 01:38:31 +02:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Duplicati.Server.Program.ApplicationExitEvent.Set();
|
|
|
|
|
if (!m_runner.Join(TimeSpan.FromSeconds(10)))
|
|
|
|
|
{
|
2021-04-10 13:41:51 -07:00
|
|
|
m_runner.Interrupt();
|
2014-07-08 01:38:31 +02:00
|
|
|
m_runner.Join(TimeSpan.FromSeconds(10));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
2012-06-06 12:58:27 +00:00
|
|
|
{
|
|
|
|
|
}
|
2011-11-21 22:28:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|