Skip to content

Commit

Permalink
Chore: bump github.com/lucas-clemente/quic-go from 0.26.0 to 0.27.0 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AkinoKaede authored and xiaokangwang committed Apr 29, 2022
1 parent 386ca0b commit 072fa3e
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 93 deletions.
57 changes: 27 additions & 30 deletions app/dns/nameserver_quic.go
Expand Up @@ -34,13 +34,13 @@ const handshakeIdleTimeout = time.Second * 8
// QUICNameServer implemented DNS over QUIC
type QUICNameServer struct {
sync.RWMutex
ips map[string]*record
ips map[string]record
pub *pubsub.Service
cleanup *task.Periodic
reqID uint32
name string
destination *net.Destination
session quic.Session
destination net.Destination
connection quic.Connection
}

// NewQUICNameServer creates DNS-over-QUIC client object for local resolving
Expand All @@ -58,10 +58,10 @@ func NewQUICNameServer(url *url.URL) (*QUICNameServer, error) {
dest := net.UDPDestination(net.ParseAddress(url.Hostname()), port)

s := &QUICNameServer{
ips: make(map[string]*record),
ips: make(map[string]record),
pub: pubsub.NewService(),
name: url.String(),
destination: &dest,
destination: dest,
}
s.cleanup = &task.Periodic{
Interval: time.Minute,
Expand Down Expand Up @@ -103,7 +103,7 @@ func (s *QUICNameServer) Cleanup() error {
}

if len(s.ips) == 0 {
s.ips = make(map[string]*record)
s.ips = make(map[string]record)
}

return nil
Expand All @@ -113,10 +113,7 @@ func (s *QUICNameServer) updateIP(req *dnsRequest, ipRec *IPRecord) {
elapsed := time.Since(req.start)

s.Lock()
rec, found := s.ips[req.domain]
if !found {
rec = &record{}
}
rec := s.ips[req.domain]
updated := false

switch req.reqType {
Expand Down Expand Up @@ -197,7 +194,7 @@ func (s *QUICNameServer) sendQuery(ctx context.Context, domain string, clientIP

conn, err := s.openStream(dnsCtx)
if err != nil {
newError("failed to open quic session").Base(err).AtError().WriteToLog()
newError("failed to open quic connection").Base(err).AtError().WriteToLog()
return
}

Expand Down Expand Up @@ -325,7 +322,7 @@ func (s *QUICNameServer) QueryIP(ctx context.Context, domain string, clientIP ne
}
}

func isActive(s quic.Session) bool {
func isActive(s quic.Connection) bool {
select {
case <-s.Context().Done():
return false
Expand All @@ -334,60 +331,60 @@ func isActive(s quic.Session) bool {
}
}

func (s *QUICNameServer) getSession(ctx context.Context) (quic.Session, error) {
var session quic.Session
func (s *QUICNameServer) getConnection(ctx context.Context) (quic.Connection, error) {
var conn quic.Connection
s.RLock()
session = s.session
if session != nil && isActive(session) {
conn = s.connection
if conn != nil && isActive(conn) {
s.RUnlock()
return session, nil
return conn, nil
}
if session != nil {
// we're recreating the session, let's create a new one
_ = session.CloseWithError(0, "")
if conn != nil {
// we're recreating the connection, let's create a new one
_ = conn.CloseWithError(0, "")
}
s.RUnlock()

s.Lock()
defer s.Unlock()

var err error
session, err = s.openSession(ctx)
conn, err = s.openConnection(ctx)
if err != nil {
// This does not look too nice, but QUIC (or maybe quic-go)
// doesn't seem stable enough.
// Maybe retransmissions aren't fully implemented in quic-go?
// Anyways, the simple solution is to make a second try when
// it fails to open the QUIC session.
session, err = s.openSession(ctx)
// it fails to open the QUIC connection.
conn, err = s.openConnection(ctx)
if err != nil {
return nil, err
}
}
s.session = session
return session, nil
s.connection = conn
return conn, nil
}

func (s *QUICNameServer) openSession(ctx context.Context) (quic.Session, error) {
func (s *QUICNameServer) openConnection(ctx context.Context) (quic.Connection, error) {
tlsConfig := tls.Config{}
quicConfig := &quic.Config{
HandshakeIdleTimeout: handshakeIdleTimeout,
}

session, err := quic.DialAddrContext(ctx, s.destination.NetAddr(), tlsConfig.GetTLSConfig(tls.WithNextProto("http/1.1", http2.NextProtoTLS, NextProtoDQ)), quicConfig)
conn, err := quic.DialAddrContext(ctx, s.destination.NetAddr(), tlsConfig.GetTLSConfig(tls.WithNextProto("http/1.1", http2.NextProtoTLS, NextProtoDQ)), quicConfig)
if err != nil {
return nil, err
}

return session, nil
return conn, nil
}

func (s *QUICNameServer) openStream(ctx context.Context) (quic.Stream, error) {
session, err := s.getSession(ctx)
conn, err := s.getConnection(ctx)
if err != nil {
return nil, err
}

// open a new stream
return session.OpenStreamSync(ctx)
return conn.OpenStreamSync(ctx)
}
4 changes: 2 additions & 2 deletions go.mod
Expand Up @@ -43,8 +43,8 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/riobard/go-bloom v0.0.0-20200614022211-cdc8013cb5b3 // indirect
github.com/xtaci/smux v1.5.15 // indirect
go4.org/intern v0.0.0-20210108033219-3eb7198706b2 // indirect
go4.org/unsafe/assume-no-moving-gc v0.0.0-20201222180813-1025295fd063 // indirect
go4.org/intern v0.0.0-20220301175310-a089fc204883 // indirect
go4.org/unsafe/assume-no-moving-gc v0.0.0-20211027215541-db492cf91b37
golang.org/x/mod v0.4.2 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/tools v0.1.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Expand Up @@ -354,9 +354,13 @@ go4.org v0.0.0-20180809161055-417644f6feb5 h1:+hE86LblG4AyDgwMCLTE6FOlM9+qjHSYS+
go4.org v0.0.0-20180809161055-417644f6feb5/go.mod h1:MkTOUMDaeVYJUOUsaDXIhWPZYa1yOyC1qaOBpL57BhE=
go4.org/intern v0.0.0-20210108033219-3eb7198706b2 h1:VFTf+jjIgsldaz/Mr00VaCSswHJrI2hIjQygE/W4IMg=
go4.org/intern v0.0.0-20210108033219-3eb7198706b2/go.mod h1:vLqJ+12kCw61iCWsPto0EOHhBS+o4rO5VIucbc9g2Cc=
go4.org/intern v0.0.0-20220301175310-a089fc204883 h1:pq5gAii+wMY+DsJ5r9I6T7CHjHxHlb4d45gChzX2SsI=
go4.org/intern v0.0.0-20220301175310-a089fc204883/go.mod h1:cS2ma+47FKrLPdXFpr7CuxiTW3eyJbWew4qx0qtQWDA=
go4.org/unsafe/assume-no-moving-gc v0.0.0-20201222175341-b30ae309168e/go.mod h1:FftLjUGFEDu5k8lt0ddY+HcrH/qU/0qk+H8j9/nTl3E=
go4.org/unsafe/assume-no-moving-gc v0.0.0-20201222180813-1025295fd063 h1:1tk03FUNpulq2cuWpXZWj649rwJpk0d20rxWiopKRmc=
go4.org/unsafe/assume-no-moving-gc v0.0.0-20201222180813-1025295fd063/go.mod h1:FftLjUGFEDu5k8lt0ddY+HcrH/qU/0qk+H8j9/nTl3E=
go4.org/unsafe/assume-no-moving-gc v0.0.0-20211027215541-db492cf91b37 h1:Tx9kY6yUkLge/pFG7IEMwDZy6CS2ajFc9TvQdPCW0uA=
go4.org/unsafe/assume-no-moving-gc v0.0.0-20211027215541-db492cf91b37/go.mod h1:FftLjUGFEDu5k8lt0ddY+HcrH/qU/0qk+H8j9/nTl3E=
golang.org/x/build v0.0.0-20190111050920-041ab4dc3f9d/go.mod h1:OWs+y06UdEOHN4y+MfF/py+xQ/tYqIWW03b70/CG9Rw=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down
106 changes: 53 additions & 53 deletions transport/internet/quic/dialer.go
Expand Up @@ -17,39 +17,39 @@ import (
"github.com/v2fly/v2ray-core/v4/transport/internet/tls"
)

type sessionContext struct {
type connectionContext struct {
rawConn *sysConn
session quic.Session
conn quic.Connection
}

var errSessionClosed = newError("session closed")
var errConnectionClosed = newError("connection closed")

func (c *sessionContext) openStream(destAddr net.Addr) (*interConn, error) {
if !isActive(c.session) {
return nil, errSessionClosed
func (c *connectionContext) openStream(destAddr net.Addr) (*interConn, error) {
if !isActive(c.conn) {
return nil, errConnectionClosed
}

stream, err := c.session.OpenStream()
stream, err := c.conn.OpenStream()
if err != nil {
return nil, err
}

conn := &interConn{
stream: stream,
local: c.session.LocalAddr(),
local: c.conn.LocalAddr(),
remote: destAddr,
}

return conn, nil
}

type clientSessions struct {
access sync.Mutex
sessions map[net.Destination][]*sessionContext
cleanup *task.Periodic
type clientConnections struct {
access sync.Mutex
conns map[net.Destination][]*connectionContext
cleanup *task.Periodic
}

func isActive(s quic.Session) bool {
func isActive(s quic.Connection) bool {
select {
case <-s.Context().Done():
return false
Expand All @@ -58,31 +58,31 @@ func isActive(s quic.Session) bool {
}
}

func removeInactiveSessions(sessions []*sessionContext) []*sessionContext {
activeSessions := make([]*sessionContext, 0, len(sessions))
for _, s := range sessions {
if isActive(s.session) {
activeSessions = append(activeSessions, s)
func removeInactiveConnections(conns []*connectionContext) []*connectionContext {
activeConnections := make([]*connectionContext, 0, len(conns))
for _, s := range conns {
if isActive(s.conn) {
activeConnections = append(activeConnections, s)
continue
}
if err := s.session.CloseWithError(0, ""); err != nil {
newError("failed to close session").Base(err).WriteToLog()
if err := s.conn.CloseWithError(0, ""); err != nil {
newError("failed to close connection").Base(err).WriteToLog()
}
if err := s.rawConn.Close(); err != nil {
newError("failed to close raw connection").Base(err).WriteToLog()
}
}

if len(activeSessions) < len(sessions) {
return activeSessions
if len(activeConnections) < len(conns) {
return activeConnections
}

return sessions
return conns
}

func openStream(sessions []*sessionContext, destAddr net.Addr) *interConn {
for _, s := range sessions {
if !isActive(s.session) {
func openStream(conns []*connectionContext, destAddr net.Addr) *interConn {
for _, s := range conns {
if !isActive(s.conn) {
continue
}

Expand All @@ -97,50 +97,50 @@ func openStream(sessions []*sessionContext, destAddr net.Addr) *interConn {
return nil
}

func (s *clientSessions) cleanSessions() error {
func (s *clientConnections) cleanConnections() error {
s.access.Lock()
defer s.access.Unlock()

if len(s.sessions) == 0 {
if len(s.conns) == 0 {
return nil
}

newSessionMap := make(map[net.Destination][]*sessionContext)
newConnMap := make(map[net.Destination][]*connectionContext)

for dest, sessions := range s.sessions {
sessions = removeInactiveSessions(sessions)
if len(sessions) > 0 {
newSessionMap[dest] = sessions
for dest, conns := range s.conns {
conns = removeInactiveConnections(conns)
if len(conns) > 0 {
newConnMap[dest] = conns
}
}

s.sessions = newSessionMap
s.conns = newConnMap
return nil
}

func (s *clientSessions) openConnection(destAddr net.Addr, config *Config, tlsConfig *tls.Config, sockopt *internet.SocketConfig) (internet.Connection, error) {
func (s *clientConnections) openConnection(destAddr net.Addr, config *Config, tlsConfig *tls.Config, sockopt *internet.SocketConfig) (internet.Connection, error) {
s.access.Lock()
defer s.access.Unlock()

if s.sessions == nil {
s.sessions = make(map[net.Destination][]*sessionContext)
if s.conns == nil {
s.conns = make(map[net.Destination][]*connectionContext)
}

dest := net.DestinationFromAddr(destAddr)

var sessions []*sessionContext
if s, found := s.sessions[dest]; found {
sessions = s
var conns []*connectionContext
if s, found := s.conns[dest]; found {
conns = s
}

if true {
conn := openStream(sessions, destAddr)
{
conn := openStream(conns, destAddr)
if conn != nil {
return conn, nil
}
}

sessions = removeInactiveSessions(sessions)
conns = removeInactiveConnections(conns)

rawConn, err := internet.ListenSystemPacket(context.Background(), &net.UDPAddr{
IP: []byte{0, 0, 0, 0},
Expand All @@ -157,33 +157,33 @@ func (s *clientSessions) openConnection(destAddr net.Addr, config *Config, tlsCo
KeepAlive: true,
}

conn, err := wrapSysConn(rawConn, config)
sysConn, err := wrapSysConn(rawConn.(*net.UDPConn), config)
if err != nil {
rawConn.Close()
return nil, err
}

session, err := quic.DialContext(context.Background(), conn, destAddr, "", tlsConfig.GetTLSConfig(tls.WithDestination(dest)), quicConfig)
conn, err := quic.DialContext(context.Background(), sysConn, destAddr, "", tlsConfig.GetTLSConfig(tls.WithDestination(dest)), quicConfig)
if err != nil {
conn.Close()
sysConn.Close()
return nil, err
}

context := &sessionContext{
session: session,
rawConn: conn,
context := &connectionContext{
conn: conn,
rawConn: sysConn,
}
s.sessions[dest] = append(sessions, context)
s.conns[dest] = append(conns, context)
return context.openStream(destAddr)
}

var client clientSessions
var client clientConnections

func init() {
client.sessions = make(map[net.Destination][]*sessionContext)
client.conns = make(map[net.Destination][]*connectionContext)
client.cleanup = &task.Periodic{
Interval: time.Minute,
Execute: client.cleanSessions,
Execute: client.cleanConnections,
}
common.Must(client.cleanup.Start())
}
Expand Down

0 comments on commit 072fa3e

Please sign in to comment.