1
0
mirror of https://github.com/proxysu/ProxySU.git synced 2024-11-22 21:26:09 +03:00

优化Caddy安装

This commit is contained in:
autumn 2021-07-10 11:57:56 +08:00
parent c33de96563
commit 6be1fdb84a
13 changed files with 141 additions and 55 deletions

View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProxySuper.Core.Models
{
public static class Caddy
{
public static string Service = @"
[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target
[Service]
#User=caddy
#Group=caddy
User=root
Group=root
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
#LimitNOFILE=1048576
#LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
#AmbientCapabilities=CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target
";
public static string DefaultCaddyFile = @"
:80 {
respond ""Hello world!"";
}
";
}
}

View File

@ -67,6 +67,7 @@
<Compile Include="Converters\ProxyTypeConverter.cs" /> <Compile Include="Converters\ProxyTypeConverter.cs" />
<Compile Include="Converters\VisibleConverter.cs" /> <Compile Include="Converters\VisibleConverter.cs" />
<Compile Include="Helpers\DateTimeUtils.cs" /> <Compile Include="Helpers\DateTimeUtils.cs" />
<Compile Include="Models\Caddy.cs" />
<Compile Include="Models\Hosts\Host.cs" /> <Compile Include="Models\Hosts\Host.cs" />
<Compile Include="Models\Hosts\LocalProxyType.cs" /> <Compile Include="Models\Hosts\LocalProxyType.cs" />
<Compile Include="Models\Hosts\LoginSecretType.cs" /> <Compile Include="Models\Hosts\LoginSecretType.cs" />

View File

@ -127,13 +127,13 @@ namespace ProxySuper.Core.Services
private void UploadCaddyFile(bool useCustomWeb = false) private void UploadCaddyFile(bool useCustomWeb = false)
{ {
var caddyStr = BuildConfig(useCustomWeb); var caddyStr = BuildConfig(useCustomWeb);
var stream = new MemoryStream(Encoding.UTF8.GetBytes(caddyStr));
if (FileExists("/etc/caddy/Caddyfile")) if (FileExists("/etc/caddy/Caddyfile"))
{ {
RunCmd("mv /etc/caddy/Caddyfile /etc/caddy/Caddyfile.back"); RunCmd("mv /etc/caddy/Caddyfile /etc/caddy/Caddyfile.back");
} }
UploadFile(stream, "/etc/caddy/Caddyfile");
RunCmd($"echo {caddyStr} > /etc/caddy/Caddyfile");
RunCmd("systemctl restart caddy"); RunCmd("systemctl restart caddy");
} }

View File

@ -1,10 +1,13 @@
using ProxySuper.Core.Helpers; using ProxySuper.Core.Helpers;
using ProxySuper.Core.Models;
using ProxySuper.Core.Models.Hosts;
using ProxySuper.Core.Models.Projects; using ProxySuper.Core.Models.Projects;
using Renci.SshNet; using Renci.SshNet;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text;
using System.Windows; using System.Windows;
namespace ProxySuper.Core.Services namespace ProxySuper.Core.Services
@ -25,6 +28,8 @@ namespace ProxySuper.Core.Services
public abstract class ProjectBase<TSettings> where TSettings : IProjectSettings public abstract class ProjectBase<TSettings> where TSettings : IProjectSettings
{ {
private SshClient _sshClient; private SshClient _sshClient;
protected Action<string> WriteOutput; protected Action<string> WriteOutput;
@ -55,7 +60,9 @@ namespace ProxySuper.Core.Services
var cmd = _sshClient.CreateCommand(cmdStr); var cmd = _sshClient.CreateCommand(cmdStr);
WriteOutput(cmdStr); WriteOutput(cmdStr);
var result = cmd.Execute(); var exe = cmd.BeginExecute();
var result = cmd.EndExecute(exe);
//var result = cmd.Execute();
WriteOutput(result); WriteOutput(result);
return result; return result;
} }
@ -299,7 +306,6 @@ namespace ProxySuper.Core.Services
/// <param name="portList"></param> /// <param name="portList"></param>
protected void OpenPort(params int[] portList) protected void OpenPort(params int[] portList)
{ {
string cmd; string cmd;
cmd = RunCmd("command -v firewall-cmd"); cmd = RunCmd("command -v firewall-cmd");
@ -323,15 +329,19 @@ namespace ProxySuper.Core.Services
else else
{ {
cmd = RunCmd("command -v ufw"); cmd = RunCmd("command -v ufw");
if (!string.IsNullOrEmpty(cmd)) if (string.IsNullOrEmpty(cmd))
{ {
RunCmd(GetInstallCmd("ufw"));
RunCmd("echo y | ufw enable");
}
foreach (var port in portList) foreach (var port in portList)
{ {
RunCmd($"ufw allow {port}/tcp"); RunCmd($"ufw allow {port}/tcp");
RunCmd($"ufw allow {port}/udp"); RunCmd($"ufw allow {port}/udp");
} }
RunCmd("yes | ufw reload"); RunCmd("yes | ufw reload");
}
} }
} }
@ -425,30 +435,55 @@ namespace ProxySuper.Core.Services
/// </summary> /// </summary>
protected void InstallCaddy() protected void InstallCaddy()
{ {
if (CmdType == CmdType.Apt) #region
RunCmd("rm -rf caddy.tar.gz");
RunCmd("rm -rf /etc/caddy");
RunCmd("rm -rf /usr/share/caddy");
var url = "https://github.com/caddyserver/caddy/releases/download/v2.4.3/caddy_2.4.3_linux_amd64.tar.gz";
if (ArchType == ArchType.arm)
{ {
RunCmd("apt install -y debian-keyring debian-archive-keyring apt-transport-https"); url = "https://github.com/caddyserver/caddy/releases/download/v2.4.3/caddy_2.4.3_linux_armv7.tar.gz";
RunCmd("echo yes | curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo apt-key add -");
RunCmd("echo yes | curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list");
RunCmd("sudo apt -y update");
RunCmd("sudo apt install -y caddy");
} }
if (CmdType == CmdType.Dnf) RunCmd($"wget -O caddy.tar.gz {url}");
{ RunCmd("mkdir /etc/caddy");
RunCmd("dnf install -y 'dnf-command(copr)'"); RunCmd("tar -zxvf caddy.tar.gz caddy -C /etc/caddy");
RunCmd("dnf copr -y enable @caddy/caddy"); RunCmd("cp -rf /etc/caddy/caddy /usr/bin");
RunCmd("dnf install -y caddy"); WriteToFile(Caddy.DefaultCaddyFile, "/etc/caddy/Caddyfile");
} WriteToFile(Caddy.Service, "/etc/systemd/system/caddy.service");
RunCmd("systemctl daemon-reload");
RunCmd("systemctl enable caddy");
if (CmdType == CmdType.Yum) RunCmd("mkdir /usr/share/caddy");
{ #endregion
RunCmd("yum install -y yum-plugin-copr");
RunCmd("yum copr -y enable @caddy/caddy");
RunCmd("yum install -y caddy");
}
RunCmd("systemctl enable caddy.service"); #region
//if (CmdType == CmdType.Apt)
//{
// RunCmd("apt install -y debian-keyring debian-archive-keyring apt-transport-https");
// RunCmd("echo yes | curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo apt-key add -");
// RunCmd("echo yes | curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list");
// RunCmd("sudo apt -y update");
// RunCmd("sudo apt install -y caddy");
//}
//if (CmdType == CmdType.Dnf)
//{
// RunCmd("dnf install -y 'dnf-command(copr)'");
// RunCmd("dnf copr -y enable @caddy/caddy");
// RunCmd("dnf install -y caddy");
//}
//if (CmdType == CmdType.Yum)
//{
// RunCmd("yum install -y yum-plugin-copr");
// RunCmd("yum copr -y enable @caddy/caddy");
// RunCmd("yum install -y caddy");
//}
//RunCmd("systemctl enable caddy.service");
#endregion
} }
/// <summary> /// <summary>
@ -457,21 +492,8 @@ namespace ProxySuper.Core.Services
protected void UninstallCaddy() protected void UninstallCaddy()
{ {
RunCmd("systemctl stop caddy"); RunCmd("systemctl stop caddy");
if (CmdType == CmdType.Apt) RunCmd("systemctl disable caddy");
{ RunCmd("rm -rf /etc/systemd/system/caddy.service");
RunCmd("sudo apt -y remove caddy");
}
if (CmdType == CmdType.Dnf)
{
RunCmd("dnf -y remove caddy");
}
if (CmdType == CmdType.Yum)
{
RunCmd("yum -y remove caddy");
}
RunCmd("rm -rf /usr/bin/caddy"); RunCmd("rm -rf /usr/bin/caddy");
RunCmd("rm -rf /usr/share/caddy"); RunCmd("rm -rf /usr/share/caddy");
RunCmd("rm -rf /etc/caddy"); RunCmd("rm -rf /etc/caddy");
@ -748,6 +770,25 @@ namespace ProxySuper.Core.Services
RunCmd($"chmod 755 {dirPath}"); RunCmd($"chmod 755 {dirPath}");
} }
protected void WriteToFile(string text, string path)
{
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(text)))
{
using (var sftp = new SftpClient(_sshClient.ConnectionInfo))
{
sftp.Connect();
try
{
sftp.UploadFile(stream, path, true);
}
finally
{
sftp.Disconnect();
}
}
}
}
/// <summary> /// <summary>
/// 上传文件 /// 上传文件
/// </summary> /// </summary>

