1 Star 0 Fork 0

apiumc/WebVPN

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Program.cs 28.89 KB
一键复制 编辑 原始数据 按行查看 历史
apiumc 提交于 2个月前 . Change
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
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;
using System.Collections.Generic;
using Microsoft.VisualBasic;
using NSec.Cryptography;
using System.Net.Sockets;
namespace UMC.ITME
{
//dotnet publish -r linux-x64 -c Release /p:PublishSingleFile=true /p:PublishTrimmed=true
public class Program
{
public static void Main(string[] args)
{
Start(args, () => { }, new Dictionary<string, Func<String>>());
}
public static void Start(string[] args, Action action, Dictionary<string, Func<String>> pairs, Func<INetHandler> func = null)
{
var variable = System.Environment.GetEnvironmentVariable("UMC");
if (String.IsNullOrEmpty(variable))
{
if (args.Length > 0)
{
variable = args[0];
}
}
else if (variable == "daemon")
{
while (true)
{
//
var process = Excel($"main daemon", true);
try
{
using (StreamReader reader = process?.StandardError)
{
string stdout = reader?.ReadToEnd();
if (String.IsNullOrEmpty(stdout) == false)
{
var filename = UMC.Data.Reflection.ConfigPath(String.Format("Static\\log\\App\\Error\\ITME\\{0:yy-MM-dd}.log", DateTime.Now));
if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(filename)))
{
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filename));
}
File.AppendAllText(filename, stdout);// $"{D
}
else
{
break;
}
}
}
finally
{
process?.WaitForExit();
process?.Dispose();
}
}
return;
}
else
{
args = variable.Split(' ', StringSplitOptions.RemoveEmptyEntries);
variable = args[0];
}
if (String.IsNullOrEmpty(variable) == false)
{
switch (variable)
{
// case "daemon":
case "main":
if (IsRun(out var rePath) == false)
{
// Console.WriteLine(args.Length);
if (args.Length == 1)
{
AppDomain.CurrentDomain.UnhandledException += UnhandledException;
}
StartUp(new string[0], action, pairs, func);
}
else
{
StartUp(new string[] { rePath }, action, pairs, func);
}
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\t开启IT管理引擎(ITME)服务");
Console.WriteLine("# [http 0-9] \t\t重置Http默认端口");
Console.WriteLine("# [https 0-9] \t\t重置Https默认端口");
Console.WriteLine("# [repost] \t\t查看外网转发服务");
Console.WriteLine("# [repost start] \t开启外网转发服务");
Console.WriteLine("# [repost stop] \t停止外网转发服务");
Console.WriteLine("# [stop] \t\t停止IT管理引擎(ITME)服务");
Console.WriteLine("# [exit] \t\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;
}
int time = 0;
while (time < 20)
{
System.Threading.Thread.Sleep(500);
if (IsRun())
{
break;
}
time++;
}
}
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()
{
return IsRun(out var _);
}
public static int AdminPort = 5188;
public static bool IsRun(out String rootPath)
{
rootPath = String.Empty;
TcpClient tcpClient = new TcpClient();
try
{
tcpClient.Connect("127.0.0.1", AdminPort);
using (var pipeClient = tcpClient.GetStream())
{
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.WriteLine("主程序目录:");
Console.ForegroundColor = ConsoleColor.DarkRed;
rootPath = System.Text.Encoding.UTF8.GetString(ls, 0, l);
Console.WriteLine(rootPath);//System.Text.Encoding.UTF8.GetString(ls, 0, l));
Console.WriteLine();
Console.ResetColor();
}
else
{
rootPath = Utility.MapPath("~");
}
pipeClient.Close();
return true;
}
}
catch //(Exception ex)
{
// Console.WriteLine(ex.Message);
return false;
}
finally
{
tcpClient.Close();
}
}
static void Write(params string[] args)
{
TcpClient tcpClient = new TcpClient();
try
{
var key = UMC.Data.Utility.Parse36Encode(UMC.Data.Utility.IntParse(new Guid(UMC.Data.Utility.MD5(System.AppContext.BaseDirectory)))); ;
tcpClient.Connect("127.0.0.1", AdminPort);
using (var pipeClient = tcpClient.GetStream())
{
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("指令接收失败,请重新输入。");
}
finally
{
tcpClient.Close();
}
}
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 "repost":
case "clear":
case "https":
case "http":
case "pool":
if (IsRun())
{
Write(args);
}
else
{
Console.WriteLine("ITME未开启,请开启。");
}
break;
case "reset":
if (args.Length > 1)
{
Write(args);
}
else
{
Console.WriteLine("reset指令格式不正确");
}
break;
case "restart":
if (IsRun() == false)
{
Excel("main", true);
Write("info");
IsDo = false;
}
else
{
Excel("main", true);
Console.WriteLine("正在重启IT管理引擎(ITME)服务");
// using (var pipeServer = new NamedPipeServerStream("APIUMCReStart", PipeDirection.InOut))
// {
// var bufer = new byte[100];
// // var IsEror = false;
// try
// {
// pipeServer.WaitForConnection();
// int l = pipeServer.Read(bufer, 0, 100);
// Console.WriteLine(System.Text.Encoding.UTF8.GetString(bufer, 0, l));
// Write("Stop");
// pipeServer.Write(bufer, 0, "StopNet".WriteBytes(bufer, 0));
// //.Split(' ');
// }
// catch
// {
// }
// }
Write("info");
IsDo = false;
}
// Write("info");
break;
case "daemon":
{
if (IsRun() == false)
{
Excel("daemon", false);
IsDo = false;
Write("info");
}
else
{
Write(args);
}
}
break;
case "start":
if (IsRun() == false)
{
Excel("main", true);
IsDo = false;
int time = 0;
while (time < 20)
{
System.Threading.Thread.Sleep(500);
if (IsRun())
{
break;
}
time++;
}
Write("info");
}
else
{
Write(args);
}
break;
default:
// if()
if (IsRun())
{
Write(args);
}
else
{
Console.WriteLine($"不能识别指令:[{args[0]}]");
}
//
break;
}
}
static Process Excel(string arg)
{
return Excel(arg, true);
}
static Process Excel(string arg, bool isRedirect)
{
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,
RedirectStandardError = isRedirect,
RedirectStandardOutput = isRedirect,
Arguments = System.IO.Path.GetFileName(file),
WorkingDirectory = System.IO.Path.GetDirectoryName(file)
};
startInfo.Environment.Add("UMC", arg);
return Process.Start(startInfo);
}
else
{
var file = process.MainModule.FileName;
ProcessStartInfo startInfo = new ProcessStartInfo(System.IO.Path.GetFileName(file))
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardInput = false,
RedirectStandardError = isRedirect,
RedirectStandardOutput = isRedirect,
WorkingDirectory = System.IO.Path.GetDirectoryName(file)
};
startInfo.Environment.Add("UMC", arg);
// startInfo.
return Process.Start(startInfo);
}
}
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();
}
}
}
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.SiteLicenseActivity());
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.HttpClientActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteAlertActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteGroupActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteCustomerActivity());
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());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteWebAuthnActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteOnlineActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteStaffActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteSecretActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteFirewallActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteFingerprintActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteBgSrcActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteForwardActivity());
UMC.Web.WebClient.Register(() => new UMC.ITME.Activities.SiteDeviceActivity());
}
static void UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var ex = e.ExceptionObject as Exception;
var filename = UMC.Data.Reflection.ConfigPath(String.Format("Static\\log\\App\\Error\\ITME\\{0:yy-MM-dd}.log", DateTime.Now));
if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(filename)))
{
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filename));
}
ITME.HttpMimeServier.Stop();
File.AppendAllText(filename, $"{DateTime.Now}:{ex}\r\n");// ex.ToString());
Excel($"main");
}
static void StartUp(string[] args, Action action, Dictionary<String, Func<String>> keyValues, Func<INetHandler> func = null)
{
//
try
{
var p = typeof(Program).Assembly.GetCustomAttributes().FirstOrDefault(r => r is System.Reflection.AssemblyInformationalVersionAttribute) as System.Reflection.AssemblyInformationalVersionAttribute;
APIProxy.Version = p?.InformationalVersion ?? "1.0.0";
}
catch
{
}
UMC.Data.HotCache.BuilderCode(typeof(UMC.Bridge.SocksUser));
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
UMC.ITME.DataFactory.Instance();
UMC.Data.DataFactory.Instance();
bool IsSync = false;
if (args.Length > 0)
{
if (String.IsNullOrEmpty(args[0]) == false)
{
IsSync = true;
UMC.Data.Reflection.Instance().SetBaseDirectory(args[0]);
}
}
action?.Invoke();
Register();
ITME.HttpMimeServier.Start(IsSync, keyValues, func);
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/apiumc/WebVPN.git
git@gitee.com:apiumc/WebVPN.git
apiumc
WebVPN
WebVPN
master

搜索帮助