diff --git a/DSCPManager.sln b/DSCPManager.sln new file mode 100644 index 0000000..c8fffd5 --- /dev/null +++ b/DSCPManager.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34511.84 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DSCPManager", "DSCPManager\DSCPManager.csproj", "{FEEDF595-F8DA-40F1-A769-1EA58CA65801}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {FEEDF595-F8DA-40F1-A769-1EA58CA65801}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FEEDF595-F8DA-40F1-A769-1EA58CA65801}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FEEDF595-F8DA-40F1-A769-1EA58CA65801}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FEEDF595-F8DA-40F1-A769-1EA58CA65801}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {EE282990-E2CB-40E8-87BA-AEE26459C086} + EndGlobalSection +EndGlobal diff --git a/DSCPManager/DSCPManager.csproj b/DSCPManager/DSCPManager.csproj new file mode 100644 index 0000000..9cd859c --- /dev/null +++ b/DSCPManager/DSCPManager.csproj @@ -0,0 +1,18 @@ + + + + zh-Hans + WinExe + net8.0-windows + enable + true + enable + app.manifest + x64 + + + + + + + \ No newline at end of file diff --git a/DSCPManager/Form1.Designer.cs b/DSCPManager/Form1.Designer.cs new file mode 100644 index 0000000..6019284 --- /dev/null +++ b/DSCPManager/Form1.Designer.cs @@ -0,0 +1,478 @@ +namespace DSCPManager +{ + partial class Form1 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + panel1 = new Panel(); + button5 = new Button(); + button4 = new Button(); + button3 = new Button(); + comboBox2 = new ComboBox(); + textBox5 = new TextBox(); + label1 = new Label(); + label7 = new Label(); + textBox8 = new TextBox(); + label10 = new Label(); + label11 = new Label(); + textBox7 = new TextBox(); + panel2 = new Panel(); + button1 = new Button(); + textBox3 = new TextBox(); + label5 = new Label(); + label4 = new Label(); + textBox2 = new TextBox(); + label3 = new Label(); + textBox1 = new TextBox(); + label2 = new Label(); + panel3 = new Panel(); + button6 = new Button(); + richTextBox1 = new RichTextBox(); + label12 = new Label(); + button2 = new Button(); + textBox4 = new TextBox(); + label6 = new Label(); + label8 = new Label(); + textBox6 = new TextBox(); + label9 = new Label(); + panel4 = new Panel(); + button7 = new Button(); + label15 = new Label(); + textBox10 = new TextBox(); + label16 = new Label(); + panel1.SuspendLayout(); + panel2.SuspendLayout(); + panel3.SuspendLayout(); + panel4.SuspendLayout(); + SuspendLayout(); + // + // panel1 + // + panel1.BorderStyle = BorderStyle.FixedSingle; + panel1.Controls.Add(button5); + panel1.Controls.Add(button4); + panel1.Controls.Add(button3); + panel1.Controls.Add(comboBox2); + panel1.Controls.Add(textBox5); + panel1.Controls.Add(label1); + panel1.Controls.Add(label7); + panel1.Controls.Add(textBox8); + panel1.Controls.Add(label10); + panel1.Controls.Add(label11); + panel1.Controls.Add(textBox7); + panel1.Location = new Point(36, 44); + panel1.Name = "panel1"; + panel1.Size = new Size(268, 372); + panel1.TabIndex = 0; + // + // button5 + // + button5.Location = new Point(130, 306); + button5.Name = "button5"; + button5.Size = new Size(101, 40); + button5.TabIndex = 17; + button5.Text = "删除"; + button5.UseVisualStyleBackColor = true; + button5.Click += button5_Click; + // + // button4 + // + button4.Location = new Point(141, 39); + button4.Name = "button4"; + button4.Size = new Size(90, 25); + button4.TabIndex = 16; + button4.Text = "读取全部策略"; + button4.UseVisualStyleBackColor = true; + button4.Click += button4_Click; + // + // button3 + // + button3.Location = new Point(14, 306); + button3.Name = "button3"; + button3.Size = new Size(101, 40); + button3.TabIndex = 15; + button3.Text = "修改"; + button3.UseVisualStyleBackColor = true; + button3.Click += button3_Click; + // + // comboBox2 + // + comboBox2.FormattingEnabled = true; + comboBox2.Location = new Point(14, 39); + comboBox2.Name = "comboBox2"; + comboBox2.Size = new Size(121, 25); + comboBox2.TabIndex = 2; + comboBox2.SelectedIndexChanged += comboBox2_SelectedIndexChanged; + // + // textBox5 + // + textBox5.Location = new Point(119, 199); + textBox5.Name = "textBox5"; + textBox5.Size = new Size(100, 23); + textBox5.TabIndex = 14; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(0, 0); + label1.Name = "label1"; + label1.Size = new Size(87, 17); + label1.TabIndex = 1; + label1.Text = "调整DSCP规则"; + // + // label7 + // + label7.AutoSize = true; + label7.Location = new Point(22, 199); + label7.Name = "label7"; + label7.Size = new Size(51, 17); + label7.TabIndex = 13; + label7.Text = "DSCP值"; + // + // textBox8 + // + textBox8.Enabled = false; + textBox8.Location = new Point(119, 90); + textBox8.Name = "textBox8"; + textBox8.Size = new Size(100, 23); + textBox8.TabIndex = 9; + // + // label10 + // + label10.AutoSize = true; + label10.Location = new Point(17, 147); + label10.Name = "label10"; + label10.Size = new Size(68, 17); + label10.TabIndex = 12; + label10.Text = "规则程序名"; + // + // label11 + // + label11.AutoSize = true; + label11.Location = new Point(17, 93); + label11.Name = "label11"; + label11.Size = new Size(56, 17); + label11.TabIndex = 10; + label11.Text = "规则名称"; + // + // textBox7 + // + textBox7.Location = new Point(119, 144); + textBox7.Name = "textBox7"; + textBox7.Size = new Size(100, 23); + textBox7.TabIndex = 11; + // + // panel2 + // + panel2.BorderStyle = BorderStyle.FixedSingle; + panel2.Controls.Add(button1); + panel2.Controls.Add(textBox3); + panel2.Controls.Add(label5); + panel2.Controls.Add(label4); + panel2.Controls.Add(textBox2); + panel2.Controls.Add(label3); + panel2.Controls.Add(textBox1); + panel2.Controls.Add(label2); + panel2.Location = new Point(310, 44); + panel2.Name = "panel2"; + panel2.Size = new Size(268, 372); + panel2.TabIndex = 2; + // + // button1 + // + button1.Location = new Point(72, 259); + button1.Name = "button1"; + button1.Size = new Size(101, 40); + button1.TabIndex = 8; + button1.Text = "创建"; + button1.UseVisualStyleBackColor = true; + button1.Click += button1_Click; + // + // textBox3 + // + textBox3.Location = new Point(116, 148); + textBox3.Name = "textBox3"; + textBox3.Size = new Size(100, 23); + textBox3.TabIndex = 7; + // + // label5 + // + label5.AutoSize = true; + label5.Location = new Point(19, 148); + label5.Name = "label5"; + label5.Size = new Size(51, 17); + label5.TabIndex = 6; + label5.Text = "DSCP值"; + // + // label4 + // + label4.AutoSize = true; + label4.Location = new Point(14, 96); + label4.Name = "label4"; + label4.Size = new Size(68, 17); + label4.TabIndex = 5; + label4.Text = "规则程序名"; + // + // textBox2 + // + textBox2.Location = new Point(116, 93); + textBox2.Name = "textBox2"; + textBox2.Size = new Size(100, 23); + textBox2.TabIndex = 4; + // + // label3 + // + label3.AutoSize = true; + label3.Location = new Point(14, 42); + label3.Name = "label3"; + label3.Size = new Size(56, 17); + label3.TabIndex = 3; + label3.Text = "规则名称"; + // + // textBox1 + // + textBox1.Location = new Point(116, 39); + textBox1.Name = "textBox1"; + textBox1.Size = new Size(100, 23); + textBox1.TabIndex = 2; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(0, 0); + label2.Name = "label2"; + label2.Size = new Size(87, 17); + label2.TabIndex = 1; + label2.Text = "创建DSCP规则"; + // + // panel3 + // + panel3.BorderStyle = BorderStyle.FixedSingle; + panel3.Controls.Add(button6); + panel3.Controls.Add(richTextBox1); + panel3.Controls.Add(label12); + panel3.Controls.Add(button2); + panel3.Controls.Add(textBox4); + panel3.Controls.Add(label6); + panel3.Controls.Add(label8); + panel3.Controls.Add(textBox6); + panel3.Controls.Add(label9); + panel3.Location = new Point(584, 44); + panel3.Name = "panel3"; + panel3.Size = new Size(268, 372); + panel3.TabIndex = 9; + // + // button6 + // + button6.Location = new Point(79, 306); + button6.Name = "button6"; + button6.Size = new Size(101, 40); + button6.TabIndex = 9; + button6.Text = "批量创建"; + button6.UseVisualStyleBackColor = true; + button6.Click += button6_Click; + // + // richTextBox1 + // + richTextBox1.Location = new Point(19, 128); + richTextBox1.Name = "richTextBox1"; + richTextBox1.Size = new Size(226, 119); + richTextBox1.TabIndex = 10; + richTextBox1.Text = ""; + // + // label12 + // + label12.AutoSize = true; + label12.Location = new Point(19, 108); + label12.Name = "label12"; + label12.Size = new Size(100, 17); + label12.TabIndex = 9; + label12.Text = "搜索到的exe程序"; + // + // button2 + // + button2.Location = new Point(28, 259); + button2.Name = "button2"; + button2.Size = new Size(206, 40); + button2.TabIndex = 8; + button2.Text = "通过路径扫描应用程序"; + button2.UseVisualStyleBackColor = true; + button2.Click += button2_Click; + // + // textBox4 + // + textBox4.Location = new Point(116, 71); + textBox4.Name = "textBox4"; + textBox4.Size = new Size(100, 23); + textBox4.TabIndex = 7; + // + // label6 + // + label6.AutoSize = true; + label6.Location = new Point(19, 74); + label6.Name = "label6"; + label6.Size = new Size(51, 17); + label6.TabIndex = 6; + label6.Text = "DSCP值"; + // + // label8 + // + label8.AutoSize = true; + label8.Location = new Point(19, 39); + label8.Name = "label8"; + label8.Size = new Size(32, 17); + label8.TabIndex = 3; + label8.Text = "前缀"; + // + // textBox6 + // + textBox6.Location = new Point(116, 33); + textBox6.Name = "textBox6"; + textBox6.PlaceholderText = "例如APEX-"; + textBox6.Size = new Size(100, 23); + textBox6.TabIndex = 2; + // + // label9 + // + label9.AutoSize = true; + label9.Location = new Point(0, 0); + label9.Name = "label9"; + label9.Size = new Size(111, 17); + label9.TabIndex = 1; + label9.Text = "批量创建DSCP规则"; + // + // panel4 + // + panel4.BorderStyle = BorderStyle.FixedSingle; + panel4.Controls.Add(button7); + panel4.Controls.Add(label15); + panel4.Controls.Add(textBox10); + panel4.Controls.Add(label16); + panel4.Location = new Point(858, 45); + panel4.Name = "panel4"; + panel4.Size = new Size(268, 372); + panel4.TabIndex = 11; + // + // button7 + // + button7.Location = new Point(73, 124); + button7.Name = "button7"; + button7.Size = new Size(101, 40); + button7.TabIndex = 9; + button7.Text = "批量删除"; + button7.UseVisualStyleBackColor = true; + button7.Click += button7_Click; + // + // label15 + // + label15.AutoSize = true; + label15.Location = new Point(19, 39); + label15.Name = "label15"; + label15.Size = new Size(32, 17); + label15.TabIndex = 3; + label15.Text = "前缀"; + // + // textBox10 + // + textBox10.Location = new Point(116, 33); + textBox10.Name = "textBox10"; + textBox10.PlaceholderText = "例如APEX-"; + textBox10.Size = new Size(100, 23); + textBox10.TabIndex = 2; + // + // label16 + // + label16.AutoSize = true; + label16.Location = new Point(0, 0); + label16.Name = "label16"; + label16.Size = new Size(111, 17); + label16.TabIndex = 1; + label16.Text = "批量删除DSCP规则"; + // + // Form1 + // + AutoScaleDimensions = new SizeF(7F, 17F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1141, 450); + Controls.Add(panel4); + Controls.Add(panel3); + Controls.Add(panel2); + Controls.Add(panel1); + Name = "Form1"; + Text = "DSCPManager"; + Load += Form1_Load; + panel1.ResumeLayout(false); + panel1.PerformLayout(); + panel2.ResumeLayout(false); + panel2.PerformLayout(); + panel3.ResumeLayout(false); + panel3.PerformLayout(); + panel4.ResumeLayout(false); + panel4.PerformLayout(); + ResumeLayout(false); + } + + #endregion + + private Panel panel1; + private Label label1; + private Panel panel2; + private Label label2; + private ComboBox comboBox2; + private TextBox textBox3; + private Label label5; + private Label label4; + private TextBox textBox2; + private Label label3; + private TextBox textBox1; + private Button button1; + private Panel panel3; + private Button button2; + private TextBox textBox4; + private Label label6; + private Label label8; + private TextBox textBox6; + private Label label9; + private Button button3; + private TextBox textBox5; + private Label label7; + private TextBox textBox8; + private Label label10; + private Label label11; + private TextBox textBox7; + private Button button4; + private Button button5; + private RichTextBox richTextBox1; + private Label label12; + private Button button6; + private Panel panel4; + private Button button7; + private Label label15; + private TextBox textBox10; + private Label label16; + } +} diff --git a/DSCPManager/Form1.cs b/DSCPManager/Form1.cs new file mode 100644 index 0000000..bb6044f --- /dev/null +++ b/DSCPManager/Form1.cs @@ -0,0 +1,122 @@ +using System.Security.Cryptography; + +namespace DSCPManager +{ + public partial class Form1 : Form + { + public Form1() + { + InitializeComponent(); + } + + private void Form1_Load(object sender, EventArgs e) + { + comboBox2.DataSource = QosOps.qosPoliciesNameList; + } + + private void button4_Click(object sender, EventArgs e) + { + RefreshList(); + } + + private void button3_Click(object sender, EventArgs e) + { + QosOps.UpdateQosPolicy(textBox8.Text, "AppPathNameMatchCondition", textBox7.Text); + QosOps.UpdateQosPolicy(textBox8.Text, "DSCPAction", textBox5.Text); + RefreshList(); + } + + private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) + { + if (comboBox2.SelectedValue != null) + { + var policy = QosOps.qosPoliciesList.First(x => x.PolicyName == comboBox2.SelectedValue); + textBox8.Text = policy.PolicyName; + textBox7.Text = policy.AppPathName; + textBox5.Text = policy.DSCPValue; + } + } + + private void button1_Click(object sender, EventArgs e) + { + QosPolicy newPolicy = new QosPolicy(textBox1.Text, textBox2.Text, textBox3.Text); + QosOps.CreateQosPolicy(newPolicy); + RefreshList(); + } + private void RefreshList() + { + QosOps.ReadQosPolicy(); + comboBox2.DataSource = null; + comboBox2.DataSource = QosOps.qosPoliciesNameList; + } + + private void button5_Click(object sender, EventArgs e) + { + QosOps.DeleteQosPolicy(textBox8.Text); + RefreshList(); + } + + private void button2_Click(object sender, EventArgs e) + { + + FolderBrowserDialog dialog = new FolderBrowserDialog(); + dialog.Description = "ѡһĿ¼Ϊ·"; + dialog.ShowNewFolderButton = true; + dialog.RootFolder = Environment.SpecialFolder.ApplicationData; + DialogResult result = dialog.ShowDialog(); + if (result == DialogResult.OK) + { + string selectedPath = dialog.SelectedPath; + string[] files = Directory.GetFiles(selectedPath, "*.exe", SearchOption.AllDirectories); + HashSet list = [.. files]; + richTextBox1.Text = ""; + foreach (string file in list) + { + int lastBackslashIndex = file.LastIndexOf('\\'); + string fileName = file.Substring(lastBackslashIndex + 1); + richTextBox1.Text += fileName; + richTextBox1.Text += "\n"; + } + } + + } + + static string GenerateRandomString(int length) + { + const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + using (var rng = RandomNumberGenerator.Create()) + { + byte[] data = new byte[length]; + rng.GetBytes(data); + return new string(data.Select(b => chars[b % chars.Length]).ToArray()); + } + } + private void button6_Click(object sender, EventArgs e) + { + var files = richTextBox1.Text.Split("\n"); + foreach (string file in files) + { + if (file == "") + { + continue; + } + QosPolicy newPolicy = new QosPolicy(textBox6.Text + GenerateRandomString(8), file, textBox4.Text); + QosOps.CreateQosPolicy(newPolicy); + } + RefreshList(); + } + + private void button7_Click(object sender, EventArgs e) + { + QosOps.ReadQosPolicy(); + foreach (var i in QosOps.qosPoliciesNameList) + { + if (i.StartsWith(textBox10.Text)) + { + QosOps.DeleteQosPolicy(i); + } + } + RefreshList(); + } + } +} diff --git a/DSCPManager/Form1.resx b/DSCPManager/Form1.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/DSCPManager/Form1.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/DSCPManager/Program.cs b/DSCPManager/Program.cs new file mode 100644 index 0000000..5120e02 --- /dev/null +++ b/DSCPManager/Program.cs @@ -0,0 +1,17 @@ +namespace DSCPManager +{ + internal static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + // To customize application configuration such as set high DPI settings or default font, + // see https://aka.ms/applicationconfiguration. + ApplicationConfiguration.Initialize(); + Application.Run(new Form1()); + } + } +} \ No newline at end of file diff --git a/DSCPManager/QosOps.cs b/DSCPManager/QosOps.cs new file mode 100644 index 0000000..c027a2a --- /dev/null +++ b/DSCPManager/QosOps.cs @@ -0,0 +1,110 @@ +using System.Management.Automation; +using System.Management.Automation.Runspaces; + +namespace DSCPManager +{ + public static class QosOps + { + public static List qosPoliciesList = new List(); + public static string[] qosPoliciesNameList = Array.Empty(); + public static void ReadQosPolicy() + { + InitialSessionState initial = InitialSessionState.CreateDefault(); + initial.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted; + Runspace runspace = RunspaceFactory.CreateRunspace(initial); + runspace.Open(); + PowerShell ps = PowerShell.Create(); + ps.Runspace = runspace; + ps.AddCommand("Get-NetQosPolicy"); + var policyList = ps.Invoke(); + + + foreach (PSObject item in policyList) + { + string policyName = item.Properties["Name"].Value.ToString(); + string appPathName = ""; + string DSCPValue = ""; + + if (item.Properties["AppPathNameMatchCondition"].Value != null) + { + appPathName = item.Properties["AppPathNameMatchCondition"].Value.ToString(); + } + if (item.Properties["DSCPAction"].Value != null) + { + DSCPValue = item.Properties["DSCPAction"].Value.ToString(); + } + + if (policyName != null && appPathName != null && DSCPValue != null) + { + var policy = new QosPolicy(policyName, appPathName, DSCPValue); + qosPoliciesList.Add(policy); + } + } + UpdateNameList(); + } + public static void DeleteQosPolicy(string name) + { + InitialSessionState initial = InitialSessionState.CreateDefault(); + initial.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted; + Runspace runspace = RunspaceFactory.CreateRunspace(initial); + runspace.Open(); + PowerShell ps = PowerShell.Create(); + ps.Runspace = runspace; + qosPoliciesList.Clear(); + ps.AddCommand("Remove-NetQosPolicy") + .AddParameter("Name", name) + .AddParameter("Confirm", false); + var result = ps.Invoke(); + MessageBox.Show("删除成功"); + } + public static void UpdateQosPolicy(string name, string key, string value) + { + InitialSessionState initial = InitialSessionState.CreateDefault(); + initial.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted; + Runspace runspace = RunspaceFactory.CreateRunspace(initial); + runspace.Open(); + PowerShell ps = PowerShell.Create(); + ps.Runspace = runspace; + qosPoliciesList.Clear(); + ps.AddCommand("Set-NetQosPolicy") + .AddParameter("Name", name) + .AddParameter(key, value); + var result = ps.Invoke(); + MessageBox.Show("修改成功"); + } + public static void CreateQosPolicy(QosPolicy policy) + { + InitialSessionState initial = InitialSessionState.CreateDefault(); + initial.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted; + Runspace runspace = RunspaceFactory.CreateRunspace(initial); + runspace.Open(); + PowerShell ps = PowerShell.Create(); + ps.Runspace = runspace; + qosPoliciesList.Clear(); + ps.AddCommand("New-NetQosPolicy") + .AddParameter("Name", policy.PolicyName) + .AddParameter("AppPathNameMatchCondition", policy.AppPathName) + .AddParameter("DSCP", policy.DSCPValue); + var result = ps.Invoke(); + MessageBox.Show("创建成功"); + } + static void UpdateNameList() + { + qosPoliciesNameList = qosPoliciesList.Select(p => p.PolicyName).ToArray(); + } + } + + public class QosPolicy + { + public QosPolicy(string policyName, string appPathName, string DSCPValue) + { + this.PolicyName = policyName; + this.AppPathName = appPathName; + this.DSCPValue = DSCPValue; + } + + public string PolicyName { get; set; } + public string AppPathName { get; set; } + public string DSCPValue { get; set; } + } +} diff --git a/DSCPManager/app.manifest b/DSCPManager/app.manifest new file mode 100644 index 0000000..438ee0d --- /dev/null +++ b/DSCPManager/app.manifest @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +