From 87895034f4fcd3224de591bd937f505a881f4f84 Mon Sep 17 00:00:00 2001 From: mzz <2017@duck.com> Date: Sun, 23 Jul 2023 17:12:01 +0800 Subject: [PATCH] optimize(vmess): auto choose cipher instead of aes-128-gcm (#232) --- component/outbound/dialer/v2ray/cipher.go | 26 +++++++++++++++++++++++ component/outbound/dialer/v2ray/v2ray.go | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 component/outbound/dialer/v2ray/cipher.go diff --git a/component/outbound/dialer/v2ray/cipher.go b/component/outbound/dialer/v2ray/cipher.go new file mode 100644 index 0000000..17bf985 --- /dev/null +++ b/component/outbound/dialer/v2ray/cipher.go @@ -0,0 +1,26 @@ +package v2ray + +import ( + "runtime" + + "golang.org/x/sys/cpu" +) + +var ( + hasGCMAsmAMD64 = cpu.X86.HasAES && cpu.X86.HasPCLMULQDQ + hasGCMAsmARM64 = cpu.ARM64.HasAES && cpu.ARM64.HasPMULL + // Keep in sync with crypto/aes/cipher_s390x.go. + hasGCMAsmS390X = cpu.S390X.HasAES && cpu.S390X.HasAESCBC && cpu.S390X.HasAESCTR && + (cpu.S390X.HasGHASH || cpu.S390X.HasAESGCM) + + hasAESGCMHardwareSupport = runtime.GOARCH == "amd64" && hasGCMAsmAMD64 || + runtime.GOARCH == "arm64" && hasGCMAsmARM64 || + runtime.GOARCH == "s390x" && hasGCMAsmS390X +) + +func getAutoCipher() string { + if hasAESGCMHardwareSupport { + return "aes-128-gcm" + } + return "chacha20-ietf-poly1305" +} diff --git a/component/outbound/dialer/v2ray/v2ray.go b/component/outbound/dialer/v2ray/v2ray.go index 8987ad0..35ef73b 100644 --- a/component/outbound/dialer/v2ray/v2ray.go +++ b/component/outbound/dialer/v2ray/v2ray.go @@ -175,7 +175,7 @@ func (s *V2Ray) Dialer(option *dialer.GlobalOption, iOption dialer.InstanceOptio if d, err = protocol.NewDialer(s.Protocol, d, protocol.Header{ ProxyAddress: net.JoinHostPort(s.Add, s.Port), - Cipher: "aes-128-gcm", + Cipher: getAutoCipher(), Password: s.ID, IsClient: true, //Flags: protocol.Flags_VMess_UsePacketAddr,