1
0
mirror of https://github.com/proxysu/ProxySU.git synced 2024-11-25 14:46:08 +03:00
ProxySU/ProxySuper.Core/Services/TrojanGoProject.cs

186 lines
6.4 KiB
C#
Raw Normal View History

2021-05-13 04:58:45 +03:00
using ProxySuper.Core.Models;
2021-05-14 14:07:19 +03:00
using ProxySuper.Core.Models.Projects;
2021-05-13 04:58:45 +03:00
using Renci.SshNet;
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 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()
{
RunCmd("systemctl stop trojan-go");
RunCmd("systemctl stop caddy");
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();
WriteOutput("检测安装系统环境完成");
WriteOutput("配置服务器端口...");
ConfigurePort();
WriteOutput("端口配置完成");
WriteOutput("安装必要的系统工具...");
ConfigureSoftware();
WriteOutput("系统工具安装完成");
WriteOutput("检测IP6...");
ConfigureIPv6();
WriteOutput("检测IP6完成");
WriteOutput("配置防火墙...");
ConfigureFirewall();
WriteOutput("防火墙配置完成");
WriteOutput("同步系统和本地时间...");
SyncTimeDiff();
WriteOutput("时间同步完成");
WriteOutput("检测域名是否绑定本机IP...");
ValidateDomain();
WriteOutput("域名检测完成");
WriteOutput("安装Trojan-Go...");
InstallTrojanGo();
WriteOutput("Trojan-Go安装完成");
WriteOutput("安装Caddy...");
InstallCaddy();
WriteOutput("Caddy安装完成");
WriteOutput("启动BBR");
EnableBBR();
UploadCaddyFile();
WriteOutput("************");
WriteOutput("安装完成,尽情享用吧......");
WriteOutput("************");
}
catch (Exception ex)
{
var errorLog = "安装终止," + ex.Message;
WriteOutput(errorLog);
MessageBox.Show(errorLog);
}
}
private void InstallTrojanGo()
{
WriteOutput("安装Trojan-Go");
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 安装失败,请联系开发者!");
}
RunCmd($"sed -i 's/User=nobody/User=root/g' /etc/systemd/system/xray.service");
RunCmd($"sed -i 's/CapabilityBoundingSet=/#CapabilityBoundingSet=/g' /etc/systemd/system/xray.service");
RunCmd($"sed -i 's/AmbientCapabilities=/#AmbientCapabilities=/g' /etc/systemd/system/xray.service");
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");
}
}
}