diff --git a/net.nutcore.aliddns/net.nutcore.aliddns/Form_ngrok.Designer.cs b/net.nutcore.aliddns/net.nutcore.aliddns/Form_ngrok.Designer.cs index 5b8512e..03a3560 100644 --- a/net.nutcore.aliddns/net.nutcore.aliddns/Form_ngrok.Designer.cs +++ b/net.nutcore.aliddns/net.nutcore.aliddns/Form_ngrok.Designer.cs @@ -30,15 +30,38 @@ { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form_ngrok)); this.groupBox_AuthToken = new System.Windows.Forms.GroupBox(); - this.groupBox_ProtocolPort = new System.Windows.Forms.GroupBox(); + this.textBox_AuthToken = new System.Windows.Forms.TextBox(); + this.groupBox_Ngrokserver = new System.Windows.Forms.GroupBox(); this.groupBox_Url = new System.Windows.Forms.GroupBox(); this.button_cancel = new System.Windows.Forms.Button(); this.button_save = new System.Windows.Forms.Button(); - this.textBox_AuthToken = new System.Windows.Forms.TextBox(); - this.groupBox1 = new System.Windows.Forms.GroupBox(); - this.textBox_NgrokDownloadUrl = new System.Windows.Forms.TextBox(); + this.label_NgrokDomain = new System.Windows.Forms.Label(); + this.label_tunnelAddr = new System.Windows.Forms.Label(); + this.label_Defaultport = new System.Windows.Forms.Label(); + this.label_http = new System.Windows.Forms.Label(); + this.label_https = new System.Windows.Forms.Label(); + this.textBox1 = new System.Windows.Forms.TextBox(); + this.textBox_tunnelAddr = new System.Windows.Forms.TextBox(); + this.textBox_httpAddr = new System.Windows.Forms.TextBox(); + this.textBox_httpsAddr = new System.Windows.Forms.TextBox(); + this.label_subDomain = new System.Windows.Forms.Label(); + this.label_lanHttp = new System.Windows.Forms.Label(); + this.label_lanhttps = new System.Windows.Forms.Label(); + this.textBox_subDomain = new System.Windows.Forms.TextBox(); + this.textBox_lanHttp = new System.Windows.Forms.TextBox(); + this.textBox_Https = new System.Windows.Forms.TextBox(); + this.label_lanTcp = new System.Windows.Forms.Label(); + this.label_remoteport = new System.Windows.Forms.Label(); + this.label_lanport = new System.Windows.Forms.Label(); + this.textBox_lanTunnel1 = new System.Windows.Forms.TextBox(); + this.textBox_remotePort1 = new System.Windows.Forms.TextBox(); + this.textBox_lanPort1 = new System.Windows.Forms.TextBox(); + this.textBox_lanTunnel2 = new System.Windows.Forms.TextBox(); + this.textBox_remotePort2 = new System.Windows.Forms.TextBox(); + this.textBox_lanPort2 = new System.Windows.Forms.TextBox(); this.groupBox_AuthToken.SuspendLayout(); - this.groupBox1.SuspendLayout(); + this.groupBox_Ngrokserver.SuspendLayout(); + this.groupBox_Url.SuspendLayout(); this.SuspendLayout(); // // groupBox_AuthToken @@ -51,27 +74,58 @@ this.groupBox_AuthToken.TabStop = false; this.groupBox_AuthToken.Text = "认证令牌"; // - // groupBox_ProtocolPort + // textBox_AuthToken // - this.groupBox_ProtocolPort.Location = new System.Drawing.Point(6, 68); - this.groupBox_ProtocolPort.Name = "groupBox_ProtocolPort"; - this.groupBox_ProtocolPort.Size = new System.Drawing.Size(273, 65); - this.groupBox_ProtocolPort.TabIndex = 1; - this.groupBox_ProtocolPort.TabStop = false; - this.groupBox_ProtocolPort.Text = "协议端口"; + this.textBox_AuthToken.Location = new System.Drawing.Point(11, 21); + this.textBox_AuthToken.Name = "textBox_AuthToken"; + this.textBox_AuthToken.Size = new System.Drawing.Size(250, 21); + this.textBox_AuthToken.TabIndex = 0; + // + // groupBox_Ngrokserver + // + this.groupBox_Ngrokserver.Controls.Add(this.textBox_httpsAddr); + this.groupBox_Ngrokserver.Controls.Add(this.textBox_httpAddr); + this.groupBox_Ngrokserver.Controls.Add(this.textBox_tunnelAddr); + this.groupBox_Ngrokserver.Controls.Add(this.textBox1); + this.groupBox_Ngrokserver.Controls.Add(this.label_https); + this.groupBox_Ngrokserver.Controls.Add(this.label_http); + this.groupBox_Ngrokserver.Controls.Add(this.label_Defaultport); + this.groupBox_Ngrokserver.Controls.Add(this.label_tunnelAddr); + this.groupBox_Ngrokserver.Controls.Add(this.label_NgrokDomain); + this.groupBox_Ngrokserver.Location = new System.Drawing.Point(6, 68); + this.groupBox_Ngrokserver.Name = "groupBox_Ngrokserver"; + this.groupBox_Ngrokserver.Size = new System.Drawing.Size(273, 97); + this.groupBox_Ngrokserver.TabIndex = 1; + this.groupBox_Ngrokserver.TabStop = false; + this.groupBox_Ngrokserver.Text = "服务端Ngrokd"; // // groupBox_Url // - this.groupBox_Url.Location = new System.Drawing.Point(6, 139); + this.groupBox_Url.Controls.Add(this.textBox_lanPort2); + this.groupBox_Url.Controls.Add(this.textBox_remotePort2); + this.groupBox_Url.Controls.Add(this.textBox_lanTunnel2); + this.groupBox_Url.Controls.Add(this.textBox_lanPort1); + this.groupBox_Url.Controls.Add(this.textBox_remotePort1); + this.groupBox_Url.Controls.Add(this.textBox_lanTunnel1); + this.groupBox_Url.Controls.Add(this.label_lanport); + this.groupBox_Url.Controls.Add(this.label_remoteport); + this.groupBox_Url.Controls.Add(this.label_lanTcp); + this.groupBox_Url.Controls.Add(this.textBox_Https); + this.groupBox_Url.Controls.Add(this.textBox_lanHttp); + this.groupBox_Url.Controls.Add(this.textBox_subDomain); + this.groupBox_Url.Controls.Add(this.label_lanhttps); + this.groupBox_Url.Controls.Add(this.label_lanHttp); + this.groupBox_Url.Controls.Add(this.label_subDomain); + this.groupBox_Url.Location = new System.Drawing.Point(6, 171); this.groupBox_Url.Name = "groupBox_Url"; - this.groupBox_Url.Size = new System.Drawing.Size(273, 90); + this.groupBox_Url.Size = new System.Drawing.Size(273, 140); this.groupBox_Url.TabIndex = 2; this.groupBox_Url.TabStop = false; - this.groupBox_Url.Text = "访问域名"; + this.groupBox_Url.Text = "内网端Ngrok"; // // button_cancel // - this.button_cancel.Location = new System.Drawing.Point(40, 291); + this.button_cancel.Location = new System.Drawing.Point(40, 317); this.button_cancel.Name = "button_cancel"; this.button_cancel.Size = new System.Drawing.Size(80, 23); this.button_cancel.TabIndex = 3; @@ -81,7 +135,7 @@ // // button_save // - this.button_save.Location = new System.Drawing.Point(156, 291); + this.button_save.Location = new System.Drawing.Point(156, 317); this.button_save.Name = "button_save"; this.button_save.Size = new System.Drawing.Size(80, 23); this.button_save.TabIndex = 4; @@ -89,49 +143,227 @@ this.button_save.UseVisualStyleBackColor = true; this.button_save.Click += new System.EventHandler(this.button_save_Click); // - // textBox_AuthToken + // label_NgrokDomain // - this.textBox_AuthToken.Location = new System.Drawing.Point(11, 21); - this.textBox_AuthToken.Name = "textBox_AuthToken"; - this.textBox_AuthToken.Size = new System.Drawing.Size(250, 21); - this.textBox_AuthToken.TabIndex = 0; + this.label_NgrokDomain.AutoSize = true; + this.label_NgrokDomain.Location = new System.Drawing.Point(9, 22); + this.label_NgrokDomain.Name = "label_NgrokDomain"; + this.label_NgrokDomain.Size = new System.Drawing.Size(35, 12); + this.label_NgrokDomain.TabIndex = 0; + this.label_NgrokDomain.Text = "域名:"; // - // groupBox1 + // label_tunnelAddr // - this.groupBox1.Controls.Add(this.textBox_NgrokDownloadUrl); - this.groupBox1.Location = new System.Drawing.Point(6, 235); - this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(273, 50); - this.groupBox1.TabIndex = 5; - this.groupBox1.TabStop = false; - this.groupBox1.Text = "Ngrok下载地址"; + this.label_tunnelAddr.AutoSize = true; + this.label_tunnelAddr.Location = new System.Drawing.Point(9, 47); + this.label_tunnelAddr.Name = "label_tunnelAddr"; + this.label_tunnelAddr.Size = new System.Drawing.Size(59, 12); + this.label_tunnelAddr.TabIndex = 1; + this.label_tunnelAddr.Text = "侦听端口:"; // - // textBox_NgrokDownloadUrl + // label_Defaultport // - this.textBox_NgrokDownloadUrl.Location = new System.Drawing.Point(11, 22); - this.textBox_NgrokDownloadUrl.Name = "textBox_NgrokDownloadUrl"; - this.textBox_NgrokDownloadUrl.Size = new System.Drawing.Size(250, 21); - this.textBox_NgrokDownloadUrl.TabIndex = 0; - this.textBox_NgrokDownloadUrl.Text = "https://ngrok.com/download"; + this.label_Defaultport.AutoSize = true; + this.label_Defaultport.Location = new System.Drawing.Point(9, 73); + this.label_Defaultport.Name = "label_Defaultport"; + this.label_Defaultport.Size = new System.Drawing.Size(83, 12); + this.label_Defaultport.TabIndex = 2; + this.label_Defaultport.Text = "默认服务端口:"; + // + // label_http + // + this.label_http.AutoSize = true; + this.label_http.Location = new System.Drawing.Point(89, 73); + this.label_http.Name = "label_http"; + this.label_http.Size = new System.Drawing.Size(29, 12); + this.label_http.TabIndex = 3; + this.label_http.Text = "http"; + // + // label_https + // + this.label_https.AutoSize = true; + this.label_https.Location = new System.Drawing.Point(174, 73); + this.label_https.Name = "label_https"; + this.label_https.Size = new System.Drawing.Size(35, 12); + this.label_https.TabIndex = 4; + this.label_https.Text = "https"; + // + // textBox1 + // + this.textBox1.Location = new System.Drawing.Point(50, 17); + this.textBox1.Name = "textBox1"; + this.textBox1.Size = new System.Drawing.Size(211, 21); + this.textBox1.TabIndex = 5; + // + // textBox_tunnelAddr + // + this.textBox_tunnelAddr.Location = new System.Drawing.Point(72, 43); + this.textBox_tunnelAddr.Name = "textBox_tunnelAddr"; + this.textBox_tunnelAddr.Size = new System.Drawing.Size(50, 21); + this.textBox_tunnelAddr.TabIndex = 6; + this.textBox_tunnelAddr.Text = "8083"; + // + // textBox_httpAddr + // + this.textBox_httpAddr.Location = new System.Drawing.Point(120, 70); + this.textBox_httpAddr.Name = "textBox_httpAddr"; + this.textBox_httpAddr.Size = new System.Drawing.Size(50, 21); + this.textBox_httpAddr.TabIndex = 7; + this.textBox_httpAddr.Text = "8081"; + // + // textBox_httpsAddr + // + this.textBox_httpsAddr.Location = new System.Drawing.Point(211, 70); + this.textBox_httpsAddr.Name = "textBox_httpsAddr"; + this.textBox_httpsAddr.Size = new System.Drawing.Size(50, 21); + this.textBox_httpsAddr.TabIndex = 8; + this.textBox_httpsAddr.Text = "8082"; + // + // label_subDomain + // + this.label_subDomain.AutoSize = true; + this.label_subDomain.Location = new System.Drawing.Point(9, 18); + this.label_subDomain.Name = "label_subDomain"; + this.label_subDomain.Size = new System.Drawing.Size(53, 12); + this.label_subDomain.TabIndex = 0; + this.label_subDomain.Text = "次级域名"; + // + // label_lanHttp + // + this.label_lanHttp.AutoSize = true; + this.label_lanHttp.Location = new System.Drawing.Point(84, 18); + this.label_lanHttp.Name = "label_lanHttp"; + this.label_lanHttp.Size = new System.Drawing.Size(77, 12); + this.label_lanHttp.TabIndex = 1; + this.label_lanHttp.Text = "http服务端口"; + // + // label_lanhttps + // + this.label_lanhttps.AutoSize = true; + this.label_lanhttps.Location = new System.Drawing.Point(178, 18); + this.label_lanhttps.Name = "label_lanhttps"; + this.label_lanhttps.Size = new System.Drawing.Size(83, 12); + this.label_lanhttps.TabIndex = 2; + this.label_lanhttps.Text = "https服务端口"; + // + // textBox_subDomain + // + this.textBox_subDomain.Location = new System.Drawing.Point(10, 37); + this.textBox_subDomain.Name = "textBox_subDomain"; + this.textBox_subDomain.Size = new System.Drawing.Size(50, 21); + this.textBox_subDomain.TabIndex = 3; + this.textBox_subDomain.Text = "web"; + // + // textBox_lanHttp + // + this.textBox_lanHttp.Location = new System.Drawing.Point(96, 37); + this.textBox_lanHttp.Name = "textBox_lanHttp"; + this.textBox_lanHttp.Size = new System.Drawing.Size(50, 21); + this.textBox_lanHttp.TabIndex = 4; + this.textBox_lanHttp.Text = "80"; + // + // textBox_Https + // + this.textBox_Https.Location = new System.Drawing.Point(191, 37); + this.textBox_Https.Name = "textBox_Https"; + this.textBox_Https.Size = new System.Drawing.Size(50, 21); + this.textBox_Https.TabIndex = 5; + this.textBox_Https.Text = "80"; + // + // label_lanTcp + // + this.label_lanTcp.AutoSize = true; + this.label_lanTcp.Location = new System.Drawing.Point(12, 65); + this.label_lanTcp.Name = "label_lanTcp"; + this.label_lanTcp.Size = new System.Drawing.Size(47, 12); + this.label_lanTcp.TabIndex = 6; + this.label_lanTcp.Text = "TCP穿透"; + // + // label_remoteport + // + this.label_remoteport.AutoSize = true; + this.label_remoteport.Location = new System.Drawing.Point(96, 65); + this.label_remoteport.Name = "label_remoteport"; + this.label_remoteport.Size = new System.Drawing.Size(53, 12); + this.label_remoteport.TabIndex = 7; + this.label_remoteport.Text = "远程端口"; + // + // label_lanport + // + this.label_lanport.AutoSize = true; + this.label_lanport.Location = new System.Drawing.Point(180, 65); + this.label_lanport.Name = "label_lanport"; + this.label_lanport.Size = new System.Drawing.Size(77, 12); + this.label_lanport.TabIndex = 8; + this.label_lanport.Text = "内网服务端口"; + // + // textBox_lanTunnel1 + // + this.textBox_lanTunnel1.Location = new System.Drawing.Point(10, 85); + this.textBox_lanTunnel1.Name = "textBox_lanTunnel1"; + this.textBox_lanTunnel1.Size = new System.Drawing.Size(50, 21); + this.textBox_lanTunnel1.TabIndex = 9; + this.textBox_lanTunnel1.Text = "ssh"; + // + // textBox_remotePort1 + // + this.textBox_remotePort1.Location = new System.Drawing.Point(96, 85); + this.textBox_remotePort1.Name = "textBox_remotePort1"; + this.textBox_remotePort1.Size = new System.Drawing.Size(50, 21); + this.textBox_remotePort1.TabIndex = 10; + this.textBox_remotePort1.Text = "2222"; + // + // textBox_lanPort1 + // + this.textBox_lanPort1.Location = new System.Drawing.Point(191, 85); + this.textBox_lanPort1.Name = "textBox_lanPort1"; + this.textBox_lanPort1.Size = new System.Drawing.Size(50, 21); + this.textBox_lanPort1.TabIndex = 11; + this.textBox_lanPort1.Text = "22"; + // + // textBox_lanTunnel2 + // + this.textBox_lanTunnel2.Location = new System.Drawing.Point(10, 113); + this.textBox_lanTunnel2.Name = "textBox_lanTunnel2"; + this.textBox_lanTunnel2.Size = new System.Drawing.Size(50, 21); + this.textBox_lanTunnel2.TabIndex = 12; + this.textBox_lanTunnel2.Text = "ftp"; + // + // textBox_remotePort2 + // + this.textBox_remotePort2.Location = new System.Drawing.Point(96, 113); + this.textBox_remotePort2.Name = "textBox_remotePort2"; + this.textBox_remotePort2.Size = new System.Drawing.Size(50, 21); + this.textBox_remotePort2.TabIndex = 13; + this.textBox_remotePort2.Text = "2221"; + // + // textBox_lanPort2 + // + this.textBox_lanPort2.Location = new System.Drawing.Point(191, 112); + this.textBox_lanPort2.Name = "textBox_lanPort2"; + this.textBox_lanPort2.Size = new System.Drawing.Size(50, 21); + this.textBox_lanPort2.TabIndex = 14; + this.textBox_lanPort2.Text = "21"; // // Form_ngrok // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(284, 319); - this.Controls.Add(this.groupBox1); + this.ClientSize = new System.Drawing.Size(284, 346); this.Controls.Add(this.button_save); this.Controls.Add(this.button_cancel); this.Controls.Add(this.groupBox_Url); - this.Controls.Add(this.groupBox_ProtocolPort); + this.Controls.Add(this.groupBox_Ngrokserver); this.Controls.Add(this.groupBox_AuthToken); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Name = "Form_ngrok"; this.Text = "Ngrok设置"; this.groupBox_AuthToken.ResumeLayout(false); this.groupBox_AuthToken.PerformLayout(); - this.groupBox1.ResumeLayout(false); - this.groupBox1.PerformLayout(); + this.groupBox_Ngrokserver.ResumeLayout(false); + this.groupBox_Ngrokserver.PerformLayout(); + this.groupBox_Url.ResumeLayout(false); + this.groupBox_Url.PerformLayout(); this.ResumeLayout(false); } @@ -139,12 +371,34 @@ #endregion private System.Windows.Forms.GroupBox groupBox_AuthToken; - private System.Windows.Forms.GroupBox groupBox_ProtocolPort; + private System.Windows.Forms.GroupBox groupBox_Ngrokserver; private System.Windows.Forms.GroupBox groupBox_Url; private System.Windows.Forms.Button button_cancel; private System.Windows.Forms.Button button_save; private System.Windows.Forms.TextBox textBox_AuthToken; - private System.Windows.Forms.GroupBox groupBox1; - private System.Windows.Forms.TextBox textBox_NgrokDownloadUrl; + private System.Windows.Forms.TextBox textBox_httpsAddr; + private System.Windows.Forms.TextBox textBox_httpAddr; + private System.Windows.Forms.TextBox textBox_tunnelAddr; + private System.Windows.Forms.TextBox textBox1; + private System.Windows.Forms.Label label_https; + private System.Windows.Forms.Label label_http; + private System.Windows.Forms.Label label_Defaultport; + private System.Windows.Forms.Label label_tunnelAddr; + private System.Windows.Forms.Label label_NgrokDomain; + private System.Windows.Forms.TextBox textBox_Https; + private System.Windows.Forms.TextBox textBox_lanHttp; + private System.Windows.Forms.TextBox textBox_subDomain; + private System.Windows.Forms.Label label_lanhttps; + private System.Windows.Forms.Label label_lanHttp; + private System.Windows.Forms.Label label_subDomain; + private System.Windows.Forms.TextBox textBox_lanPort2; + private System.Windows.Forms.TextBox textBox_remotePort2; + private System.Windows.Forms.TextBox textBox_lanTunnel2; + private System.Windows.Forms.TextBox textBox_lanPort1; + private System.Windows.Forms.TextBox textBox_remotePort1; + private System.Windows.Forms.TextBox textBox_lanTunnel1; + private System.Windows.Forms.Label label_lanport; + private System.Windows.Forms.Label label_remoteport; + private System.Windows.Forms.Label label_lanTcp; } } \ No newline at end of file diff --git a/net.nutcore.aliddns/net.nutcore.aliddns/net.nutcore.aliddns.csproj b/net.nutcore.aliddns/net.nutcore.aliddns/net.nutcore.aliddns.csproj index 6d7593e..56ee17c 100644 --- a/net.nutcore.aliddns/net.nutcore.aliddns/net.nutcore.aliddns.csproj +++ b/net.nutcore.aliddns/net.nutcore.aliddns/net.nutcore.aliddns.csproj @@ -97,8 +97,10 @@ main.cs + + Form_About.cs diff --git a/net.nutcore.aliddns/net.nutcore.aliddns/ngrok.cs b/net.nutcore.aliddns/net.nutcore.aliddns/ngrok.cs new file mode 100644 index 0000000..b77218f --- /dev/null +++ b/net.nutcore.aliddns/net.nutcore.aliddns/ngrok.cs @@ -0,0 +1,191 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace net.nutcore.aliddns +{ + class ngrok + { + private static readonly string NgrokExecutable = "ngrok.exe"; + private static readonly string NgrokYaml = "ngrok.yaml"; + public static readonly string CurrentDirectory = Path.GetDirectoryName(Application.ExecutablePath); + public static readonly string FileNgrokExecutable = Path.Combine(CurrentDirectory, NgrokExecutable); + public static readonly string FileConfig = Path.Combine(CurrentDirectory, NgrokYaml); + private static string LocalHost = "localhost:4040"; + + public class Config + { + public string authtoken { get; set; } + public string region { get; set; } + public bool console_ui { get; set; } + public string log_level { get; set; } + public string log_format { get; set; } + public string log { get; set; } + public string web_addr { get; set; } + public bool run_website { get; set; } + public bool run_ssh { get; set; } + public Tunnel tunnels { get; set; } + } + + public class Tunnel + { + public Protocol website { get; set; } + public Protocol ssh { get; set; } + } + + public class Protocol + { + public int addr { get; set; } + public string proto { get; set; } + public string auth { get; set; } + } + + public class Response + { + public JsonTunnel[] tunnels { get; set; } + } + + public class JsonTunnel + { + public string name { get; set; } + public string public_url { get; set; } + public string proto { get; set; } + } + + public void Ngrok() + { + if (!File.Exists(FileConfig)) + { + var config = new Config + { + authtoken = string.Empty, + console_ui = true, + region = "us", + log_level = "info", + log_format = "logfmt", + log = "ngrok.log", + web_addr = LocalHost, + run_website = true, + run_ssh = false, + tunnels = new Tunnel + { + website = new Protocol + { + addr = 80, + proto = "http" + }, + ssh = new Protocol + { + addr = 22, + proto = "tcp" + } + } + }; + + var serializer = new SerializerBuilder().Build(); + var yaml = serializer.Serialize(config); + File.WriteAllText(FileConfig, yaml); + } + } + + public bool IsExists() + { + return File.Exists(FileNgrokExecutable); + } + + public Response GetResponse() + { + try + { + using (WebClient web = new WebClient()) + { + var content = web.DownloadString($"http://{LocalHost}/api/tunnels"); + return JsonConvert.DeserializeObject(content); + } + } + catch + { + return null; + } + } + + public Config Load() + { + var yaml = File.ReadAllText(FileConfig); + var deserializer = new DeserializerBuilder().Build(); + var config = deserializer.Deserialize(yaml); + + LocalHost = config.web_addr; + return config; + } + + public void Save(string token, int http, int tcp, bool website, bool ssh) + { + var config = Load(); + config.authtoken = token; + config.tunnels.website.addr = http; + config.tunnels.ssh.addr = tcp; + config.run_website = website; + config.run_ssh = ssh; + + var serializer = new SerializerBuilder().Build(); + var yaml = serializer.Serialize(config); + File.WriteAllText(FileConfig, yaml); + } + + public async Task Start(int code = 0) + { + var exec = new ProcessStartInfo(); + exec.WorkingDirectory = CurrentDirectory; + exec.FileName = NgrokExecutable; + exec.CreateNoWindow = true; + exec.UseShellExecute = false; + exec.Arguments = $"start -config \"{NgrokYaml}\" "; + + switch (code) + { + case 1: + exec.Arguments += "website"; + break; + + case 2: + exec.Arguments += "ssh"; + break; + + default: + exec.Arguments += "website ssh"; + break; + } + + try + { + await Task.Run(() => + { + var proc = Process.Start(exec); + proc.WaitForExit(); + proc.Dispose(); + }); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + } + + public async Task Stop() + { + await Task.Run(() => + { + Process[] pList = Process.GetProcessesByName("Ngrok"); + foreach (Process p in pList) + { + Console.WriteLine($"Kill: {p.Id}"); + p.Kill(); + p.WaitForExit(); + p.Dispose(); + } + }); + } + } +} diff --git a/net.nutcore.aliddns/net.nutcore.aliddns/yml.cs b/net.nutcore.aliddns/net.nutcore.aliddns/yml.cs new file mode 100644 index 0000000..1d572ec --- /dev/null +++ b/net.nutcore.aliddns/net.nutcore.aliddns/yml.cs @@ -0,0 +1,244 @@ +using System; +using System.IO; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace net.nutcore.aliddns +{ + class YML + { + // 所有行 + private String[] lines; + // 格式化为节点 + private List nodeList = new List(); + // 文件所在地址 + private String path; + + public YML(String path) + { + this.path = path; + this.lines = File.ReadAllLines(path); + + for (int i = 0; i < lines.Length; i++) + { + String line = lines[i]; + if (line.Trim() == "") + { + Console.WriteLine("空白行,行号:" + (i + 1)); + continue; + } + else if (line.Trim().Substring(0, 1) == "#") + { + Console.WriteLine("注释行,行号:" + (i + 1)); + continue; + } + + String[] kv = Regex.Split(line, ":", RegexOptions.IgnoreCase); + findPreSpace(line); + Node node = new Node(); + node.space = findPreSpace(line); + node.name = kv[0].Trim(); + + // 去除前后空白符 + String fline = line.Trim(); + int first = fline.IndexOf(':'); + node.value = first == fline.Length - 1 ? null : fline.Substring(first + 2, fline.Length - first - 2); + node.parent = findParent(node.space); + nodeList.Add(node); + } + + this.formatting(); + } + + // 修改值 允许key为多级 例如:spring.datasource.url + public void modify(String key, String value) + { + Node node = findNodeByKey(key); + if (node != null) + { + node.value = value; + } + } + + // 读取值 + public String read(String key, String value) + { + Node node = findNodeByKey(key); + if (node != null) + { + return node.value; + } + return null; + } + + // 根据key找节点 + private Node findNodeByKey(String key) + { + String[] ks = key.Split('.'); + for (int i = 0; i < nodeList.Count; i++) + { + Node node = nodeList[i]; + if (node.name == ks[ks.Length - 1]) + { + // 判断父级 + Node tem = node; + // 统计匹配到的次数 + int count = 1; + for (int j = ks.Length - 2; j >= 0; j--) + { + if (tem.parent.name == ks[j]) + { + count++; + // 继续检查父级 + tem = tem.parent; + } + } + + if (count == ks.Length) + { + return node; + } + } + } + return null; + } + + // 保存到文件中 + public void save() + { + StreamWriter stream = File.CreateText(this.path); + for (int i = 0; i < nodeList.Count; i++) + { + Node node = nodeList[i]; + StringBuilder sb = new StringBuilder(); + // 放入前置空格 + for (int j = 0; j < node.tier; j++) + { + sb.Append(" "); + } + sb.Append(node.name); + sb.Append(": "); + if (node.value != null) + { + sb.Append(node.value); + } + stream.WriteLine(sb.ToString()); + } + stream.Flush(); + stream.Close(); + } + + // 格式化 + public void formatting() + { + // 先找出根节点 + List parentNode = new List(); + for (int i = 0; i < nodeList.Count; i++) + { + Node node = nodeList[i]; + if (node.parent == null) + { + parentNode.Add(node); + } + } + + List fNodeList = new List(); + // 遍历根节点 + for (int i = 0; i < parentNode.Count; i++) + { + Node node = parentNode[i]; + fNodeList.Add(node); + findChildren(node, fNodeList); + } + + Console.WriteLine("完成"); + + // 指针指向格式化后的 + nodeList = fNodeList; + } + + + // 层级 + int tier = 0; + // 查找孩子并进行分层 + private void findChildren(Node node, List fNodeList) + { + // 当前层 默认第一级,根在外层进行操作 + tier++; + + for (int i = 0; i < nodeList.Count; i++) + { + Node item = nodeList[i]; + if (item.parent == node) + { + item.tier = tier; + fNodeList.Add(item); + findChildren(item, fNodeList); + } + } + + // 走出一层 + tier--; + } + + //查找前缀空格数量 + private int findPreSpace(String str) + { + List chars = str.ToList(); + int count = 0; + foreach (char c in chars) + { + if (c == ' ') + { + count++; + } + else + { + break; + } + } + return count; + } + + // 根据缩进找上级 + private Node findParent(int space) + { + + if (nodeList.Count == 0) + { + return null; + } + else + { + // 倒着找上级 + for (int i = nodeList.Count - 1; i >= 0; i--) + { + Node node = nodeList[i]; + if (node.space < space) + { + return node; + } + } + // 如果没有找到 返回null + return null; + } + } + + // 私有节点类 + private class Node + { + // 名称 + public String name { get; set; } + // 值 + public String value { get; set; } + // 父级 + public Node parent { get; set; } + // 前缀空格 + public int space { get; set; } + // 所属层级 + public int tier { get; set; } + } + } +}