Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion WgAPI/Commands/WireGuardCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace WgAPI
using System.IO;
using System.Text;

namespace WgAPI
{
public class WireGuardCommand
{
Expand All @@ -16,6 +19,34 @@ public WireGuardCommand(string @switch, WhichExe whichExe, params string[] args)
public WhichExe WhichExe { get; protected set; }

public string StandardInput { get; protected set; } = string.Empty;


/// <summary>
/// Remove "WireGuard.exe" / "wg-quick" specific settings which aren't supported by "wg.exe" / "wg".
/// This is similar to the "wg-quick strip" command under Linux, used for example in: wg syncconf wg0 &lt;(wg-quick strip wg0)
/// Make sure to pass a temporary file path to avoid overwriting the original configuration.
/// </summary>
/// <param name="temporaryFilePath"></param>
public static void StripConfigFile(string temporaryFilePath)
{
var lines = File.ReadAllLines(temporaryFilePath);
var section = "";
var sb = new StringBuilder((int)new FileInfo(temporaryFilePath).Length);
foreach (var rawLine in lines)
{
var line = rawLine.Trim();
if (line.StartsWith('[') && line.EndsWith(']'))
section = line.Substring(1, line.Length - 2);

// "[Interface] Address=..." isn't supported by wg (i.e.: wg syncconf)
if (section == "Interface" && line.Replace(" ", "").StartsWith("Address="))
continue;

sb.AppendLine(rawLine);
}

File.WriteAllText(temporaryFilePath, sb.ToString());
}
}

public enum WhichExe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,11 @@ public override void Configure()
// Update the tunnel service, if everyone is happy
if ((Fulfilled || !AnyClients) && serverConfigurationPrerequisite.Fulfilled && new TunnelServicePrerequisite().Fulfilled)
{
using (TemporaryFile temporaryFile = new(originalFilePath: ServerConfigurationPrerequisite.ServerWGPath, newFilePath: ServerConfigurationPrerequisite.ServerWGPathWithCustomTunnelName))
using (TemporaryFile temporaryFile = new(originalFilePath: ServerConfigurationPrerequisite.ServerWGPath, newFilePath: ServerConfigurationPrerequisite.ServerWGPathWithCustomTunnelName + ".tmp"))
{
// remove config settings that arent's supported by wg
WireGuardCommand.StripConfigFile(temporaryFile.NewFilePath);

string output = new WireGuardExe().ExecuteCommand(new SyncConfigurationCommand(GlobalAppSettings.Instance.TunnelServiceName, temporaryFile.NewFilePath), out int exitCode);

if (exitCode != 0)
Expand Down
2 changes: 1 addition & 1 deletion WgServerforWindows/Models/ServerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public ServerConfiguration()
{
// Server properties
PrivateKeyProperty.TargetTypes.Add(GetType());
//AddressProperty.TargetTypes.Add(GetType());
AddressProperty.TargetTypes.Add(GetType());
ListenPortProperty.TargetTypes.Add(GetType());

// Client properties
Expand Down
5 changes: 4 additions & 1 deletion WgServerforWindows/Models/ServerConfigurationPrerequisite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,11 @@ public override void Configure()
// Update the tunnel service, if everyone is happy
if (Fulfilled && (clientConfigurationsPrerequisite.Fulfilled || !ClientConfigurationsPrerequisite.AnyClients) && new TunnelServicePrerequisite().Fulfilled)
{
using (TemporaryFile temporaryFile = new(ServerWGPath, ServerWGPathWithCustomTunnelName))
using (TemporaryFile temporaryFile = new(ServerWGPath, ServerWGPathWithCustomTunnelName + ".tmp"))
{
// remove config settings that arent's supported by wg
WireGuardCommand.StripConfigFile(temporaryFile.NewFilePath);

// Sync conf to tunnel
string output = new WireGuardExe().ExecuteCommand(new SyncConfigurationCommand(GlobalAppSettings.Instance.TunnelServiceName, temporaryFile.NewFilePath), out int exitCode);

Expand Down
Loading