using System; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Reflection; using System.Resources; using System.Threading; using Ultron.Ngrok.Utils; using System.Net; using System.IO; using Ultron.Ngrok.Properties; namespace Ultron.Ngrok { public partial class FormMain : Form { #region 属性 /// /// 命令行 /// public CmdUtils cmd { get; set; } /// /// 设置 /// public SettingForm setting { get; set; } /// /// 数据集名称 /// public string AssemblyProduct { get { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false); if (attributes.Length == 0) { return ""; } return ((AssemblyProductAttribute)attributes[0]).Product; } } /// /// 数据集版本 /// public string AssemblyVersion { get { return Assembly.GetExecutingAssembly().GetName().Version.ToString(); } } #endregion #region 初始化 /// /// 构造 /// public FormMain() { //引用资源处理 AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; //初始化界面 InitializeComponent(); //初始化配置 InitSetting(); } /// /// 窗体Load /// /// /// private void FormMain_Load(object sender, EventArgs e) { //进程检测 CheckProcessAndKill(setting.ExeFile); //初始化CMD new Thread(new ThreadStart(InitCmd)).Start(); //检查更新 new Thread(new ThreadStart(CheckUpdateAsync)).Start(); } #endregion #region 事件 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) { setting.ReLoadConfig(); setting.ShowDialog(); } private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e) { Process.Start("explorer.exe", "http://git.iwangyu.cn/wixy/Ultron.Ngrok/wiki"); } private void 关于ToolStripMenuItem_Click(object sender, EventArgs e) { AboutBox about = new AboutBox(); about.ShowDialog(); } #endregion #region 方法 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; } public void InitCmd() { //创建CMD cmd = new CmdUtils(); cmd.ProcessMessageEvent += DealMessage; cmd.ProcessKilled += cmd_Exited; } /// /// 检查更新 /// public void CheckUpdateAsync() { string checkUrl = "https://blog.wixy.cn/api/release/" + AssemblyProduct + "/" + AssemblyVersion; checkUrl = "https://blog.wixy.cn/api/release/NisStartTool/0.0.1"; HttpGet(checkUrl); } 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; } public void HttpGet(string url) { WebClient wc = new WebClient(); Uri uri = new Uri(url, UriKind.RelativeOrAbsolute); wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted); wc.OpenReadAsync(uri); } public void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { StreamReader read = new StreamReader(e.Result); string str = read.ReadToEnd(); if (str.Contains("\"code\":1")) return; else { if (MessageBox.Show("检测到新版本,立即更新?", "版本更新", MessageBoxButtons.YesNo) == DialogResult.Yes) { int verStart = str.IndexOf("\"version\":\"") + 11; int verEnd = str.IndexOf("\",\"title\""); string version = str.Substring(verStart, verEnd - verStart); string updateExe = Directory.GetCurrentDirectory() + @"\autoUpdate.exe"; if (File.Exists(updateExe)) { File.Delete(updateExe); } FileStream stream = new FileStream(updateExe, FileMode.OpenOrCreate); stream.Write(Resources.autoUpdate, 0, Resources.autoUpdate.Length); stream.Close(); RunUpdateProgram(version); } } } public void RunUpdateProgram(string verion) { Application.Exit(); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "autoUpdate.exe"; startInfo.Arguments = "https://cloud-disk-1251608065.cos.ap-guangzhou.myqcloud.com/uNgrok_" + verion + ".exe" + " uNgrok.exe"; startInfo.WindowStyle = ProcessWindowStyle.Normal; Process.Start(startInfo); } #endregion } }