View File

@ -164,7 +164,6 @@ namespace ProxySuper.Core.Services
certName: "xray_ssl.crt", certName: "xray_ssl.crt",
keyName: "xray_ssl.key"); keyName: "xray_ssl.key");
RunCmd("systemctl restart xray");
WriteOutput("************ 安装证书完成 ************"); WriteOutput("************ 安装证书完成 ************");
} }
@ -247,12 +246,12 @@ namespace ProxySuper.Core.Services
private void UploadCaddyFile(bool useCustomWeb = false) private void UploadCaddyFile(bool useCustomWeb = false)
{ {
var configJson = XrayConfigBuilder.BuildCaddyConfig(Parameters, useCustomWeb); var configJson = XrayConfigBuilder.BuildCaddyConfig(Parameters, useCustomWeb);
var stream = new MemoryStream(Encoding.UTF8.GetBytes(configJson));
if (FileExists("/etc/caddy/Caddyfile")) if (FileExists("/etc/caddy/Caddyfile"))
{ {
RunCmd("mv /etc/caddy/Caddyfile /etc/caddy/Caddyfile.back"); RunCmd("mv /etc/caddy/Caddyfile /etc/caddy/Caddyfile.back");
} }
UploadFile(stream, "/etc/caddy/Caddyfile"); WriteToFile(configJson, "/etc/caddy/Caddyfile");
RunCmd("systemctl restart caddy"); RunCmd("systemctl restart caddy");
} }
@ -295,8 +294,7 @@ namespace ProxySuper.Core.Services
var configJson = XrayConfigBuilder.BuildXrayConfig(Parameters); var configJson = XrayConfigBuilder.BuildXrayConfig(Parameters);
var stream = new MemoryStream(Encoding.UTF8.GetBytes(configJson)); WriteToFile(configJson, "/usr/local/etc/xray/config.json");
UploadFile(stream, "/usr/local/etc/xray/config.json");
RunCmd("systemctl restart xray"); RunCmd("systemctl restart xray");
} }

