禁用网络安装卸载服务,安全需要
This commit is contained in:
parent
a9802fe01b
commit
3d6ded4f85
|
@ -1,8 +1,5 @@
|
|||
using System.Diagnostics;
|
||||
using NewLife;
|
||||
using NewLife.Agent;
|
||||
using NewLife;
|
||||
using NewLife.Log;
|
||||
using NewLife.Model;
|
||||
using NewLife.Net;
|
||||
using NewLife.Remoting;
|
||||
using NewLife.Serialization;
|
||||
|
@ -122,110 +119,110 @@ public class StarService : DisposeBase, IApi
|
|||
if (Session is INetSession ns && !ns.Remote.Address.IsLocal()) throw new InvalidOperationException("禁止非本机操作!");
|
||||
}
|
||||
|
||||
/// <summary>杀死并启动进程</summary>
|
||||
/// <param name="processId">进程</param>
|
||||
/// <param name="delay">延迟结束的秒数</param>
|
||||
/// <param name="fileName">文件名</param>
|
||||
/// <param name="arguments">参数</param>
|
||||
/// <param name="workingDirectory">工作目录</param>
|
||||
/// <returns></returns>
|
||||
[Api(nameof(KillAndStart))]
|
||||
public Object KillAndStart(Int32 processId, Int32 delay, String fileName, String arguments, String workingDirectory)
|
||||
{
|
||||
CheckLocal();
|
||||
///// <summary>杀死并启动进程</summary>
|
||||
///// <param name="processId">进程</param>
|
||||
///// <param name="delay">延迟结束的秒数</param>
|
||||
///// <param name="fileName">文件名</param>
|
||||
///// <param name="arguments">参数</param>
|
||||
///// <param name="workingDirectory">工作目录</param>
|
||||
///// <returns></returns>
|
||||
//[Api(nameof(KillAndStart))]
|
||||
//public Object KillAndStart(Int32 processId, Int32 delay, String fileName, String arguments, String workingDirectory)
|
||||
//{
|
||||
// CheckLocal();
|
||||
|
||||
var p = Process.GetProcessById(processId);
|
||||
if (p == null) throw new InvalidOperationException($"无效进程Id[{processId}]");
|
||||
// var p = Process.GetProcessById(processId);
|
||||
// if (p == null) throw new InvalidOperationException($"无效进程Id[{processId}]");
|
||||
|
||||
var name = p.ProcessName;
|
||||
var pid = 0;
|
||||
// var name = p.ProcessName;
|
||||
// var pid = 0;
|
||||
|
||||
ThreadPool.QueueUserWorkItem(s =>
|
||||
{
|
||||
WriteLog("杀死进程 {0}/{1},等待 {2}秒", processId, p.ProcessName, delay);
|
||||
// ThreadPool.QueueUserWorkItem(s =>
|
||||
// {
|
||||
// WriteLog("杀死进程 {0}/{1},等待 {2}秒", processId, p.ProcessName, delay);
|
||||
|
||||
if (delay > 0) Thread.Sleep(delay * 1000);
|
||||
// if (delay > 0) Thread.Sleep(delay * 1000);
|
||||
|
||||
try
|
||||
{
|
||||
if (!p.HasExited)
|
||||
{
|
||||
p.Kill();
|
||||
p.WaitForExit(5_000);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
XTrace.WriteException(ex);
|
||||
}
|
||||
// try
|
||||
// {
|
||||
// if (!p.HasExited)
|
||||
// {
|
||||
// p.Kill();
|
||||
// p.WaitForExit(5_000);
|
||||
// }
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// XTrace.WriteException(ex);
|
||||
// }
|
||||
|
||||
// 启动进程
|
||||
if (!fileName.IsNullOrEmpty())
|
||||
{
|
||||
WriteLog("启动进程:{0} {1} {2}", fileName, arguments, workingDirectory);
|
||||
// // 启动进程
|
||||
// if (!fileName.IsNullOrEmpty())
|
||||
// {
|
||||
// WriteLog("启动进程:{0} {1} {2}", fileName, arguments, workingDirectory);
|
||||
|
||||
var si = new ProcessStartInfo
|
||||
{
|
||||
FileName = fileName,
|
||||
Arguments = arguments,
|
||||
WorkingDirectory = workingDirectory,
|
||||
// var si = new ProcessStartInfo
|
||||
// {
|
||||
// FileName = fileName,
|
||||
// Arguments = arguments,
|
||||
// WorkingDirectory = workingDirectory,
|
||||
|
||||
// false时目前控制台合并到当前控制台,一起退出;
|
||||
// true时目标控制台独立窗口,不会一起退出;
|
||||
UseShellExecute = true,
|
||||
};
|
||||
// // false时目前控制台合并到当前控制台,一起退出;
|
||||
// // true时目标控制台独立窗口,不会一起退出;
|
||||
// UseShellExecute = true,
|
||||
// };
|
||||
|
||||
var p2 = Process.Start(si);
|
||||
pid = p2.Id;
|
||||
// var p2 = Process.Start(si);
|
||||
// pid = p2.Id;
|
||||
|
||||
WriteLog("应用[{0}]启动成功 PID={1}", p2.ProcessName, p2.Id);
|
||||
}
|
||||
});
|
||||
// WriteLog("应用[{0}]启动成功 PID={1}", p2.ProcessName, p2.Id);
|
||||
// }
|
||||
// });
|
||||
|
||||
return new { name, pid };
|
||||
}
|
||||
// return new { name, pid };
|
||||
//}
|
||||
|
||||
/// <summary>安装应用服务(星尘代理守护)</summary>
|
||||
/// <param name="service"></param>
|
||||
/// <returns></returns>
|
||||
[Api(nameof(Install))]
|
||||
public ProcessInfo Install(ServiceInfo service)
|
||||
{
|
||||
XTrace.WriteLine("Install<={0}", service.ToJson());
|
||||
///// <summary>安装应用服务(星尘代理守护)</summary>
|
||||
///// <param name="service"></param>
|
||||
///// <returns></returns>
|
||||
//[Api(nameof(Install))]
|
||||
//public ProcessInfo Install(ServiceInfo service)
|
||||
//{
|
||||
// XTrace.WriteLine("Install<={0}", service.ToJson());
|
||||
|
||||
CheckLocal();
|
||||
// CheckLocal();
|
||||
|
||||
var rs = Manager.Install(service);
|
||||
if (rs != null)
|
||||
{
|
||||
var set = Setting.Current;
|
||||
set.Services = Manager.Services;
|
||||
set.Save();
|
||||
}
|
||||
// var rs = Manager.Install(service);
|
||||
// if (rs != null)
|
||||
// {
|
||||
// var set = Setting.Current;
|
||||
// set.Services = Manager.Services;
|
||||
// set.Save();
|
||||
// }
|
||||
|
||||
return rs;
|
||||
}
|
||||
// return rs;
|
||||
//}
|
||||
|
||||
/// <summary>卸载应用服务</summary>
|
||||
/// <param name="serviceName"></param>
|
||||
/// <returns></returns>
|
||||
[Api(nameof(Uninstall))]
|
||||
public Boolean Uninstall(String serviceName)
|
||||
{
|
||||
XTrace.WriteLine("Uninstall<={0}", serviceName);
|
||||
///// <summary>卸载应用服务</summary>
|
||||
///// <param name="serviceName"></param>
|
||||
///// <returns></returns>
|
||||
//[Api(nameof(Uninstall))]
|
||||
//public Boolean Uninstall(String serviceName)
|
||||
//{
|
||||
// XTrace.WriteLine("Uninstall<={0}", serviceName);
|
||||
|
||||
CheckLocal();
|
||||
// CheckLocal();
|
||||
|
||||
var rs = Manager.Uninstall(serviceName, "ServiceUninstall");
|
||||
if (rs)
|
||||
{
|
||||
var set = Setting.Current;
|
||||
set.Services = Manager.Services;
|
||||
set.Save();
|
||||
}
|
||||
// var rs = Manager.Uninstall(serviceName, "ServiceUninstall");
|
||||
// if (rs)
|
||||
// {
|
||||
// var set = Setting.Current;
|
||||
// set.Services = Manager.Services;
|
||||
// set.Save();
|
||||
// }
|
||||
|
||||
return rs;
|
||||
}
|
||||
// return rs;
|
||||
//}
|
||||
#endregion
|
||||
|
||||
#region 日志
|
||||
|
|
|
@ -8,7 +8,6 @@ using NewLife.Log;
|
|||
using NewLife.Messaging;
|
||||
using NewLife.Net;
|
||||
using NewLife.Remoting;
|
||||
using Stardust.Managers;
|
||||
using Stardust.Models;
|
||||
|
||||
namespace Stardust;
|
||||
|
@ -343,47 +342,46 @@ public class LocalStarClient
|
|||
#endregion
|
||||
|
||||
#region 安装卸载应用服务
|
||||
/// <summary>安装应用服务(星尘代理守护)</summary>
|
||||
/// <param name="service"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<ProcessInfo> Install(ServiceInfo service)
|
||||
{
|
||||
Init();
|
||||
///// <summary>安装应用服务(星尘代理守护)</summary>
|
||||
///// <param name="service"></param>
|
||||
///// <returns></returns>
|
||||
//public async Task<ProcessInfo> Install(ServiceInfo service)
|
||||
//{
|
||||
// Init();
|
||||
|
||||
return await _client.InvokeAsync<ProcessInfo>("Install", service);
|
||||
}
|
||||
// return await _client.InvokeAsync<ProcessInfo>("Install", service);
|
||||
//}
|
||||
|
||||
/// <summary>安装应用服务(星尘代理守护)</summary>
|
||||
/// <param name="name">服务名,唯一标识</param>
|
||||
/// <param name="fileName">文件</param>
|
||||
/// <param name="arguments">参数</param>
|
||||
/// <param name="workingDirectory">工作目录</param>
|
||||
/// <returns></returns>
|
||||
public async Task<ProcessInfo> Install(String name, String fileName, String arguments = null, String workingDirectory = null)
|
||||
{
|
||||
Init();
|
||||
///// <summary>安装应用服务(星尘代理守护)</summary>
|
||||
///// <param name="name">服务名,唯一标识</param>
|
||||
///// <param name="fileName">文件</param>
|
||||
///// <param name="arguments">参数</param>
|
||||
///// <param name="workingDirectory">工作目录</param>
|
||||
///// <returns></returns>
|
||||
//public async Task<ProcessInfo> Install(String name, String fileName, String arguments = null, String workingDirectory = null)
|
||||
//{
|
||||
// Init();
|
||||
|
||||
return await _client.InvokeAsync<ProcessInfo>("Install", new ServiceInfo
|
||||
{
|
||||
Name = name,
|
||||
FileName = fileName,
|
||||
Arguments = arguments,
|
||||
WorkingDirectory = workingDirectory,
|
||||
Enable = true,
|
||||
//ReloadOnChange = true,
|
||||
});
|
||||
}
|
||||
// return await _client.InvokeAsync<ProcessInfo>("Install", new ServiceInfo
|
||||
// {
|
||||
// Name = name,
|
||||
// FileName = fileName,
|
||||
// Arguments = arguments,
|
||||
// WorkingDirectory = workingDirectory,
|
||||
// Enable = true,
|
||||
// //ReloadOnChange = true,
|
||||
// });
|
||||
//}
|
||||
|
||||
/// <summary>卸载应用服务</summary>
|
||||
/// <param name="serviceName"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Boolean> Uninstall(String serviceName)
|
||||
{
|
||||
Init();
|
||||
|
||||
return await _client.InvokeAsync<Boolean>("Uninstall", serviceName);
|
||||
}
|
||||
///// <summary>卸载应用服务</summary>
|
||||
///// <param name="serviceName"></param>
|
||||
///// <returns></returns>
|
||||
//public async Task<Boolean> Uninstall(String serviceName)
|
||||
//{
|
||||
// Init();
|
||||
|
||||
// return await _client.InvokeAsync<Boolean>("Uninstall", serviceName);
|
||||
//}
|
||||
#endregion
|
||||
|
||||
#region 搜索
|
||||
|
|
|
@ -139,24 +139,24 @@ namespace Test
|
|||
{
|
||||
XTrace.WriteLine("Test6");
|
||||
|
||||
var client = new LocalStarClient();
|
||||
//client.GetInfo();
|
||||
//var client = new LocalStarClient();
|
||||
////client.GetInfo();
|
||||
|
||||
try
|
||||
{
|
||||
//await client.GetInfoAsync();
|
||||
var rs = await client.Install("test666", "ping", "qq.com");
|
||||
XTrace.WriteLine("Install={0}", rs.ToJson(true));
|
||||
//try
|
||||
//{
|
||||
// //await client.GetInfoAsync();
|
||||
// var rs = await client.Install("test666", "ping", "qq.com");
|
||||
// XTrace.WriteLine("Install={0}", rs.ToJson(true));
|
||||
|
||||
Thread.Sleep(3000);
|
||||
// Thread.Sleep(3000);
|
||||
|
||||
var rs2 = await client.Uninstall("test666");
|
||||
XTrace.WriteLine("Uninstall={0}", rs2);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
XTrace.Log.Error("星尘探测失败!{0}", ex.Message);
|
||||
}
|
||||
// var rs2 = await client.Uninstall("test666");
|
||||
// XTrace.WriteLine("Uninstall={0}", rs2);
|
||||
//}
|
||||
//catch (Exception ex)
|
||||
//{
|
||||
// XTrace.Log.Error("星尘探测失败!{0}", ex.Message);
|
||||
//}
|
||||
}
|
||||
|
||||
static void Test7()
|
||||
|
|
Loading…
Reference in New Issue