v2rayN/v2rayN/ServiceLib/ViewModels/AddServerViewModel.cs

97 lines
3.2 KiB
C#
Raw Normal View History

2023-01-01 11:42:01 +00:00
using System.Reactive;
2025-01-30 09:10:05 +00:00
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
2023-01-01 11:42:01 +00:00
2024-08-20 06:15:29 +00:00
namespace ServiceLib.ViewModels
2023-01-01 11:42:01 +00:00
{
public class AddServerViewModel : MyReactiveObject
2023-01-01 11:42:01 +00:00
{
[Reactive]
public ProfileItem SelectedSource { get; set; }
2024-08-30 05:15:25 +00:00
2024-08-27 05:18:18 +00:00
[Reactive]
public string? CoreType { get; set; }
2023-01-01 11:42:01 +00:00
public ReactiveCommand<Unit, Unit> SaveCmd { get; }
2024-08-22 11:51:10 +00:00
public AddServerViewModel(ProfileItem profileItem, Func<EViewAction, object?, Task<bool>>? updateView)
2023-01-01 11:42:01 +00:00
{
2024-10-07 01:51:41 +00:00
_config = AppHandler.Instance.Config;
_updateView = updateView;
2023-01-01 11:42:01 +00:00
2024-10-21 05:46:13 +00:00
SaveCmd = ReactiveCommand.CreateFromTask(async () =>
{
await SaveServerAsync();
});
if (profileItem.IndexId.IsNullOrEmpty())
2023-01-01 11:42:01 +00:00
{
profileItem.Network = Global.DefaultNetwork;
profileItem.HeaderType = Global.None;
profileItem.RequestHost = "";
profileItem.StreamSecurity = "";
2023-01-01 11:42:01 +00:00
SelectedSource = profileItem;
}
else
{
2024-03-26 06:26:03 +00:00
SelectedSource = JsonUtils.DeepCopy(profileItem);
2023-01-01 11:42:01 +00:00
}
CoreType = SelectedSource?.CoreType?.ToString();
2023-01-01 11:42:01 +00:00
}
2024-08-22 11:51:10 +00:00
private async Task SaveServerAsync()
2023-01-01 11:42:01 +00:00
{
if (SelectedSource.Remarks.IsNullOrEmpty())
2023-01-01 11:42:01 +00:00
{
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.Enqueue(ResUI.PleaseFillRemarks);
2023-01-01 11:42:01 +00:00
return;
}
if (SelectedSource.Address.IsNullOrEmpty())
2023-01-01 11:42:01 +00:00
{
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.Enqueue(ResUI.FillServerAddress);
2023-01-01 11:42:01 +00:00
return;
}
var port = SelectedSource.Port.ToString();
if (port.IsNullOrEmpty() || !Utils.IsNumeric(port)
|| SelectedSource.Port <= 0 || SelectedSource.Port >= Global.MaxPort)
2023-01-01 11:42:01 +00:00
{
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.Enqueue(ResUI.FillCorrectServerPort);
2023-01-01 11:42:01 +00:00
return;
}
if (SelectedSource.ConfigType == EConfigType.Shadowsocks)
2023-01-01 11:42:01 +00:00
{
if (SelectedSource.Id.IsNullOrEmpty())
2023-01-01 11:42:01 +00:00
{
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.Enqueue(ResUI.FillPassword);
2023-01-01 11:42:01 +00:00
return;
}
if (SelectedSource.Security.IsNullOrEmpty())
2023-01-01 11:42:01 +00:00
{
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.Enqueue(ResUI.PleaseSelectEncryption);
2023-01-01 11:42:01 +00:00
return;
}
}
2025-03-06 06:34:31 +00:00
if (SelectedSource.ConfigType is not EConfigType.SOCKS and not EConfigType.HTTP)
2023-01-01 11:42:01 +00:00
{
if (SelectedSource.Id.IsNullOrEmpty())
2023-01-01 11:42:01 +00:00
{
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.Enqueue(ResUI.FillUUID);
2023-01-01 11:42:01 +00:00
return;
}
}
SelectedSource.CoreType = CoreType.IsNullOrEmpty() ? null : (ECoreType)Enum.Parse(typeof(ECoreType), CoreType);
2023-01-01 11:42:01 +00:00
2024-10-20 03:51:05 +00:00
if (await ConfigHandler.AddServer(_config, SelectedSource) == 0)
2023-01-01 11:42:01 +00:00
{
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.Enqueue(ResUI.OperationSuccess);
2024-10-08 06:55:06 +00:00
_updateView?.Invoke(EViewAction.CloseWindow, null);
2023-01-01 11:42:01 +00:00
}
else
{
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.Enqueue(ResUI.OperationFailed);
2023-01-01 11:42:01 +00:00
}
}
}
2025-01-30 09:10:05 +00:00
}