1
0
mirror of https://github.com/proxysu/ProxySU.git synced 2024-11-22 21:26:09 +03:00
ProxySU/ProxySuper.Core/Services/TrojanGoConfigBuilder.cs

67 lines
2.4 KiB
C#
Raw Normal View History

2021-05-13 04:58:45 +03:00
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
2021-05-14 14:07:19 +03:00
using ProxySuper.Core.Models.Projects;
2021-05-13 04:58:45 +03:00
using System.IO;
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;
2021-05-23 12:40:06 +03:00
if (parameters.EnableWebSocket)
{
settings["websocket"]["enabled"] = true;
settings["websocket"]["path"] = parameters.WebSocketPath;
2021-05-24 13:57:17 +03:00
settings["websocket"]["host"] = parameters.Domain;
2021-05-23 12:40:06 +03:00
}
2021-05-13 04:58:45 +03:00
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);
2021-05-21 13:03:40 +03:00
caddyStr = caddyStr.Replace("##domain##", parameters.Domain);
caddyStr = caddyStr.Replace("##port##", WebPort.ToString());
2021-05-13 04:58:45 +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());
caddyStr = caddyStr.Replace("##reverse_proxy##", $"reverse_proxy {prefix}{domain} {{ \n header_up Host {domain} \n }}");
}
else
{
caddyStr = caddyStr.Replace("##reverse_proxy##", "");
}
return caddyStr;
}
}
}