mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-01-15 15:12:41 +08:00
Compare commits
3 Commits
leak-fix
...
dns-dead-l
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
73baf47358 | ||
|
|
ecc2f73108 | ||
|
|
f45ca197a2 |
@@ -162,6 +162,7 @@ type udpConn struct {
|
||||
uplink stats.Counter
|
||||
downlink stats.Counter
|
||||
inactive bool
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func (c *udpConn) setInactive() {
|
||||
@@ -204,6 +205,9 @@ func (c *udpConn) Write(buf []byte) (int, error) {
|
||||
}
|
||||
|
||||
func (c *udpConn) Close() error {
|
||||
if c.cancel != nil {
|
||||
c.cancel()
|
||||
}
|
||||
common.Must(c.done.Close())
|
||||
common.Must(common.Close(c.writer))
|
||||
return nil
|
||||
@@ -260,6 +264,7 @@ func (w *udpWorker) getConnection(id connID) (*udpConn, bool) {
|
||||
defer w.Unlock()
|
||||
|
||||
if conn, found := w.activeConn[id]; found && !conn.done.Done() {
|
||||
conn.updateActivity()
|
||||
return conn, true
|
||||
}
|
||||
|
||||
@@ -307,7 +312,8 @@ func (w *udpWorker) callback(b *buf.Buffer, source net.Destination, originalDest
|
||||
common.Must(w.checker.Start())
|
||||
|
||||
go func() {
|
||||
ctx := w.ctx
|
||||
ctx, cancel := context.WithCancel(w.ctx)
|
||||
conn.cancel = cancel
|
||||
sid := session.NewID()
|
||||
ctx = c.ContextWithID(ctx, sid)
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
go_errors "errors"
|
||||
"io"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/xtls/xray-core/common"
|
||||
@@ -369,6 +370,10 @@ type outboundConn struct {
|
||||
access sync.Mutex
|
||||
dialer func() (stat.Connection, error)
|
||||
|
||||
closeOnce sync.Once
|
||||
dialOnce sync.Once
|
||||
closed atomic.Bool
|
||||
|
||||
conn net.Conn
|
||||
connReady chan struct{}
|
||||
}
|
||||
@@ -378,24 +383,28 @@ func (c *outboundConn) dial() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if c.closed.Load() {
|
||||
return errors.New("connection closed during dial")
|
||||
}
|
||||
c.conn = conn
|
||||
c.connReady <- struct{}{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *outboundConn) Write(b []byte) (int, error) {
|
||||
c.dialOnce.Do(func() {
|
||||
c.dial()
|
||||
})
|
||||
c.access.Lock()
|
||||
|
||||
if c.conn == nil {
|
||||
if err := c.dial(); err != nil {
|
||||
c.access.Unlock()
|
||||
errors.LogWarningInner(context.Background(), err, "failed to dial outbound connection")
|
||||
return len(b), nil
|
||||
}
|
||||
}
|
||||
|
||||
conn := c.conn
|
||||
c.access.Unlock()
|
||||
|
||||
if conn == nil {
|
||||
_, open := <-c.connReady
|
||||
if !open {
|
||||
return 0, io.EOF
|
||||
}
|
||||
conn = c.conn
|
||||
}
|
||||
return c.conn.Write(b)
|
||||
}
|
||||
|
||||
@@ -417,11 +426,14 @@ func (c *outboundConn) Read(b []byte) (int, error) {
|
||||
}
|
||||
|
||||
func (c *outboundConn) Close() error {
|
||||
c.access.Lock()
|
||||
close(c.connReady)
|
||||
if c.conn != nil {
|
||||
c.conn.Close()
|
||||
}
|
||||
c.access.Unlock()
|
||||
c.closeOnce.Do(func() {
|
||||
c.access.Lock()
|
||||
c.closed.Store(true)
|
||||
close(c.connReady)
|
||||
if c.conn != nil {
|
||||
c.conn.Close()
|
||||
}
|
||||
c.access.Unlock()
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -128,6 +128,7 @@ func (s *Server) handleUDPPayload(ctx context.Context, conn stat.Connection, dis
|
||||
|
||||
conn.Write(data.Bytes())
|
||||
})
|
||||
defer udpServer.RemoveRay()
|
||||
|
||||
inbound := session.InboundFromContext(ctx)
|
||||
var dest *net.Destination
|
||||
|
||||
@@ -245,13 +245,15 @@ func (s *Server) handleUDPPayload(ctx context.Context, conn stat.Connection, dis
|
||||
udpMessage, err := EncodeUDPPacket(request, payload.Bytes())
|
||||
payload.Release()
|
||||
|
||||
defer udpMessage.Release()
|
||||
if err != nil {
|
||||
errors.LogWarningInner(ctx, err, "failed to write UDP response")
|
||||
return
|
||||
}
|
||||
defer udpMessage.Release()
|
||||
|
||||
conn.Write(udpMessage.Bytes())
|
||||
})
|
||||
defer udpServer.RemoveRay()
|
||||
|
||||
inbound := session.InboundFromContext(ctx)
|
||||
if inbound != nil && inbound.Source.IsValid() {
|
||||
|
||||
@@ -259,6 +259,7 @@ func (s *Server) handleUDPPayload(ctx context.Context, clientReader *PacketReade
|
||||
errors.LogWarningInner(ctx, err, "failed to write response")
|
||||
}
|
||||
})
|
||||
defer udpServer.RemoveRay()
|
||||
|
||||
inbound := session.InboundFromContext(ctx)
|
||||
user := inbound.User
|
||||
|
||||
@@ -206,7 +206,10 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s
|
||||
|
||||
first := buf.FromBytes(make([]byte, buf.Size))
|
||||
first.Clear()
|
||||
firstLen, _ := first.ReadFrom(connection)
|
||||
firstLen, errR := first.ReadFrom(connection)
|
||||
if errR != nil {
|
||||
return errR
|
||||
}
|
||||
errors.LogInfo(ctx, "firstLen = ", firstLen)
|
||||
|
||||
reader := &buf.BufferedReader{
|
||||
|
||||
Reference in New Issue
Block a user