1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-29 10:45:22 +00:00

Add uTLS ALPN Control

This commit is contained in:
Shelikhoo 2023-01-12 15:33:08 +00:00
parent beec73e12d
commit 7b434ced58
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316
2 changed files with 19 additions and 3 deletions

View File

@ -9,6 +9,12 @@ option java_multiple_files = true;
import "common/protoext/extensions.proto";
import "transport/internet/tls/config.proto";
enum ForcedALPN{
TRANSPORT_PREFERENCE_TAKE_PRIORITY = 0;
NO_ALPN = 1;
UTLS_PRESET = 2;
}
message Config {
option (v2ray.core.common.protoext.message_opt).type = "security";
option (v2ray.core.common.protoext.message_opt).short_name = "utls";
@ -16,4 +22,5 @@ message Config {
v2ray.core.transport.internet.tls.Config tls_config = 1;
string imitate = 2;
bool noSNI = 3;
ForcedALPN force_alpn = 4;
}

View File

@ -30,7 +30,9 @@ func (e Engine) Client(conn net.Conn, opts ...security.Option) (security.Conn, e
for _, v := range opts {
switch s := v.(type) {
case security.OptionWithALPN:
options = append(options, tls.WithNextProto(s.ALPNs...))
if e.config.ForceAlpn == ForcedALPN_TRANSPORT_PREFERENCE_TAKE_PRIORITY {
options = append(options, tls.WithNextProto(s.ALPNs...))
}
case security.OptionWithDestination:
options = append(options, tls.WithDestination(s.Dest))
default:
@ -65,9 +67,16 @@ func (e Engine) Client(conn net.Conn, opts ...security.Option) (security.Conn, e
// ALPN is necessary for protocols like websocket to work. The uTLS setting may be overwritten on call into
// BuildHandshakeState, so we need to check the original tls settings.
if tlsConfig.NextProtos != nil {
for _, v := range utlsClientConn.Extensions {
for n, v := range utlsClientConn.Extensions {
if aplnExtension, ok := v.(*utls.ALPNExtension); ok {
aplnExtension.AlpnProtocols = tlsConfig.NextProtos
if e.config.ForceAlpn == ForcedALPN_TRANSPORT_PREFERENCE_TAKE_PRIORITY {
aplnExtension.AlpnProtocols = tlsConfig.NextProtos
break
}
if e.config.ForceAlpn == ForcedALPN_NO_ALPN {
utlsClientConn.Extensions = append(utlsClientConn.Extensions[:n], utlsClientConn.Extensions[n+1:]...)
break
}
}
}
}