Skip to content

fix: various fixes for tunnel startup to work #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 19 additions & 1 deletion Coder.Desktop.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35707.178 d17.12
VisualStudioVersion = 17.12.35707.178
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Vpn", "Vpn\Vpn.csproj", "{B342F896-C721-4AA5-A0F6-0BFA8EBAFACB}"
EndProject
Expand All @@ -21,6 +21,8 @@ Project("{C7167F0D-BC9F-4E6E-AFE1-012C56B48DB5}") = "Package", "Package\Package.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "App", "App\App.csproj", "{800C7E2D-0D86-4554-9903-B879DA6FA2CE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Vpn.DebugClient", "Vpn.DebugClient\Vpn.DebugClient.csproj", "{1BBFDF88-B25F-4C07-99C2-30DA384DB730}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -185,6 +187,22 @@ Global
{800C7E2D-0D86-4554-9903-B879DA6FA2CE}.Release|x64.Build.0 = Release|x64
{800C7E2D-0D86-4554-9903-B879DA6FA2CE}.Release|x86.ActiveCfg = Release|x86
{800C7E2D-0D86-4554-9903-B879DA6FA2CE}.Release|x86.Build.0 = Release|x86
{1BBFDF88-B25F-4C07-99C2-30DA384DB730}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1BBFDF88-B25F-4C07-99C2-30DA384DB730}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1BBFDF88-B25F-4C07-99C2-30DA384DB730}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{1BBFDF88-B25F-4C07-99C2-30DA384DB730}.Debug|ARM64.Build.0 = Debug|Any CPU
{1BBFDF88-B25F-4C07-99C2-30DA384DB730}.Debug|x64.ActiveCfg = Debug|Any CPU
{1BBFDF88-B25F-4C07-99C2-30DA384DB730}.Debug|x64.Build.0 = Debug|Any CPU
{1BBFDF88-B25F-4C07-99C2-30DA384DB730}.Debug|x86.ActiveCfg = Debug|Any CPU
{1BBFDF88-B25F-4C07-99C2-30DA384DB730}.Debug|x86.Build.0 = Debug|Any CPU
{1BBFDF88-B25F-4C07-99C2-30DA384DB730}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1BBFDF88-B25F-4C07-99C2-30DA384DB730}.Release|Any CPU.Build.0 = Release|Any CPU
{1BBFDF88-B25F-4C07-99C2-30DA384DB730}.Release|ARM64.ActiveCfg = Release|Any CPU
{1BBFDF88-B25F-4C07-99C2-30DA384DB730}.Release|ARM64.Build.0 = Release|Any CPU
{1BBFDF88-B25F-4C07-99C2-30DA384DB730}.Release|x64.ActiveCfg = Release|Any CPU
{1BBFDF88-B25F-4C07-99C2-30DA384DB730}.Release|x64.Build.0 = Release|Any CPU
{1BBFDF88-B25F-4C07-99C2-30DA384DB730}.Release|x86.ActiveCfg = Release|Any CPU
{1BBFDF88-B25F-4C07-99C2-30DA384DB730}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
128 changes: 128 additions & 0 deletions Vpn.DebugClient/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using System.IO.Pipes;
using Coder.Desktop.Vpn.Proto;

namespace Coder.Desktop.Vpn.DebugClient;

public static class Program
{
private static Speaker<ClientMessage, ServiceMessage>? _speaker;

private static string? _coderUrl;
private static string? _apiToken;

public static void Main()
{
Console.WriteLine("Type 'exit' to exit the program");
Console.WriteLine("Type 'connect' to connect to the service");
Console.WriteLine("Type 'disconnect' to disconnect from the service");
Console.WriteLine("Type 'configure' to set the parameters");
Console.WriteLine("Type 'start' to send a start command with the current parameters");
Console.WriteLine("Type 'stop' to send a stop command");
while (true)
{
Console.Write("> ");
var input = Console.ReadLine()?.Trim();
try
{
switch (input)
{
case "exit":
return;
case "connect":
Connect();
break;
case "disconnect":
Disconnect();
break;
case "configure":
Configure();
break;
case "start":
Start();
break;
case "stop":
Stop();
break;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex}");
}
}
}

private static void Connect()
{
var client = new NamedPipeClientStream(".", "Coder.Desktop.Vpn", PipeDirection.InOut, PipeOptions.Asynchronous);
client.Connect();
Console.WriteLine("Connected to named pipe.");

_speaker = new Speaker<ClientMessage, ServiceMessage>(client);
_speaker.Receive += message => { Console.WriteLine($"Received({message.Message.MsgCase}: {message.Message}"); };
_speaker.Error += exception =>
{
Console.WriteLine($"Error: {exception}");
Disconnect();
};
_speaker.StartAsync().Wait();
Console.WriteLine("Speaker started.");
}

private static void Disconnect()
{
_speaker?.DisposeAsync().AsTask().Wait();
_speaker = null;
Console.WriteLine("Disconnected from named pipe");
}

private static void Configure()
{
Console.Write("Coder URL: ");
_coderUrl = Console.ReadLine()?.Trim();
Console.Write("API Token: ");
_apiToken = Console.ReadLine()?.Trim();
}

private static void Start()
{
if (_speaker is null)
{
Console.WriteLine("Not connected to Coder.Desktop.Vpn.");
return;
}

var message = new ClientMessage
{
Start = new StartRequest
{
CoderUrl = _coderUrl,
ApiToken = _apiToken,
},
};
Console.WriteLine("Sending start message...");
var sendTask = _speaker.SendRequestAwaitReply(message).AsTask();
Console.WriteLine("Start message sent, awaiting reply.");
sendTask.Wait();
Console.WriteLine($"Received reply: {sendTask.Result.Message}");
}

private static void Stop()
{
if (_speaker is null)
{
Console.WriteLine("Not connected to Coder.Desktop.Vpn.");
return;
}

var message = new ClientMessage
{
Stop = new StopRequest(),
};
Console.WriteLine("Sending stop message...");
var sendTask = _speaker.SendRequestAwaitReply(message);
Console.WriteLine("Stop message sent, awaiting reply.");
var reply = sendTask.AsTask().Result;
Console.WriteLine($"Received reply: {reply.Message}");
}
}
17 changes: 17 additions & 0 deletions Vpn.DebugClient/Vpn.DebugClient.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RootNamespace>Coder.Desktop.Vpn.DebugClient</RootNamespace>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Vpn\Vpn.csproj"/>
<ProjectReference Include="..\Vpn.Proto\Vpn.Proto.csproj"/>
</ItemGroup>

