2021-07-08 13:37:32 +03:00
|
|
|
|
using ProxySuper.Core.Models.Projects;
|
2021-05-13 04:58:45 +03:00
|
|
|
|
using Renci.SshNet;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
|
|
|
|
namespace ProxySuper.Core.Services
|
|
|
|
|
{
|
|
|
|
|
public class TrojanGoProject : ProjectBase<TrojanGoSettings>
|
|
|
|
|
{
|
|
|
|
|
public TrojanGoProject(SshClient sshClient, TrojanGoSettings parameters, Action<string> writeOutput) : base(sshClient, parameters, writeOutput)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-21 13:03:40 +03:00
|
|
|
|
public void InstallCertToTrojanGo()
|
|
|
|
|
{
|
|
|
|
|
EnsureRootAuth();
|
|
|
|
|
EnsureSystemEnv();
|
|
|
|
|
InstallCert(
|
|
|
|
|
dirPath: "/usr/local/etc/trojan-go",
|
|
|
|
|
certName: "trojan-go.crt",
|
|
|
|
|
keyName: "trojan-go.key");
|
|
|
|
|
|
|
|
|
|
RunCmd("systemctl restart trojan-go");
|
|
|
|
|
WriteOutput("************ 安装证书完成 ************");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UploadWeb(Stream stream)
|
|
|
|
|
{
|
|
|
|
|
EnsureRootAuth();
|
|
|
|
|
EnsureSystemEnv();
|
|
|
|
|
if (!FileExists("/usr/share/caddy"))
|
|
|
|
|
{
|
|
|
|
|
RunCmd("mkdir /usr/share/caddy");
|
|
|
|
|
}
|
|
|
|
|
RunCmd("rm -rf /usr/share/caddy/*");
|
|
|
|
|
UploadFile(stream, "/usr/share/caddy/caddy.zip");
|
|
|
|
|
RunCmd("unzip /usr/share/caddy/caddy.zip -d /usr/share/caddy");
|
|
|
|
|
RunCmd("chmod -R 777 /usr/share/caddy");
|
|
|
|
|
UploadCaddyFile(useCustomWeb: true);
|
|
|
|
|
WriteOutput("************ 上传网站模板完成 ************");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Uninstall()
|
|
|
|
|
{
|
2021-06-11 06:00:57 +03:00
|
|
|
|
EnsureRootAuth();
|
|
|
|
|
EnsureSystemEnv();
|
|
|
|
|
|
2021-05-21 13:03:40 +03:00
|
|
|
|
RunCmd("systemctl stop trojan-go");
|
2021-05-24 13:57:17 +03:00
|
|
|
|
base.UninstallCaddy();
|
2021-05-21 13:03:40 +03:00
|
|
|
|
|
|
|
|
|
RunCmd("rm -rf /usr/local/bin/trojan-go");
|
|
|
|
|
RunCmd("rm -rf /usr/local/etc/trojan-go");
|
|
|
|
|
|
|
|
|
|
RunCmd("acme.sh --uninstall");
|
|
|
|
|
RunCmd("rm -r ~/.acme.sh");
|
|
|
|
|
|
|
|
|
|
WriteOutput("卸载Trojan-Go完成");
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 04:58:45 +03:00
|
|
|
|
public override void Install()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
EnsureRootAuth();
|
|
|
|
|
|
2021-05-21 13:03:40 +03:00
|
|
|
|
if (FileExists("/usr/local/bin/trojan-go"))
|
2021-05-13 04:58:45 +03:00
|
|
|
|
{
|
2021-05-21 13:03:40 +03:00
|
|
|
|
var btnResult = MessageBox.Show("已经安装Trojan-Go,是否需要重装?", "提示", MessageBoxButton.YesNo);
|
2021-05-13 04:58:45 +03:00
|
|
|
|
if (btnResult == MessageBoxResult.No)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("安装终止", "提示");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WriteOutput("检测安装系统环境...");
|
|
|
|
|
EnsureSystemEnv();
|
2021-07-12 12:58:51 +03:00
|
|
|
|
WriteOutput("检测安装系统环境完成");
|
2021-05-13 04:58:45 +03:00
|
|
|
|
|
|
|
|
|
WriteOutput("安装必要的系统工具...");
|
|
|
|
|
ConfigureSoftware();
|
|
|
|
|
WriteOutput("系统工具安装完成");
|
|
|
|
|
|
|
|
|
|
WriteOutput("配置防火墙...");
|
2021-07-12 12:58:51 +03:00
|
|
|
|
ConfigFirewalld();
|
2021-05-13 04:58:45 +03:00
|
|
|
|
WriteOutput("防火墙配置完成");
|
|
|
|
|
|
2021-07-13 05:12:15 +03:00
|
|
|
|
WriteOutput("检测网络环境");
|
|
|
|
|
EnsureIP();
|
|
|
|
|
WriteOutput("检测网络环境完成");
|
|
|
|
|
|
2021-05-13 04:58:45 +03:00
|
|
|
|
WriteOutput("同步系统和本地时间...");
|
|
|
|
|
SyncTimeDiff();
|
|
|
|
|
WriteOutput("时间同步完成");
|
|
|
|
|
|
|
|
|
|
WriteOutput("检测域名是否绑定本机IP...");
|
|
|
|
|
ValidateDomain();
|
|
|
|
|
WriteOutput("域名检测完成");
|
|
|
|
|
|
|
|
|
|
WriteOutput("安装Caddy...");
|
|
|
|
|
InstallCaddy();
|
2021-05-24 13:57:17 +03:00
|
|
|
|
UploadCaddyFile();
|
2021-05-13 04:58:45 +03:00
|
|
|
|
WriteOutput("Caddy安装完成");
|
|
|
|
|
|
2021-05-26 14:12:13 +03:00
|
|
|
|
WriteOutput("安装Trojan-Go...");
|
|
|
|
|
InstallTrojanGo();
|
|
|
|
|
WriteOutput("Trojan-Go安装完成");
|
|
|
|
|
|
|
|
|
|
|
2021-05-13 04:58:45 +03:00
|
|
|
|
WriteOutput("启动BBR");
|
|
|
|
|
EnableBBR();
|
|
|
|
|
|
2021-05-24 13:57:17 +03:00
|
|
|
|
RunCmd("systemctl restart trojan-go");
|
2021-05-13 04:58:45 +03:00
|
|
|
|
WriteOutput("************");
|
|
|
|
|
WriteOutput("安装完成,尽情享用吧......");
|
|
|
|
|
WriteOutput("************");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
var errorLog = "安装终止," + ex.Message;
|
|
|
|
|
WriteOutput(errorLog);
|
2021-06-18 15:53:46 +03:00
|
|
|
|
MessageBox.Show("安装失败,请联系开发者或上传日志文件(Logs文件夹下)到github提问。");
|
2021-05-13 04:58:45 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InstallTrojanGo()
|
|
|
|
|
{
|
|
|
|
|
RunCmd(@"curl https://raw.githubusercontent.com/proxysu/shellscript/master/trojan-go.sh yes | bash");
|
2021-05-21 13:03:40 +03:00
|
|
|
|
var success = FileExists("/usr/local/bin/trojan-go");
|
2021-05-13 04:58:45 +03:00
|
|
|
|
if (success == false)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("trojan-go 安装失败,请联系开发者!");
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 13:57:17 +03:00
|
|
|
|
RunCmd($"sed -i 's/User=nobody/User=root/g' /etc/systemd/system/trojan-go.service");
|
|
|
|
|
RunCmd($"sed -i 's/CapabilityBoundingSet=/#CapabilityBoundingSet=/g' /etc/systemd/system/trojan-go.service");
|
|
|
|
|
RunCmd($"sed -i 's/AmbientCapabilities=/#AmbientCapabilities=/g' /etc/systemd/system/trojan-go.service");
|
2021-05-13 04:58:45 +03:00
|
|
|
|
RunCmd($"systemctl daemon-reload");
|
|
|
|
|
|
|
|
|
|
RunCmd("systemctl enable trojan-go");
|
|
|
|
|
RunCmd("systemctl start trojan-go");
|
|
|
|
|
WriteOutput("Trojan-Go 安装完成");
|
|
|
|
|
|
|
|
|
|
InstallCert(
|
|
|
|
|
dirPath: "/usr/local/etc/trojan-go",
|
|
|
|
|
certName: "trojan-go.crt",
|
|
|
|
|
keyName: "trojan-go.key");
|
|
|
|
|
|
|
|
|
|
if (FileExists("/usr/local/etc/trojan-go/config.json"))
|
|
|
|
|
{
|
|
|
|
|
RunCmd("mv /usr/local/etc/trojan-go/config.json config.json.old");
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-21 13:03:40 +03:00
|
|
|
|
UploadTrojanGoSettings();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UploadTrojanGoSettings()
|
|
|
|
|
{
|
2021-05-13 04:58:45 +03:00
|
|
|
|
// 上传配置
|
|
|
|
|
var settings = TrojanGoConfigBuilder.BuildTrojanGoConfig(Parameters);
|
|
|
|
|
var stream = new MemoryStream(Encoding.UTF8.GetBytes(settings));
|
|
|
|
|
UploadFile(stream, "/usr/local/etc/trojan-go/config.json");
|
|
|
|
|
RunCmd("systemctl restart trojan-go");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UploadCaddyFile(bool useCustomWeb = false)
|
|
|
|
|
{
|
|
|
|
|
var config = TrojanGoConfigBuilder.BuildCaddyConfig(Parameters, useCustomWeb);
|
|
|
|
|
var stream = new MemoryStream(Encoding.UTF8.GetBytes(config));
|
|
|
|
|
if (FileExists("/etc/caddy/Caddyfile"))
|
|
|
|
|
{
|
|
|
|
|
RunCmd("mv /etc/caddy/Caddyfile /etc/caddy/Caddyfile.back");
|
|
|
|
|
}
|
|
|
|
|
UploadFile(stream, "/etc/caddy/Caddyfile");
|
|
|
|
|
RunCmd("systemctl restart caddy");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|