Skip to content

Commit 1411643

Browse files
committed
Add system proxy for Linux
1 parent fde2a76 commit 1411643

File tree

9 files changed

+169
-35
lines changed

9 files changed

+169
-35
lines changed

v2rayN/ServiceLib/Models/CmdItem.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace ServiceLib.Models
2+
{
3+
public class CmdItem
4+
{
5+
public string? Cmd { get; set; }
6+
public string? Arguments { get; set; }
7+
}
8+
}

v2rayN/ServiceLib/ServiceLib.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<PackageReference Include="WebDav.Client" Version="2.8.0" />
1717
<PackageReference Include="YamlDotNet" Version="16.1.2" />
1818
<PackageReference Include="QRCoder" Version="1.6.0" />
19+
<PackageReference Include="CliWrap" Version="3.6.6" />
1920
</ItemGroup>
2021

2122
<ItemGroup>

v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,15 @@ private void RefreshSubItems()
6767
coreType = ECoreType.mihomo.ToString(),
6868
remarks = ResUI.menuCheckUpdate,
6969
});
70-
_checkUpdateItem.Add(new CheckUpdateItem()
70+
if (Utils.IsWindows())
7171
{
72-
isSelected = true,
73-
coreType = ECoreType.sing_box.ToString(),
74-
remarks = ResUI.menuCheckUpdate,
75-
});
72+
_checkUpdateItem.Add(new CheckUpdateItem()
73+
{
74+
isSelected = true,
75+
coreType = ECoreType.sing_box.ToString(),
76+
remarks = ResUI.menuCheckUpdate,
77+
});
78+
}
7679
_checkUpdateItem.Add(new CheckUpdateItem()
7780
{
7881
isSelected = true,

v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,14 @@ public MainWindowViewModel(bool isAdministrator, Func<EViewAction, object?, Task
295295
});
296296
OpenTheFileLocationCmd = ReactiveCommand.Create(() =>
297297
{
298-
Utils.ProcessStart("Explorer", $"/select,{Utils.GetConfigPath()}");
298+
if (Utils.IsWindows())
299+
{
300+
Utils.ProcessStart("Explorer", $"/select,{Utils.GetConfigPath()}");
301+
}
302+
else if (Utils.IsLinux())
303+
{
304+
Utils.ProcessStart("nautilus", Utils.GetConfigPath());
305+
}
299306
});
300307

301308
ReloadCmd = ReactiveCommand.Create(() =>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using CliWrap;
2+
using CliWrap.Buffered;
3+
4+
namespace v2rayN.Desktop.Common
5+
{
6+
public class ProxySettingLinux
7+
{
8+
public static async Task SetProxy(string host, int port)
9+
{
10+
var lstCmd = GetSetCmds(host, port);
11+
12+
await ExecCmd(lstCmd);
13+
}
14+
15+
public static async Task UnsetProxy()
16+
{
17+
var lstCmd = GetUnsetCmds();
18+
19+
await ExecCmd(lstCmd);
20+
}
21+
22+
private static async Task ExecCmd(List<CmdItem> lstCmd)
23+
{
24+
foreach (var cmd in lstCmd)
25+
{
26+
if (cmd is null || cmd.Cmd.IsNullOrEmpty() || cmd.Arguments.IsNullOrEmpty())
27+
{ continue; }
28+
29+
await Task.Delay(10);
30+
var result = await Cli.Wrap(cmd.Cmd)
31+
.WithArguments(cmd.Arguments)
32+
.ExecuteBufferedAsync();
33+
34+
if (result.ExitCode != 0)
35+
{
36+
//Logging.SaveLog($"Command failed {cmd.Cmd},{cmd.Arguments}");
37+
Logging.SaveLog(result.ToString() ?? "");
38+
}
39+
}
40+
}
41+
42+
private static List<CmdItem> GetSetCmds(string host, int port)
43+
{
44+
//TODO KDE //XDG_CURRENT_DESKTOP
45+
List<string> lstType = ["http", "https", "socks", "ftp"];
46+
List<CmdItem> lstCmd = [];
47+
48+
lstCmd.Add(new CmdItem()
49+
{
50+
Cmd = "gsettings",
51+
Arguments = "set org.gnome.system.proxy mode manual"
52+
});
53+
54+
foreach (string type in lstType)
55+
{
56+
lstCmd.AddRange(GetSetCmdByType(type, host, port));
57+
}
58+
59+
return lstCmd;
60+
}
61+
62+
private static List<CmdItem> GetSetCmdByType(string type, string host, int port)
63+
{
64+
List<CmdItem> lstCmd = [];
65+
lstCmd.Add(new()
66+
{
67+
Cmd = "gsettings",
68+
Arguments = $"set org.gnome.system.proxy.{type} host {host}",
69+
});
70+
71+
lstCmd.Add(new()
72+
{
73+
Cmd = "gsettings",
74+
Arguments = $"set org.gnome.system.proxy.{type} port {port}",
75+
});
76+
77+
return lstCmd;
78+
}
79+
80+
private static List<CmdItem> GetUnsetCmds()
81+
{
82+
//TODO KDE
83+
List<CmdItem> lstCmd = [];
84+
85+
lstCmd.Add(new CmdItem()
86+
{
87+
Cmd = "gsettings",
88+
Arguments = "set org.gnome.system.proxy mode none"
89+
});
90+
91+
return lstCmd;
92+
}
93+
}
94+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace v2rayN.Desktop.Common
2+
{
3+
public class ProxySettingOSX
4+
{
5+
public static async Task SetProxy(string host, int port)
6+
{
7+
}
8+
9+
public static async Task UnsetProxy()
10+
{
11+
}
12+
}
13+
}
Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace v2rayN.Desktop.Handler
1+
using v2rayN.Desktop.Common;
2+
3+
namespace v2rayN.Desktop.Handler
24
{
35
public static class SysProxyHandler
46
{
@@ -14,48 +16,56 @@ public static async Task<bool> UpdateSysProxy(Config config, bool forceDisable)
1416
try
1517
{
1618
int port = LazyConfig.Instance.GetLocalPort(EInboundProtocol.http);
19+
int portSocks = LazyConfig.Instance.GetLocalPort(EInboundProtocol.socks);
20+
int portPac = LazyConfig.Instance.GetLocalPort(EInboundProtocol.pac);
1721
if (port <= 0)
1822
{
1923
return false;
2024
}
2125
if (type == ESysProxyType.ForcedChange)
2226
{
23-
var strProxy = $"{Global.Loopback}:{port}";
24-
await SetProxy(strProxy);
27+
if (Utils.IsWindows())
28+
{
29+
//TODO
30+
}
31+
else if (Utils.IsLinux())
32+
{
33+
await ProxySettingLinux.SetProxy(Global.Loopback, port);
34+
}
35+
else if (Utils.IsOSX())
36+
{
37+
await ProxySettingOSX.SetProxy(Global.Loopback, port);
38+
}
2539
}
2640
else if (type == ESysProxyType.ForcedClear)
2741
{
28-
await UnsetProxy();
42+
if (Utils.IsWindows())
43+
{
44+
//TODO
45+
}
46+
else if (Utils.IsLinux())
47+
{
48+
await ProxySettingLinux.UnsetProxy();
49+
}
50+
else if (Utils.IsOSX())
51+
{
52+
await ProxySettingOSX.UnsetProxy();
53+
}
2954
}
30-
else if (type == ESysProxyType.Unchanged)
55+
else if (type == ESysProxyType.Pac)
3156
{
3257
}
58+
59+
//if (type != ESysProxyType.Pac)
60+
//{
61+
// PacHandler.Stop();
62+
//}
3363
}
3464
catch (Exception ex)
3565
{
3666
Logging.SaveLog(ex.Message, ex);
3767
}
3868
return true;
3969
}
40-
41-
private static async Task SetProxy(string? strProxy)
42-
{
43-
await Task.Run(() =>
44-
{
45-
var httpProxy = strProxy is null ? null : $"{Global.HttpProtocol}{strProxy}";
46-
var socksProxy = strProxy is null ? null : $"{Global.SocksProtocol}{strProxy}";
47-
var noProxy = $"localhost,127.0.0.0/8,::1";
48-
49-
Environment.SetEnvironmentVariable("http_proxy", httpProxy, EnvironmentVariableTarget.User);
50-
Environment.SetEnvironmentVariable("https_proxy", httpProxy, EnvironmentVariableTarget.User);
51-
Environment.SetEnvironmentVariable("all_proxy", socksProxy, EnvironmentVariableTarget.User);
52-
Environment.SetEnvironmentVariable("no_proxy", noProxy, EnvironmentVariableTarget.User);
53-
});
54-
}
55-
56-
private static async Task UnsetProxy()
57-
{
58-
await SetProxy(null);
59-
}
6070
}
6171
}

v2rayN/v2rayN.Desktop/Views/MainWindow.axaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@
165165
<ComboBoxItem Content="{x:Static resx:ResUI.menuSystemProxyClear}" />
166166
<ComboBoxItem Content="{x:Static resx:ResUI.menuSystemProxySet}" />
167167
<ComboBoxItem Content="{x:Static resx:ResUI.menuSystemProxyNothing}" />
168-
<ComboBoxItem Content="{x:Static resx:ResUI.menuSystemProxyPac}" />
169168
</ComboBox>
170169

171170
<ComboBox

v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.ComponentModel;
1212
using System.Reactive.Disposables;
1313
using v2rayN.Desktop.Common;
14+
using v2rayN.Desktop.Handler;
1415

1516
namespace v2rayN.Desktop.Views
1617
{
@@ -124,8 +125,6 @@ public MainWindow()
124125
menuRebootAsAdmin.IsVisible = false;
125126
menuSettingsSetUWP.IsVisible = false;
126127
menuGlobalHotkeySetting.IsVisible = false;
127-
menuOpenTheFileLocation.IsVisible = false;
128-
cmbSystemProxy.IsVisible = false;
129128
if (_config.tunModeItem.enableTun)
130129
{
131130
ViewModel.EnableTun = true;
@@ -255,7 +254,7 @@ private async Task<bool> UpdateViewHandler(EViewAction action, object? obj)
255254

256255
case EViewAction.UpdateSysProxy:
257256
if (obj is null) return false;
258-
// await SysProxyHandler.UpdateSysProxy(_config, (bool)obj);
257+
await SysProxyHandler.UpdateSysProxy(_config, (bool)obj);
259258
break;
260259

261260
case EViewAction.AddServerViaClipboard:

0 commit comments

Comments
 (0)