1
0
mirror of https://github.com/proxysu/ProxySU.git synced 2024-11-28 08:06:07 +03:00

add hysteria editor

This commit is contained in:
test 2023-02-27 17:26:55 +08:00
parent 07c8d9bb2b
commit f371277362
10 changed files with 269 additions and 19 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace ProxySuper.Core.Models.Projects namespace ProxySuper.Core.Models.Projects
{ {
public class HysteriaSettings public class HysteriaSettings : IProjectSettings
{ {
public string Domain { get; set; } = ""; public string Domain { get; set; } = "";
@ -21,5 +21,13 @@ namespace ProxySuper.Core.Models.Projects
public int UpMbps { get; set; } = 300; public int UpMbps { get; set; } = 300;
public int DownMbps { get; set; } = 300; public int DownMbps { get; set; } = 300;
public List<int> FreePorts
{
get
{
return new List<int> { Port };
}
}
} }
} }

View File

@ -108,6 +108,7 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Helpers\Utils.cs" /> <Compile Include="Helpers\Utils.cs" />
<Compile Include="Services\BrookService.cs" /> <Compile Include="Services\BrookService.cs" />
<Compile Include="Services\HysteriaService.cs" />
<Compile Include="Services\MTProtoGoService.cs" /> <Compile Include="Services\MTProtoGoService.cs" />
<Compile Include="Services\NaiveProxyService.cs" /> <Compile Include="Services\NaiveProxyService.cs" />
<Compile Include="Services\ServiceBase.cs" /> <Compile Include="Services\ServiceBase.cs" />

View File

