From 35e547525528979c274a22ac82983770241ea823 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Tue, 15 Oct 2024 20:22:26 +0800 Subject: [PATCH] Add system proxy for kde --- v2rayN/ServiceLib/Common/Utils.cs | 2 +- v2rayN/ServiceLib/Models/CmdItem.cs | 2 +- .../Common/ProxySettingLinux.cs | 122 ++++++++++++++---- 3 files changed, 98 insertions(+), 28 deletions(-) diff --git a/v2rayN/ServiceLib/Common/Utils.cs b/v2rayN/ServiceLib/Common/Utils.cs index c482d149..ec381d81 100644 --- a/v2rayN/ServiceLib/Common/Utils.cs +++ b/v2rayN/ServiceLib/Common/Utils.cs @@ -600,7 +600,7 @@ namespace ServiceLib.Common return await GetCliWrapOutput(filePath, arg != null ? new List() { arg } : null); } - private static async Task GetCliWrapOutput(string filePath, IEnumerable? args) + public static async Task GetCliWrapOutput(string filePath, IEnumerable? args) { try { diff --git a/v2rayN/ServiceLib/Models/CmdItem.cs b/v2rayN/ServiceLib/Models/CmdItem.cs index 545c9685..d9c1a4a5 100644 --- a/v2rayN/ServiceLib/Models/CmdItem.cs +++ b/v2rayN/ServiceLib/Models/CmdItem.cs @@ -3,6 +3,6 @@ public class CmdItem { public string? Cmd { get; set; } - public string? Arguments { get; set; } + public List? Arguments { get; set; } } } \ No newline at end of file diff --git a/v2rayN/v2rayN.Desktop/Common/ProxySettingLinux.cs b/v2rayN/v2rayN.Desktop/Common/ProxySettingLinux.cs index f4c81ec1..abde3751 100644 --- a/v2rayN/v2rayN.Desktop/Common/ProxySettingLinux.cs +++ b/v2rayN/v2rayN.Desktop/Common/ProxySettingLinux.cs @@ -20,8 +20,10 @@ { foreach (var cmd in lstCmd) { - if (cmd is null || cmd.Cmd.IsNullOrEmpty() || cmd.Arguments.IsNullOrEmpty()) - { continue; } + if (cmd is null || cmd.Cmd.IsNullOrEmpty() || cmd.Arguments is null) + { + continue; + } await Task.Delay(10); await Utils.GetCliWrapOutput(cmd.Cmd, cmd.Arguments); } @@ -29,54 +31,122 @@ private static List GetSetCmds(string host, int port) { - //TODO KDE //XDG_CURRENT_DESKTOP - List lstType = ["http", "https", "socks", "ftp"]; + var isKde = IsKde(out var configDir); + List lstType = ["", "http", "https", "socks", "ftp"]; List lstCmd = []; - lstCmd.Add(new CmdItem() + if (isKde) { - Cmd = "gsettings", - Arguments = "set org.gnome.system.proxy mode manual" - }); + foreach (var type in lstType) + { + lstCmd.AddRange(GetSetCmd4Kde(type, host, port, configDir)); + } + } + else + { + foreach (var type in lstType) + { + lstCmd.AddRange(GetSetCmd4Gnome(type, host, port)); + } + } + return lstCmd; + } - foreach (string type in lstType) + private static List GetUnsetCmds() + { + var isKde = IsKde(out var configDir); + List lstCmd = []; + + if (isKde) + { + lstCmd.Add(new CmdItem() + { + Cmd = "kwriteconfig5", + Arguments = ["--file", $"{configDir}/kioslaverc", "--group", "Proxy Settings", "--key", "ProxyType", "0"] + }); + } + else { - lstCmd.AddRange(GetSetCmdByType(type, host, port)); + lstCmd.Add(new CmdItem() + { + Cmd = "gsettings", + Arguments = ["set", "org.gnome.system.proxy", "mode", "none"] + }); } return lstCmd; } - private static List GetSetCmdByType(string type, string host, int port) + private static List GetSetCmd4Kde(string type, string host, int port, string configDir) { List lstCmd = []; - lstCmd.Add(new() - { - Cmd = "gsettings", - Arguments = $"set org.gnome.system.proxy.{type} host {host}", - }); - lstCmd.Add(new() + if (type.IsNullOrEmpty()) + { + lstCmd.Add(new() + { + Cmd = "kwriteconfig5", + Arguments = ["--file", $"{configDir}/kioslaverc", "--group", "Proxy Settings", "--key", "ProxyType", "1"] + }); + } + else { - Cmd = "gsettings", - Arguments = $"set org.gnome.system.proxy.{type} port {port}", - }); + var type2 = type.Equals("https") ? "http" : type; + lstCmd.Add(new CmdItem() + { + Cmd = "kwriteconfig5", + Arguments = ["--file", $"{configDir}/kioslaverc", "--group", "Proxy Settings", "--key", $"{type}Proxy", $"{type2}://{host}:{port}"] + }); + } return lstCmd; } - private static List GetUnsetCmds() + private static List GetSetCmd4Gnome(string type, string host, int port) { - //TODO KDE List lstCmd = []; - lstCmd.Add(new CmdItem() + if (type.IsNullOrEmpty()) { - Cmd = "gsettings", - Arguments = "set org.gnome.system.proxy mode none" - }); + lstCmd.Add(new() + { + Cmd = "gsettings", + Arguments = ["set", "org.gnome.system.proxy", "mode", "manual"] + }); + } + else + { + lstCmd.Add(new() + { + Cmd = "gsettings", + Arguments = ["set", $"org.gnome.system.proxy.{type}", "host", host] + }); + + lstCmd.Add(new() + { + Cmd = "gsettings", + Arguments = ["set", $"org.gnome.system.proxy.{type}", "port", $"{port}"] + }); + } return lstCmd; } + + private static bool IsKde(out string configDir) + { + configDir = "/home"; + var desktop = Environment.GetEnvironmentVariable("XDG_CURRENT_DESKTOP"); + var isKde = string.Equals(desktop, "KDE", StringComparison.OrdinalIgnoreCase); + if (isKde) + { + var homeDir = Environment.GetEnvironmentVariable("HOME"); + if (homeDir != null) + { + configDir = Path.Combine(homeDir, ".config"); + } + } + + return isKde; + } } } \ No newline at end of file