2021-07-08 13:37:32 +03:00
|
|
|
|
using ProxySuper.Core.Models.Projects;
|
2021-05-25 13:28:37 +03:00
|
|
|
|
using Renci.SshNet;
|
2021-05-24 13:57:17 +03:00
|
|
|
|
using System;
|
2021-05-25 13:28:37 +03:00
|
|
|
|
using System.IO;
|
2021-05-24 13:57:17 +03:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
|
|
|
|
namespace ProxySuper.Core.Services
|
|
|
|
|
{
|
|
|
|
|
public class NaiveProxyProject : ProjectBase<NaiveProxySettings>
|
|
|
|
|
{
|
2021-05-25 13:28:37 +03:00
|
|
|
|
public NaiveProxyProject(SshClient sshClient, NaiveProxySettings parameters, Action<string> writeOutput) : base(sshClient, parameters, writeOutput)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Uninstall()
|
|
|
|
|
{
|
2021-06-11 06:00:57 +03:00
|
|
|
|
RunCmd("rm -rf caddy_install.sh");
|
|
|
|
|
RunCmd("curl -o caddy_install.sh https://raw.githubusercontent.com/proxysu/shellscript/master/Caddy-Naive/caddy-naive-install.sh");
|
|
|
|
|
RunCmd("yes | bash caddy_install.sh uninstall");
|
|
|
|
|
RunCmd("rm -rf caddy_install.sh");
|
2021-05-25 13:28:37 +03:00
|
|
|
|
WriteOutput("ProxyNaive卸载完成");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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("************ 上传网站模板完成 ************");
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 13:57:17 +03:00
|
|
|
|
public override void Install()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
EnsureRootAuth();
|
|
|
|
|
|
|
|
|
|
WriteOutput("检测安装系统环境...");
|
|
|
|
|
EnsureSystemEnv();
|
|
|
|
|
WriteOutput("检测安装系统环境完成");
|
|
|
|
|
|
|
|
|
|
WriteOutput("配置服务器端口...");
|
|
|
|
|
ConfigurePort();
|
|
|
|
|
WriteOutput("端口配置完成");
|
|
|
|
|
|
|
|
|
|
WriteOutput("安装必要的系统工具...");
|
|
|
|
|
ConfigureSoftware();
|
|
|
|
|
WriteOutput("系统工具安装完成");
|
|
|
|
|
|
|
|
|
|
WriteOutput("配置防火墙...");
|
|
|
|
|
ConfigureFirewall();
|
|
|
|
|
WriteOutput("防火墙配置完成");
|
|
|
|
|
|
|
|
|
|
WriteOutput("同步系统和本地时间...");
|
|
|
|
|
SyncTimeDiff();
|
|
|
|
|
WriteOutput("时间同步完成");
|
|
|
|
|
|
|
|
|
|
WriteOutput("检测域名是否绑定本机IP...");
|
|
|
|
|
ValidateDomain();
|
|
|
|
|
WriteOutput("域名检测完成");
|
|
|
|
|
|
2021-05-25 13:28:37 +03:00
|
|
|
|
WriteOutput("安装NaiveProxy...");
|
|
|
|
|
InstallNaiveProxy();
|
|
|
|
|
WriteOutput("NaiveProxy安装完成");
|
2021-05-24 13:57:17 +03:00
|
|
|
|
|
|
|
|
|
WriteOutput("启动BBR");
|
|
|
|
|
EnableBBR();
|
|
|
|
|
|
|
|
|
|
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-24 13:57:17 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InstallNaiveProxy()
|
|
|
|
|
{
|
|
|
|
|
WriteOutput("安装 NaiveProxy");
|
|
|
|
|
RunCmd(@"curl https://raw.githubusercontent.com/proxysu/shellscript/master/Caddy-Naive/caddy-naive-install.sh yes | bash");
|
2021-06-17 13:54:07 +03:00
|
|
|
|
// 允许开机启动
|
|
|
|
|
RunCmd("systemctl enable caddy");
|
2021-05-25 13:28:37 +03:00
|
|
|
|
UploadCaddyFile(false);
|
|
|
|
|
ConfigNetwork();
|
|
|
|
|
WriteOutput("NaiveProxy 安装完成");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ConfigNetwork()
|
|
|
|
|
{
|
|
|
|
|
WriteOutput("优化网络参数");
|
|
|
|
|
RunCmd(@"bash -c 'echo ""fs.file-max = 51200"" >> /etc/sysctl.conf'");
|
|
|
|
|
RunCmd(@"bash -c 'echo ""net.core.rmem_max = 67108864"" >> /etc/sysctl.conf'");
|
|
|
|
|
RunCmd(@"bash -c 'echo ""net.core.wmem_max = 67108864"" >> /etc/sysctl.conf'");
|
|
|
|
|
RunCmd(@"bash -c 'echo ""net.core.rmem_default = 65536"" >> /etc/sysctl.conf'");
|
|
|
|
|
RunCmd(@"bash -c 'echo ""net.core.wmem_default = 65536"" >> /etc/sysctl.conf'");
|
|
|
|
|
RunCmd(@"bash -c 'echo ""net.core.netdev_max_backlog = 4096"" >> /etc/sysctl.conf'");
|
|
|
|
|
RunCmd(@"bash -c 'echo ""net.core.somaxconn = 4096"" >> /etc/sysctl.conf'");
|
|
|
|
|
RunCmd(@"bash -c 'echo ""net.ipv4.tcp_syncookies = 1"" >> /etc/sysctl.conf'");
|
|
|
|
|
RunCmd(@"bash -c 'echo ""net.ipv4.tcp_tw_reuse = 1"" >> /etc/sysctl.conf'");
|
|
|
|
|
RunCmd(@"bash -c 'echo ""net.ipv4.tcp_tw_recycle = 0"" >> /etc/sysctl.conf'");
|
|
|
|
|
RunCmd(@"bash -c 'echo ""net.ipv4.tcp_fin_timeout = 30"" >> /etc/sysctl.conf'");
|
|
|
|
|
RunCmd(@"bash -c 'echo ""net.ipv4.tcp_keepalive_time = 1200"" >> /etc/sysctl.conf'");
|
|
|
|
|
RunCmd(@"bash -c 'echo ""net.ipv4.ip_local_port_range = 10000 65000"" >> /etc/sysctl.conf'");
|
|
|
|
|
RunCmd(@"bash -c 'echo ""net.ipv4.tcp_max_syn_backlog = 4096"" >> /etc/sysctl.conf'");
|
|
|
|
|
RunCmd(@"bash -c 'echo ""net.ipv4.tcp_max_tw_buckets = 5000"" >> /etc/sysctl.conf'");
|
|
|
|
|
RunCmd(@"bash -c 'echo ""net.ipv4.tcp_rmem = 4096 87380 67108864"" >> /etc/sysctl.conf'");
|
|
|
|
|
RunCmd(@"bash -c 'echo ""net.ipv4.tcp_wmem = 4096 65536 67108864"" >> /etc/sysctl.conf'");
|
|
|
|
|
RunCmd(@"bash -c 'echo ""net.ipv4.tcp_mtu_probing = 1"" >> /etc/sysctl.conf'");
|
|
|
|
|
RunCmd(@"sysctl -p");
|
|
|
|
|
WriteOutput("网络参数优化完成");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UploadCaddyFile(bool useCustomWeb = false)
|
|
|
|
|
{
|
|
|
|
|
var caddyStr = BuildConfig(useCustomWeb);
|
|
|
|
|
var stream = new MemoryStream(Encoding.UTF8.GetBytes(caddyStr));
|
|
|
|
|
|
|
|
|
|
if (FileExists("/etc/caddy/Caddyfile"))
|
2021-05-24 13:57:17 +03:00
|
|
|
|
{
|
2021-05-25 13:28:37 +03:00
|
|
|
|
RunCmd("mv /etc/caddy/Caddyfile /etc/caddy/Caddyfile.back");
|
2021-05-24 13:57:17 +03:00
|
|
|
|
}
|
2021-05-25 13:28:37 +03:00
|
|
|
|
UploadFile(stream, "/etc/caddy/Caddyfile");
|
|
|
|
|
RunCmd("systemctl restart caddy");
|
|
|
|
|
}
|
2021-05-24 13:57:17 +03:00
|
|
|
|
|
2021-05-25 13:28:37 +03:00
|
|
|
|
private string BuildConfig(bool useCustomWeb = false)
|
|
|
|
|
{
|
|
|
|
|
var jsonStr = File.ReadAllText("Templates/NaiveProxy/naive_server.caddyfile");
|
|
|
|
|
jsonStr = jsonStr.Replace("##port##", Parameters.Port.ToString());
|
|
|
|
|
jsonStr = jsonStr.Replace("##domain##", Parameters.Domain);
|
|
|
|
|
jsonStr = jsonStr.Replace("##basicauth##", $"basic_auth {Parameters.UserName} {Parameters.Password}");
|
2021-05-24 13:57:17 +03:00
|
|
|
|
|
2021-05-25 13:28:37 +03:00
|
|
|
|
if (!useCustomWeb && !string.IsNullOrEmpty(Parameters.MaskDomain))
|
|
|
|
|
{
|
|
|
|
|
var prefix = "http://";
|
|
|
|
|
if (Parameters.MaskDomain.StartsWith("https://"))
|
|
|
|
|
{
|
|
|
|
|
prefix = "https://";
|
|
|
|
|
}
|
|
|
|
|
var domain = Parameters.MaskDomain
|
|
|
|
|
.TrimStart("http://".ToCharArray())
|
|
|
|
|
.TrimStart("https://".ToCharArray());
|
|
|
|
|
|
|
|
|
|
jsonStr = jsonStr.Replace("##reverse_proxy##", $"reverse_proxy {prefix}{domain} {{ \n header_up Host {domain} \n }}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
jsonStr = jsonStr.Replace("##reverse_proxy##", "");
|
|
|
|
|
jsonStr = jsonStr.Replace("#file_server", "file_server");
|
|
|
|
|
}
|
|
|
|
|
return jsonStr;
|
2021-05-24 13:57:17 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|