Skip to content

Commit 9d63896

Browse files
committed
Use tun mode without running with sudo in linux
1 parent f921ff7 commit 9d63896

15 files changed

Lines changed: 190 additions & 19 deletions

v2rayN/ServiceLib/Handler/CoreHandler.cs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public async Task LoadCore(ProfileItem? node)
6868
{
6969
ShowMsg(true, $"{node.GetSummary()}");
7070
await CoreStop();
71+
await Task.Delay(100);
7172
await CoreStart(node);
7273

7374
//In tun mode, do a delay check and restart the core
@@ -169,21 +170,12 @@ private async Task CoreStart(ProfileItem node)
169170
ShowMsg(false, $"{Environment.OSVersion} - {(Environment.Is64BitOperatingSystem ? 64 : 32)}");
170171
ShowMsg(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
171172

172-
//ECoreType coreType;
173-
//if (node.configType != EConfigType.Custom && _config.tunModeItem.enableTun)
174-
//{
175-
// coreType = ECoreType.sing_box;
176-
//}
177-
//else
178-
//{
179-
// coreType = LazyConfig.Instance.GetCoreType(node, node.configType);
180-
//}
181173
var coreType = AppHandler.Instance.GetCoreType(node, node.ConfigType);
182174
_config.RunningCoreType = coreType;
183175
var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(coreType);
184176

185177
var displayLog = node.ConfigType != EConfigType.Custom || node.DisplayLog;
186-
var proc = await RunProcess(coreInfo, Global.CoreConfigFileName, displayLog);
178+
var proc = await RunProcess(coreInfo, Global.CoreConfigFileName, displayLog, true);
187179
if (proc is null)
188180
{
189181
return;
@@ -225,7 +217,7 @@ private async Task CoreStart(ProfileItem node)
225217
if (result.Success)
226218
{
227219
var coreInfo2 = CoreInfoHandler.Instance.GetCoreInfo(preCoreType);
228-
var proc2 = await RunProcess(coreInfo2, Global.CorePreConfigFileName, true);
220+
var proc2 = await RunProcess(coreInfo2, Global.CorePreConfigFileName, true, true);
229221
if (proc2 is not null)
230222
{
231223
_processPre = proc2;
@@ -243,7 +235,7 @@ private async Task<int> CoreStartSpeedtest(string configPath, ECoreType coreType
243235
try
244236
{
245237
var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(coreType);
246-
var proc = await RunProcess(coreInfo, Global.CoreSpeedtestConfigFileName, true);
238+
var proc = await RunProcess(coreInfo, Global.CoreSpeedtestConfigFileName, true, false);
247239
if (proc is null)
248240
{
249241
return -1;
@@ -264,17 +256,28 @@ private void ShowMsg(bool notify, string msg)
264256
_updateFunc?.Invoke(notify, msg);
265257
}
266258

259+
private bool IsNeedSudo(ECoreType eCoreType)
260+
{
261+
return _config.TunModeItem.EnableTun
262+
&& eCoreType == ECoreType.sing_box
263+
&& Utils.IsLinux()
264+
&& _config.TunModeItem.LinuxSudoPassword.IsNotEmpty()
265+
;
266+
}
267+
267268
#endregion Private
268269

269270
#region Process
270271

271-
private async Task<Process?> RunProcess(CoreInfo coreInfo, string configPath, bool displayLog)
272+
private async Task<Process?> RunProcess(CoreInfo coreInfo, string configPath, bool displayLog, bool mayNeedSudo)
272273
{
273274
var fileName = CoreFindExe(coreInfo);
274275
if (Utils.IsNullOrEmpty(fileName))
275276
{
276277
return null;
277278
}
279+
280+
var isNeedSudo = mayNeedSudo && IsNeedSudo(coreInfo.CoreType);
278281
try
279282
{
280283
Process proc = new()
@@ -292,6 +295,15 @@ private void ShowMsg(bool notify, string msg)
292295
StandardErrorEncoding = displayLog ? Encoding.UTF8 : null,
293296
}
294297
};
298+
299+
if (isNeedSudo)
300+
{
301+
proc.StartInfo.FileName = $"/bin/sudo";
302+
proc.StartInfo.Arguments = $"-S {fileName} {string.Format(coreInfo.Arguments, configPath)}";
303+
proc.StartInfo.StandardInputEncoding = Encoding.UTF8;
304+
proc.StartInfo.RedirectStandardInput = true;
305+
}
306+
295307
var startUpErrorMessage = new StringBuilder();
296308
var startUpSuccessful = false;
297309
if (displayLog)
@@ -313,6 +325,15 @@ private void ShowMsg(bool notify, string msg)
313325
};
314326
}
315327
proc.Start();
328+
329+
if (isNeedSudo)
330+
{
331+
await Task.Delay(10);
332+
await proc.StandardInput.WriteLineAsync(_config.TunModeItem.LinuxSudoPassword);
333+
await Task.Delay(10);
334+
await proc.StandardInput.WriteLineAsync(_config.TunModeItem.LinuxSudoPassword);
335+
}
336+
316337
if (displayLog)
317338
{
318339
proc.BeginOutputReadLine();

v2rayN/ServiceLib/Models/ConfigItems.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ public class TunModeItem
161161
public int Mtu { get; set; }
162162
public bool EnableExInbound { get; set; }
163163
public bool EnableIPv6Address { get; set; }
164+
public string? LinuxSudoPassword { get; set; }
164165
}
165166

166167
[Serializable]

v2rayN/ServiceLib/Resx/ResUI.Designer.cs

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v2rayN/ServiceLib/Resx/ResUI.fa-Ir.resx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,4 +1369,16 @@
13691369
<data name="TbRuleMatchingTips" xml:space="preserve">
13701370
<value>(Domain or IP or ProcName) and Port and Protocol and InboundTag =&gt; OutboundTag</value>
13711371
</data>
1372+
<data name="TbSettingsLinuxSudoPassword" xml:space="preserve">
1373+
<value>Linux system sudo password</value>
1374+
</data>
1375+
<data name="TbSettingsLinuxSudoPasswordTip" xml:space="preserve">
1376+
<value>The password will only be stored in the local file.</value>
1377+
</data>
1378+
<data name="TbSettingsLinuxSudoPasswordIsEmpty" xml:space="preserve">
1379+
<value>Please set the sudo password in Tun mode settings first</value>
1380+
</data>
1381+
<data name="TbSettingsLinuxSudoPasswordNotSudoRunApp" xml:space="preserve">
1382+
<value>Please do not run this app with sudo</value>
1383+
</data>
13721384
</root>

v2rayN/ServiceLib/Resx/ResUI.resx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,4 +1369,16 @@
13691369
<data name="LvMemo" xml:space="preserve">
13701370
<value>Remarks Memo</value>
13711371
</data>
1372+
<data name="TbSettingsLinuxSudoPassword" xml:space="preserve">
1373+
<value>Linux system sudo password</value>
1374+
</data>
1375+
<data name="TbSettingsLinuxSudoPasswordTip" xml:space="preserve">
1376+
<value>The password will only be stored in the local file.</value>
1377+
</data>
1378+
<data name="TbSettingsLinuxSudoPasswordIsEmpty" xml:space="preserve">
1379+
<value>Please set the sudo password in Tun mode settings first</value>
1380+
</data>
1381+
<data name="TbSettingsLinuxSudoPasswordNotSudoRunApp" xml:space="preserve">
1382+
<value>Please do not run this app with sudo</value>
1383+
</data>
13721384
</root>

v2rayN/ServiceLib/Resx/ResUI.ru.resx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,4 +1369,16 @@
13691369
<data name="menuRemoteBackupAndRestore" xml:space="preserve">
13701370
<value>Remote (WebDAV)</value>
13711371
</data>
1372+
<data name="TbSettingsLinuxSudoPassword" xml:space="preserve">
1373+
<value>Linux system sudo password</value>
1374+
</data>
1375+
<data name="TbSettingsLinuxSudoPasswordTip" xml:space="preserve">
1376+
<value>The password will only be stored in the local file.</value>
1377+
</data>
1378+
<data name="TbSettingsLinuxSudoPasswordIsEmpty" xml:space="preserve">
1379+
<value>Please set the sudo password in Tun mode settings first</value>
1380+
</data>
1381+
<data name="TbSettingsLinuxSudoPasswordNotSudoRunApp" xml:space="preserve">
1382+
<value>Please do not run this app with sudo</value>
1383+
</data>
13721384
</root>

v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,4 +1366,16 @@
13661366
<data name="LvMemo" xml:space="preserve">
13671367
<value>备注备忘</value>
13681368
</data>
1369+
<data name="TbSettingsLinuxSudoPassword" xml:space="preserve">
1370+
<value>Linux系统的sudo密码</value>
1371+
</data>
1372+
<data name="TbSettingsLinuxSudoPasswordTip" xml:space="preserve">
1373+
<value>密码只会存储在本地文件中,没有密码无法开启Tun</value>
1374+
</data>
1375+
<data name="TbSettingsLinuxSudoPasswordIsEmpty" xml:space="preserve">
1376+
<value>请先在Tun模式设置中设置sudo密码</value>
1377+
</data>
1378+
<data name="TbSettingsLinuxSudoPasswordNotSudoRunApp" xml:space="preserve">
1379+
<value>请不要用sudo运行本app</value>
1380+
</data>
13691381
</root>

v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,4 +1366,16 @@
13661366
<data name="TbPath7" xml:space="preserve">
13671367
<value>混淆密碼(obfs password)</value>
13681368
</data>
1369+
<data name="TbSettingsLinuxSudoPassword" xml:space="preserve">
1370+
<value>Linux系統的sudo密碼</value>
1371+
</data>
1372+
<data name="TbSettingsLinuxSudoPasswordTip" xml:space="preserve">
1373+
<value>密碼只會儲存在本機檔案中,沒有密碼無法開啟Tun</value>
1374+
</data>
1375+
<data name="TbSettingsLinuxSudoPasswordIsEmpty" xml:space="preserve">
1376+
<value>請先在Tun模式設定中設定sudo密碼</value>
1377+
</data>
1378+
<data name="TbSettingsLinuxSudoPasswordNotSudoRunApp" xml:space="preserve">
1379+
<value>請不要用sudo來運行本app</value>
1380+
</data>
13691381
</root>

v2rayN/ServiceLib/ServiceLib.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
1515
<PackageReference Include="Splat.NLog" Version="15.2.22" />
1616
<PackageReference Include="WebDav.Client" Version="2.8.0" />
17-
<PackageReference Include="YamlDotNet" Version="16.1.3" />
17+
<PackageReference Include="YamlDotNet" Version="16.2.0" />
1818
<PackageReference Include="QRCoder" Version="1.6.0" />
1919
<PackageReference Include="CliWrap" Version="3.6.7" />
2020
<PackageReference Include="SkiaSharp.QrCode" Version="0.7.0" />

v2rayN/ServiceLib/ViewModels/OptionSettingViewModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public class OptionSettingViewModel : MyReactiveObject
8585
[Reactive] public int TunMtu { get; set; }
8686
[Reactive] public bool TunEnableExInbound { get; set; }
8787
[Reactive] public bool TunEnableIPv6Address { get; set; }
88+
[Reactive] public string TunLinuxSudoPassword { get; set; }
8889

8990
#endregion Tun mode
9091

@@ -197,6 +198,7 @@ private async Task Init()
197198
TunMtu = _config.TunModeItem.Mtu;
198199
TunEnableExInbound = _config.TunModeItem.EnableExInbound;
199200
TunEnableIPv6Address = _config.TunModeItem.EnableIPv6Address;
201+
TunLinuxSudoPassword = _config.TunModeItem.LinuxSudoPassword;
200202

201203
#endregion Tun mode
202204

@@ -340,6 +342,7 @@ private async Task SaveSettingAsync()
340342
_config.TunModeItem.Mtu = TunMtu;
341343
_config.TunModeItem.EnableExInbound = TunEnableExInbound;
342344
_config.TunModeItem.EnableIPv6Address = TunEnableIPv6Address;
345+
_config.TunModeItem.LinuxSudoPassword = TunLinuxSudoPassword;
343346

344347
//coreType
345348
await SaveCoreType();

0 commit comments

Comments
 (0)