@ -0,0 +1,168 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ProxySuper.Core.Models.Hosts;
using ProxySuper.Core.Models.Projects;
using Renci.SshNet.Messages;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace ProxySuper.Core.Services
{
public class HysteriaService : ServiceBase<HysteriaSettings>
{
public HysteriaService(Host host, HysteriaSettings settings) : base(host, settings)
{
}
public void Install()
{
try
{
Progress.Step = "安装Hysteria";
Progress.Percentage = 0;
Progress.Desc = "检测系统环境";
EnsureRootUser();
EnsureSystemEnv();
Progress.Percentage = 20;
Progress.Desc = "安装必要的系统工具";
InstallSystemTools();
Progress.Percentage = 40;
Progress.Desc = "配置防火墙";
ConfigFirewalld();
Progress.Percentage = 50;
Progress.Step = "检测网络环境";
EnsureNetwork();
Progress.Percentage = 60;
Progress.Desc = "检测域名是否绑定本机IP";
ValidateDomain();
Progress.Percentage = 80;
Progress.Step = "上传Hysteria配置文件";
UploadConfigFile();
Progress.Step = "安装Hysteria服务";
InstallHysteria();
Progress.Percentage = 100;
Progress.Step = "安装Hysteria成功";
Progress.Desc = "安装Hysteria成功";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void Uninstall()
{
Task.Factory.StartNew(() =>
{
try
{
Progress.Step = "卸载Hysteria";
Progress.Percentage = 0;
Progress.Desc = "停止Hysteria服务";
RunCmd("systemctl stop Hysteria");
RunCmd("systemctl disable Hysteria");
Progress.Percentage = 30;
Progress.Desc = "删除Hysteria相关文件";
RunCmd("rm -rf /etc/systemd/system/Hysteria.service");
RunCmd("rm -rf /usr/bin/Hysteria");
Progress.Percentage = 80;
Progress.Desc = "重置防火墙设置";
ResetFirewalld();
Progress.Percentage = 100;
Progress.Desc = "卸载完成";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
});
}
private string HysteriaServiceTemp = @"
[Unit]
Description=hysteria service
After=network.target syslog.target
Wants=network.target
[Service]
Type=simple
ExecStart=##run_cmd##
[Install]
WantedBy=multi-user.target";
private void InstallHysteria()
{
Progress.Desc = "执行Hysteria安装文件";
string url = "https://github.com/apernet/hysteria/releases/download/v1.3.3/hysteria-linux-386";
string targetPath = "/user/bin/hysteria/hysteria-linux-386";
if (ArchType == ArchType.arm)
{
url = url.Replace("hysteria-linux-386", "hysteria-linux-arm");
targetPath = targetPath.Replace("hysteria-linux-386", "hysteria-linux-arm");
}
RunCmd($"curl -L {url} -o /usr/bin/hysteria");
RunCmd("chmod +x /usr/bin/hysteria");
Progress.Desc = "设置Hysteria服务";
var cmd = targetPath + " server";
var hysteriaService = HysteriaServiceTemp.Replace("##run_cmd##", cmd);
RunCmd("rm -rf /etc/systemd/system/hysteria.service");
RunCmd("touch /etc/systemd/system/hysteria.service");
RunCmd($"echo \"{hysteriaService}\" > /etc/systemd/system/hysteria.service");
RunCmd("sudo chmod 777 /etc/systemd/system/hysteria.service");
Progress.Desc = "启动Hysteria服务";
RunCmd("systemctl enable hysteria");
RunCmd("systemctl restart hysteria");
}
private const string ConfigFilePath = @"Templates\Hysteria\config.json";
private void UploadConfigFile()
{
var text = File.ReadAllText(ConfigFilePath, Encoding.UTF8);
var json = JsonConvert.DeserializeObject(text);
var obj = JToken.FromObject(json) as dynamic;
obj["listen"] = Settings.Port;
obj["acme"]["domains"][0] = Settings.Domain;
obj["email"] = Settings.Email;
obj["obfs"] = Settings.Obfs;
var configJson = JsonConvert.SerializeObject(
obj,
Formatting.Indented,
new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
});
WriteToFile(configJson, "/user/bin/hysteria/config.json");
}
}
}

View File

@ -108,6 +108,8 @@ namespace ProxySuper.Core.ViewModels
public IMvxCommand AddBrookCommand => new MvxAsyncCommand(AddBrookRecord); public IMvxCommand AddBrookCommand => new MvxAsyncCommand(AddBrookRecord);
public IMvxCommand AddHysteriaCommand => new MvxAsyncCommand(AddHysteriaRecord);
public IMvxCommand RemoveCommand => new MvxAsyncCommand<string>(DeleteRecord); public IMvxCommand RemoveCommand => new MvxAsyncCommand<string>(DeleteRecord);
public IMvxCommand EditCommand => new MvxAsyncCommand<string>(EditRecord); public IMvxCommand EditCommand => new MvxAsyncCommand<string>(EditRecord);
@ -204,6 +206,21 @@ namespace ProxySuper.Core.ViewModels
SaveToJson(); SaveToJson();
} }
public async Task AddHysteriaRecord()
{
Record record = new Record();
record.Id = Utils.GetTickID();
record.Host = new Host();
record.HysteriaSettings = new HysteriaSettings();
var result = await _navigationService.Navigate<HysteriaEditorViewModel, Record, Record>(record);
if (result == null) return;
Records.Add(result);
SaveToJson();
}
public async Task EditRecord(string id) public async Task EditRecord(string id)
{ {

View File

@ -1,9 +1,12 @@
using MvvmCross.ViewModels; using MvvmCross.Commands;
using MvvmCross.ViewModels;
using ProxySuper.Core.Models; using ProxySuper.Core.Models;
using ProxySuper.Core.Models.Hosts; using ProxySuper.Core.Models.Hosts;
using ProxySuper.Core.Models.Projects; using ProxySuper.Core.Models.Projects;
using ProxySuper.Core.Services;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -12,14 +15,52 @@ namespace ProxySuper.Core.ViewModels
{ {
public class HysteriaInstallViewModel : MvxViewModel<Record> public class HysteriaInstallViewModel : MvxViewModel<Record>
{ {
public Host Host { get; set; } public Host _host { get; set; }
public HysteriaSettings Settings { get; set; } public HysteriaSettings _settings { get; set; }
public HysteriaService _service { get; set; }
public override void Prepare(Record parameter) public override void Prepare(Record parameter)
{ {
Host = parameter.Host; _host = parameter.Host;
Settings = parameter.HysteriaSettings; _settings = parameter.HysteriaSettings;
}
public override Task Initialize()
{
_service = new HysteriaService(_host, _settings);
_service.Progress.StepUpdate = () => RaisePropertyChanged("Progress");
_service.Progress.LogsUpdate = () => RaisePropertyChanged("Logs");
_service.Connect();
return base.Initialize();
}
public override void ViewDestroy(bool viewFinishing = true)
{
_service.Disconnect();
this.SaveInstallLog();
base.ViewDestroy(viewFinishing);
}
public ProjectProgress Progress => _service.Progress;
public string Logs => _service.Progress.Logs;
public IMvxCommand InstallCommand => new MvxCommand(_service.Install);
public IMvxCommand UninstallCommand => new MvxCommand(_service.Uninstall);
private void SaveInstallLog()
{
if (!Directory.Exists("Logs"))
{
Directory.CreateDirectory("Logs");
}
var fileName = System.IO.Path.Combine("Logs", DateTime.Now.ToString("yyyy-MM-dd hh-mm") + ".hysteria.txt");
File.WriteAllText(fileName, Logs);
} }
} }
} }

View File

@ -413,6 +413,9 @@
<Generator>SettingsSingleFileGenerator</Generator> <Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput> <LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None> </None>
<None Include="Templates\Hysteria\config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Templates\NaiveProxy\naive_server.caddyfile"> <None Include="Templates\NaiveProxy\naive_server.caddyfile">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>

View File

@ -0,0 +1,10 @@
{
"listen": ":36712",
"acme": {
"domains": [
"your.domain.com"
],
"email": "your@email.com"
},
"obfs": "8ZuA2Zpqhuk8yakXvMjDqEXBwY"
}

View File

@ -21,6 +21,7 @@
<MenuItem Padding="0,5" Header="Trojan-Go" Command="{Binding AddTrojanGoCommand}"></MenuItem> <MenuItem Padding="0,5" Header="Trojan-Go" Command="{Binding AddTrojanGoCommand}"></MenuItem>
<MenuItem Padding="0,5" Header="NaiveProxy" Command="{Binding AddNaiveProxyCommand}"></MenuItem> <MenuItem Padding="0,5" Header="NaiveProxy" Command="{Binding AddNaiveProxyCommand}"></MenuItem>
<MenuItem Padding="0,5" Header="Brook" Command="{Binding AddBrookCommand}"></MenuItem> <MenuItem Padding="0,5" Header="Brook" Command="{Binding AddBrookCommand}"></MenuItem>
<MenuItem Padding="0,5" Header="Hysteria" Command="{Binding AddHysteriaCommand}"></MenuItem>
</MenuItem> </MenuItem>
<MenuItem Header="{DynamicResource MainMenuActions}" Padding="10,3"> <MenuItem Header="{DynamicResource MainMenuActions}" Padding="10,3">

View File

@ -1,4 +1,5 @@
<Window x:Class="ProxySuper.WPF.Views.Hysteria.HysteriaEditorView" <views:MvxWindow x:Class="ProxySuper.WPF.Views.Hysteria.HysteriaEditorView"
xmlns:views="clr-namespace:MvvmCross.Platforms.Wpf.Views;assembly=MvvmCross.Platforms.Wpf"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
@ -50,17 +51,17 @@
<Label Content="{DynamicResource HysteriaObfs}" Grid.Row="2" Grid.Column="0" /> <Label Content="{DynamicResource HysteriaObfs}" Grid.Row="2" Grid.Column="0" />
<TextBox Text="{Binding Settings.Obfs}" Grid.Row="2" Grid.Column="1" /> <TextBox Text="{Binding Settings.Obfs}" Grid.Row="2" Grid.Column="1" />
<Label Content="{DynamicResource HysteriaProtocol}" Grid.Row="3" Grid.Column="0" /> <Label Content="{DynamicResource HysteriaEmail}" Grid.Row="3" Grid.Column="0" />
<TextBox Text="{Binding Settings.Protocol}" Grid.Row="3" Grid.Column="1" /> <TextBox Text="{Binding Settings.Email}" Grid.Row="3" Grid.Column="1" />
<Label Content="{DynamicResource HysteriaEmail}" Grid.Row="4" Grid.Column="0" /> <!--<Label Content="{DynamicResource HysteriaProtocol}" Grid.Row="4" Grid.Column="0" />
<TextBox Text="{Binding Settings.Email}" Grid.Row="4" Grid.Column="1" /> <TextBox Text="{Binding Settings.Protocol}" Grid.Row="4" Grid.Column="1" />-->
<Label Content="{DynamicResource HysteriaUpMbps}" Grid.Row="5" Grid.Column="0" /> <!--<Label Content="{DynamicResource HysteriaUpMbps}" Grid.Row="5" Grid.Column="0" />
<TextBox Text="{Binding Settings.UpMbps}" Grid.Row="5" Grid.Column="1" /> <TextBox Text="{Binding Settings.UpMbps}" Grid.Row="5" Grid.Column="1" />
<Label Content="{DynamicResource HysteriaDownMbps}" Grid.Row="6" Grid.Column="0" /> <Label Content="{DynamicResource HysteriaDownMbps}" Grid.Row="6" Grid.Column="0" />
<TextBox Text="{Binding Settings.DownMbps}" Grid.Row="6" Grid.Column="1" /> <TextBox Text="{Binding Settings.DownMbps}" Grid.Row="6" Grid.Column="1" />-->
<Border Grid.Row="8" <Border Grid.Row="8"
Grid.ColumnSpan="3" Grid.ColumnSpan="3"
@ -84,4 +85,4 @@
</Border> </Border>
</Grid> </Grid>
</Grid> </Grid>
</Window> </views:MvxWindow>

View File

@ -1,4 +1,6 @@
using System; using MvvmCross.Platforms.Wpf.Presenters.Attributes;
using MvvmCross.Platforms.Wpf.Views;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -14,10 +16,8 @@ using System.Windows.Shapes;
namespace ProxySuper.WPF.Views.Hysteria namespace ProxySuper.WPF.Views.Hysteria
{ {
/// <summary> [MvxWindowPresentation]
/// HysteriaEditorView.xaml 的交互逻辑 public partial class HysteriaEditorView : MvxWindow
/// </summary>
public partial class HysteriaEditorView : Window
{ {
public HysteriaEditorView() public HysteriaEditorView()
{ {