From be9ae4a8de3ca832466660853100ef573f9f3110 Mon Sep 17 00:00:00 2001 From: autumn Date: Mon, 23 Aug 2021 15:59:34 +0800 Subject: [PATCH] MTProxy-Go --- .../Models/Projects/ProjectType.cs | 1 + ProxySuper.Core/Models/Record.cs | 6 ++++ ProxySuper.Core/ProxySuper.Core.csproj | 1 + ProxySuper.Core/Services/BrookService.cs | 3 ++ ProxySuper.Core/Services/MTProxyGoService.cs | 27 +++++++++++----- ProxySuper.Core/Services/NaiveProxyService.cs | 3 ++ ProxySuper.Core/Services/ServiceBase.cs | 17 +++++----- ProxySuper.Core/Services/TrojanGoService.cs | 2 ++ ProxySuper.Core/ViewModels/HomeViewModel.cs | 20 ++++++++++++ .../ViewModels/MTProxyGoConfigViewModel.cs | 21 ++++++++++++ .../ViewModels/MTProxyGoInstallViewModel.cs | 19 +++++++++-- ProxySuper.WPF/ProxySuper.WPF.csproj | 13 ++++++-- .../Views/MTProxyGo/MTProxyGoConfigView.xaml | 32 +++++++++++++++++++ .../MTProxyGo/MTProxyGoConfigView.xaml.cs | 28 ++++++++++++++++ ...allView.xaml => MTProxyGoInstallView.xaml} | 2 +- ...w.xaml.cs => MTProxyGoInstallView.xaml.cs} | 4 +-- 16 files changed, 175 insertions(+), 24 deletions(-) create mode 100644 ProxySuper.Core/ViewModels/MTProxyGoConfigViewModel.cs create mode 100644 ProxySuper.WPF/Views/MTProxyGo/MTProxyGoConfigView.xaml create mode 100644 ProxySuper.WPF/Views/MTProxyGo/MTProxyGoConfigView.xaml.cs rename ProxySuper.WPF/Views/MTProxyGo/{MTProxyInstallView.xaml => MTProxyGoInstallView.xaml} (98%) rename ProxySuper.WPF/Views/MTProxyGo/{MTProxyInstallView.xaml.cs => MTProxyGoInstallView.xaml.cs} (85%) diff --git a/ProxySuper.Core/Models/Projects/ProjectType.cs b/ProxySuper.Core/Models/Projects/ProjectType.cs index 027a80a..58549b0 100644 --- a/ProxySuper.Core/Models/Projects/ProjectType.cs +++ b/ProxySuper.Core/Models/Projects/ProjectType.cs @@ -7,5 +7,6 @@ NaiveProxy = 2, Brook = 3, V2ray = 4, + MTProxyGo = 5, } } diff --git a/ProxySuper.Core/Models/Record.cs b/ProxySuper.Core/Models/Record.cs index ac36903..522920a 100644 --- a/ProxySuper.Core/Models/Record.cs +++ b/ProxySuper.Core/Models/Record.cs @@ -3,6 +3,7 @@ using Newtonsoft.Json; using ProxySuper.Core.Models.Hosts; using ProxySuper.Core.Models.Projects; using ProxySuper.Core.Services; +using System; using System.Text; namespace ProxySuper.Core.Models @@ -64,6 +65,8 @@ namespace ProxySuper.Core.Models if (NaiveProxySettings != null) return ProjectType.NaiveProxy; + if (MTProxyGoSettings != null) return ProjectType.MTProxyGo; + return ProjectType.Brook; } } @@ -82,6 +85,9 @@ namespace ProxySuper.Core.Models } } + [JsonIgnore] + public Action OnSave { get; set; } = () => { }; + public string GetShareLink() { if (Type == ProjectType.V2ray) diff --git a/ProxySuper.Core/ProxySuper.Core.csproj b/ProxySuper.Core/ProxySuper.Core.csproj index 6d6fd8e..bb0752f 100644 --- a/ProxySuper.Core/ProxySuper.Core.csproj +++ b/ProxySuper.Core/ProxySuper.Core.csproj @@ -105,6 +105,7 @@ + diff --git a/ProxySuper.Core/Services/BrookService.cs b/ProxySuper.Core/Services/BrookService.cs index ff75996..aefa01a 100644 --- a/ProxySuper.Core/Services/BrookService.cs +++ b/ProxySuper.Core/Services/BrookService.cs @@ -69,6 +69,9 @@ namespace ProxySuper.Core.Services Progress.Percentage = 100; Progress.Step = "安装Brook成功"; Progress.Desc = "安装Brook成功"; + + AppendCommand("分享连接:"); + AppendCommand(ShareLink.BuildBrook(Settings)); } catch (Exception ex) { diff --git a/ProxySuper.Core/Services/MTProxyGoService.cs b/ProxySuper.Core/Services/MTProxyGoService.cs index 5c429d9..acb6a24 100644 --- a/ProxySuper.Core/Services/MTProxyGoService.cs +++ b/ProxySuper.Core/Services/MTProxyGoService.cs @@ -41,7 +41,7 @@ namespace ProxySuper.Core.Services Progress.Percentage = 50; Progress.Step = "5. 生成密钥"; - Settings.SecretText = RunCmd($"docker run nineseconds/mtg generate-secret {Settings.Cleartext}"); + Settings.SecretText = RunCmd($"docker run nineseconds/mtg generate-secret {Settings.Cleartext}").TrimEnd('\n'); Progress.Percentage = 65; Progress.Step = "6. 生成配置文件"; @@ -49,8 +49,8 @@ namespace ProxySuper.Core.Services RunCmd("touch /etc/mtg.toml"); Progress.Desc = "写入配置内容"; - RunCmd($"echo secret=\"{Settings.SecretText}\" > /etc/mtg.toml"); - RunCmd($"echo bind-to=\"0.0.0.0:{Settings.Port}\" >> /etc/mtg.toml"); + RunCmd($"echo \"secret=\\\"{Settings.SecretText}\\\"\" > /etc/mtg.toml"); + RunCmd($"echo \"bind-to=\\\"0.0.0.0:{Settings.Port}\\\"\" >> /etc/mtg.toml"); Progress.Percentage = 80; Progress.Step = "7. 启动MTProxy服务"; @@ -60,6 +60,10 @@ namespace ProxySuper.Core.Services Progress.Step = "安装完成"; Progress.Percentage = 100; + AppendCommand("Host: " + Settings.Domain); + AppendCommand("Port: " + Settings.Port); + AppendCommand("Secret: " + Settings.SecretText); + } catch (Exception ex) { @@ -102,17 +106,20 @@ namespace ProxySuper.Core.Services try { Progress.Percentage = 0; - Progress.Step = "卸载MTProxy"; + Progress.Step = "更新MTProxy配置"; - - Progress.Desc = "停止MTProxy服务"; + Progress.Desc = "暂停MTProxy服务"; var cid = RunCmd("docker ps -q --filter name=mtg"); RunCmd($"docker stop {cid}"); Progress.Percentage = 50; + Progress.Desc = "生成密钥"; + Settings.SecretText = RunCmd($"docker run nineseconds/mtg generate-secret {Settings.Cleartext}").TrimEnd('\n'); + Progress.Percentage = 65; + Progress.Desc = "修改配置文件"; - RunCmd($"echo secret=\"{Settings.SecretText}\" > /etc/mtg.toml"); - RunCmd($"echo bind-to=\"0.0.0.0:{Settings.Port}\" >> /etc/mtg.toml"); + RunCmd($"echo \"secret=\\\"{Settings.SecretText}\\\"\" > /etc/mtg.toml"); + RunCmd($"echo \"bind-to=\\\"0.0.0.0:{Settings.Port}\\\"\" >> /etc/mtg.toml"); Progress.Percentage = 80; Progress.Desc = "重启MTProxy服务"; @@ -120,6 +127,10 @@ namespace ProxySuper.Core.Services Progress.Percentage = 100; Progress.Desc = "更新配置成功"; + + AppendCommand("Host: " + Settings.Domain); + AppendCommand("Port: " + Settings.Port); + AppendCommand("Secret: " + Settings.SecretText); } catch (Exception ex) { diff --git a/ProxySuper.Core/Services/NaiveProxyService.cs b/ProxySuper.Core/Services/NaiveProxyService.cs index a74f9be..dfbf008 100644 --- a/ProxySuper.Core/Services/NaiveProxyService.cs +++ b/ProxySuper.Core/Services/NaiveProxyService.cs @@ -66,6 +66,9 @@ namespace ProxySuper.Core.Services Progress.Percentage = 100; Progress.Step = "NaiveProxy安装成功"; Progress.Desc = string.Empty; + + AppendCommand("分享连接:"); + AppendCommand(ShareLink.BuildNaiveProxy(Settings)); } catch (Exception ex) { diff --git a/ProxySuper.Core/Services/ServiceBase.cs b/ProxySuper.Core/Services/ServiceBase.cs index f28d268..d6168b9 100644 --- a/ProxySuper.Core/Services/ServiceBase.cs +++ b/ProxySuper.Core/Services/ServiceBase.cs @@ -561,6 +561,15 @@ namespace ProxySuper.Core.Services RunCmd("mv /etc/resolv.conf.proxysu /etc/resolv.conf"); } + protected void AppendCommand(string command) + { + if (!command.EndsWith("\n")) + { + command += "\n"; + } + Progress.Logs += command; + } + private List FilterFastestIP() { string[] gateNat64 = { @@ -739,14 +748,6 @@ namespace ProxySuper.Core.Services } } - private void AppendCommand(string command) - { - if (!command.EndsWith("\n")) - { - command += "\n"; - } - Progress.Logs += command; - } private ConnectionInfo CreateConnectionInfo() { diff --git a/ProxySuper.Core/Services/TrojanGoService.cs b/ProxySuper.Core/Services/TrojanGoService.cs index 46058e4..eac9470 100644 --- a/ProxySuper.Core/Services/TrojanGoService.cs +++ b/ProxySuper.Core/Services/TrojanGoService.cs @@ -82,6 +82,8 @@ namespace ProxySuper.Core.Services RunCmd("systemctl enable trojan-go"); RunCmd("systemctl restart trojan-go"); + AppendCommand("分享连接:"); + AppendCommand(ShareLink.BuildTrojanGo(Settings)); Progress.Percentage = 100; Progress.Step = "安装成功"; diff --git a/ProxySuper.Core/ViewModels/HomeViewModel.cs b/ProxySuper.Core/ViewModels/HomeViewModel.cs index 370d28f..2d01c58 100644 --- a/ProxySuper.Core/ViewModels/HomeViewModel.cs +++ b/ProxySuper.Core/ViewModels/HomeViewModel.cs @@ -247,6 +247,14 @@ namespace ProxySuper.Core.ViewModels record.Host = result.Host; record.BrookSettings = result.BrookSettings; } + if (record.Type == ProjectType.MTProxyGo) + { + result = await _navigationService.Navigate(record); + if (result == null) return; + + record.Host = result.Host; + record.MTProxyGoSettings = result.MTProxyGoSettings; + } SaveToJson(); } @@ -291,12 +299,17 @@ namespace ProxySuper.Core.ViewModels { await _navigationService.Navigate(record.BrookSettings); } + if (record.Type == ProjectType.MTProxyGo) + { + await _navigationService.Navigate(record.MTProxyGoSettings); + } } public async Task GoToInstall(string id) { var record = Records.FirstOrDefault(x => x.Id == id); if (record == null) return; + record.OnSave = SaveToJson; if (record.Type == ProjectType.V2ray) { @@ -318,6 +331,13 @@ namespace ProxySuper.Core.ViewModels { await _navigationService.Navigate(record); } + if (record.Type == ProjectType.MTProxyGo) + { + await _navigationService.Navigate(record); + } + + SaveToJson(); } + } } diff --git a/ProxySuper.Core/ViewModels/MTProxyGoConfigViewModel.cs b/ProxySuper.Core/ViewModels/MTProxyGoConfigViewModel.cs new file mode 100644 index 0000000..b9473dc --- /dev/null +++ b/ProxySuper.Core/ViewModels/MTProxyGoConfigViewModel.cs @@ -0,0 +1,21 @@ +using MvvmCross.ViewModels; +using ProxySuper.Core.Models; +using ProxySuper.Core.Models.Projects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProxySuper.Core.ViewModels +{ + public class MTProxyGoConfigViewModel : MvxViewModel + { + public MTProxyGoSettings Settings { get; set; } + + public override void Prepare(MTProxyGoSettings parameter) + { + Settings = parameter; + } + } +} diff --git a/ProxySuper.Core/ViewModels/MTProxyGoInstallViewModel.cs b/ProxySuper.Core/ViewModels/MTProxyGoInstallViewModel.cs index 59886cd..74888c1 100644 --- a/ProxySuper.Core/ViewModels/MTProxyGoInstallViewModel.cs +++ b/ProxySuper.Core/ViewModels/MTProxyGoInstallViewModel.cs @@ -21,10 +21,13 @@ namespace ProxySuper.Core.ViewModels MTProxyGoService _mtproxyService; + Action _onSave; + public override void Prepare(Record parameter) { _host = parameter.Host; _settings = parameter.MTProxyGoSettings; + _onSave = parameter.OnSave; } public override Task Initialize() @@ -56,9 +59,21 @@ namespace ProxySuper.Core.ViewModels #region Command - public IMvxCommand InstallCommand => new MvxCommand(_mtproxyService.Install); + public IMvxCommand InstallCommand => new MvxCommand(() => + { + _mtproxyService.Install(); - public IMvxCommand UpdateSettingsCommand => new MvxCommand(_mtproxyService.UpdateSettings); + // 安装时生成的Secret需要保存 + _onSave(); + }); + + public IMvxCommand UpdateSettingsCommand => new MvxCommand(() => + { + _mtproxyService.UpdateSettings(); + + // 安装时生成的Secret需要保存 + _onSave(); + }); public IMvxCommand UninstallCommand => new MvxCommand(_mtproxyService.Uninstall); diff --git a/ProxySuper.WPF/ProxySuper.WPF.csproj b/ProxySuper.WPF/ProxySuper.WPF.csproj index 66be202..bc4d57b 100644 --- a/ProxySuper.WPF/ProxySuper.WPF.csproj +++ b/ProxySuper.WPF/ProxySuper.WPF.csproj @@ -135,11 +135,14 @@ HomeView.xaml + + MTProxyGoConfigView.xaml + MTProxyGoEditorView.xaml - - MTProxyInstallView.xaml + + MTProxyGoInstallView.xaml NaiveProxyConfigView.xaml @@ -284,11 +287,15 @@ MSBuild:Compile PreserveNewest + + Designer + MSBuild:Compile + Designer MSBuild:Compile - + Designer MSBuild:Compile diff --git a/ProxySuper.WPF/Views/MTProxyGo/MTProxyGoConfigView.xaml b/ProxySuper.WPF/Views/MTProxyGo/MTProxyGoConfigView.xaml new file mode 100644 index 0000000..064606e --- /dev/null +++ b/ProxySuper.WPF/Views/MTProxyGo/MTProxyGoConfigView.xaml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + diff --git a/ProxySuper.WPF/Views/MTProxyGo/MTProxyGoConfigView.xaml.cs b/ProxySuper.WPF/Views/MTProxyGo/MTProxyGoConfigView.xaml.cs new file mode 100644 index 0000000..d64b71e --- /dev/null +++ b/ProxySuper.WPF/Views/MTProxyGo/MTProxyGoConfigView.xaml.cs @@ -0,0 +1,28 @@ +using MvvmCross.Platforms.Wpf.Views; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Shapes; + +namespace ProxySuper.WPF.Views.MTProxyGo +{ + /// + /// MTProxyGoConfigView.xaml 的交互逻辑 + /// + public partial class MTProxyGoConfigView : MvxWindow + { + public MTProxyGoConfigView() + { + InitializeComponent(); + } + } +} diff --git a/ProxySuper.WPF/Views/MTProxyGo/MTProxyInstallView.xaml b/ProxySuper.WPF/Views/MTProxyGo/MTProxyGoInstallView.xaml similarity index 98% rename from ProxySuper.WPF/Views/MTProxyGo/MTProxyInstallView.xaml rename to ProxySuper.WPF/Views/MTProxyGo/MTProxyGoInstallView.xaml index 210485e..a67d821 100644 --- a/ProxySuper.WPF/Views/MTProxyGo/MTProxyInstallView.xaml +++ b/ProxySuper.WPF/Views/MTProxyGo/MTProxyGoInstallView.xaml @@ -1,4 +1,4 @@ - /// MTProxyInstallView.xaml 的交互逻辑 /// - public partial class MTProxyInstallView : MvxWindow + public partial class MTProxyGoInstallView : MvxWindow { - public MTProxyInstallView() + public MTProxyGoInstallView() { InitializeComponent(); }