Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Phleesty committed Nov 18, 2024
2 parents 96492a6 + 294cabc commit dc5c753
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 51 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ A GUI client for Windows and Linux, support [Xray core](https://github.com/XTLS/


## How to use
- If you are new to this, please download v2rayN-With-Core.zip from [releases](https://github.com/2dust/v2rayN/releases)
- Otherwise please download v2rayN.zip (you will also need to download cores in the bin directory)
- Run v2rayN.exe
Check [Release files introduction](https://github.com/2dust/v2rayN/wiki/Release-files-introduction) and select the version you need to download
### Windows
- Run `v2rayN.exe`
### Linux
- `chmod +x v2rayN` Run `./v2rayN`
```
Debian 9+
Ubuntu 16.04+
Fedora 30+
```

## Requirements
- (6.35 and above)[Microsoft .NET 8.0 Desktop Runtime ](https://dotnet.microsoft.com/en-us/download/dotnet/8.0)
- (6.33 and below)[Microsoft .NET 6.0 Desktop Runtime ](https://dotnet.microsoft.com/en-us/download/dotnet/6.0)
- [Microsoft .NET 8.0 Desktop Runtime ](https://dotnet.microsoft.com/en-us/download/dotnet/8.0)
- [Supported cores](https://github.com/2dust/v2rayN/wiki/List-of-supported-cores)


Expand Down
5 changes: 5 additions & 0 deletions v2rayN/AmazTool/AmazTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@
<Copyright>Copyright © 2017-2024 (GPLv3)</Copyright>
<FileVersion>1.3.0</FileVersion>
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="Assets\en-US.json" />
<EmbeddedResource Include="Assets\zh-CN.json" />
</ItemGroup>

</Project>
File renamed without changes.
File renamed without changes.
48 changes: 22 additions & 26 deletions v2rayN/AmazTool/LocalizationHelper.cs
Original file line number Diff line number Diff line change
@@ -1,63 +1,59 @@
using System.Globalization;
using System.Globalization;
using System.Reflection;
using System.Text.Json;

namespace AmazTool
{
public class LocalizationHelper
{
private static Dictionary<string, string> languageResources = new();
private static Dictionary<string, string> _languageResources = [];

static LocalizationHelper()
{
// 加载语言资源
LoadLanguageResources();
}

/// <summary>
/// 加载外部 JSON 文件中的语言资源
/// </summary>
private static void LoadLanguageResources()
{
try
{
string currentLanguage = CultureInfo.CurrentCulture.Name;
var currentLanguage = CultureInfo.CurrentCulture.Name;
if (currentLanguage != "zh-CN" && currentLanguage != "en-US")
{
currentLanguage = "en-US";
}

string jsonFilePath = $"{currentLanguage}.json";
if (!File.Exists(jsonFilePath))
{
jsonFilePath = "en-US.json";
}
var resourceName = $"AmazTool.Assets.{currentLanguage}.json";
var assembly = Assembly.GetExecutingAssembly();

var json = File.ReadAllText(jsonFilePath);
using var stream = assembly.GetManifestResourceStream(resourceName);
if (stream == null) return;

using StreamReader reader = new(stream);
var json = reader.ReadToEnd();
if (!string.IsNullOrEmpty(json))
{
languageResources = JsonSerializer.Deserialize<Dictionary<string, string>>(json) ?? new Dictionary<string, string>();
_languageResources = JsonSerializer.Deserialize<Dictionary<string, string>>(json) ?? new Dictionary<string, string>();
}
}
catch (IOException ex)
{
Console.WriteLine($"Failed to read language resource file: {ex.Message}");
}
catch (JsonException ex)
{
Console.WriteLine($"Failed to parse JSON data: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to load language resources: {ex.Message}");
languageResources = []; // 初始化为空字典
Console.WriteLine($"Unexpected error occurred: {ex.Message}");
}
}

/// <summary>
/// 获取系统当前语言的本地化字符串
/// </summary>
/// <param name="key">要翻译的关键字</param>
/// <returns>对应语言的本地化字符串,如果没有找到则返回关键字</returns>
public static string GetLocalizedValue(string key)
{
if (languageResources != null && languageResources.TryGetValue(key, out var translation))
{
return translation;
}

return key;
return _languageResources.TryGetValue(key, out var translation) ? translation : key;
}
}
}
44 changes: 35 additions & 9 deletions v2rayN/ServiceLib/Handler/CoreHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private bool IsNeedSudo(ECoreType eCoreType)
return _config.TunModeItem.EnableTun
&& eCoreType == ECoreType.sing_box
&& Utils.IsLinux()
&& _config.TunModeItem.LinuxSudoPwd.IsNotEmpty()
//&& _config.TunModeItem.LinuxSudoPwd.IsNotEmpty()
;
}

Expand All @@ -279,7 +279,6 @@ private bool IsNeedSudo(ECoreType eCoreType)
return null;
}

var isNeedSudo = mayNeedSudo && IsNeedSudo(coreInfo.CoreType);
try
{
Process proc = new()
Expand All @@ -298,14 +297,10 @@ private bool IsNeedSudo(ECoreType eCoreType)
}
};

var isNeedSudo = mayNeedSudo && IsNeedSudo(coreInfo.CoreType);
if (isNeedSudo)
{
proc.StartInfo.FileName = $"/bin/sudo";
proc.StartInfo.Arguments = $"-S {fileName.AppendQuotes()} {string.Format(coreInfo.Arguments, Utils.GetConfigPath(configPath).AppendQuotes())}";
proc.StartInfo.WorkingDirectory = null;
proc.StartInfo.StandardInputEncoding = Encoding.UTF8;
proc.StartInfo.RedirectStandardInput = true;
Logging.SaveLog(proc.StartInfo.Arguments);
await RunProcessAsLinuxRoot(proc, fileName, coreInfo, configPath);
}

var startUpErrorMessage = new StringBuilder();
Expand All @@ -330,7 +325,7 @@ private bool IsNeedSudo(ECoreType eCoreType)
}
proc.Start();

if (isNeedSudo)
if (isNeedSudo && _config.TunModeItem.LinuxSudoPwd.IsNotEmpty())
{
var pwd = DesUtils.Decrypt(_config.TunModeItem.LinuxSudoPwd);
await Task.Delay(10);
Expand Down Expand Up @@ -366,6 +361,37 @@ private bool IsNeedSudo(ECoreType eCoreType)
}
}

private async Task RunProcessAsLinuxRoot(Process proc, string fileName, CoreInfo coreInfo, string configPath)
{
var cmdLine = $"{fileName.AppendQuotes()} {string.Format(coreInfo.Arguments, Utils.GetConfigPath(configPath).AppendQuotes())}";

//Prefer shell scripts
var shFilePath = Utils.GetBinPath("run_as_root.sh");
File.Delete(shFilePath);
var sb = new StringBuilder();
sb.AppendLine("#!/bin/sh");
sb.AppendLine(cmdLine);
await File.WriteAllTextAsync(shFilePath, sb.ToString());
await Utils.SetLinuxChmod(shFilePath);

//Replace command
var args = File.Exists(shFilePath) ? shFilePath : cmdLine;
if (_config.TunModeItem.LinuxSudoPwd.IsNotEmpty())
{
proc.StartInfo.FileName = $"/bin/sudo";
proc.StartInfo.Arguments = $"-S {args}";
}
else
{
proc.StartInfo.FileName = $"/bin/pkexec";
proc.StartInfo.Arguments = $"{args}";
}
proc.StartInfo.WorkingDirectory = null;
proc.StartInfo.StandardInputEncoding = Encoding.UTF8;
proc.StartInfo.RedirectStandardInput = true;
Logging.SaveLog(proc.StartInfo.Arguments);
}

private async Task KillProcess(Process? proc)
{
if (proc is null)
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@
<value>Linux系统的sudo密码</value>
</data>
<data name="TbSettingsLinuxSudoPasswordTip" xml:space="preserve">
<value>密码已加密且只存储在本地文件中,无密码无法开启Tun</value>
<value>密码已加密且只存储在本地文件中,无密码则每次都要输入</value>
</data>
<data name="TbSettingsLinuxSudoPasswordIsEmpty" xml:space="preserve">
<value>请先在Tun模式设置中设置sudo密码</value>
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@
<value>Linux系統的sudo密碼</value>
</data>
<data name="TbSettingsLinuxSudoPasswordTip" xml:space="preserve">
<value>密碼已加密且只儲存在本機檔案中,無密碼無法開啟Tun</value>
<value>密碼已加密且只儲存在本機檔案中,無密碼則每次都要輸入</value>
</data>
<data name="TbSettingsLinuxSudoPasswordIsEmpty" xml:space="preserve">
<value>請先在Tun模式設定中設定sudo密碼</value>
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/ServiceLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>7.1.2-ph2</Version>
<Version>7.1.3</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
12 changes: 6 additions & 6 deletions v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,16 +416,16 @@ private async Task DoEnableTun(bool c)
// When running as a non-administrator, reboot to administrator mode
if (EnableTun && AllowEnableTun() == false)
{
_config.TunModeItem.EnableTun = false;
if (Utils.IsWindows())
{
_config.TunModeItem.EnableTun = false;
Locator.Current.GetService<MainWindowViewModel>()?.RebootAsAdmin();
return;
}
else if (Utils.IsLinux())
{
NoticeHandler.Instance.SendMessageAndEnqueue(ResUI.TbSettingsLinuxSudoPasswordIsEmpty);
}
return;
//else if (Utils.IsLinux())
//{
// NoticeHandler.Instance.SendMessageAndEnqueue(ResUI.TbSettingsLinuxSudoPasswordIsEmpty);
//}
}
await ConfigHandler.SaveConfig(_config);
Locator.Current.GetService<MainWindowViewModel>()?.Reload();
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN.Desktop/Views/ProfilesView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ private void RestoreUI()
item2.Width = new DataGridLength(item.Width, DataGridLengthUnitType.Pixel);
item2.DisplayIndex = displayIndex++;
}
if (item.Name.StartsWith("to"))
if (item.Name.ToLower().StartsWith("to"))
{
if (!_config.GuiItem.EnableStatistics)
{
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Views/ProfilesView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ private void RestoreUI()
item2.Width = item.Width;
item2.DisplayIndex = displayIndex++;
}
if (item.Name.StartsWith("to"))
if (item.Name.ToLower().StartsWith("to"))
{
if (!_config.GuiItem.EnableStatistics)
{
Expand Down

0 comments on commit dc5c753

Please sign in to comment.