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

261 lines
9.1 KiB
C#
Raw Normal View History

2021-05-15 11:45:36 +03:00
using Newtonsoft.Json;
using ProxySuper.Core.Models.Projects;
using System;
using System.Text;
using System.Web;
namespace ProxySuper.Core.Services
{
public class ShareLink
{
2021-07-07 11:56:53 +03:00
public static string BuildBrook(BrookSettings settings)
{
var password = HttpUtility.UrlEncode(settings.Password);
if (settings.BrookType == BrookType.server)
{
var address = HttpUtility.UrlEncode($"{settings.IP}:{settings.Port}");
return $"brook://server?password={password}&server={address}";
}
if (settings.BrookType == BrookType.wsserver)
{
var address = HttpUtility.UrlEncode($"ws://{settings.IP}:{settings.Port}");
2021-07-12 12:58:51 +03:00
return $"brook://wsserver?password={password}&wsserver={address}";
2021-07-07 11:56:53 +03:00
}
if (settings.BrookType == BrookType.wssserver)
{
var address = HttpUtility.UrlEncode($"wss://{settings.Domain}:{settings.Port}");
2021-07-12 12:58:51 +03:00
return $"brook://wssserver?password={password}&wssserver={address}";
2021-07-07 11:56:53 +03:00
}
if (settings.BrookType == BrookType.socks5)
{
var address = HttpUtility.UrlEncode($"socks5://{settings.IP}:{settings.Port}");
return $"brook://socks5?password={password}&socks5={address}";
}
return string.Empty;
}
2021-05-25 13:28:37 +03:00
public static string BuildNaiveProxy(NaiveProxySettings settings)
{
StringBuilder strBuilder = new StringBuilder();
strBuilder.Append("naive+https://");
strBuilder.Append($"{settings.UserName}:{settings.Password}");
strBuilder.Append($"@{settings.Domain}:{settings.Port}");
strBuilder.Append("?padding=true#naive_proxy");
return strBuilder.ToString();
}
2021-05-23 12:40:06 +03:00
public static string BuildTrojanGo(TrojanGoSettings settings)
{
StringBuilder strBuilder = new StringBuilder();
strBuilder.Append("trojan-go://");
2021-05-24 13:57:17 +03:00
strBuilder.Append($"{HttpUtility.UrlEncode(settings.Password)}@{settings.Domain}:{settings.Port}/?");
if (settings.EnableWebSocket)
{
2021-06-02 12:51:27 +03:00
strBuilder.Append($"type=ws&path={HttpUtility.UrlEncode(settings.WebSocketPath)}&");
2021-05-24 13:57:17 +03:00
}
else
{
strBuilder.Append("type=original&");
}
strBuilder.Append($"#{HttpUtility.UrlEncode("trojan-go")}");
2021-05-23 12:40:06 +03:00
return strBuilder.ToString();
}
2021-08-20 06:52:45 +03:00
public static string Build(RayType xrayType, V2raySettings settings)
2021-05-15 11:45:36 +03:00
{
switch (xrayType)
{
2021-08-20 06:52:45 +03:00
case RayType.VLESS_TCP:
case RayType.VLESS_TCP_XTLS:
case RayType.VLESS_WS:
case RayType.VLESS_KCP:
case RayType.VLESS_gRPC:
case RayType.Trojan_TCP:
2021-05-15 11:45:36 +03:00
return BuildVlessShareLink(xrayType, settings);
2021-08-20 06:52:45 +03:00
case RayType.VMESS_TCP:
case RayType.VMESS_WS:
case RayType.VMESS_KCP:
2021-05-15 11:45:36 +03:00
return BuildVmessShareLink(xrayType, settings);
2021-08-20 06:52:45 +03:00
case RayType.ShadowsocksAEAD:
2021-05-15 11:45:36 +03:00
return BuildShadowSocksShareLink(settings);
default:
return string.Empty;
}
}
2021-08-20 06:52:45 +03:00
private static string BuildShadowSocksShareLink(V2raySettings settings)
2021-05-15 11:45:36 +03:00
{
2021-05-22 12:14:27 +03:00
var _method = settings.ShadowSocksMethod;
var _password = settings.ShadowSocksPassword;
2021-05-15 11:45:36 +03:00
var _server = settings.Domain;
var _port = settings.ShadowSocksPort;
2021-05-16 04:12:47 +03:00
var base64URL = Utils.Base64Encode($"{_method}:{_password}@{_server}:{_port}");
2021-05-15 11:45:36 +03:00
return "ss://" + base64URL + "#ShadowSocks";
}
2021-08-20 06:52:45 +03:00
private static string BuildVmessShareLink(RayType xrayType, V2raySettings settings)
2021-05-15 11:45:36 +03:00
{
var vmess = new Vmess
{
v = "2",
add = settings.Domain,
port = settings.Port.ToString(),
id = settings.UUID,
aid = "0",
net = "",
type = "none",
2021-06-02 12:51:27 +03:00
host = "",
2021-05-15 11:45:36 +03:00
path = "",
tls = "tls",
ps = "",
};
switch (xrayType)
{
2021-08-20 06:52:45 +03:00
case RayType.VMESS_TCP:
2021-05-15 11:45:36 +03:00
vmess.ps = "vmess-tcp-tls";
vmess.net = "tcp";
vmess.type = "http";
vmess.path = settings.VMESS_TCP_Path;
break;
2021-08-20 06:52:45 +03:00
case RayType.VMESS_WS:
2021-05-15 11:45:36 +03:00
vmess.ps = "vmess-ws-tls";
vmess.net = "ws";
vmess.type = "none";
vmess.path = settings.VMESS_WS_Path;
break;
2021-08-20 06:52:45 +03:00
case RayType.VMESS_KCP:
2021-05-15 11:45:36 +03:00
vmess.ps = "vmess-mKCP";
vmess.port = settings.VMESS_KCP_Port.ToString();
vmess.net = "kcp";
vmess.type = settings.VMESS_KCP_Type;
vmess.path = settings.VMESS_KCP_Seed;
vmess.tls = "";
break;
default:
return string.Empty;
}
2021-05-16 04:12:47 +03:00
var base64Url = Utils.Base64Encode(JsonConvert.SerializeObject(vmess));
2021-05-15 11:45:36 +03:00
return $"vmess://" + base64Url;
}
2021-08-20 06:52:45 +03:00
private static string BuildVlessShareLink(RayType xrayType, V2raySettings settings)
2021-05-15 11:45:36 +03:00
{
var _protocol = string.Empty;
var _uuid = settings.UUID;
var _domain = settings.Domain;
var _port = settings.Port;
var _type = string.Empty;
var _encryption = "none";
var _security = "tls";
var _path = "/";
var _host = settings.Domain;
var _descriptiveText = string.Empty;
var _headerType = "none";
var _seed = string.Empty;
switch (xrayType)
{
2021-08-20 06:52:45 +03:00
case RayType.VLESS_TCP:
2021-05-15 11:45:36 +03:00
_protocol = "vless";
_type = "tcp";
_descriptiveText = "vless-tcp-tls";
break;
2021-08-20 06:52:45 +03:00
case RayType.VLESS_TCP_XTLS:
2021-05-15 11:45:36 +03:00
_protocol = "vless";
_type = "tcp";
_security = "xtls";
_descriptiveText = "vless-tcp-xtls";
break;
2021-08-20 06:52:45 +03:00
case RayType.VLESS_WS:
2021-05-15 11:45:36 +03:00
_protocol = "vless";
_type = "ws";
_path = settings.VLESS_WS_Path;
_descriptiveText = "vless-ws-tls";
break;
2021-08-20 06:52:45 +03:00
case RayType.VLESS_KCP:
2021-05-15 11:45:36 +03:00
_protocol = "vless";
_type = "kcp";
_headerType = settings.VLESS_KCP_Type;
_seed = settings.VLESS_KCP_Seed;
_port = settings.VLESS_KCP_Port;
_security = "none";
_descriptiveText = "vless-mKCP";
break;
2021-08-20 06:52:45 +03:00
case RayType.VLESS_gRPC:
2021-05-15 11:45:36 +03:00
_protocol = "vless";
_type = "grpc";
2021-06-30 13:36:27 +03:00
_port = settings.VLESS_gRPC_Port;
2021-05-15 11:45:36 +03:00
_descriptiveText = "vless-gRPC";
break;
2021-08-20 06:52:45 +03:00
case RayType.Trojan_TCP:
2021-05-15 11:45:36 +03:00
_protocol = "trojan";
_uuid = settings.TrojanPassword;
_descriptiveText = "trojan-tcp";
break;
default:
throw new Exception("暂未实现的协议");
}
string parametersURL = string.Empty;
2021-08-20 06:52:45 +03:00
if (xrayType != RayType.Trojan_TCP)
2021-05-15 11:45:36 +03:00
{
// 4.3 传输层相关段
parametersURL = $"?type={_type}&encryption={_encryption}&security={_security}&path={HttpUtility.UrlEncode(_path)}&headerType={_headerType}";
// kcp
2021-08-20 06:52:45 +03:00
if (xrayType == RayType.VLESS_KCP)
2021-05-15 11:45:36 +03:00
{
parametersURL += $"&seed={_seed}";
}
// 4.4 TLS 相关段
2022-11-23 17:06:09 +03:00
if (settings is XraySettings)
2021-05-15 11:45:36 +03:00
{
2022-11-23 17:06:09 +03:00
var xraySettings = settings as XraySettings;
parametersURL += $"&flow={xraySettings.Flow}";
2021-05-15 11:45:36 +03:00
}
2021-06-30 13:36:27 +03:00
2021-08-20 06:52:45 +03:00
if (xrayType == RayType.VLESS_gRPC)
2021-06-30 13:36:27 +03:00
{
parametersURL += $"&serviceName={settings.VLESS_gRPC_ServiceName}&mode=gun";
}
2021-05-15 11:45:36 +03:00
}
return $"{_protocol}://{HttpUtility.UrlEncode(_uuid)}@{_domain}:{_port}{parametersURL}#{HttpUtility.UrlEncode(_descriptiveText)}";
}
}
class Vmess
{
public string v { get; set; }
public string ps { get; set; }
public string add { get; set; }
public string port { get; set; }
public string id { get; set; }
public string aid { get; set; }
public string net { get; set; }
public string type { get; set; }
public string host { get; set; }
public string path { get; set; }
public string tls { get; set; }
}
}