From 010964f272908e1c93bd7c4fc4e60ee3f4c07fc8 Mon Sep 17 00:00:00 2001
From: Darien Raymond <admin@v2ray.com>
Date: Thu, 22 Nov 2018 11:57:17 +0100
Subject: [PATCH] tweak quic parameters

---
 transport/internet/quic/dialer.go | 19 +++++++++++++++----
 transport/internet/quic/hub.go    | 22 ++++++++++++++--------
 2 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/transport/internet/quic/dialer.go b/transport/internet/quic/dialer.go
index 2c417bd8..8156565c 100644
--- a/transport/internet/quic/dialer.go
+++ b/transport/internet/quic/dialer.go
@@ -3,6 +3,7 @@ package quic
 import (
 	"context"
 	"sync"
+	"time"
 
 	"v2ray.com/core/transport/internet/tls"
 
@@ -29,7 +30,12 @@ func (s *clientSessions) getSession(destAddr net.Addr, config *Config, tlsConfig
 	dest := net.DestinationFromAddr(destAddr)
 
 	if session, found := s.sessions[dest]; found {
-		return session, nil
+		select {
+		case <-session.Context().Done():
+			// Session has been closed. Creating a new one.
+		default:
+			return session, nil
+		}
 	}
 
 	rawConn, err := internet.ListenSystemPacket(context.Background(), &net.UDPAddr{
@@ -41,9 +47,14 @@ func (s *clientSessions) getSession(destAddr net.Addr, config *Config, tlsConfig
 	}
 
 	quicConfig := &quic.Config{
-		Versions:           []quic.VersionNumber{quic.VersionMilestone0_10_0},
-		ConnectionIDLength: 12,
-		KeepAlive:          true,
+		Versions:                              []quic.VersionNumber{quic.VersionMilestone0_10_0},
+		ConnectionIDLength:                    12,
+		KeepAlive:                             true,
+		HandshakeTimeout:                      time.Second * 4,
+		IdleTimeout:                           time.Second * 300,
+		MaxReceiveStreamFlowControlWindow:     128 * 1024,
+		MaxReceiveConnectionFlowControlWindow: 512 * 1024,
+		MaxIncomingUniStreams:                 -1,
 	}
 
 	conn, err := wrapSysConn(rawConn, config)
diff --git a/transport/internet/quic/hub.go b/transport/internet/quic/hub.go
index 001806ea..2e36075c 100644
--- a/transport/internet/quic/hub.go
+++ b/transport/internet/quic/hub.go
@@ -2,6 +2,7 @@ package quic
 
 import (
 	"context"
+	"time"
 
 	quic "github.com/lucas-clemente/quic-go"
 	"v2ray.com/core/common"
@@ -50,13 +51,13 @@ func (l *Listener) keepAccepting() {
 }
 
 // Addr implements internet.Listener.Addr.
-func (v *Listener) Addr() net.Addr {
-	return v.listener.Addr()
+func (l *Listener) Addr() net.Addr {
+	return l.listener.Addr()
 }
 
 // Close implements internet.Listener.Close.
-func (v *Listener) Close() error {
-	return v.listener.Close()
+func (l *Listener) Close() error {
+	return l.listener.Close()
 }
 
 // Listen creates a new Listener based on configurations.
@@ -83,10 +84,15 @@ func Listen(ctx context.Context, address net.Address, port net.Port, streamSetti
 	}
 
 	quicConfig := &quic.Config{
-		Versions:           []quic.VersionNumber{quic.VersionMilestone0_10_0},
-		ConnectionIDLength: 12,
-		KeepAlive:          true,
-		AcceptCookie:       func(net.Addr, *quic.Cookie) bool { return true },
+		Versions:                              []quic.VersionNumber{quic.VersionMilestone0_10_0},
+		ConnectionIDLength:                    12,
+		KeepAlive:                             true,
+		HandshakeTimeout:                      time.Second * 4,
+		IdleTimeout:                           time.Second * 300,
+		MaxReceiveStreamFlowControlWindow:     128 * 1024,
+		MaxReceiveConnectionFlowControlWindow: 512 * 1024,
+		MaxIncomingStreams:                    256,
+		MaxIncomingUniStreams:                 -1,
 	}
 
 	conn, err := wrapSysConn(rawConn, config)