mirror of
https://github.com/2dust/v2rayN.git
synced 2026-10-21 20:55:01 +03:00
Refactor Transport (#9004)
* Refactor transport * Rename tcp to raw * Fix * Fix Fix raw http ui Fill xhttp default mode Fix share uri Remove RawHost Fix singbox tcp http path Fix vmess share uri * Tidy Resx * Fix * Rename TransportExtra to TransportExtraItem --------- Co-authored-by: 2dust <31833384+2dust@users.noreply.github.com>
This commit is contained in:
@@ -84,6 +84,8 @@ public partial class CoreConfigSingboxService
|
||||
try
|
||||
{
|
||||
var protocolExtra = _node.GetProtocolExtra();
|
||||
var transportExtra = _node.GetTransportExtra();
|
||||
var network = _node.GetNetwork();
|
||||
outbound.server = _node.Address;
|
||||
outbound.server_port = _node.Port;
|
||||
outbound.type = Global.ProtocolTypes[_node.ConfigType];
|
||||
@@ -114,27 +116,23 @@ public partial class CoreConfigSingboxService
|
||||
outbound.password = _node.Password;
|
||||
outbound.udp_over_tcp = protocolExtra.Uot == true ? true : null;
|
||||
|
||||
if (_node.Network == nameof(ETransport.tcp) && _node.HeaderType == Global.TcpHeaderHttp)
|
||||
if (network == nameof(ETransport.raw) && transportExtra.RawHeaderType == Global.RawHeaderHttp)
|
||||
{
|
||||
outbound.plugin = "obfs-local";
|
||||
outbound.plugin_opts = $"obfs=http;obfs-host={_node.RequestHost};";
|
||||
outbound.plugin_opts = $"obfs=http;obfs-host={transportExtra.Host};";
|
||||
}
|
||||
else
|
||||
{
|
||||
var pluginArgs = string.Empty;
|
||||
if (_node.Network == nameof(ETransport.ws))
|
||||
if (network == nameof(ETransport.ws))
|
||||
{
|
||||
pluginArgs += "mode=websocket;";
|
||||
pluginArgs += $"host={_node.RequestHost};";
|
||||
pluginArgs += $"host={transportExtra.Host};";
|
||||
// https://github.com/shadowsocks/v2ray-plugin/blob/e9af1cdd2549d528deb20a4ab8d61c5fbe51f306/args.go#L172
|
||||
// Equal signs and commas [and backslashes] must be escaped with a backslash.
|
||||
var path = _node.Path.Replace("\\", "\\\\").Replace("=", "\\=").Replace(",", "\\,");
|
||||
var path = (transportExtra.Path ?? string.Empty).Replace("\\", "\\\\").Replace("=", "\\=").Replace(",", "\\,");
|
||||
pluginArgs += $"path={path};";
|
||||
}
|
||||
else if (_node.Network == nameof(ETransport.quic))
|
||||
{
|
||||
pluginArgs += "mode=quic;";
|
||||
}
|
||||
if (_node.StreamSecurity == Global.StreamSecurity)
|
||||
{
|
||||
pluginArgs += "tls;";
|
||||
@@ -381,9 +379,18 @@ public partial class CoreConfigSingboxService
|
||||
{
|
||||
serverName = _node.Sni;
|
||||
}
|
||||
else if (_node.RequestHost.IsNotEmpty())
|
||||
else
|
||||
{
|
||||
serverName = Utils.String2List(_node.RequestHost)?.First();
|
||||
var host = _node.GetNetwork() switch
|
||||
{
|
||||
nameof(ETransport.raw) => _node.GetTransportExtra().Host,
|
||||
nameof(ETransport.ws) => _node.GetTransportExtra().Host,
|
||||
nameof(ETransport.httpupgrade) => _node.GetTransportExtra().Host,
|
||||
nameof(ETransport.xhttp) => _node.GetTransportExtra().Host,
|
||||
nameof(ETransport.grpc) => _node.GetTransportExtra().GrpcAuthority,
|
||||
_ => null,
|
||||
};
|
||||
serverName = Utils.String2List(host)?.First();
|
||||
}
|
||||
var tls = new Tls4Sbox()
|
||||
{
|
||||
@@ -438,23 +445,20 @@ public partial class CoreConfigSingboxService
|
||||
try
|
||||
{
|
||||
var transport = new Transport4Sbox();
|
||||
var transportExtra = _node.GetTransportExtra();
|
||||
var useragent = _config.CoreBasicItem.DefUserAgent ?? string.Empty;
|
||||
var useragentValue = Global.TcpHttpUserAgentTexts.GetValueOrDefault(useragent, useragent);
|
||||
var useragentValue = Global.RawHttpUserAgentTexts.GetValueOrDefault(useragent, useragent);
|
||||
|
||||
switch (_node.GetNetwork())
|
||||
{
|
||||
case nameof(ETransport.h2):
|
||||
transport.type = nameof(ETransport.http);
|
||||
transport.host = _node.RequestHost.IsNullOrEmpty() ? null : Utils.String2List(_node.RequestHost);
|
||||
transport.path = _node.Path.NullIfEmpty();
|
||||
break;
|
||||
|
||||
case nameof(ETransport.tcp): //http
|
||||
if (_node.HeaderType == Global.TcpHeaderHttp)
|
||||
case nameof(ETransport.raw): //http
|
||||
if (transportExtra.RawHeaderType == Global.RawHeaderHttp)
|
||||
{
|
||||
transport.type = nameof(ETransport.http);
|
||||
transport.host = _node.RequestHost.IsNullOrEmpty() ? null : Utils.String2List(_node.RequestHost);
|
||||
transport.path = _node.Path.NullIfEmpty();
|
||||
transport.host = transportExtra.Host.IsNullOrEmpty()
|
||||
? null
|
||||
: Utils.String2List(transportExtra.Host);
|
||||
transport.path = transportExtra.Path.NullIfEmpty();
|
||||
if (!useragentValue.IsNullOrEmpty())
|
||||
{
|
||||
transport.headers ??= new();
|
||||
@@ -465,7 +469,7 @@ public partial class CoreConfigSingboxService
|
||||
|
||||
case nameof(ETransport.ws):
|
||||
transport.type = nameof(ETransport.ws);
|
||||
var wsPath = _node.Path;
|
||||
var wsPath = transportExtra.Path;
|
||||
|
||||
// Parse eh and ed parameters from path using regex
|
||||
if (!wsPath.IsNullOrEmpty())
|
||||
@@ -494,11 +498,11 @@ public partial class CoreConfigSingboxService
|
||||
}
|
||||
|
||||
transport.path = wsPath.NullIfEmpty();
|
||||
if (_node.RequestHost.IsNotEmpty())
|
||||
if (transportExtra.Host.IsNotEmpty())
|
||||
{
|
||||
transport.headers = new()
|
||||
{
|
||||
Host = _node.RequestHost
|
||||
Host = transportExtra.Host
|
||||
};
|
||||
}
|
||||
if (!useragentValue.IsNullOrEmpty())
|
||||
@@ -510,8 +514,8 @@ public partial class CoreConfigSingboxService
|
||||
|
||||
case nameof(ETransport.httpupgrade):
|
||||
transport.type = nameof(ETransport.httpupgrade);
|
||||
transport.path = _node.Path.NullIfEmpty();
|
||||
transport.host = _node.RequestHost.NullIfEmpty();
|
||||
transport.path = transportExtra.Path.NullIfEmpty();
|
||||
transport.host = transportExtra.Host.NullIfEmpty();
|
||||
if (!useragentValue.IsNullOrEmpty())
|
||||
{
|
||||
transport.headers ??= new();
|
||||
@@ -520,13 +524,9 @@ public partial class CoreConfigSingboxService
|
||||
|
||||
break;
|
||||
|
||||
case nameof(ETransport.quic):
|
||||
transport.type = nameof(ETransport.quic);
|
||||
break;
|
||||
|
||||
case nameof(ETransport.grpc):
|
||||
transport.type = nameof(ETransport.grpc);
|
||||
transport.service_name = _node.Path;
|
||||
transport.service_name = transportExtra.GrpcServiceName;
|
||||
transport.idle_timeout = _config.GrpcItem.IdleTimeout?.ToString("##s");
|
||||
transport.ping_timeout = _config.GrpcItem.HealthCheckTimeout?.ToString("##s");
|
||||
transport.permit_without_stream = _config.GrpcItem.PermitWithoutStream;
|
||||
|
||||
@@ -353,8 +353,49 @@ public partial class CoreConfigV2rayService
|
||||
network = "hysteria";
|
||||
}
|
||||
streamSettings.network = network;
|
||||
var host = _node.RequestHost.TrimEx();
|
||||
var path = _node.Path.TrimEx();
|
||||
var transport = _node.GetTransportExtra();
|
||||
var host = string.Empty;
|
||||
var path = string.Empty;
|
||||
var kcpSeed = string.Empty;
|
||||
var headerType = string.Empty;
|
||||
var xhttpExtra = string.Empty;
|
||||
switch (network)
|
||||
{
|
||||
case nameof(ETransport.raw):
|
||||
host = transport.Host?.TrimEx() ?? string.Empty;
|
||||
path = transport.Path?.TrimEx() ?? string.Empty;
|
||||
headerType = transport.RawHeaderType?.TrimEx() ?? string.Empty;
|
||||
break;
|
||||
|
||||
case nameof(ETransport.kcp):
|
||||
kcpSeed = transport.KcpSeed?.TrimEx() ?? string.Empty;
|
||||
headerType = transport.KcpHeaderType?.TrimEx() ?? string.Empty;
|
||||
break;
|
||||
|
||||
case nameof(ETransport.ws):
|
||||
host = transport.Host?.TrimEx() ?? string.Empty;
|
||||
path = transport.Path?.TrimEx() ?? string.Empty;
|
||||
break;
|
||||
|
||||
case nameof(ETransport.httpupgrade):
|
||||
host = transport.Host?.TrimEx() ?? string.Empty;
|
||||
path = transport.Path?.TrimEx() ?? string.Empty;
|
||||
break;
|
||||
|
||||
case nameof(ETransport.xhttp):
|
||||
host = transport.Host?.TrimEx() ?? string.Empty;
|
||||
path = transport.Path?.TrimEx() ?? string.Empty;
|
||||
headerType = transport.XhttpMode?.TrimEx() ?? string.Empty;
|
||||
xhttpExtra = transport.XhttpExtra?.TrimEx() ?? string.Empty;
|
||||
break;
|
||||
|
||||
case nameof(ETransport.grpc):
|
||||
host = transport.GrpcAuthority?.TrimEx() ?? string.Empty;
|
||||
path = transport.GrpcServiceName?.TrimEx() ?? string.Empty;
|
||||
headerType = transport.GrpcMode?.TrimEx() ?? string.Empty;
|
||||
break;
|
||||
}
|
||||
|
||||
var sni = _node.Sni.TrimEx();
|
||||
var useragent = _config.CoreBasicItem.DefUserAgent ?? string.Empty;
|
||||
|
||||
@@ -440,19 +481,19 @@ public partial class CoreConfigV2rayService
|
||||
kcpSettings.readBufferSize = _config.KcpItem.ReadBufferSize;
|
||||
kcpSettings.writeBufferSize = _config.KcpItem.WriteBufferSize;
|
||||
var kcpFinalmask = new Finalmask4Ray();
|
||||
if (Global.KcpHeaderMaskMap.TryGetValue(_node.HeaderType, out var header))
|
||||
if (Global.KcpHeaderMaskMap.TryGetValue(headerType, out var header))
|
||||
{
|
||||
kcpFinalmask.udp =
|
||||
[
|
||||
new Mask4Ray
|
||||
{
|
||||
type = header,
|
||||
settings = _node.HeaderType == "dns" && !host.IsNullOrEmpty() ? new MaskSettings4Ray { domain = host } : null
|
||||
settings = null
|
||||
}
|
||||
];
|
||||
}
|
||||
kcpFinalmask.udp ??= [];
|
||||
if (path.IsNullOrEmpty())
|
||||
if (kcpSeed.IsNullOrEmpty())
|
||||
{
|
||||
kcpFinalmask.udp.Add(new Mask4Ray
|
||||
{
|
||||
@@ -464,7 +505,7 @@ public partial class CoreConfigV2rayService
|
||||
kcpFinalmask.udp.Add(new Mask4Ray
|
||||
{
|
||||
type = "mkcp-aes128gcm",
|
||||
settings = new MaskSettings4Ray { password = path }
|
||||
settings = new MaskSettings4Ray { password = kcpSeed }
|
||||
});
|
||||
}
|
||||
streamSettings.kcpSettings = kcpSettings;
|
||||
@@ -522,63 +563,25 @@ public partial class CoreConfigV2rayService
|
||||
{
|
||||
xhttpSettings.host = host;
|
||||
}
|
||||
if (_node.HeaderType.IsNotEmpty() && Global.XhttpMode.Contains(_node.HeaderType))
|
||||
if (headerType.IsNotEmpty() && Global.XhttpMode.Contains(headerType))
|
||||
{
|
||||
xhttpSettings.mode = _node.HeaderType;
|
||||
xhttpSettings.mode = headerType;
|
||||
}
|
||||
if (_node.Extra.IsNotEmpty())
|
||||
if (xhttpExtra.IsNotEmpty())
|
||||
{
|
||||
xhttpSettings.extra = JsonUtils.ParseJson(_node.Extra);
|
||||
xhttpSettings.extra = JsonUtils.ParseJson(xhttpExtra);
|
||||
}
|
||||
|
||||
streamSettings.xhttpSettings = xhttpSettings;
|
||||
FillOutboundMux(outbound);
|
||||
|
||||
break;
|
||||
//h2
|
||||
case nameof(ETransport.h2):
|
||||
HttpSettings4Ray httpSettings = new();
|
||||
|
||||
if (host.IsNotEmpty())
|
||||
{
|
||||
httpSettings.host = Utils.String2List(host);
|
||||
}
|
||||
httpSettings.path = path;
|
||||
|
||||
streamSettings.httpSettings = httpSettings;
|
||||
|
||||
break;
|
||||
//quic
|
||||
case nameof(ETransport.quic):
|
||||
QuicSettings4Ray quicsettings = new()
|
||||
{
|
||||
security = host,
|
||||
key = path,
|
||||
header = new Header4Ray
|
||||
{
|
||||
type = _node.HeaderType
|
||||
}
|
||||
};
|
||||
streamSettings.quicSettings = quicsettings;
|
||||
if (_node.StreamSecurity == Global.StreamSecurity)
|
||||
{
|
||||
if (sni.IsNotEmpty())
|
||||
{
|
||||
streamSettings.tlsSettings.serverName = sni;
|
||||
}
|
||||
else
|
||||
{
|
||||
streamSettings.tlsSettings.serverName = _node.Address;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case nameof(ETransport.grpc):
|
||||
GrpcSettings4Ray grpcSettings = new()
|
||||
{
|
||||
authority = host.NullIfEmpty(),
|
||||
serviceName = path,
|
||||
multiMode = _node.HeaderType == Global.GrpcMultiMode,
|
||||
multiMode = headerType == Global.GrpcMultiMode,
|
||||
idle_timeout = _config.GrpcItem.IdleTimeout,
|
||||
health_check_timeout = _config.GrpcItem.HealthCheckTimeout,
|
||||
permit_without_stream = _config.GrpcItem.PermitWithoutStream,
|
||||
@@ -645,20 +648,20 @@ public partial class CoreConfigV2rayService
|
||||
break;
|
||||
|
||||
default:
|
||||
//tcp
|
||||
if (_node.HeaderType == Global.TcpHeaderHttp)
|
||||
// raw
|
||||
if (headerType == Global.RawHeaderHttp)
|
||||
{
|
||||
TcpSettings4Ray tcpSettings = new()
|
||||
RawSettings4Ray rawSettings = new()
|
||||
{
|
||||
header = new Header4Ray
|
||||
{
|
||||
type = _node.HeaderType
|
||||
type = headerType
|
||||
}
|
||||
};
|
||||
|
||||
//request Host
|
||||
var request = EmbedUtils.GetEmbedText(Global.V2raySampleHttpRequestFileName);
|
||||
var useragentValue = Global.TcpHttpUserAgentTexts.GetValueOrDefault(useragent, useragent);
|
||||
var useragentValue = Global.RawHttpUserAgentTexts.GetValueOrDefault(useragent, useragent);
|
||||
var arrHost = host.Split(',');
|
||||
var host2 = string.Join(",".AppendQuotes(), arrHost);
|
||||
request = request.Replace("$requestHost$", $"{host2.AppendQuotes()}");
|
||||
@@ -671,9 +674,9 @@ public partial class CoreConfigV2rayService
|
||||
pathHttp = string.Join(",".AppendQuotes(), arrPath);
|
||||
}
|
||||
request = request.Replace("$requestPath$", $"{pathHttp.AppendQuotes()}");
|
||||
tcpSettings.header.request = JsonUtils.Deserialize<object>(request);
|
||||
rawSettings.header.request = JsonUtils.Deserialize<object>(request);
|
||||
|
||||
streamSettings.tcpSettings = tcpSettings;
|
||||
streamSettings.rawSettings = rawSettings;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user