Windows下探测安装StarAgent通过

This commit is contained in:
大石头 2020-12-03 23:10:59 +08:00
parent 80b243946a
commit 7f3a9e5868
2 changed files with 28 additions and 32 deletions

View File

@ -69,21 +69,6 @@ namespace StarAgent
if (delay > 0) Thread.Sleep(delay * 1000);
// 如果要杀的是自己,则重启服务
var args = Environment.CommandLine.Split(" ");
if (processId == Process.GetCurrentProcess().Id && args.Contains("-s"))
{
try
{
Host.Restart(Service.ServiceName);
return;
}
catch (Exception ex)
{
XTrace.WriteException(ex);
}
}
try
{
if (!p.HasExited)

View File

@ -1,9 +1,10 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using NewLife;
using NewLife.Http;
using NewLife.Log;
@ -122,7 +123,16 @@ namespace Stardust
var p = Process.GetProcesses().FirstOrDefault(e => e.ProcessName == "StarAgent");
if (p != null)
{
target = Path.GetDirectoryName(p.MainWindowTitle);
try
{
target = Path.GetDirectoryName(p.MainModule.FileName);
}
catch
{
target = Path.GetDirectoryName(p.MainWindowTitle);
}
XTrace.WriteLine("发现进程StarAgentProcessId={0}target={1}", p.Id, target);
}
}
@ -163,26 +173,14 @@ namespace Stardust
var fileName = info?.FileName ?? target.CombinePath(Runtime.Linux ? "StarAgent" : "StarAgent.exe");
if (File.Exists(fileName) && Runtime.Linux) Process.Start("chmod", $"+x {fileName}");
// 让对方自己退出
if (info != null)
{
_client.Invoke<String>("KillAndStart", new
{
processId = p.Id,
fileName = info.FileName,
});
Thread.Sleep(1000);
return true;
}
// 重启目标
if (p != null)
{
try
{
if (!p.HasExited) p.Kill();
p.Kill();
}
catch (Win32Exception) { }
catch (Exception ex)
{
XTrace.WriteException(ex);
@ -193,7 +191,7 @@ namespace Stardust
{
if (info?.Arguments == "-s")
{
Process.Start(fileName, "-restart");
Process.Start(fileName, "-start");
}
else
{
@ -209,6 +207,19 @@ namespace Stardust
return true;
}
/// <summary>探测并安装星尘代理</summary>
/// <param name="url">zip包下载源</param>
/// <param name="version">版本号</param>
/// <param name="target">目标目录</param>
public static Task ProbeAsync(String url = null, String version = null, String target = null)
{
return Task.Run(() =>
{
var client = new LocalStarClient();
client.ProbeAndInstall(url, version, target);
});
}
#endregion
}
}