2019-04-01 08:17:56 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Resources;
|
|
|
|
|
using System.Threading;
|
2019-04-01 14:10:58 +08:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.IO;
|
2019-07-16 10:54:02 +08:00
|
|
|
|
using Ultron.Ngrok.Utils;
|
2019-04-01 08:17:56 +08:00
|
|
|
|
using Ultron.Ngrok.Properties;
|
|
|
|
|
|
|
|
|
|
namespace Ultron.Ngrok
|
|
|
|
|
{
|
|
|
|
|
public partial class FormMain : Form
|
|
|
|
|
{
|
2019-07-16 10:54:02 +08:00
|
|
|
|
public delegate void CmdInitAlready();
|
|
|
|
|
|
2019-04-01 14:10:58 +08:00
|
|
|
|
#region 属性
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 命令行
|
|
|
|
|
/// </summary>
|
|
|
|
|
public CmdUtils cmd { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置
|
|
|
|
|
/// </summary>
|
|
|
|
|
public SettingForm setting { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 数据集名称
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string AssemblyProduct
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
|
|
|
|
|
if (attributes.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
return ((AssemblyProductAttribute)attributes[0]).Product;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 数据集版本
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string AssemblyVersion
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Assembly.GetExecutingAssembly().GetName().Version.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-01 08:17:56 +08:00
|
|
|
|
|
2019-04-01 14:10:58 +08:00
|
|
|
|
#endregion
|
2019-04-01 08:17:56 +08:00
|
|
|
|
|
2019-04-01 14:10:58 +08:00
|
|
|
|
#region 初始化
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 构造
|
|
|
|
|
/// </summary>
|
2019-04-01 08:17:56 +08:00
|
|
|
|
public FormMain()
|
|
|
|
|
{
|
|
|
|
|
//引用资源处理
|
|
|
|
|
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
|
|
|
|
|
|
2019-04-01 14:10:58 +08:00
|
|
|
|
//初始化界面
|
2019-04-01 08:17:56 +08:00
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
|
|
|
|
//初始化配置
|
|
|
|
|
InitSetting();
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-01 14:10:58 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 窗体Load
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void FormMain_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
//进程检测
|
|
|
|
|
CheckProcessAndKill(setting.ExeFile);
|
|
|
|
|
|
2019-07-16 10:54:02 +08:00
|
|
|
|
CmdInitAlready callback = new CmdInitAlready(CmdCallBack);
|
2019-04-01 14:10:58 +08:00
|
|
|
|
//初始化CMD
|
2019-07-16 10:54:02 +08:00
|
|
|
|
Thread cmdThread = new Thread(InitCmd);
|
|
|
|
|
cmdThread.IsBackground = true;
|
|
|
|
|
cmdThread.Start(callback);
|
2019-04-01 14:10:58 +08:00
|
|
|
|
|
|
|
|
|
//检查更新
|
2019-07-16 10:54:02 +08:00
|
|
|
|
Thread updateThread = new Thread(new ThreadStart(CheckUpdateAsync));
|
|
|
|
|
updateThread.IsBackground = true;
|
|
|
|
|
updateThread.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CmdCallBack()
|
|
|
|
|
{
|
|
|
|
|
if(setting.ServerConfig.AutoStart)
|
|
|
|
|
{
|
|
|
|
|
startBtn_Click(null, null);
|
|
|
|
|
}
|
2019-04-01 14:10:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2019-07-16 10:54:02 +08:00
|
|
|
|
|
2019-04-01 14:10:58 +08:00
|
|
|
|
#region 事件
|
|
|
|
|
|
|
|
|
|
private void startBtn_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
//网络检测
|
|
|
|
|
NetworkUtils.CheckServeStatus(this);
|
2019-07-16 10:54:02 +08:00
|
|
|
|
cmd.SendMsg(setting.ExecuteCommand,setting.ServerConfig.HideWindow);
|
2019-04-01 14:10:58 +08:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2019-07-16 10:54:02 +08:00
|
|
|
|
Process.Start("explorer.exe", "https://git.wixy.cn/wixy/Ultron.Ngrok/wiki/帮助");
|
2019-04-01 14:10:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
AboutBox about = new AboutBox();
|
|
|
|
|
about.ShowDialog();
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-16 10:54:02 +08:00
|
|
|
|
private void NotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
|
|
|
|
|
{
|
2019-08-30 09:37:01 +08:00
|
|
|
|
if (this.WindowState == FormWindowState.Minimized)
|
|
|
|
|
this.WindowState = FormWindowState.Normal;
|
|
|
|
|
if (this.Visible == false)
|
|
|
|
|
this.Show();
|
2019-07-16 10:54:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (setting.ServerConfig.UseNotifyMenu)
|
|
|
|
|
{
|
2019-08-30 09:37:01 +08:00
|
|
|
|
notifyIcon.Visible = true;
|
|
|
|
|
this.Hide();
|
2019-07-16 10:54:02 +08:00
|
|
|
|
e.Cancel = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (MessageBox.Show("是否确认退出程序?", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
// 关闭所有的线程
|
|
|
|
|
this.Dispose();
|
|
|
|
|
this.Close();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
e.Cancel = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-30 09:37:01 +08:00
|
|
|
|
|
2019-07-16 10:54:02 +08:00
|
|
|
|
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (MessageBox.Show("是否确认退出程序?", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
this.Dispose();
|
|
|
|
|
this.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
WindowState = FormWindowState.Normal;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-01 14:10:58 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 方法
|
|
|
|
|
|
2019-04-01 08:17:56 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-16 10:54:02 +08:00
|
|
|
|
public void InitCmd(object obj)
|
2019-04-01 14:10:58 +08:00
|
|
|
|
{
|
2019-04-01 08:17:56 +08:00
|
|
|
|
//创建CMD
|
2019-04-01 14:10:58 +08:00
|
|
|
|
cmd = new CmdUtils();
|
2019-04-01 08:17:56 +08:00
|
|
|
|
cmd.ProcessMessageEvent += DealMessage;
|
|
|
|
|
cmd.ProcessKilled += cmd_Exited;
|
2019-07-16 10:54:02 +08:00
|
|
|
|
CmdInitAlready callback = obj as CmdInitAlready;
|
|
|
|
|
callback();
|
2019-04-01 08:17:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-01 14:10:58 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 检查更新
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void CheckUpdateAsync()
|
2019-04-01 08:17:56 +08:00
|
|
|
|
{
|
2019-04-01 14:10:58 +08:00
|
|
|
|
string checkUrl = "https://blog.wixy.cn/api/release/" + AssemblyProduct + "/" + AssemblyVersion;
|
|
|
|
|
HttpGet(checkUrl);
|
2019-04-01 08:17:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-01 14:10:58 +08:00
|
|
|
|
public void DealMessage(int proId, string message)
|
2019-04-01 08:17:56 +08:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-01 14:10:58 +08:00
|
|
|
|
public void HttpGet(string url)
|
2019-04-01 08:17:56 +08:00
|
|
|
|
{
|
2020-01-21 15:16:39 +08:00
|
|
|
|
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();
|
|
|
|
|
|
2019-04-01 14:10:58 +08:00
|
|
|
|
WebClient wc = new WebClient();
|
|
|
|
|
Uri uri = new Uri(url, UriKind.RelativeOrAbsolute);
|
|
|
|
|
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
|
|
|
|
|
wc.OpenReadAsync(uri);
|
2019-04-01 08:17:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-01 14:10:58 +08:00
|
|
|
|
public void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
|
2019-04-01 08:17:56 +08:00
|
|
|
|
{
|
2019-08-30 09:37:01 +08:00
|
|
|
|
try
|
2019-04-01 14:10:58 +08:00
|
|
|
|
{
|
2019-08-30 09:37:01 +08:00
|
|
|
|
StreamReader read = new StreamReader(e.Result);
|
|
|
|
|
string str = read.ReadToEnd();
|
|
|
|
|
if (str.Contains("\"code\":1"))
|
|
|
|
|
return;
|
|
|
|
|
else
|
2019-04-01 14:10:58 +08:00
|
|
|
|
{
|
2019-08-30 09:37:01 +08:00
|
|
|
|
if (MessageBox.Show("检测到新版本,立即更新?", "版本更新", MessageBoxButtons.YesNo) == DialogResult.Yes)
|
2019-04-01 14:10:58 +08:00
|
|
|
|
{
|
2019-08-30 09:37:01 +08:00
|
|
|
|
int verStart = str.IndexOf("\"version\":\"") + 11;
|
|
|
|
|
int verEnd = str.IndexOf("\",\"title\"");
|
|
|
|
|
string version = str.Substring(verStart, verEnd - verStart);
|
|
|
|
|
|
|
|
|
|
RunUpdateProgram(version);
|
2019-04-01 14:10:58 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-30 09:37:01 +08:00
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
2020-01-21 15:16:39 +08:00
|
|
|
|
string msg = ex.InnerException == null ? ex.Message : ex.InnerException.Message;
|
|
|
|
|
MessageBox.Show($"无法检测更新:{msg}");
|
2019-08-30 09:37:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-01 14:10:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RunUpdateProgram(string verion)
|
|
|
|
|
{
|
|
|
|
|
ProcessStartInfo startInfo = new ProcessStartInfo();
|
|
|
|
|
startInfo.FileName = "autoUpdate.exe";
|
2020-01-21 15:16:39 +08:00
|
|
|
|
startInfo.Arguments = "\"https://cloud-disk-1251608065.cos.ap-guangzhou.myqcloud.com/uNgrok_" + verion + ".exe" + " uNgrok.exe\"";
|
2019-04-01 14:10:58 +08:00
|
|
|
|
startInfo.WindowStyle = ProcessWindowStyle.Normal;
|
2020-01-21 15:16:39 +08:00
|
|
|
|
startInfo.UseShellExecute = false;
|
2019-04-01 14:10:58 +08:00
|
|
|
|
Process.Start(startInfo);
|
2020-01-21 15:16:39 +08:00
|
|
|
|
Environment.Exit(0);
|
2019-04-01 08:17:56 +08:00
|
|
|
|
}
|
2019-04-01 14:10:58 +08:00
|
|
|
|
|
|
|
|
|
#endregion
|
2019-07-16 10:54:02 +08:00
|
|
|
|
|
2019-04-01 08:17:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|