From 3b79a7ca44107e6f5d17614be55ab1bee8a910c1 Mon Sep 17 00:00:00 2001 From: nuxt-autumn Date: Sun, 16 May 2021 09:12:47 +0800 Subject: [PATCH] update --- .../{Services/Base64.cs => Helpers/Utils.cs} | 17 +- ProxySuper.Core/Models/Hosts/Host.cs | 2 - .../Models/Projects/IProjectSettings.cs | 10 +- .../Models/Projects/TrojanGoSettings.cs | 8 +- .../Models/Projects/XraySettings.cs | 26 +- ProxySuper.Core/Models/Record.cs | 42 ++- ProxySuper.Core/ProxySuper.Core.csproj | 2 +- ProxySuper.Core/Services/ShareLink.cs | 4 +- ProxySuper.Core/ViewModels/HomeViewModel.cs | 27 +- .../ViewModels/XrayEditorViewModel.cs | 33 ++- ProxySuper.WPF/Controls/HostControl.xaml | 8 +- .../Controls/XraySettingsControl.xaml | 248 +++++++++--------- ProxySuper.WPF/Resources/Languages/en.xaml | 5 +- ProxySuper.WPF/Resources/Languages/zh_cn.xaml | 5 +- ProxySuper.WPF/Views/HomeView.xaml | 10 +- ProxySuper.WPF/Views/HomeView.xaml.cs | 2 +- ProxySuper.WPF/Views/XrayEditorView.xaml | 34 ++- 17 files changed, 300 insertions(+), 183 deletions(-) rename ProxySuper.Core/{Services/Base64.cs => Helpers/Utils.cs} (51%) diff --git a/ProxySuper.Core/Services/Base64.cs b/ProxySuper.Core/Helpers/Utils.cs similarity index 51% rename from ProxySuper.Core/Services/Base64.cs rename to ProxySuper.Core/Helpers/Utils.cs index d315656..764c276 100644 --- a/ProxySuper.Core/Services/Base64.cs +++ b/ProxySuper.Core/Helpers/Utils.cs @@ -1,23 +1,32 @@ -using System; +using Newtonsoft.Json; +using System; using System.Collections.Generic; +using System.IO; using System.Linq; +using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading.Tasks; namespace ProxySuper.Core.Services { - public class Base64 + public class Utils { - public static string Encode(string plainText) + public static string Base64Encode(string plainText) { var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText); return System.Convert.ToBase64String(plainTextBytes); } - public static string Decode(string base64EncodedData) + public static string Base64Decode(string base64EncodedData) { var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData); return System.Text.Encoding.UTF8.GetString(base64EncodedBytes); } + + public static T DeepClone(T obj) where T : class + { + var serialized = JsonConvert.SerializeObject(obj); + return JsonConvert.DeserializeObject(serialized); + } } } diff --git a/ProxySuper.Core/Models/Hosts/Host.cs b/ProxySuper.Core/Models/Hosts/Host.cs index 6331846..af9c61e 100644 --- a/ProxySuper.Core/Models/Hosts/Host.cs +++ b/ProxySuper.Core/Models/Hosts/Host.cs @@ -6,8 +6,6 @@ using System.Threading.Tasks; namespace ProxySuper.Core.Models.Hosts { - - public class Host { diff --git a/ProxySuper.Core/Models/Projects/IProjectSettings.cs b/ProxySuper.Core/Models/Projects/IProjectSettings.cs index 6bd2874..45b45eb 100644 --- a/ProxySuper.Core/Models/Projects/IProjectSettings.cs +++ b/ProxySuper.Core/Models/Projects/IProjectSettings.cs @@ -6,26 +6,26 @@ using System.Threading.Tasks; namespace ProxySuper.Core.Models.Projects { - public interface IProjectSettings + public class IProjectSettings { /// /// 端口 /// - int Port { get; set; } + public virtual int Port { get; set; } /// /// 域名 /// - string Domain { get; set; } + public virtual string Domain { get; set; } /// /// 额外需要开放的端口 /// - List FreePorts { get; } + public virtual List FreePorts { get; } /// /// 类型 /// - ProjectType Type { get; set; } + public virtual ProjectType Type { get; set; } } } diff --git a/ProxySuper.Core/Models/Projects/TrojanGoSettings.cs b/ProxySuper.Core/Models/Projects/TrojanGoSettings.cs index f33a19f..71ef261 100644 --- a/ProxySuper.Core/Models/Projects/TrojanGoSettings.cs +++ b/ProxySuper.Core/Models/Projects/TrojanGoSettings.cs @@ -8,7 +8,7 @@ namespace ProxySuper.Core.Models.Projects { public class TrojanGoSettings : IProjectSettings { - public List FreePorts + public override List FreePorts { get { @@ -16,17 +16,17 @@ namespace ProxySuper.Core.Models.Projects } } - public ProjectType Type { get; set; } = ProjectType.TrojanGo; + public override ProjectType Type { get; set; } = ProjectType.TrojanGo; /// /// 域名 /// - public string Domain { get; set; } + public override string Domain { get; set; } /// /// 端口 /// - public int Port { get; set; } + public override int Port { get; set; } /// /// 密码 diff --git a/ProxySuper.Core/Models/Projects/XraySettings.cs b/ProxySuper.Core/Models/Projects/XraySettings.cs index e5f0d8b..45d83ab 100644 --- a/ProxySuper.Core/Models/Projects/XraySettings.cs +++ b/ProxySuper.Core/Models/Projects/XraySettings.cs @@ -8,7 +8,7 @@ namespace ProxySuper.Core.Models.Projects { public partial class XraySettings : IProjectSettings { - public List FreePorts + public override List FreePorts { get { @@ -21,23 +21,23 @@ namespace ProxySuper.Core.Models.Projects } } - public ProjectType Type { get; set; } = ProjectType.Xray; + public override ProjectType Type { get; set; } = ProjectType.Xray; + + /// + /// 端口 + /// + public override int Port { get; set; } + + /// + /// 域名 + /// + public override string Domain { get; set; } /// /// UUID /// public string UUID { get; set; } - /// - /// 端口 - /// - public int Port { get; set; } - - /// - /// 域名 - /// - public string Domain { get; set; } - /// /// 伪装域名 /// @@ -46,7 +46,7 @@ namespace ProxySuper.Core.Models.Projects /// /// 安装类型 /// - public List Types { get; set; } + public List Types { get; set; } = new List(); /// /// 根据xray类型获取路径 diff --git a/ProxySuper.Core/Models/Record.cs b/ProxySuper.Core/Models/Record.cs index dfcad7c..8b94828 100644 --- a/ProxySuper.Core/Models/Record.cs +++ b/ProxySuper.Core/Models/Record.cs @@ -1,7 +1,12 @@ -using Newtonsoft.Json; +using MvvmCross; +using MvvmCross.Commands; +using MvvmCross.Navigation; +using MvvmCross.ViewModels; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using ProxySuper.Core.Models.Hosts; using ProxySuper.Core.Models.Projects; +using ProxySuper.Core.ViewModels; using System; using System.Collections.Generic; using System.Linq; @@ -10,19 +15,46 @@ using System.Threading.Tasks; namespace ProxySuper.Core.Models { - public class Record + public class Record : MvxViewModel { + [JsonProperty("id")] + public string Id { get; set; } + + [JsonProperty("host")] public Host Host { get; set; } [JsonProperty("settings")] - public dynamic Settings { get; set; } + public XraySettings XraySettings { get; set; } + + [JsonProperty("trojanGoSettings")] + public TrojanGoSettings TrojanGoSettings { get; set; } - public string Type + [JsonIgnore] + public ProjectType Type { get { - return Settings.type ?? "Xray"; + if (XraySettings != null) return ProjectType.Xray; + return ProjectType.TrojanGo; + } + } + + + public IMvxCommand NavToEditorCommand => new MvxAsyncCommand(NavigateToEditor); + + public async Task NavigateToEditor() + { + var nav = Mvx.IoCProvider.Resolve(); + if (Type == ProjectType.Xray) + { + var result = await nav.Navigate(this); + if (result == null) return; + + this.Host = result.Host; + this.XraySettings = result.XraySettings; + + await RaisePropertyChanged("Host"); } } } diff --git a/ProxySuper.Core/ProxySuper.Core.csproj b/ProxySuper.Core/ProxySuper.Core.csproj index 6e97043..a060376 100644 --- a/ProxySuper.Core/ProxySuper.Core.csproj +++ b/ProxySuper.Core/ProxySuper.Core.csproj @@ -77,7 +77,7 @@ - + diff --git a/ProxySuper.Core/Services/ShareLink.cs b/ProxySuper.Core/Services/ShareLink.cs index 11e683c..0fb6651 100644 --- a/ProxySuper.Core/Services/ShareLink.cs +++ b/ProxySuper.Core/Services/ShareLink.cs @@ -41,7 +41,7 @@ namespace ProxySuper.Core.Services var _server = settings.Domain; var _port = settings.ShadowSocksPort; - var base64URL = Base64.Encode($"{_method}:{_password}@{_server}:{_port}"); + var base64URL = Utils.Base64Encode($"{_method}:{_password}@{_server}:{_port}"); return "ss://" + base64URL + "#ShadowSocks"; } @@ -88,7 +88,7 @@ namespace ProxySuper.Core.Services return string.Empty; } - var base64Url = Base64.Encode(JsonConvert.SerializeObject(vmess)); + var base64Url = Utils.Base64Encode(JsonConvert.SerializeObject(vmess)); return $"vmess://" + base64Url; } diff --git a/ProxySuper.Core/ViewModels/HomeViewModel.cs b/ProxySuper.Core/ViewModels/HomeViewModel.cs index a8022ea..fa20fc7 100644 --- a/ProxySuper.Core/ViewModels/HomeViewModel.cs +++ b/ProxySuper.Core/ViewModels/HomeViewModel.cs @@ -1,7 +1,10 @@ -using MvvmCross.Navigation; +using MvvmCross.Commands; +using MvvmCross.Navigation; using MvvmCross.ViewModels; using Newtonsoft.Json; using ProxySuper.Core.Models; +using ProxySuper.Core.Models.Hosts; +using ProxySuper.Core.Models.Projects; using System; using System.Collections.Generic; using System.IO; @@ -26,14 +29,32 @@ namespace ProxySuper.Core.ViewModels var json = File.ReadAllText("Data/Record.json"); var records = JsonConvert.DeserializeObject>(json); this.Records = new MvxObservableCollection(); + records.ForEach(item => { + if (string.IsNullOrEmpty(item.Id)) + { + item.Id = Guid.NewGuid().ToString(); + } this.Records.Add(item); }); - - } public MvxObservableCollection Records { get; set; } + + public IMvxCommand AddXrayCommand => new MvxAsyncCommand(AddXrayRecord); + + public async Task AddXrayRecord() + { + Record record = new Record(); + record.Id = Guid.NewGuid().ToString(); + record.Host = new Host(); + record.XraySettings = new XraySettings(); + + var result = await _navigationService.Navigate(record); + if (result == null) return; + + Records.Add(result); + } } } diff --git a/ProxySuper.Core/ViewModels/XrayEditorViewModel.cs b/ProxySuper.Core/ViewModels/XrayEditorViewModel.cs index 580472a..c08dfc6 100644 --- a/ProxySuper.Core/ViewModels/XrayEditorViewModel.cs +++ b/ProxySuper.Core/ViewModels/XrayEditorViewModel.cs @@ -1,4 +1,6 @@ -using MvvmCross.Commands; +using MvvmCross; +using MvvmCross.Commands; +using MvvmCross.Navigation; using MvvmCross.ViewModels; using Newtonsoft.Json; using ProxySuper.Core.Models; @@ -15,21 +17,42 @@ using System.Windows.Input; namespace ProxySuper.Core.ViewModels { - public partial class XrayEditorViewModel : MvxViewModel + public partial class XrayEditorViewModel : MvxViewModel { - public XrayEditorViewModel() + private IMvxNavigationService _navigationService; + public XrayEditorViewModel(IMvxNavigationService mvxNavigationService) { + _navigationService = mvxNavigationService; _randomUuid = new MvxCommand(() => GetUuid()); + SaveCommand = new MvxCommand(() => Save()); } + + public string Id { get; set; } + public Host Host { get; set; } public XraySettings Settings { get; set; } + public IMvxCommand SaveCommand { get; } + public override void Prepare(Record parameter) { - Host = parameter.Host; - Settings = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(parameter.Settings)); + var record = Utils.DeepClone(parameter); + Id = record.Id; + Host = record.Host; + Settings = record.XraySettings; + } + + public void Save() + { + var result = new Record() + { + Id = Id, + Host = Host, + XraySettings = Settings, + }; + _navigationService.Close(this, result); } } diff --git a/ProxySuper.WPF/Controls/HostControl.xaml b/ProxySuper.WPF/Controls/HostControl.xaml index ce397ce..0515293 100644 --- a/ProxySuper.WPF/Controls/HostControl.xaml +++ b/ProxySuper.WPF/Controls/HostControl.xaml @@ -45,26 +45,26 @@