Skip to content

Instantly share code, notes, and snippets.

  • Select an option

  • Save michele-tn/b8e9d018da0170c7f90db36adf56585e to your computer and use it in GitHub Desktop.

Select an option

Save michele-tn/b8e9d018da0170c7f90db36adf56585e to your computer and use it in GitHub Desktop.
GetRUSTDESKLatestVersion_NIGHTLY (C#)
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Principal;
using Newtonsoft.Json.Linq;
class Program
{
private const string Repo = "rustdesk/rustdesk";
private const string FilenamePattern = "x86_64.exe";
private const string UriPattern = "nightly";
private const string NightlyFileName = "rustdesk_nightly.exe";
private const string LatestFileName = "rustdesk_latest.exe";
static void Main(string[] args)
{
if (!IsAdministrator())
{
RestartAsAdministrator();
return;
}
bool preRelease = true; // Set to true for nightly, false for latest
string fileName = preRelease ? NightlyFileName : LatestFileName;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
string downloadUri = GetRustdeskDownloadUri(preRelease);
DownloadFile(downloadUri, fileName);
StartProcess(fileName);
string username = Environment.UserName;
string configFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "RustDesk", "config", "RustDesk2.toml");
bool fileExists = WaitForFile(configFilePath, 60); // Wait up to 60 seconds
if (fileExists)
{
Console.WriteLine("RustDesk2.toml found. Terminating RustDesk.");
KillProcess(fileName);
ConfigureSelfHostedServer(username, configFilePath);
// Attendi tre secondi prima di riavviare RustDesk
System.Threading.Thread.Sleep(3000);
StartProcess(fileName);
Console.WriteLine($"The modified RustDesk2.toml file is located at: {configFilePath}");
}
else
{
Console.WriteLine("Error: RustDesk2.toml not found within the expected time.");
}
}
static bool IsAdministrator()
{
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
static void RestartAsAdministrator()
{
var startInfo = new ProcessStartInfo
{
FileName = Process.GetCurrentProcess().MainModule.FileName,
UseShellExecute = true,
Verb = "runas" // run as administrator
};
try
{
Process.Start(startInfo);
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred while trying to restart as administrator: {ex.Message}");
}
}
static string GetRustdeskDownloadUri(bool preRelease)
{
string releasesUri = preRelease ? $"https://api.github.com/repos/{Repo}/releases" : $"https://api.github.com/repos/{Repo}/releases/latest";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (compatible; AcmeInc/1.0)");
var response = client.GetStringAsync(releasesUri).Result;
var releases = JArray.Parse(response);
foreach (var release in releases)
{
if (preRelease)
{
var asset = release["assets"].FirstOrDefault(a => a["name"].ToString().Contains(FilenamePattern) && a["browser_download_url"].ToString().Contains(UriPattern));
if (asset != null)
{
return asset["browser_download_url"].ToString();
}
}
else
{
var asset = release["assets"].FirstOrDefault(a => a["name"].ToString().Contains(FilenamePattern));
if (asset != null)
{
return asset["browser_download_url"].ToString();
}
}
}
}
throw new Exception("Download URL not found");
}
static void DownloadFile(string downloadUri, string fileName)
{
using (HttpClient client = new HttpClient())
{
var fileData = client.GetByteArrayAsync(downloadUri).Result;
File.WriteAllBytes(fileName, fileData);
Console.WriteLine($"{fileName} downloaded successfully.");
}
}
static void StartProcess(string fileName)
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = fileName,
UseShellExecute = false,
CreateNoWindow = true
}
};
process.Start();
Console.WriteLine($"{fileName} started.");
}
static void KillProcess(string fileName)
{
foreach (var process in Process.GetProcessesByName(Path.GetFileNameWithoutExtension(fileName)))
{
process.Kill();
}
Console.WriteLine($"{fileName} terminated.");
}
static void ConfigureSelfHostedServer(string username, string configFilePath)
{
string IP_SelfHosted_Server = "YOUR_SELF_HOSTED_SERVER_IP";
string KEY_SelfHosted_Server = "YOUR_SELF_HOSTED_SERVER_KEY";
string nl = Environment.NewLine;
string n2 = nl + nl;
string customConfig = $"rendezvous_server = '{IP_SelfHosted_Server}'{nl}" +
"nat_type = 1" + nl +
"serial = 0" + n2 +
"[options]" + nl +
$"custom-rendezvous-server = '{IP_SelfHosted_Server}'" + nl +
$"key = '{KEY_SelfHosted_Server}'" + nl +
"relay-server = ''" + nl +
"api-server = ''";
File.WriteAllText(configFilePath, customConfig);
}
static bool WaitForFile(string filePath, int timeoutSeconds)
{
int waited = 0;
while (!File.Exists(filePath) && waited < timeoutSeconds)
{
Console.WriteLine("RustDesk2.toml does not exist, checking again...");
System.Threading.Thread.Sleep(1000); // Wait another second
waited++;
}
return File.Exists(filePath);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment