223 lines
6.8 KiB
C#
223 lines
6.8 KiB
C#
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 = 7100;
|
||
int vuePort = 9527;
|
||
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();
|
||
|
||
|
||
|
||
if (int.TryParse(nginxPortTBox.Text, out vuePort) == false || int.TryParse(apiPortTBox.Text, out apiPort) == false)
|
||
{
|
||
MessageBox.Show("请输入合法的端口");
|
||
}
|
||
|
||
if (IsPortInUse(vuePort))
|
||
{
|
||
MessageBox.Show("服务设置的前端端口被占用,请选择其他端口");
|
||
}
|
||
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)
|
||
{
|
||
|
||
#region 修改 nginx 配置文件 启动nginx
|
||
|
||
|
||
var nginxConfigPath = Path.Combine(AppContext.BaseDirectory, @$"nginx-1.20.1\conf\nginx.conf");
|
||
|
||
if (!File.Exists(nginxConfigPath))
|
||
{
|
||
MessageBox.Show("预设路径不存在nginx");
|
||
}
|
||
|
||
var nginxConfig = File.ReadAllText(nginxConfigPath);
|
||
|
||
nginxConfig= nginxConfig.Replace("9527", vuePort.ToString());
|
||
nginxConfig= nginxConfig.Replace("7100", apiPort.ToString());
|
||
|
||
|
||
|
||
File.WriteAllText(nginxConfigPath, nginxConfig);
|
||
|
||
|
||
|
||
// 获取nginx.exe所在的目录
|
||
string nginxPath = Path.Combine(AppContext.BaseDirectory, @$"nginx-1.20.1\");
|
||
// 创建ProcessStartInfo对象,指定要启动的可执行文件及其参数
|
||
ProcessStartInfo psi = new ProcessStartInfo(nginxPath + "nginx.exe");
|
||
// 指定工作目录,即进入nginx.exe所在的目录
|
||
psi.WorkingDirectory = nginxPath;
|
||
// 启动可执行文件
|
||
Process.Start(psi);
|
||
|
||
|
||
#endregion
|
||
|
||
|
||
|
||
|
||
|
||
|
||
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();
|
||
|
||
//process.StandardInput.WriteLine(Path.Combine(AppContext.BaseDirectory, "startNginx.bat"));
|
||
|
||
|
||
|
||
|
||
// 删除已存在的服务
|
||
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
|
||
}
|
||
}
|
||
}
|