View File

@ -76,7 +76,7 @@ namespace ProxySuper.Core.ViewModels
result = RunCmd(@"cat /dev/urandom | tr -dc '_A-Z#\-+=a-z(0-9%^>)]{<|' | head -c 20 ; echo ''"); result = RunCmd(@"cat /dev/urandom | tr -dc '_A-Z#\-+=a-z(0-9%^>)]{<|' | head -c 20 ; echo ''");
string setPassword = result.TrimEnd('\r', '\n') + '\n'; string setPassword = result.TrimEnd('\r', '\n') + '\n';
RunCmd(cmdPre + $"echo -e \"{setPassword}{setPassword}\" | sudo passwd root"); RunCmd(cmdPre + $"echo \"{setPassword}{setPassword}\" | sudo passwd root");
RunCmd("sudo systemctl restart sshd"); RunCmd("sudo systemctl restart sshd");
RootUserName = "root"; RootUserName = "root";

View File

@ -38,6 +38,7 @@ namespace ProxySuper.WPF.Views
WriteOutput("正在登陆服务器 ..."); WriteOutput("正在登陆服务器 ...");
var conneInfo = CreateConnectionInfo(ViewModel.Host); var conneInfo = CreateConnectionInfo(ViewModel.Host);
conneInfo.Timeout = TimeSpan.FromSeconds(60);
_sshClient = new SshClient(conneInfo); _sshClient = new SshClient(conneInfo);
try try
{ {

View File

@ -62,7 +62,7 @@
<Label Content="{DynamicResource HostPassword}" Grid.Row="2" Grid.Column="0" /> <Label Content="{DynamicResource HostPassword}" Grid.Row="2" Grid.Column="0" />
<TextBox IsReadOnly="True" Text="{Binding RootPassword}" Grid.Row="2" Grid.Column="1" /> <TextBox IsReadOnly="True" Text="{Binding RootPassword}" Grid.Row="2" Grid.Column="1" />
<Button Height="28" Grid.Row="3" Grid.Column="3" Command="{Binding ExecuteCommand}">一键Root</Button> <Button Height="28" Grid.Row="3" Grid.Column="3" Command="{Binding ExecuteCommand}" Content="{DynamicResource MainMenuActionsGetRoot}" />
</Grid> </Grid>
</GroupBox> </GroupBox>
</StackPanel> </StackPanel>

View File

@ -47,6 +47,7 @@ namespace ProxySuper.WPF.Views
WriteOutput("正在登陆服务器 ..."); WriteOutput("正在登陆服务器 ...");
var conneInfo = CreateConnectionInfo(ViewModel.Host); var conneInfo = CreateConnectionInfo(ViewModel.Host);
conneInfo.Timeout = TimeSpan.FromSeconds(60);
_sshClient = new SshClient(conneInfo); _sshClient = new SshClient(conneInfo);
try try
{ {

View File

@ -44,6 +44,7 @@ namespace ProxySuper.WPF.Views
WriteOutput("正在登陆服务器 ..."); WriteOutput("正在登陆服务器 ...");
var conneInfo = CreateConnectionInfo(ViewModel.Host); var conneInfo = CreateConnectionInfo(ViewModel.Host);
conneInfo.Timeout = TimeSpan.FromSeconds(60);
_sshClient = new SshClient(conneInfo); _sshClient = new SshClient(conneInfo);
try try
{ {

View File

@ -75,6 +75,7 @@ namespace ProxySuper.WPF.Views
{ {
WriteOutput("正在登陆服务器 ..."); WriteOutput("正在登陆服务器 ...");
var conneInfo = CreateConnectionInfo(ViewModel.Host); var conneInfo = CreateConnectionInfo(ViewModel.Host);
conneInfo.Timeout = TimeSpan.FromSeconds(60);
_sshClient = new SshClient(conneInfo); _sshClient = new SshClient(conneInfo);
try try
{ {