1
0
mirror of https://github.com/proxysu/ProxySU.git synced 2024-11-30 00:56:07 +03:00
ProxySU/ProxySU_Core/Models/ShareLink.cs

197 lines
6.7 KiB
C#
Raw Normal View History

2021-03-17 06:58:56 +03:00
using Newtonsoft.Json;
using ProxySU_Core.Common;
2021-03-27 06:47:02 +03:00
using ProxySU_Core.Models.Developers;
2021-03-17 06:58:56 +03:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace ProxySU_Core.Models
{
public class ShareLink
{
public static string Build(XrayType xrayType, XraySettings settings)
{
2021-03-27 06:47:02 +03:00
2021-03-17 06:58:56 +03:00
switch (xrayType)
{
2021-03-26 13:13:32 +03:00
case XrayType.VLESS_TCP:
2021-03-17 06:58:56 +03:00
case XrayType.VLESS_TCP_XTLS:
2021-03-26 13:13:32 +03:00
case XrayType.VLESS_WS:
2021-04-17 06:50:31 +03:00
case XrayType.VLESS_KCP:
2021-04-18 13:11:35 +03:00
case XrayType.VLESS_gRPC:
2021-03-26 13:13:32 +03:00
case XrayType.Trojan_TCP:
2021-03-17 06:58:56 +03:00
return BuildVlessShareLink(xrayType, settings);
2021-03-26 13:13:32 +03:00
case XrayType.VMESS_TCP:
case XrayType.VMESS_WS:
2021-03-27 06:47:02 +03:00
case XrayType.VMESS_KCP:
2021-03-17 06:58:56 +03:00
return BuildVmessShareLink(xrayType, settings);
2021-03-27 06:47:02 +03:00
case XrayType.ShadowsocksAEAD:
return BuildShadowSocksShareLink(settings);
2021-03-17 06:58:56 +03:00
default:
return string.Empty;
}
}
2021-03-27 06:47:02 +03:00
private static string BuildShadowSocksShareLink(XraySettings settings)
{
var _method = settings.ShadowsocksMethod;
var _password = settings.ShadowsocksPassword;
var _server = settings.Domain;
2021-04-05 05:22:43 +03:00
var _port = settings.ShadowSocksPort;
2021-03-27 06:47:02 +03:00
var base64URL = Base64.Encode($"{_method}:{_password}@{_server}:{_port}");
2021-04-19 11:02:13 +03:00
return "ss://" + base64URL + "#ShadowSocks";
2021-03-27 06:47:02 +03:00
}
2021-03-17 06:58:56 +03:00
private static string BuildVmessShareLink(XrayType xrayType, XraySettings settings)
{
var vmess = new Vmess
{
v = "2",
add = settings.Domain,
port = settings.Port.ToString(),
id = settings.UUID,
aid = "0",
net = "",
type = "none",
host = settings.Domain,
path = "",
tls = "tls",
ps = "",
};
switch (xrayType)
{
2021-03-26 13:13:32 +03:00
case XrayType.VMESS_TCP:
2021-03-17 06:58:56 +03:00
vmess.ps = "vmess-tcp-tls";
vmess.net = "tcp";
vmess.type = "http";
vmess.path = settings.VMESS_TCP_Path;
break;
2021-03-26 13:13:32 +03:00
case XrayType.VMESS_WS:
2021-03-17 06:58:56 +03:00
vmess.ps = "vmess-ws-tls";
vmess.net = "ws";
vmess.type = "none";
vmess.path = settings.VMESS_WS_Path;
break;
2021-03-27 06:47:02 +03:00
case XrayType.VMESS_KCP:
vmess.ps = "vmess-mKCP";
2021-04-17 06:50:31 +03:00
vmess.port = settings.VMESS_KCP_Port.ToString();
2021-03-27 06:47:02 +03:00
vmess.net = "kcp";
vmess.type = settings.VMESS_KCP_Type;
vmess.path = settings.VMESS_KCP_Seed;
vmess.tls = "";
break;
2021-03-17 06:58:56 +03:00
default:
return string.Empty;
}
var base64Url = Base64.Encode(JsonConvert.SerializeObject(vmess));
return $"vmess://" + base64Url;
}
private static string BuildVlessShareLink(XrayType xrayType, XraySettings settings)
{
var _protocol = string.Empty;
var _uuid = settings.UUID;
var _domain = settings.Domain;
var _port = settings.Port;
var _type = string.Empty;
2021-04-18 13:11:35 +03:00
var _encryption = "none";
2021-03-17 06:58:56 +03:00
var _security = "tls";
var _path = "/";
var _host = settings.Domain;
var _descriptiveText = string.Empty;
2021-04-17 06:50:31 +03:00
var _headerType = "none";
var _seed = string.Empty;
2021-03-17 06:58:56 +03:00
switch (xrayType)
{
2021-03-26 13:13:32 +03:00
case XrayType.VLESS_TCP:
2021-03-17 06:58:56 +03:00
_protocol = "vless";
_type = "tcp";
_descriptiveText = "vless-tcp-tls";
break;
case XrayType.VLESS_TCP_XTLS:
_protocol = "vless";
_type = "tcp";
_security = "xtls";
_descriptiveText = "vless-tcp-xtls";
break;
2021-03-26 13:13:32 +03:00
case XrayType.VLESS_WS:
2021-03-17 06:58:56 +03:00
_protocol = "vless";
_type = "ws";
_path = settings.VLESS_WS_Path;
_descriptiveText = "vless-ws-tls";
break;
2021-04-17 06:50:31 +03:00
case XrayType.VLESS_KCP:
_protocol = "vless";
_type = "kcp";
_headerType = settings.VLESS_KCP_Type;
_seed = settings.VLESS_KCP_Seed;
_port = settings.VLESS_KCP_Port;
2021-04-18 13:13:40 +03:00
_security = "none";
2021-04-17 15:58:15 +03:00
_descriptiveText = "vless-mKCP";
break;
case XrayType.VLESS_gRPC:
_protocol = "vless";
_type = "grpc";
_path = settings.VLESS_gRPC_ServiceName;
_descriptiveText = "vless-gRPC";
2021-03-17 06:58:56 +03:00
break;
2021-03-26 13:13:32 +03:00
case XrayType.Trojan_TCP:
2021-03-17 06:58:56 +03:00
_protocol = "trojan";
2021-03-27 06:47:02 +03:00
_uuid = settings.TrojanPassword;
2021-03-17 06:58:56 +03:00
_descriptiveText = "trojan-tcp";
break;
default:
throw new Exception("暂未实现的协议");
}
string parametersURL = string.Empty;
2021-03-26 13:13:32 +03:00
if (xrayType != XrayType.Trojan_TCP)
2021-03-17 06:58:56 +03:00
{
// 4.3 传输层相关段
2021-04-19 11:02:13 +03:00
parametersURL = $"?type={_type}&encryption={_encryption}&security={_security}&path={HttpUtility.UrlEncode(_path)}&headerType={_headerType}";
2021-03-17 06:58:56 +03:00
2021-04-17 06:50:31 +03:00
// kcp
if (xrayType == XrayType.VLESS_KCP)
{
parametersURL += $"&seed={_seed}";
}
2021-03-17 06:58:56 +03:00
// 4.4 TLS 相关段
if (xrayType == XrayType.VLESS_TCP_XTLS)
{
parametersURL += "&flow=xtls-rprx-direct";
}
}
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; }
}
}