20 Star 213 Fork 54

apiumc / IT管理引擎

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Program.cs 21.58 KB
一键复制 编辑 原始数据 按行查看 历史
apiumc 提交于 2024-05-06 20:50 . Change
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
using System.Globalization;
using System;
using System.Collections;
using System.IO;
using UMC.Net;
using System.Net;
using UMC.Data;
using System.Threading;
using System.Diagnostics;
using System.IO.Pipes;
using System.Runtime.InteropServices;
using System.Linq;
using System.Reflection;
using SkiaSharp;
[assembly: UMC.Web.Apiumc]
namespace UMC.ITME
{
public class Program
{
public static void Main(string[] args)
{
var variable = System.Environment.GetEnvironmentVariable("UMC");
if (String.IsNullOrEmpty(variable))
{
if (args.Length > 0)
{
variable = args[0];
}
}
if (String.IsNullOrEmpty(variable) == false)
{
switch (variable)
{
case "main":
if (IsRun() == false)
{
StartUp(args.Length == 2 ? args[1] : String.Empty);
}
return;
case "image":
{
using (System.IO.Stream stream = typeof(ITME.WebServlet).Assembly//UMC.Proxy
.GetManifestResourceStream("UMC.ITME.Resources.app.png"))
{
using SKCodec sKCodec = SKCodec.Create(stream);
}
}
return;
}
}
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("# [start] \t开启IT管理引擎(ITME)服务");
Console.WriteLine("# [http 0-9] \t重置Http默认端口");
Console.WriteLine("# [https 0-9] \t重置Https默认端口");
// Console.WriteLine("# [dns a-z [ip]] \t设置域名临时解释");
Console.WriteLine("# [vpn] \t查看Web VPN服务");
Console.WriteLine("# [vpn start] \t开启Web VPN服务");
Console.WriteLine("# [vpn stop] \t停止Web VPN服务");
Console.WriteLine("# [stop] \t停止IT管理引擎(ITME)服务");
Console.WriteLine("# [exit] \t退出指令程序");
Console.WriteLine();
Console.ResetColor();
ConfigDbProvider();
if (args.Length > 0)
{
IsDo = false;
Command(args);
}
else
{
IsDo = true;
if (IsRun() == false)
{
Excel("main");
if (OperatingSystem.IsWindows())
{
Console.Write("特别注意:");
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("需要退出程序时,请用[exit]指令退出当前程序");
Console.WriteLine();
Console.ResetColor();
}
else
{
IsDo = false;
}
}
Write("info");
while (IsDo)
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write("指令:");
Console.ResetColor();
var cmds = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (cmds.Length > 0)
{
if (cmds[0] == "exit")
{
break;
}
Command(cmds);
}
}
}
}
static bool IsDo = false;
public static bool IsRun()
{
using (var pipeClient = new NamedPipeClientStream(".", "APIUMC", PipeDirection.InOut))
{
try
{
var key = UMC.Data.Utility.Parse36Encode(UMC.Data.Utility.IntParse(new Guid(UMC.Data.Utility.MD5(System.AppContext.BaseDirectory)))); ;
pipeClient.Connect(10);
pipeClient.Write(System.Text.Encoding.UTF8.GetBytes($"check {key}"));
var ls = new byte[0x200];
var l = pipeClient.Read(ls, 0, ls.Length);
if (l > 0)
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine(System.Text.Encoding.UTF8.GetString(ls, 0, l));
Console.WriteLine();
Console.ResetColor();
}
pipeClient.Close();
return true;
}
catch //(Exception ex)
{
// Console.WriteLine(ex.Message);
return false;
}
}
}
static void Write(params string[] args)
{
try
{
var key = UMC.Data.Utility.Parse36Encode(UMC.Data.Utility.IntParse(new Guid(UMC.Data.Utility.MD5(System.AppContext.BaseDirectory)))); ;
using (var pipeClient = new NamedPipeClientStream(".", "APIUMC", PipeDirection.InOut))
{
pipeClient.Connect(10000);
pipeClient.Write(System.Text.Encoding.UTF8.GetBytes(String.Join(" ", args)));
var ls = new byte[0x200];
int size = ls.Length;
int index = 0;
int total = 0;
int start = 0;
do
{
total = pipeClient.Read(ls, index, size) + index;
for (var i = 0; i < total; i++)
{
switch (ls[i])
{
case 10:
if (start < i)
{
Console.Write(System.Text.Encoding.UTF8.GetString(ls, start, i - start));
}
start = i + 1;
Console.ResetColor();
Console.WriteLine();
break;
case 12:
if (start < i)
{
Console.Write(System.Text.Encoding.UTF8.GetString(ls, start, i - start));
}
start = i + 1;
Console.ResetColor();
break;
case 13:
if (start < i)
{
Console.Write(System.Text.Encoding.UTF8.GetString(ls, start, i - start));
}
start = i + 1;
Console.ForegroundColor = ConsoleColor.Red;
break;
case 7:
if (start < i)
{
Console.Write(System.Text.Encoding.UTF8.GetString(ls, start, i - start));
}
start = i + 1;
Console.ForegroundColor = ConsoleColor.DarkYellow;
break;
case 8:
if (start < i)
{
Console.Write(System.Text.Encoding.UTF8.GetString(ls, start, i - start));
}
start = i + 1;
Console.ForegroundColor = ConsoleColor.DarkGreen;
break;
}
}
if (start < total)
{
Array.Copy(ls, start, ls, 0, total - start);
index = total - start;
size = ls.Length - index;
start = 0;
}
}
while (total > index);
if (start < total)
{
Console.WriteLine(System.Text.Encoding.UTF8.GetString(ls, start, total - start));
}
Console.WriteLine();
}
}
catch
{
Console.WriteLine("指令接收失败,请重新输入。");
}
}
static void Command(string[] args)
{
switch (args[0])
{
case "stop":
if (IsRun())
{
Write(args);
}
else
{
Console.WriteLine("ITME未开启,请开启。");
}
break;
case "biome":
case "ssl":
case "dns":
case "vpn":
case "clear":
case "https":
case "http":
case "pool":
if (IsRun())
{
Write(args);
}
else
{
Console.WriteLine("ITME未开启,请开启。");
}
break;
case "reload":
if (IsRun())
{
Write(new string[] { "reset" });
}
else
{
Excel("main");
}
break;
case "restart":
if (IsRun())
{
Write(new string[] { "stop" });
Thread.Sleep(5000);
Excel("main");
}
else
{
Excel("main");
}
IsDo = false;
Write("info");
break;
case "start":
if (IsRun() == false)
{
Excel("main");
IsDo = false;
Write("info");
}
else
{
Write(args);
}
break;
default:
Console.WriteLine($"不能识别指令:[{args[0]}]");
break;
}
}
static void Excel(string arg)
{
var process = System.Diagnostics.Process.GetCurrentProcess();
if (String.Equals(process.ProcessName.ToLower(), "dotnet"))
{
var file = Environment.GetCommandLineArgs()[0];
ProcessStartInfo startInfo = new ProcessStartInfo("dotnet")
{
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardInput = false,
RedirectStandardOutput = false,
Arguments = System.IO.Path.GetFileName(file),
WorkingDirectory = System.IO.Path.GetDirectoryName(file)
};
startInfo.Environment.Add("UMC", arg);
Process.Start(startInfo);
}
else
{
var file = process.MainModule.FileName;
ProcessStartInfo startInfo = new ProcessStartInfo(System.IO.Path.GetFileName(file))
{
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardInput = false,
RedirectStandardOutput = false,
WorkingDirectory = System.IO.Path.GetDirectoryName(file)
};
startInfo.Environment.Add("UMC", arg);
Process.Start(startInfo);
}
}
private static void UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
UMC.Data.Utility.Error("Unhandled", DateTime.Now, $"{e.ExceptionObject}");
ITME.HttpMimeServier.Stop();
Thread thread = new Thread(() =>
{
Excel("main");
Environment.Exit(0);
})
{
IsBackground = true
};
if (OperatingSystem.IsWindows())
{
thread.SetApartmentState(ApartmentState.STA);
}
thread.Start();
Thread.Sleep(5000);
}
static void ConfigDbProvider()
{
var urlKey = RuntimeInformation.RuntimeIdentifier;
var native = "/native/cavif";
if (OperatingSystem.IsWindows())
{
native = "/native/cavif.exe";
if (urlKey.EndsWith("x86"))
{
urlKey = "win-x86";
}
else if (urlKey.EndsWith("x64"))
{
urlKey = "win-x64";
}
else if (urlKey.EndsWith("arm"))
{
urlKey = "win-arm64";
}
}
else if (OperatingSystem.IsMacOS())
{
if (urlKey.EndsWith("arm64"))
{
urlKey = "osx-arm64";
}
else
{
urlKey = "osx-x64";
}
}
else if (OperatingSystem.IsLinux())
{
if (urlKey.EndsWith("musl-x64"))
{
urlKey = "linux-musl-x64";
}
else if (urlKey.EndsWith("x64"))
{
urlKey = "linux-x64";
}
else if (urlKey.EndsWith("arm64"))
{
urlKey = "linux-arm64";
}
else if (urlKey.EndsWith("arm"))
{
urlKey = "linux-arm";
}
}
if (String.IsNullOrEmpty(urlKey) == false)
{
var file = UMC.Data.Utility.MapPath(native);
if (System.IO.File.Exists(file) == false)
{
ManualResetEvent mre = new ManualResetEvent(false);
var url = new Uri($"https://wdk.oss-accelerate.aliyuncs.com/AppResources/{urlKey}.zip");
var downloadFile = file + ".download";
url.WebRequest().Get(r =>
{
if (r.StatusCode == HttpStatusCode.OK)
{
var count = r.ContentLength;
int size = 0;
var stream = Utility.Writer(downloadFile, false);
r.ReadAsData((b, c, l) =>
{
size += l;
if (b.Length == 0)
{
Console.WriteLine();
stream.Flush();
stream.Close();
stream.Dispose();
try
{
System.IO.Compression.ZipFile.ExtractToDirectory(downloadFile, UMC.Data.Utility.MapPath("/"), true);
if (native.EndsWith("exe"))
{
Utility.Move(UMC.Data.Utility.MapPath("/cavif.exe"), file);
}
else
{
Utility.Move(UMC.Data.Utility.MapPath("/cavif"), file);
}
}
catch
{
Console.SetCursorPosition(0, Console.CursorTop);
Console.WriteLine($"图片组件下载失败,影响图片功能");
}
File.Delete(downloadFile);
mre.Set();
}
else
{
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write($"正在下载图片组件{size * 100 / count:0}%");
stream.Write(b, c, l);
}
});
}
else
{
Console.WriteLine("图片组件下载失败");
mre.Set();
}
});
mre.WaitOne();
}
}
}
public static void Register()
{
UMC.Web.WebClient.Register(() => new UMC.Web.Activities.AccountFlow());
UMC.Web.WebClient.Register(() => new UMC.Web.Activities.AccountBiometricActivity());
UMC.Web.WebClient.Register(() => new UMC.Web.Activities.SettingsFlow());
UMC.Web.WebClient.Register(() => new UMC.Web.Activities.SystemFlow());
UMC.Web.WebClient.Register(() => new UMC.Web.Activities.SystemSetupActivity());
UMC.Web.WebClient.Register(() => new UMC.Web.Activities.SettingsAreaActivity());
UMC.Web.WebClient.Register(() => new UMC.Web.Activities.SettingsSelectOrganizeActivity());
UMC.Web.WebClient.Register(() => new UMC.Web.Activities.SettingsSelectRoleActivity());
UMC.Web.WebClient.Register(() => new UMC.Web.Activities.SettingsSelectUserActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.WebFactory());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteBuilderActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SitePictureActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteScanningActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.HttpBridgeActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.SiteLogConfActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteAppActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteAuthActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteConfActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteConfImageActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteLogActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteMimeActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteServerActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteUserActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.UIConfigActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteUIActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteSearchActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteSetupActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteUMCActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteMarketActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteSSHActivities());
// Renci.SshNet.Security.KeyExchangeECCurve25519
}
static void StartUp(string path)
{
AppDomain.CurrentDomain.UnhandledException += UnhandledException;
var p = typeof(Program).Assembly.GetCustomAttributes().FirstOrDefault(r => r is System.Reflection.AssemblyInformationalVersionAttribute) as System.Reflection.AssemblyInformationalVersionAttribute;
APIProxy.Version = p?.InformationalVersion ?? "1.0.0";
Register();
// UMC.Data.HotCache.Namespace(typeof(UMC.Proxy.Entities.Cookie).Namespace);
UMC.ITME.DataFactory.Instance();
UMC.Data.DataFactory.Instance();
if (String.IsNullOrEmpty(path) == false)
{
UMC.Data.Reflection.Instance().SetBaseDirectory(path);
}
ITME.HttpMimeServier.Start();
}
}
}
C#
1
https://gitee.com/apiumc/ITME.git
git@gitee.com:apiumc/ITME.git
apiumc
ITME
IT管理引擎
main

搜索帮助