Skip to content

Commit

Permalink
Feat: use different qtls modules for different Go version
Browse files Browse the repository at this point in the history
  • Loading branch information
AkinoKaede committed Apr 5, 2022
1 parent 56371d8 commit e0df70b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
19 changes: 19 additions & 0 deletions common/protocol/quic/qtls_go116.go
@@ -0,0 +1,19 @@
//go:build go1.16 && !go1.17
// +build go1.16,!go1.17

package quic

import (
"crypto/cipher"

"github.com/marten-seemann/qtls-go1-16"
)

type (
// A CipherSuiteTLS13 is a cipher suite for TLS 1.3
CipherSuiteTLS13 = qtls.CipherSuiteTLS13
)

func AEADAESGCMTLS13(key, fixedNonce []byte) cipher.AEAD {
return qtls.AEADAESGCMTLS13(key, fixedNonce)
}
19 changes: 19 additions & 0 deletions common/protocol/quic/qtls_go117.go
@@ -0,0 +1,19 @@
//go:build go1.17 && !go1.18
// +build go1.17,!go1.18

package quic

import (
"crypto/cipher"

"github.com/marten-seemann/qtls-go1-17"
)

type (
// A CipherSuiteTLS13 is a cipher suite for TLS 1.3
CipherSuiteTLS13 = qtls.CipherSuiteTLS13
)

func AEADAESGCMTLS13(key, fixedNonce []byte) cipher.AEAD {
return qtls.AEADAESGCMTLS13(key, fixedNonce)
}
19 changes: 19 additions & 0 deletions common/protocol/quic/qtls_go118.go
@@ -0,0 +1,19 @@
//go:build go1.18
// +build go1.18

package quic

import (
"crypto/cipher"

"github.com/marten-seemann/qtls-go1-18"
)

type (
// A CipherSuiteTLS13 is a cipher suite for TLS 1.3
CipherSuiteTLS13 = qtls.CipherSuiteTLS13
)

func AEADAESGCMTLS13(key, fixedNonce []byte) cipher.AEAD {
return qtls.AEADAESGCMTLS13(key, fixedNonce)
}
7 changes: 3 additions & 4 deletions common/protocol/quic/sniff.go
Expand Up @@ -8,7 +8,6 @@ import (
"io"

"github.com/lucas-clemente/quic-go/quicvarint"
"github.com/marten-seemann/qtls-go1-17"
"golang.org/x/crypto/hkdf"

"github.com/v2fly/v2ray-core/v5/common"
Expand Down Expand Up @@ -37,10 +36,10 @@ const (
var (
quicSaltOld = []byte{0xaf, 0xbf, 0xec, 0x28, 0x99, 0x93, 0xd2, 0x4c, 0x9e, 0x97, 0x86, 0xf1, 0x9c, 0x61, 0x11, 0xe0, 0x43, 0x90, 0xa8, 0x99}
quicSalt = []byte{0x38, 0x76, 0x2c, 0xf7, 0xf5, 0x59, 0x34, 0xb3, 0x4d, 0x17, 0x9a, 0xe6, 0xa4, 0xc8, 0x0c, 0xad, 0xcc, 0xbb, 0x7f, 0x0a}
initialSuite = &qtls.CipherSuiteTLS13{
initialSuite = &CipherSuiteTLS13{
ID: tls.TLS_AES_128_GCM_SHA256,
KeyLen: 16,
AEAD: qtls.AEADAESGCMTLS13,
AEAD: AEADAESGCMTLS13,
Hash: crypto.SHA256,
}
errNotQuic = errors.New("not quic")
Expand Down Expand Up @@ -153,7 +152,7 @@ func SniffQUIC(b []byte) (*SniffHeader, error) {

key := hkdfExpandLabel(crypto.SHA256, secret, []byte{}, "quic key", 16)
iv := hkdfExpandLabel(crypto.SHA256, secret, []byte{}, "quic iv", 12)
cipher := qtls.AEADAESGCMTLS13(key, iv)
cipher := AEADAESGCMTLS13(key, iv)
nonce := cache.Extend(int32(cipher.NonceSize()))
binary.BigEndian.PutUint64(nonce[len(nonce)-8:], uint64(packetNumber))
decrypted, err := cipher.Open(b[extHdrLen:extHdrLen], nonce, data, b[:extHdrLen])
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Expand Up @@ -13,7 +13,9 @@ require (
github.com/gorilla/websocket v1.5.0
github.com/jhump/protoreflect v1.12.0
github.com/lucas-clemente/quic-go v0.26.0
github.com/marten-seemann/qtls-go1-16 v0.1.5
github.com/marten-seemann/qtls-go1-17 v0.1.1
github.com/marten-seemann/qtls-go1-18 v0.1.1
github.com/miekg/dns v1.1.48
github.com/pelletier/go-toml v1.9.4
github.com/pires/go-proxyproto v0.6.2
Expand Down Expand Up @@ -45,8 +47,6 @@ require (
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40 // indirect
github.com/marten-seemann/qtls-go1-16 v0.1.5 // indirect
github.com/marten-seemann/qtls-go1-18 v0.1.1 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/onsi/ginkgo v1.16.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down

0 comments on commit e0df70b

Please sign in to comment.