From d3cffbd76a806094c579030ff7e277907707f6fc Mon Sep 17 00:00:00 2001 From: hahahrfool <75717694+hahahrfool@users.noreply.github.com> Date: Sun, 3 Apr 2022 20:41:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E8=AE=A2=E4=BB=A3=E7=A0=81,=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B;=E4=BF=AE=E5=A4=8Dquic=E6=97=A0=E6=B3=95=E5=A4=9A?= =?UTF-8?q?=E8=B7=AF=E5=A4=8D=E7=94=A8=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/test.yml | 3 --- examples/quic.client.toml | 2 +- examples/vlesss.client.toml | 1 + quic/quic.go | 33 ++++++++++++++++++++++++++++++++- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a00e8e4..6551fb2 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -22,9 +22,6 @@ jobs: with: go-version: 1.18 - - name: Try Build - run: | - make - name: Test run: go test -v ./... diff --git a/examples/quic.client.toml b/examples/quic.client.toml index 59179c2..39a518e 100644 --- a/examples/quic.client.toml +++ b/examples/quic.client.toml @@ -11,7 +11,7 @@ host = "127.0.0.1" port = 4434 version = 0 insecure = true -utls = true +#utls = true #quic无法使用utls, 没办法 advancedLayer = "quic" #alpn = ["asdfsadf"] # 如果alpn没指定,则会自动使用 "h3"作为alpn,quic包的限制,导致必须指定一个alpn diff --git a/examples/vlesss.client.toml b/examples/vlesss.client.toml index bd98f67..76a1f4e 100644 --- a/examples/vlesss.client.toml +++ b/examples/vlesss.client.toml @@ -41,6 +41,7 @@ host = "127.0.0.1" # 同listen对应配置, 可填ip或域名;如果 netwo # 除了host之外,也可使用 ip = "127.0.0.1"; 一般用了host就不需要再写ip, # 但是如果用了cdn 的话, 就要单独提供 ip 和 host # 单独提供 ip 的好处就是不用解析域名了 +# 另外,如果你的vps是ipv6机,则 ipv6 两端要加中括号 , 变成类似 "[2408:0000]"" 这种 # tls = true diff --git a/quic/quic.go b/quic/quic.go index 5d7f9d9..cd56a8a 100644 --- a/quic/quic.go +++ b/quic/quic.go @@ -5,6 +5,7 @@ import ( "context" "crypto/tls" "net" + "sync" "time" "github.com/hahahrfool/v2ray_simple/netLayer" @@ -51,7 +52,7 @@ func (qsc StreamConn) RemoteAddr() net.Addr { } const ( - our_maxidletimeout = time.Second * 45 + our_maxidletimeout = time.Hour * 2 //time.Second * 45 //idletimeout 设的时间越长越不容易断连. our_HandshakeIdleTimeout = time.Second * 8 our_ConnectionIDLength = 12 ) @@ -147,7 +148,33 @@ func ListenInitialLayers(addr string, tlsConf tls.Config, useHysteria bool, hyst return } +var ( + clientconnMap = make(map[netLayer.HashableAddr]quic.Session) + clientconnMutex sync.RWMutex +) + +func isActive(s quic.Session) bool { + select { + case <-s.Context().Done(): + return false + default: + return true + } +} + func DialCommonInitialLayer(serverAddr *netLayer.Addr, tlsConf tls.Config, useHysteria bool, hysteriaMaxByteCount int, hysteria_manual bool) any { + + hash := serverAddr.GetHashable() + + clientconnMutex.RLock() + existSession, has := clientconnMap[hash] + clientconnMutex.RUnlock() + if has { + if isActive(existSession) { + return existSession + } + } + session, err := quic.DialAddr(serverAddr.String(), &tlsConf, &our_DialConfig) if err != nil { if ce := utils.CanLogErr("quic dial"); ce != nil { @@ -172,6 +199,10 @@ func DialCommonInitialLayer(serverAddr *netLayer.Addr, tlsConf tls.Config, useHy } } + clientconnMutex.Lock() + clientconnMap[hash] = session + clientconnMutex.Unlock() + return session }