Skip to content

Commit

Permalink
Merge pull request #6 from Cp0204/master
Browse files Browse the repository at this point in the history
可注册到系统服务(需管理员权限运行开启,实现免登录自启)
  • Loading branch information
codemonkey-m authored Jun 27, 2020
2 parents f33c7b9 + 0fcbb1a commit 52f4fe8
Show file tree
Hide file tree
Showing 8 changed files with 647 additions and 15 deletions.
2 changes: 0 additions & 2 deletions FrpClient-Win/App.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>

<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
Expand All @@ -18,5 +17,4 @@
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
</entityFramework>

</configuration>
31 changes: 27 additions & 4 deletions FrpClient-Win/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions FrpClient-Win/Form1.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Security.Principal;
using System.Windows.Forms;
using Microsoft.Win32;

Expand Down Expand Up @@ -41,6 +42,24 @@ private void OnMainFormLoad(object sender, EventArgs e)
Application.Exit();
}

//检查是否管理员身份
WindowsPrincipal winPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
bool isAdmin = winPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
if(isAdmin) {
ProcOutput.AppendText("启动身份:管理员" + "\r\n");
// 判断服务状态
if(ServiceHelper.Status(strRegName).ToString() == "NotExist") {
AutoRunService.Checked = RestartService2.Enabled = false;
} else {
AutoRunService.Checked = true;
ProcOutput.AppendText($"已注册到系统服务,当前状态:{ServiceHelper.Status(strRegName).ToString()}" + "\r\n");
}
} else {
AutoRunService.Enabled = false;
RestartService2.Enabled = false;
ProcOutput.AppendText("启动身份:非管理员" + "\r\n");
}

InitList(true);
UpdateStartButton();
//notifyIcon.Icon = this.Icon;
Expand Down Expand Up @@ -235,5 +254,33 @@ private void About_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://github.com/codemonkey-m/FrpClient-Win");
}

private void AutoRunService_Click(object sender, EventArgs e) {
if(ServiceHelper.Status(strRegName).ToString() == "NotExist") {
//安装
ServiceHelper.Install(
strRegName, // 服务名
strRegName, // 显示名称
@"""" + Application.ExecutablePath + @""" service", // 映像路径,可带参数,若路径有空格,需给路径(不含参数)套上双引号
"Frp Windows客户端", // 服务描述
ServiceStartType.Auto, // 启动类型
ServiceAccount.LocalSystem, // 运行帐户,可选,默认是LocalSystem,即至尊帐户
null // 依赖服务,要填服务名称,没有则为null或空数组,可选
);
AutoRunService.Checked = RestartService2.Enabled = true;
ServiceHelper.Restart(strRegName);
ProcOutput.AppendText("已注册到系统服务,并启动(控制台无输出)..." + "\r\n");
} else {
//卸载
ServiceHelper.Uninstall(strRegName);
AutoRunService.Checked = RestartService2.Enabled = false;
ProcOutput.AppendText("已删除系统服务,因系统机制,如重新注册需重启本程序(或exploere.exe)..." + "\r\n");
}
}

private void RestartService2_Click(object sender, EventArgs e) {
ServiceHelper.Restart(strRegName);
ProcOutput.AppendText($"已重启系统服务,当前状态:{ServiceHelper.Status(strRegName).ToString()}(控制台无输出)..." + "\r\n");
}
}
}
9 changes: 8 additions & 1 deletion FrpClient-Win/FrpClient-Win.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -55,6 +56,12 @@
<Compile Include="AddItemDlg.Designer.cs">
<DependentUpon>AddItemDlg.cs</DependentUpon>
</Compile>
<Compile Include="autorunService.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="autorunService.Designer.cs">
<DependentUpon>autorunService.cs</DependentUpon>
</Compile>
<Compile Include="DB.cs" />
<Compile Include="Form1.cs">
<SubType>Form</SubType>
Expand All @@ -70,6 +77,7 @@
<Compile Include="ServerConfigDlg.Designer.cs">
<DependentUpon>ServerConfigDlg.cs</DependentUpon>
</Compile>
<Compile Include="ServiceHelper.cs" />
<EmbeddedResource Include="AddItemDlg.resx">
<DependentUpon>AddItemDlg.cs</DependentUpon>
</EmbeddedResource>
Expand Down Expand Up @@ -102,7 +110,6 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Content Include="frp.ico" />
<Content Include="R.ico" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
20 changes: 12 additions & 8 deletions FrpClient-Win/Program.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ServiceProcess;
using System.Windows.Forms;

namespace FrpClient_Win
namespace FrpClient_Win
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
if(args.Length > 0 && args[0] == "service") {
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new autorunService() };
ServiceBase.Run(ServicesToRun);
} else {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
}
Loading

0 comments on commit 52f4fe8

Please sign in to comment.