Skip to content

Commit

Permalink
Add system proxy for kde
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Oct 15, 2024
1 parent fb649c0 commit 35e5475
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 28 deletions.
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ public static Dictionary<string, string> GetSystemHosts()
return await GetCliWrapOutput(filePath, arg != null ? new List<string>() { arg } : null);
}

private static async Task<string?> GetCliWrapOutput(string filePath, IEnumerable<string>? args)
public static async Task<string?> GetCliWrapOutput(string filePath, IEnumerable<string>? args)
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/Models/CmdItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public class CmdItem
{
public string? Cmd { get; set; }
public string? Arguments { get; set; }
public List<string>? Arguments { get; set; }
}
}
122 changes: 96 additions & 26 deletions v2rayN/v2rayN.Desktop/Common/ProxySettingLinux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,63 +20,133 @@ private static async Task ExecCmd(List<CmdItem> lstCmd)
{
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);
}
}

private static List<CmdItem> GetSetCmds(string host, int port)
{
//TODO KDE //XDG_CURRENT_DESKTOP
List<string> lstType = ["http", "https", "socks", "ftp"];
var isKde = IsKde(out var configDir);
List<string> lstType = ["", "http", "https", "socks", "ftp"];
List<CmdItem> 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<CmdItem> GetUnsetCmds()
{
var isKde = IsKde(out var configDir);
List<CmdItem> 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<CmdItem> GetSetCmdByType(string type, string host, int port)
private static List<CmdItem> GetSetCmd4Kde(string type, string host, int port, string configDir)
{
List<CmdItem> 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<CmdItem> GetUnsetCmds()
private static List<CmdItem> GetSetCmd4Gnome(string type, string host, int port)
{
//TODO KDE
List<CmdItem> 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;
}
}
}

0 comments on commit 35e5475

Please sign in to comment.