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
|
|
|
{
|
2024-08-10 02:02:00 +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;
|
2024-08-10 02:02:00 +00:00
|
|
|
_updateView = updateView;
|
2023-01-01 11:42:01 +00:00
|
|
|
|
2024-10-21 05:46:13 +00:00
|
|
|
SaveCmd = ReactiveCommand.CreateFromTask(async () =>
|
|
|
|
|
{
|
|
|
|
|
await SaveServerAsync();
|
|
|
|
|
});
|
|
|
|
|
|
2024-10-23 09:00:33 +00:00
|
|
|
if (profileItem.IndexId.IsNullOrEmpty())
|
2023-01-01 11:42:01 +00:00
|
|
|
{
|
2024-10-23 09:00:33 +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
|
|
|
}
|
2024-10-23 09:00:33 +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
|
|
|
{
|
2025-03-05 11:44:49 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-05 11:44:49 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2024-10-23 09:00:33 +00:00
|
|
|
var port = SelectedSource.Port.ToString();
|
2025-03-05 11:44:49 +00:00
|
|
|
if (port.IsNullOrEmpty() || !Utils.IsNumeric(port)
|
2024-10-23 09:00:33 +00:00
|
|
|
|| 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;
|
|
|
|
|
}
|
2024-10-23 09:00:33 +00:00
|
|
|
if (SelectedSource.ConfigType == EConfigType.Shadowsocks)
|
2023-01-01 11:42:01 +00:00
|
|
|
{
|
2025-03-05 11:44:49 +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;
|
|
|
|
|
}
|
2025-03-05 11:44:49 +00:00
|
|
|
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
|
|
|
{
|
2025-03-05 11:44:49 +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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-23 09:00:33 +00:00
|
|
|
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
|
|
|
}
|