182 lines
6.0 KiB
C#
182 lines
6.0 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.ComponentModel;
|
|||
|
using System.Data;
|
|||
|
using System.Drawing;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Windows.Forms;
|
|||
|
using System.Diagnostics;
|
|||
|
using System.Reflection;
|
|||
|
using System.Resources;
|
|||
|
using System.Threading;
|
|||
|
using System.Text.RegularExpressions;
|
|||
|
using System.IO;
|
|||
|
using System.Net.NetworkInformation;
|
|||
|
using YamlDotNet.Serialization;
|
|||
|
using YamlDotNet.Serialization.NamingConventions;
|
|||
|
using Ultron.Ngrok.Utils;
|
|||
|
using Ultron.Ngrok.Model;
|
|||
|
using Ultron.Ngrok.Properties;
|
|||
|
|
|||
|
namespace Ultron.Ngrok
|
|||
|
{
|
|||
|
public partial class FormMain : Form
|
|||
|
{
|
|||
|
CmdUtils cmd = new CmdUtils();
|
|||
|
|
|||
|
SettingForm setting;
|
|||
|
|
|||
|
public FormMain()
|
|||
|
{
|
|||
|
//引用资源处理
|
|||
|
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
|
|||
|
|
|||
|
//初始化
|
|||
|
InitializeComponent();
|
|||
|
|
|||
|
//初始化配置
|
|||
|
InitSetting();
|
|||
|
}
|
|||
|
|
|||
|
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
|
|||
|
{
|
|||
|
string dllName = args.Name.Contains(",") ? args.Name.Substring(0, args.Name.IndexOf(',')) : args.Name.Replace(".dll", "");
|
|||
|
dllName = dllName.Replace(".", "_");
|
|||
|
if (dllName.EndsWith("_resources")) return null;
|
|||
|
ResourceManager rm = new ResourceManager(GetType().Namespace + ".Properties.Resources", Assembly.GetExecutingAssembly());
|
|||
|
byte[] bytes = (byte[])rm.GetObject(dllName);
|
|||
|
return Assembly.Load(bytes);
|
|||
|
}
|
|||
|
|
|||
|
public void CheckProcessAndKill(string processName)
|
|||
|
{
|
|||
|
Process[] proces = Process.GetProcessesByName(processName);
|
|||
|
foreach (var item in proces)
|
|||
|
{
|
|||
|
WriteLog("检测到进程:" + item.ProcessName + ",ID:" + item.Id + "");
|
|||
|
item.Kill();
|
|||
|
WriteLog("结束进程:" + item.Id + "");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void CheckProcessAndKill(int id)
|
|||
|
{
|
|||
|
Process proces = Process.GetProcessById(id);
|
|||
|
if (proces != null)
|
|||
|
{
|
|||
|
WriteLog("检测到进程:" + proces.ProcessName + ",ID:" + proces.Id + "");
|
|||
|
proces.Kill();
|
|||
|
WriteLog("结束进程:" + proces.Id + "");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void InitSetting()
|
|||
|
{
|
|||
|
setting = new SettingForm();
|
|||
|
setting.WriteLogToForm += WriteLog;
|
|||
|
}
|
|||
|
|
|||
|
private void InitCmd() {
|
|||
|
//创建CMD
|
|||
|
cmd.ProcessMessageEvent += DealMessage;
|
|||
|
cmd.ProcessKilled += cmd_Exited;
|
|||
|
}
|
|||
|
|
|||
|
private void startBtn_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
//网络检测
|
|||
|
NetworkUtils.CheckServeStatus(this);
|
|||
|
cmd.SendMsg(setting.ExecuteCommand);
|
|||
|
|
|||
|
startBtn.Enabled = false;
|
|||
|
endBtn.Enabled = true;
|
|||
|
}
|
|||
|
|
|||
|
private void endBtn_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
CheckProcessAndKill(setting.ExeFile);
|
|||
|
cmd.CloseCmd();
|
|||
|
startBtn.Enabled = true;
|
|||
|
endBtn.Enabled = false;
|
|||
|
}
|
|||
|
|
|||
|
private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
AboutBox about = new AboutBox();
|
|||
|
about.ShowDialog();
|
|||
|
}
|
|||
|
|
|||
|
private void FormMain_Load(object sender, EventArgs e)
|
|||
|
{
|
|||
|
//进程检测
|
|||
|
CheckProcessAndKill(setting.ExeFile);
|
|||
|
|
|||
|
//初始化CMD
|
|||
|
new Thread(new ThreadStart(InitCmd)).Start();
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public void DealMessage(int proId,string message)
|
|||
|
{
|
|||
|
if (message.StartsWith("Server failed to allocate tunnel"))
|
|||
|
{
|
|||
|
if (message.Contains("address already in use"))
|
|||
|
{
|
|||
|
string tcpErr = message.Substring(message.IndexOf("listen tcp"));
|
|||
|
WriteLog(tcpErr);
|
|||
|
int domainIndex = message.IndexOf("0.0.0.0:") + 8;
|
|||
|
int portIndex = message.IndexOf(": bind:");
|
|||
|
string port = message.Substring(domainIndex, portIndex - domainIndex);
|
|||
|
MessageBox.Show("通道分配失败,端口:" + port + "已被使用!");
|
|||
|
}
|
|||
|
else if (message.Contains("is already registered"))
|
|||
|
{
|
|||
|
string tcpErr = message.Substring(message.IndexOf("The tunnel "));
|
|||
|
WriteLog(tcpErr);
|
|||
|
int domainIndex = message.IndexOf("http://");
|
|||
|
int strIndex = message.IndexOf("is already");
|
|||
|
string domain = message.Substring(domainIndex, strIndex - domainIndex);
|
|||
|
MessageBox.Show("通道分配失败,域名:" + domain + "已被使用!");
|
|||
|
}
|
|||
|
|
|||
|
CheckProcessAndKill(proId);
|
|||
|
startBtn.Enabled = true;
|
|||
|
endBtn.Enabled = false;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
if (message.Contains("Microsoft Windows") || message.Contains("Microsoft Corporation") || message.Contains(".exe -config ngrok.cfg start"))
|
|||
|
return;
|
|||
|
WriteLog(message);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void WriteLog(string message)
|
|||
|
{
|
|||
|
StringBuilder sb = new StringBuilder(cmdLogTextArea.Text);
|
|||
|
cmdLogTextArea.Text = sb.AppendLine(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss ") + message).ToString();
|
|||
|
cmdLogTextArea.SelectionStart = cmdLogTextArea.Text.Length;
|
|||
|
cmdLogTextArea.ScrollToCaret();
|
|||
|
}
|
|||
|
|
|||
|
public void cmd_Exited(object sender, EventArgs e)
|
|||
|
{
|
|||
|
WriteLog("结束运行!");
|
|||
|
startBtn.Enabled = true;
|
|||
|
endBtn.Enabled = false;
|
|||
|
}
|
|||
|
|
|||
|
private void 设置ToolStripMenuItem_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
setting.ReLoadConfig();
|
|||
|
setting.ShowDialog();
|
|||
|
}
|
|||
|
|
|||
|
private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
}
|
|||
|
}
|
|||
|
}
|