</Project>
30 changes: 30 additions & 0 deletions Vpn.DebugClient/packages.lock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"version": 1,
"dependencies": {
"net8.0": {
"Google.Protobuf": {
"type": "Transitive",
"resolved": "3.29.1",
"contentHash": "kDFLP72bPu4GwquVN7HtFnfqxhQs4CLbUEyOc/0yV48HB0Pxf7tDK7zIx6ifaQwGp+RSoLY1sz1CXqoAEAu+AQ=="
},
"System.IO.Pipelines": {
"type": "Transitive",
"resolved": "9.0.0",
"contentHash": "eA3cinogwaNB4jdjQHOP3Z3EuyiDII7MT35jgtnsA4vkn0LUrrSHsU0nzHTzFzmaFYeKV7MYyMxOocFzsBHpTw=="
},
"vpn": {
"type": "Project",
"dependencies": {
"System.IO.Pipelines": "[9.0.0, )",
"Vpn.Proto": "[1.0.0, )"
}
},
"vpn.proto": {
"type": "Project",
"dependencies": {
"Google.Protobuf": "[3.29.1, )"
}
}
}
}
}
35 changes: 35 additions & 0 deletions Vpn.Service/Create-Service.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Elevate to administrator
if (-not ([Security.Principal.WindowsPrincipal]([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "Elevating script to run as administrator..."
Start-Process powershell.exe -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$($MyInvocation.MyCommand.Path)`"" -Verb RunAs
exit
}

$name = "Coder Desktop (Debug)"
$binaryPath = Join-Path -Path $PSScriptRoot -ChildPath "bin/Debug/net8.0-windows/Vpn.Service.exe"

try {
Write-Host "Creating service..."
New-Service -Name $name -BinaryPathName "`"$binaryPath`"" -DisplayName $name -StartupType Automatic

$sddl = & sc.exe sdshow $name
if (-not $sddl) {
throw "Failed to retrieve security descriptor for service '$name'"
}
Write-Host "Current security descriptor: '$sddl'"
$sddl = $sddl.Trim() -replace "D:", "D:(A;;RPWP;;;WD)" # allow everyone to start, stop, pause, and query the service
Write-Host "Setting security descriptor: '$sddl'"
& sc.exe sdset $name $sddl

Write-Host "Starting service..."
Start-Service -Name $name

if ((Get-Service -Name $name -ErrorAction Stop).Status -ne "Running") {
throw "Service '$name' is not running"
}
Write-Host "Service '$name' created and started successfully"
} catch {
Write-Host $_ -ForegroundColor Red
Write-Host "Press Return to exit..."
Read-Host
}
18 changes: 18 additions & 0 deletions Vpn.Service/Delete-Service.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Elevate to administrator
if (-not ([Security.Principal.WindowsPrincipal]([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "Elevating script to run as administrator..."
Start-Process powershell.exe -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$($MyInvocation.MyCommand.Path)`"" -Verb RunAs
exit
}

$name = "Coder Desktop (Debug)"

try {
Stop-Service -Name $name -Force -ErrorAction SilentlyContinue
sc.exe delete $name
Write-Host "Service '$name' deleted"
} catch {
Write-Host $_ -ForegroundColor Red
Write-Host "Press Return to exit..."
Read-Host
}
2 changes: 2 additions & 0 deletions Vpn.Service/Downloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class AuthenticodeDownloadValidator : IDownloadValidator
{
private readonly string _expectedName;

// ReSharper disable once ConvertToPrimaryConstructor
public AuthenticodeDownloadValidator(string expectedName)
{
_expectedName = expectedName;
Expand Down Expand Up @@ -79,6 +80,7 @@ public class AssemblyVersionDownloadValidator : IDownloadValidator
{
private readonly string _expectedAssemblyVersion;

// ReSharper disable once ConvertToPrimaryConstructor
public AssemblyVersionDownloadValidator(string expectedAssemblyVersion)
{
_expectedAssemblyVersion = expectedAssemblyVersion;
Expand Down
Loading