2025-04-25 07:19:49 +00:00
|
|
|
using Avalonia.Controls;
|
|
|
|
|
using Avalonia.Threading;
|
2025-07-27 02:56:58 +00:00
|
|
|
using CliWrap.Buffered;
|
2025-04-25 07:19:49 +00:00
|
|
|
using DialogHostAvalonia;
|
2025-08-17 08:52:51 +00:00
|
|
|
using ServiceLib.Manager;
|
2025-04-25 07:19:49 +00:00
|
|
|
|
|
|
|
|
namespace v2rayN.Desktop.Views;
|
|
|
|
|
|
|
|
|
|
public partial class SudoPasswordInputView : UserControl
|
|
|
|
|
{
|
|
|
|
|
public SudoPasswordInputView()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
|
|
|
|
this.Loaded += (s, e) => txtPassword.Focus();
|
|
|
|
|
|
2025-07-27 02:56:58 +00:00
|
|
|
btnSave.Click += async (_, _) => await SavePasswordAsync();
|
|
|
|
|
|
|
|
|
|
btnCancel.Click += (_, _) =>
|
|
|
|
|
{
|
|
|
|
|
DialogHost.Close(null);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task SavePasswordAsync()
|
|
|
|
|
{
|
|
|
|
|
if (txtPassword.Text.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
txtPassword.Focus();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var password = txtPassword.Text;
|
|
|
|
|
btnSave.IsEnabled = false;
|
|
|
|
|
|
|
|
|
|
try
|
2025-04-25 07:19:49 +00:00
|
|
|
{
|
2025-07-27 02:56:58 +00:00
|
|
|
// Verify if the password is correct
|
|
|
|
|
if (await CheckSudoPasswordAsync(password))
|
2025-04-25 07:19:49 +00:00
|
|
|
{
|
2025-07-27 02:56:58 +00:00
|
|
|
// Password verification successful, return password and close dialog
|
|
|
|
|
await Dispatcher.UIThread.InvokeAsync(() =>
|
|
|
|
|
{
|
|
|
|
|
DialogHost.Close(null, password);
|
|
|
|
|
});
|
2025-04-25 07:19:49 +00:00
|
|
|
}
|
2025-07-27 02:56:58 +00:00
|
|
|
else
|
2025-04-25 07:19:49 +00:00
|
|
|
{
|
2025-07-27 02:56:58 +00:00
|
|
|
// Password verification failed, display error and let user try again
|
|
|
|
|
NoticeHandler.Instance.Enqueue(ResUI.SudoIncorrectPasswordTip);
|
|
|
|
|
txtPassword.Focus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logging.SaveLog("SudoPassword", ex);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
btnSave.IsEnabled = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-25 07:19:49 +00:00
|
|
|
|
2025-07-27 02:56:58 +00:00
|
|
|
private async Task<bool> CheckSudoPasswordAsync(string password)
|
|
|
|
|
{
|
|
|
|
|
try
|
2025-04-25 07:19:49 +00:00
|
|
|
{
|
2025-07-29 11:28:09 +00:00
|
|
|
// Use sudo echo command to verify password
|
2025-07-29 12:23:42 +00:00
|
|
|
var arg = new List<string>() { "-c", "sudo -S echo SUDO_CHECK" };
|
2025-07-30 11:52:45 +00:00
|
|
|
var result = await CliWrap.Cli.Wrap(Global.LinuxBash)
|
2025-07-27 02:56:58 +00:00
|
|
|
.WithArguments(arg)
|
|
|
|
|
.WithStandardInputPipe(CliWrap.PipeSource.FromString(password))
|
|
|
|
|
.ExecuteBufferedAsync();
|
|
|
|
|
|
|
|
|
|
return result.ExitCode == 0;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logging.SaveLog("CheckSudoPassword", ex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-04-25 07:19:49 +00:00
|
|
|
}
|
|
|
|
|
}
|