Remote .NET development with tunnels using Visual Studio Code and Synology NAS

 


Visual Studio Code allows you setup a remote development environment that using secure tunnels. In this article I'm going to show you how to use a Synology NAS running DSM 7.2 as the remote environment. We'll install some development tools on the NAS and the .NET SDK and configure the environment properly using our $PATH variable and run the Code CLI as a service so after your Synology reboots, the secure tunnel will be started.

1. Install git and nano on Synology

The first thing we need to do is install some new packages on the Synology NAS. These packages come from the community contributors at SynoCommunity.com. These packages will allow us to install git and nano, the terminal file editor.
  1. Open the Synology DSM web interface
  2. Open the main menu
  3. Select Package Center
  4. Select Settings
  5. Select the Package Sources tab
  6. Select Add
  7. Use SynoCommunity as the name and https://packages.synocommunity.com as the location



In Package Center you will now have a new tab named Community. Select this tab and install these packages:
  1. git
  2. SynoCli File Tools

2. Install the .NET SDK on Synology

The next step is to SSH into the Synology NAS to install the .NET SDK. We will use the official install script from Microsoft to download the latest LTS version of .NET, which as of now is .NET 6 but soon to be .NET 8.

Download the .NET install script

wget https://dot.net/v1/dotnet-install.sh -O dotnet-install.sh

Make the script executable

chmod +x ./dotnet-install.sh

Install the .NET SDK

./dotnet-install.sh --version latest

Verify installation

You should now have a local installation of .NET in your home directory. Run ls -a to see all files there, and you'll see a hidden .dotnet directory containing the SDK.

cd ~/.dotnet
./dotnet --version

3. Adding dotnet to the path

We have one small problem to solve and that's making sure that no matter where we are in the filesystem, dotnet is recognized as a command. We can do this by adding the .NET SDK installation directory to our user's path variable. This variable is a list of directories that should be searched when typing in a command.

Create or update the ~/.profile file

nano ~/.profile

If this file exists already, then add the following line at the end. If it's a new file, the only thing in the file will be the following line. Replace USER_NAME with your user name.

export PATH=$PATH:/var/services/homes/USER_NAME/.dotnet

Verify $PATH

  1. Disconnect and reconnect from SSH
  2. Run: echo $PATH and check for /var/services/homes/USER_NAME/.dotnet
  3. Run: dotnet --version in your home directory

4. Install Code CLI

Next, we'll install the Code CLI. This will allow you to run a headless instance of VS Code. You can connect to this instance through a secure tunnel using VS Code on your computer or vscode.dev in your browser.
🧠 Note: The Code CLI is available for different architectures. Since I'm using a Synology DS220+ with an Intel Celeron J4025 CPU, I'll install the x64 version.

Download the Code CLI 

curl -Lk 'https://code.visualstudio.com/sha/download?build=stable&os=cli-alpine-x64' --output vscode_cli.tar.gz

Extract the Code CLI

tar -xf vscode_cli.tar.gz

Start a tunnel

./code tunnel

You should see the VS Code instance initializing. There will be a URL displayed along with a 8 character code. Open the URL in a browser and type in the code to authorize your GitHub account with the tunnel.

Install Code tunnel as a service

The code tunnel is only running because we told it to with the last command. If you close this SSH connection, the tunnel will be stopped. The solution is to run the code tunnel as a service so it is automatically started when the Synology boots. Also, if there is a problem and the process crashes, the operating system should restart the tunnel service.

Synology uses systemd to manage services. We need to write a systemd service script that describes the services properties.

Copy the Code CLI to /usr/local/bin

sudo cp ~/code /usr/local/bin

Create Code tunnel .service file

sudo nano /etc/systemd/system/code-tunnel.service

This new file will contain our service properties. Modify this GitHub gist so your user name and group name are specified.

Enable and start the tunnel service

All that's left is to enable and start the service you just created. We need to refresh the services that systemd manages by reloading it.

sudo systemctl daemon-reload
sudo systemctl enable code-tunnel.service
sudo systemctl start code-tunnel.service

Verify the service

You can use the systemctl command to check on the status of a service by name. To check the status of code-tunnel.service use the following command:

sudo systemctl status code-tunnel.service

5. Connect to the tunnel from Visual Studio Code

You can use either Visual Studio Code desktop app or vscode.dev to connect to your tunnel. This means, you can have a full self-hosted development environment that works in a browser. Casual coding sessions on the couch with my iPad Pro are now possible 🧑‍💻🛋️.

Connecting vscode.dev to your Synology tunnel

  1. Navigate to vscode.dev
  2. Select Connect to Tunnel...
  3. Login to your GitHub account
  4. Select your remote connection in the command palette
  5. The workspace will reload and you'll be connected to your secure tunnel
Connecting to a tunnel from Visual Studio Code
Visual Studio Code connected to tunnel displaying Synology using uname -a

Connecting Visual Studio Code for Mac, Windows or Linux to your Synology tunnel

Install the Remote - Tunnels extension for Visual Studio Code to add this functionality.

Open the Remote Explorer tab from the sidebar. Hover over Tunnels and select the ➕ icon. Login to your GitHub account and select your tunnel from the command palette.
Open your remote connection by selecting the ➡ icon. Your workspace will reload on the remote connection.
In the bottom left corner of Visual Studio Code, you will see the remote connection you are connected to. Selecting the green area will allow you manage that connection.

Comments

Popular posts from this blog

Create a Discord bot in C# and .NET - Part 1

Create a Discord bot in C# and .NET - Part 2

Create a Discord bot in C# and .NET - Part 3