From 6be1fdb84a0b9e87244e3c4526b9a5fdcf7888a2 Mon Sep 17 00:00:00 2001 From: autumn Date: Sat, 10 Jul 2021 11:57:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96Caddy=E5=AE=89=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProxySuper.Core/Models/Caddy.cs | 42 ++++++ ProxySuper.Core/ProxySuper.Core.csproj | 1 + ProxySuper.Core/Services/NaiveProxyProject.cs | 4 +- ProxySuper.Core/Services/ProjectBase.cs | 127 ++++++++++++------ ProxySuper.Core/Services/XrayProject.cs | 8 +- .../ViewModels/EnableRootViewModel.cs | 4 +- ProxySuper.WPF/Resources/Languages/en.xaml | 2 +- ProxySuper.WPF/Resources/Languages/zh_cn.xaml | 2 +- .../Views/BrookInstallerView.xaml.cs | 1 + ProxySuper.WPF/Views/EnableRootView.xaml | 2 +- .../Views/NaiveProxyInstallerView.xaml.cs | 1 + .../Views/TrojanGoInstallerView.xaml.cs | 1 + .../Views/XrayInstallerView.xaml.cs | 1 + 13 files changed, 141 insertions(+), 55 deletions(-) create mode 100644 ProxySuper.Core/Models/Caddy.cs diff --git a/ProxySuper.Core/Models/Caddy.cs b/ProxySuper.Core/Models/Caddy.cs new file mode 100644 index 0000000..93e3c22 --- /dev/null +++ b/ProxySuper.Core/Models/Caddy.cs @@ -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!""; +} +"; + } +} diff --git a/ProxySuper.Core/ProxySuper.Core.csproj b/ProxySuper.Core/ProxySuper.Core.csproj index 0fa66fd..5de987e 100644 --- a/ProxySuper.Core/ProxySuper.Core.csproj +++ b/ProxySuper.Core/ProxySuper.Core.csproj @@ -67,6 +67,7 @@ + diff --git a/ProxySuper.Core/Services/NaiveProxyProject.cs b/ProxySuper.Core/Services/NaiveProxyProject.cs index 9953763..0e53a52 100644 --- a/ProxySuper.Core/Services/NaiveProxyProject.cs +++ b/ProxySuper.Core/Services/NaiveProxyProject.cs @@ -127,13 +127,13 @@ namespace ProxySuper.Core.Services private void UploadCaddyFile(bool useCustomWeb = false) { var caddyStr = BuildConfig(useCustomWeb); - var stream = new MemoryStream(Encoding.UTF8.GetBytes(caddyStr)); if (FileExists("/etc/caddy/Caddyfile")) { RunCmd("mv /etc/caddy/Caddyfile /etc/caddy/Caddyfile.back"); } - UploadFile(stream, "/etc/caddy/Caddyfile"); + + RunCmd($"echo {caddyStr} > /etc/caddy/Caddyfile"); RunCmd("systemctl restart caddy"); } diff --git a/ProxySuper.Core/Services/ProjectBase.cs b/ProxySuper.Core/Services/ProjectBase.cs index e8d89dd..8986d42 100644 --- a/ProxySuper.Core/Services/ProjectBase.cs +++ b/ProxySuper.Core/Services/ProjectBase.cs @@ -1,10 +1,13 @@ using ProxySuper.Core.Helpers; +using ProxySuper.Core.Models; +using ProxySuper.Core.Models.Hosts; using ProxySuper.Core.Models.Projects; using Renci.SshNet; using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text; using System.Windows; namespace ProxySuper.Core.Services @@ -25,6 +28,8 @@ namespace ProxySuper.Core.Services public abstract class ProjectBase where TSettings : IProjectSettings { + + private SshClient _sshClient; protected Action WriteOutput; @@ -55,7 +60,9 @@ namespace ProxySuper.Core.Services var cmd = _sshClient.CreateCommand(cmdStr); WriteOutput(cmdStr); - var result = cmd.Execute(); + var exe = cmd.BeginExecute(); + var result = cmd.EndExecute(exe); + //var result = cmd.Execute(); WriteOutput(result); return result; } @@ -299,7 +306,6 @@ namespace ProxySuper.Core.Services /// protected void OpenPort(params int[] portList) { - string cmd; cmd = RunCmd("command -v firewall-cmd"); @@ -323,15 +329,19 @@ namespace ProxySuper.Core.Services else { cmd = RunCmd("command -v ufw"); - if (!string.IsNullOrEmpty(cmd)) + if (string.IsNullOrEmpty(cmd)) { - foreach (var port in portList) - { - RunCmd($"ufw allow {port}/tcp"); - RunCmd($"ufw allow {port}/udp"); - } - RunCmd("yes | ufw reload"); + RunCmd(GetInstallCmd("ufw")); + RunCmd("echo y | ufw enable"); } + + foreach (var port in portList) + { + RunCmd($"ufw allow {port}/tcp"); + RunCmd($"ufw allow {port}/udp"); + } + RunCmd("yes | ufw reload"); + } } @@ -425,30 +435,55 @@ namespace ProxySuper.Core.Services /// 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"); - 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"); + url = "https://github.com/caddyserver/caddy/releases/download/v2.4.3/caddy_2.4.3_linux_armv7.tar.gz"; } - if (CmdType == CmdType.Dnf) - { - RunCmd("dnf install -y 'dnf-command(copr)'"); - RunCmd("dnf copr -y enable @caddy/caddy"); - RunCmd("dnf install -y caddy"); - } + RunCmd($"wget -O caddy.tar.gz {url}"); + RunCmd("mkdir /etc/caddy"); + RunCmd("tar -zxvf caddy.tar.gz caddy -C /etc/caddy"); + RunCmd("cp -rf /etc/caddy/caddy /usr/bin"); + 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("yum install -y yum-plugin-copr"); - RunCmd("yum copr -y enable @caddy/caddy"); - RunCmd("yum install -y caddy"); - } + RunCmd("mkdir /usr/share/caddy"); + #endregion - 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 } /// @@ -457,21 +492,8 @@ namespace ProxySuper.Core.Services protected void UninstallCaddy() { RunCmd("systemctl stop caddy"); - if (CmdType == CmdType.Apt) - { - 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("systemctl disable caddy"); + RunCmd("rm -rf /etc/systemd/system/caddy.service"); RunCmd("rm -rf /usr/bin/caddy"); RunCmd("rm -rf /usr/share/caddy"); RunCmd("rm -rf /etc/caddy"); @@ -748,6 +770,25 @@ namespace ProxySuper.Core.Services 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(); + } + } + } + } + /// /// 上传文件 /// diff --git a/ProxySuper.Core/Services/XrayProject.cs b/ProxySuper.Core/Services/XrayProject.cs index 9c743cb..8c4e4bf 100644 --- a/ProxySuper.Core/Services/XrayProject.cs +++ b/ProxySuper.Core/Services/XrayProject.cs @@ -164,7 +164,6 @@ namespace ProxySuper.Core.Services certName: "xray_ssl.crt", keyName: "xray_ssl.key"); - RunCmd("systemctl restart xray"); WriteOutput("************ 安装证书完成 ************"); } @@ -247,12 +246,12 @@ namespace ProxySuper.Core.Services private void UploadCaddyFile(bool useCustomWeb = false) { var configJson = XrayConfigBuilder.BuildCaddyConfig(Parameters, useCustomWeb); - var stream = new MemoryStream(Encoding.UTF8.GetBytes(configJson)); + if (FileExists("/etc/caddy/Caddyfile")) { RunCmd("mv /etc/caddy/Caddyfile /etc/caddy/Caddyfile.back"); } - UploadFile(stream, "/etc/caddy/Caddyfile"); + WriteToFile(configJson, "/etc/caddy/Caddyfile"); RunCmd("systemctl restart caddy"); } @@ -295,8 +294,7 @@ namespace ProxySuper.Core.Services var configJson = XrayConfigBuilder.BuildXrayConfig(Parameters); - var stream = new MemoryStream(Encoding.UTF8.GetBytes(configJson)); - UploadFile(stream, "/usr/local/etc/xray/config.json"); + WriteToFile(configJson, "/usr/local/etc/xray/config.json"); RunCmd("systemctl restart xray"); } diff --git a/ProxySuper.Core/ViewModels/EnableRootViewModel.cs b/ProxySuper.Core/ViewModels/EnableRootViewModel.cs index e4bc5d9..96b45d1 100644 --- a/ProxySuper.Core/ViewModels/EnableRootViewModel.cs +++ b/ProxySuper.Core/ViewModels/EnableRootViewModel.cs @@ -76,8 +76,8 @@ namespace ProxySuper.Core.ViewModels result = RunCmd(@"cat /dev/urandom | tr -dc '_A-Z#\-+=a-z(0-9%^>)]{<|' | head -c 20 ; echo ''"); string setPassword = result.TrimEnd('\r', '\n') + '\n'; - RunCmd(cmdPre + $"echo -e \"{setPassword}{setPassword}\" | sudo passwd root"); - RunCmd("sudo systemctl restart sshd "); + RunCmd(cmdPre + $"echo \"{setPassword}{setPassword}\" | sudo passwd root"); + RunCmd("sudo systemctl restart sshd"); RootUserName = "root"; RootPassword = setPassword.Trim('\n'); diff --git a/ProxySuper.WPF/Resources/Languages/en.xaml b/ProxySuper.WPF/Resources/Languages/en.xaml index e6ec17b..6e66ce3 100644 --- a/ProxySuper.WPF/Resources/Languages/en.xaml +++ b/ProxySuper.WPF/Resources/Languages/en.xaml @@ -9,7 +9,7 @@ Install Settings The following is a static web page connection provided by netizens, please check whether there is an index.html file by yourself - + Add Host Actions diff --git a/ProxySuper.WPF/Resources/Languages/zh_cn.xaml b/ProxySuper.WPF/Resources/Languages/zh_cn.xaml index c927ee1..8db8c0a 100644 --- a/ProxySuper.WPF/Resources/Languages/zh_cn.xaml +++ b/ProxySuper.WPF/Resources/Languages/zh_cn.xaml @@ -9,7 +9,7 @@ 安装 配置 如下是网友提供的静态网页连接,请自行检查是否有index.html文件 - + 添加主机 操作 diff --git a/ProxySuper.WPF/Views/BrookInstallerView.xaml.cs b/ProxySuper.WPF/Views/BrookInstallerView.xaml.cs index 03d4b47..071fa64 100644 --- a/ProxySuper.WPF/Views/BrookInstallerView.xaml.cs +++ b/ProxySuper.WPF/Views/BrookInstallerView.xaml.cs @@ -38,6 +38,7 @@ namespace ProxySuper.WPF.Views WriteOutput("正在登陆服务器 ..."); var conneInfo = CreateConnectionInfo(ViewModel.Host); + conneInfo.Timeout = TimeSpan.FromSeconds(60); _sshClient = new SshClient(conneInfo); try { diff --git a/ProxySuper.WPF/Views/EnableRootView.xaml b/ProxySuper.WPF/Views/EnableRootView.xaml index 549b1f8..a3c1fe1 100644 --- a/ProxySuper.WPF/Views/EnableRootView.xaml +++ b/ProxySuper.WPF/Views/EnableRootView.xaml @@ -62,7 +62,7 @@