1
0
mirror of https://github.com/proxysu/ProxySU.git synced 2024-11-23 05:36:08 +03:00
ProxySU/ProxySuper.Core/Services/TrojanGoConfigBuilder.cs
2021-05-21 18:03:40 +08:00

66 lines
2.2 KiB
C#

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ProxySuper.Core.Models;
using ProxySuper.Core.Models.Projects;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProxySuper.Core.Services
{
public class TrojanGoConfigBuilder
{
public static readonly int WebPort = 8088;
public static readonly string TrojanGoSettingPath = @"Templates\trojan-go\trojan-go.json";
public static readonly string CaddyFilePath = @"Templates\trojan-go\base.caddyfile";
public static string BuildTrojanGoConfig(TrojanGoSettings parameters)
{
var jsonStr = File.ReadAllText(TrojanGoSettingPath);
var settings = JToken.FromObject(JsonConvert.DeserializeObject(jsonStr));
settings["remote_port"] = WebPort;
settings["password"][0] = parameters.Password;
settings["ssl"]["sni"] = parameters.Domain;
return JsonConvert.SerializeObject(settings, Formatting.Indented, new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
});
}
public static string BuildCaddyConfig(TrojanGoSettings parameters, bool useCustomWeb = false)
{
var caddyStr = File.ReadAllText(CaddyFilePath);
caddyStr = caddyStr.Replace("##domain##", parameters.Domain);
caddyStr = caddyStr.Replace("##port##", WebPort.ToString());
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());
caddyStr = caddyStr.Replace("##reverse_proxy##", $"reverse_proxy {prefix}{domain} {{ \n header_up Host {domain} \n }}");
}
else
{
caddyStr = caddyStr.Replace("##reverse_proxy##", "");
}
return caddyStr;
}
}
}