Skip to content

Commit

Permalink
Change service name and description (#61)
Browse files Browse the repository at this point in the history
* Change service name and description

* Remove old service after upgrade
  • Loading branch information
koho committed May 30, 2023
1 parent d369849 commit 8302e3a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 40 deletions.
90 changes: 53 additions & 37 deletions installer/actions/actions/CustomAction.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using Microsoft.Deployment.WindowsInstaller;
using System;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Runtime.InteropServices;
using System.ServiceProcess;
Expand Down Expand Up @@ -90,6 +88,45 @@ public static int ForceDeleteDirectory(string path)
}
}

public static void RemoveServices(Session session, string prefix, string binPath)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController controller in services)
{
ManagementObject wmiService = new ManagementObject("Win32_Service.Name='" + controller.ServiceName + "'");
wmiService.Get();
string pathName = wmiService.GetPropertyValue("PathName").ToString();
string path1 = pathName.Substring(0, Math.Min(binPath.Length, pathName.Length));
string path2 = pathName.Substring(1, Math.Min(binPath.Length, pathName.Length - 1));
if ((binPath.ToLower().Equals(path1.ToLower()) || binPath.ToLower().Equals(path2.ToLower())) && controller.ServiceName.StartsWith(prefix))
{
try
{
controller.Stop();
controller.WaitForStatus(ServiceControllerStatus.Stopped);
}
catch (Exception)
{
session.Log("Failed to stop " + controller.ServiceName);
}

ServiceInstaller installer = new ServiceInstaller
{
Context = new InstallContext(),
ServiceName = controller.ServiceName
};
try
{
installer.Uninstall(null);
}
catch (Exception)
{
session.Log("Failed to uninstall " + controller.ServiceName);
}
}
}
}

[CustomAction]
public static ActionResult KillProcesses(Session session)
{
Expand Down Expand Up @@ -142,41 +179,7 @@ public static ActionResult EvaluateFrpServices(Session session)
{
return ActionResult.Success;
}
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController controller in services)
{
ManagementObject wmiService = new ManagementObject("Win32_Service.Name='" + controller.ServiceName + "'");
wmiService.Get();
string pathName = wmiService.GetPropertyValue("PathName").ToString();
string path1 = pathName.Substring(0, Math.Min(binPath.Length, pathName.Length));
string path2 = pathName.Substring(1, Math.Min(binPath.Length, pathName.Length - 1));
if (binPath.ToLower().Equals(path1.ToLower()) || binPath.ToLower().Equals(path2.ToLower()))
{
try
{
controller.Stop();
controller.WaitForStatus(ServiceControllerStatus.Stopped);
}
catch (Exception)
{
session.Log("Failed to stop " + controller.ServiceName);
}

ServiceInstaller installer = new ServiceInstaller
{
Context = new InstallContext(),
ServiceName = controller.ServiceName
};
try
{
installer.Uninstall(null);
}
catch (Exception)
{
session.Log("Failed to uninstall " + controller.ServiceName);
}
}
}
RemoveServices(session, "", binPath);
return ActionResult.Success;
}

Expand Down Expand Up @@ -245,5 +248,18 @@ public static ActionResult MoveFrpProfiles(Session session)
}
return ActionResult.Success;
}

[CustomAction]
public static ActionResult RemoveOldFrpServices(Session session)
{
session.Log("Remove old FRP Services");
string binPath = session["CustomActionData"];
if (string.IsNullOrEmpty(binPath))
{
return ActionResult.Success;
}
RemoveServices(session, "FRPC$", binPath);
return ActionResult.Success;
}
}
}
10 changes: 10 additions & 0 deletions installer/msi/frpmgr.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<CustomAction Id="RemoveFrpFiles.SetProperty" Return="check" Property="RemoveFrpFiles" Value="[INSTALLFOLDER]" />
<CustomAction Id="SetLangConfig.SetProperty" Return="check" Property="SetLangConfig" Value="[#LangConfig]" />
<CustomAction Id="MoveFrpProfiles.SetProperty" Return="check" Property="MoveFrpProfiles" Value="[INSTALLFOLDER]" />
<CustomAction Id="RemoveOldFrpServices.SetProperty" Return="check" Property="RemoveOldFrpServices" Value="[#MainApplication]" />

<!--
Launch application
Expand Down Expand Up @@ -160,5 +161,14 @@
<Custom Action="MoveFrpProfiles.SetProperty" After="InstallFiles" />
<Custom Action="MoveFrpProfiles" After="MoveFrpProfiles.SetProperty">NOT (REMOVE="ALL")</Custom>
</InstallExecuteSequence>

<!--
Delete old version frp services
-->
<CustomAction Id="RemoveOldFrpServices" BinaryKey="actions.dll" DllEntry="RemoveOldFrpServices" Impersonate="no" Execute="deferred" />
<InstallExecuteSequence>
<Custom Action="RemoveOldFrpServices.SetProperty" After="InstallFiles" />
<Custom Action="RemoveOldFrpServices" After="RemoveOldFrpServices.SetProperty">NOT (REMOVE="ALL")</Custom>
</InstallExecuteSequence>
</Fragment>
</Wix>
2 changes: 1 addition & 1 deletion services/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func InstallService(name string, configPath string, manual bool) error {
StartType: mgr.StartAutomatic,
ErrorControl: mgr.ErrorNormal,
DisplayName: DisplayNameOfClient(name),
Description: "FRP Client Daemon Service",
Description: "FRP Runtime Service for FRP Manager.",
SidType: windows.SERVICE_SID_TYPE_UNRESTRICTED,
}
if manual {
Expand Down
5 changes: 3 additions & 2 deletions services/service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package services

import (
"crypto/md5"
"fmt"
"os"
"path/filepath"
Expand All @@ -14,11 +15,11 @@ import (
)

func ServiceNameOfClient(name string) string {
return fmt.Sprintf("FRPC$%s", name)
return fmt.Sprintf("frpmgr_%x", md5.Sum([]byte(name)))
}

func DisplayNameOfClient(name string) string {
return "FRP Client: " + name
return "FRP Manager: " + name
}

type frpService struct {
Expand Down

0 comments on commit 8302e3a

Please sign in to comment.