自动扫描局域网的StarAgent

This commit is contained in:
大石头 2021-11-30 18:01:15 +08:00
parent 0bbbbdd8e2
commit b3899e064f
4 changed files with 105 additions and 32 deletions

View File

@ -46,11 +46,6 @@ namespace StarAgent
{
XTrace.WriteLine(info.ToJson());
var p = Process.GetCurrentProcess();
var asmx = AssemblyX.Entry;
var fileName = p.MainModule.FileName;
var args = Environment.CommandLine.TrimStart(Path.ChangeExtension(fileName, ".dll")).Trim();
var set = Setting;
// 使用对方送过来的星尘服务端地址
if (set.Server.IsNullOrEmpty() && !info.Server.IsNullOrEmpty())
@ -70,16 +65,11 @@ namespace StarAgent
}
}
return new AgentInfo
{
Version = asmx?.Version,
ProcessId = p.Id,
ProcessName = p.ProcessName,
FileName = fileName,
Arguments = args,
Server = set.Server,
Services = Manager?.Services.Select(e => e.Name).ToArray(),
};
var ai = AgentInfo.GetLocal();
ai.Server = set.Server;
ai.Services = Manager?.Services.Select(e => e.Name).ToArray();
return ai;
}
/// <summary>杀死并启动进程</summary>

View File

@ -1,13 +1,17 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Threading.Tasks;
using NewLife;
using NewLife.Http;
using NewLife.Log;
using NewLife.Messaging;
using NewLife.Net;
using NewLife.Reflection;
using NewLife.Remoting;
@ -36,22 +40,8 @@ namespace Stardust
/// <summary>实例化</summary>
public LocalStarClient()
{
var p = Process.GetCurrentProcess();
var asmx = AssemblyX.Entry;
var fileName = p.MainModule.FileName;
var args = Environment.CommandLine.TrimStart(Path.ChangeExtension(fileName, ".dll")).Trim();
var set = StarSetting.Current;
_local = new AgentInfo
{
Version = asmx?.Version,
ProcessId = p.Id,
ProcessName = p.ProcessName,
FileName = fileName,
Arguments = args,
Server = set.Server,
};
_local = AgentInfo.GetLocal();
_local.Server = StarSetting.Current.Server;
}
#endregion
@ -264,7 +254,52 @@ namespace Stardust
#endregion
#region
/// <summary>在局域网中广播扫描所有StarAgent</summary>
/// <param name="local">本地信息,用于告知对方我是谁</param>
/// <param name="timeout"></param>
/// <returns></returns>
public static IEnumerable<AgentInfo> Scan(AgentInfo local = null, Int32 timeout = 15_000)
{
var encoder = new JsonEncoder { Log = XTrace.Log };
// 构造请求消息
//var ms = new MemoryStream();
//var writer = new BinaryWriter(ms);
//writer.Write("Info");
//writer.Write(0);
//var msg = new DefaultMessage
//{
// Payload = ms.ToArray()
//};
//var buf = msg.ToPacket().ToArray();
var buf = encoder.CreateRequest("Info", null).ToPacket().ToArray();
// 广播消息
var udp = new UdpClient();
udp.Send(buf, buf.Length, new IPEndPoint(IPAddress.Broadcast, 5500));
var end = DateTime.Now.AddSeconds(timeout);
while (DateTime.Now < end)
{
var rs = new DefaultMessage();
IPEndPoint ep = null;
buf = udp.Receive(ref ep);
if (buf != null && rs.Read(buf) && encoder.Decode(rs, out var action, out _, out var data))
{
//ms = rs.Payload.GetStream();
//var reader=new BinaryReader(ms);
//var name=reader.ReadString();
//var code = reader.ReadInt32();
//var data=reader
var js = encoder.DecodeResult(action, data, rs);
var info = (AgentInfo)encoder.Convert(js, typeof(AgentInfo));
yield return info;
}
}
}
#endregion
}
}

View File

@ -1,10 +1,17 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using NewLife;
using NewLife.Reflection;
namespace Stardust.Models
{
/// <summary>代理信息</summary>
public class AgentInfo
{
#region
/// <summary>进程标识</summary>
public Int32 ProcessId { get; set; }
@ -20,6 +27,11 @@ namespace Stardust.Models
/// <summary>命令参数</summary>
public String Arguments { get; set; }
/// <summary>
/// 本地IP地址
/// </summary>
public String IP { get; set; }
/// <summary>服务端地址</summary>
public String Server { get; set; }
@ -27,5 +39,31 @@ namespace Stardust.Models
/// 应用服务
/// </summary>
public String[] Services { get; set; }
#endregion
#region
/// <summary>
/// 获取本地信息
/// </summary>
/// <returns></returns>
public static AgentInfo GetLocal()
{
var p = Process.GetCurrentProcess();
var asmx = AssemblyX.Entry;
var fileName = p.MainModule.FileName;
var args = Environment.CommandLine.TrimStart(Path.ChangeExtension(fileName, ".dll")).Trim();
var ip = NetHelper.GetIPsWithCache().Where(ip => ip.IsIPv4() && !IPAddress.IsLoopback(ip) && ip.GetAddressBytes()[0] != 169).Join();
return new AgentInfo
{
Version = asmx?.FileVersion,
ProcessId = p.Id,
ProcessName = p.ProcessName,
FileName = fileName,
Arguments = args,
IP = ip,
};
}
#endregion
}
}

View File

@ -7,6 +7,7 @@ using NewLife;
using NewLife.Log;
using NewLife.Messaging;
using NewLife.Remoting;
using NewLife.Serialization;
using Renci.SshNet;
using Stardust;
using Stardust.Monitors;
@ -20,7 +21,7 @@ namespace Test
{
XTrace.UseConsole();
Test4();
Test5();
Console.WriteLine("OK!");
Console.ReadKey();
@ -127,5 +128,14 @@ namespace Test
XTrace.WriteLine(ep + "");
XTrace.WriteLine(rs.ToStr());
}
static void Test5()
{
var rs = LocalStarClient.Scan();
foreach (var item in rs)
{
XTrace.WriteLine(item.ToJson());
}
}
}
}