using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net.NetworkInformation; using System.Windows.Forms; using System.Net; using System.Security.Cryptography; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Diagnostics; namespace Start { public partial class Main : Form { public Main() { InitializeComponent(); var physicalAddressList = NetworkInterface.GetAllNetworkInterfaces().Select(t => t.GetPhysicalAddress().ToString()); this.machineTextBox.Text = physicalAddressList.FirstOrDefault()?.ToString(); this.KeySecreteTextBox.Text = Md5($"{this.machineTextBox.Text}_XINGCANG"); } int apiPort = 0; private void connectButton_Click(object sender, EventArgs e) { string connectionString = $"Server={serverTextBox.Text};User Id={usernameTextBox.Text};Password={passwordTextBox.Text};"; SqlConnection connection = new SqlConnection(connectionString); try { connection.Open(); MessageBox.Show("连接成功!"); } catch (Exception ex) { MessageBox.Show($"连接失败:{ex.Message}"); } finally { connection.Close(); } } private void portBtn_Click(object sender, EventArgs e) { nginxPortTBox.Text.Trim(); var nginxPort = 0; if (int.TryParse(nginxPortTBox.Text, out nginxPort) == false || int.TryParse(apiPortTBox.Text, out apiPort) == false) { MessageBox.Show("请输入合法的端口"); } if (IsPortInUse(nginxPort)) { MessageBox.Show("nginx 设置的端口被占用,请选择其他端口"); } if (IsPortInUse(apiPort)) { MessageBox.Show("服务设置的端口被占用,请选择其他端口"); } } private static bool IsPortInUse(int port) { IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties(); IPEndPoint[] tcpEndPoints = ipProperties.GetActiveTcpListeners(); foreach (IPEndPoint endPoint in tcpEndPoints) { if (endPoint.Port == port) { return true; } } return false; } public static string Md5(string target) { using (MD5 md5 = MD5.Create()) { // MD5非线程安全 byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(target)); StringBuilder sb = new StringBuilder(32); for (int i = 0; i < bytes.Length; ++i) sb.Append(bytes[i].ToString("x2")); return sb.ToString(); } } private void activeBtn_Click(object sender, EventArgs e) { //string nginxPath = @"C:\nginx\nginx.exe"; //string workingDirectory = @"C:\nginx"; //string configFile = @"C:\nginx\conf\nginx.conf"; //ProcessStartInfo psi = new ProcessStartInfo(); //psi.FileName = nginxPath; //psi.WorkingDirectory = workingDirectory; //psi.Arguments = "-c \"" + configFile + "\""; //Process.Start(psi); var configObj = new { key = this.machineTextBox.Text, value = this.KeySecreteTextBox.Text.Trim(), user = usernameTextBox.Text, server = serverTextBox.Text, password = passwordTextBox.Text, database = string.Empty }; File.WriteAllText($@"C:\ProgramData\.xingcang\config.json", JsonConvert.SerializeObject(configObj)); var serviceConfig = File.ReadAllText("ServiceConfig.json"); var jObject = JObject.Parse(serviceConfig); var binPath = Path.Combine(AppContext.BaseDirectory, jObject["binPath"].ToString().TrimStart('/')); var createStr = $@"sc create {jObject["serviceName"].ToString()} binPath= ""{binPath} --urls=""http://127.0.0.1:{apiPort}"" --env CertificateApply"" DisplayName= ""{jObject["serviceDisplayName"].ToString()}"" start= auto"; #region 创建服务 Process process = new Process(); process.StartInfo.FileName = "cmd.exe"; process.StartInfo.RedirectStandardInput = true; process.StartInfo.UseShellExecute = false; process.Start(); //启动nginx process.StandardInput.WriteLine($"start {Path.Combine(AppContext.BaseDirectory, "nginx-1.4.7/nginx.exe")}" ); // 删除已存在的服务 process.StandardInput.WriteLine($"sc delete {jObject["serviceName"].ToString()}"); // 执行 sc create 命令来创建服务 process.StandardInput.WriteLine(createStr); process.Exited += (sender, e) => { int exitCode = process.ExitCode; if (exitCode == 0) { // 执行成功 MessageBox.Show("Batch file executed successfully."); } else { // 执行失败 MessageBox.Show("Batch file execution failed with error code: " + exitCode); } }; //执行数据库脚本 process.StandardInput.WriteLine(Path.Combine(AppContext.BaseDirectory, "sql/bin.bat")); // 执行 sc start 启动服务 process.StandardInput.WriteLine($"sc start {jObject["serviceName"].ToString()}"); // 关闭进程流并等待进程退出 process.StandardInput.Flush(); process.StandardInput.Close(); process.WaitForExit(); #endregion } } }