mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-12-18 21:24:37 +03:00
Compare commits
3 Commits
optimize-p
...
dns-dead-l
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
73baf47358 | ||
|
|
ecc2f73108 | ||
|
|
f45ca197a2 |
@@ -5,6 +5,7 @@ import (
|
|||||||
go_errors "errors"
|
go_errors "errors"
|
||||||
"io"
|
"io"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/xtls/xray-core/common"
|
"github.com/xtls/xray-core/common"
|
||||||
@@ -369,6 +370,10 @@ type outboundConn struct {
|
|||||||
access sync.Mutex
|
access sync.Mutex
|
||||||
dialer func() (stat.Connection, error)
|
dialer func() (stat.Connection, error)
|
||||||
|
|
||||||
|
closeOnce sync.Once
|
||||||
|
dialOnce sync.Once
|
||||||
|
closed atomic.Bool
|
||||||
|
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
connReady chan struct{}
|
connReady chan struct{}
|
||||||
}
|
}
|
||||||
@@ -378,24 +383,28 @@ func (c *outboundConn) dial() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if c.closed.Load() {
|
||||||
|
return errors.New("connection closed during dial")
|
||||||
|
}
|
||||||
c.conn = conn
|
c.conn = conn
|
||||||
c.connReady <- struct{}{}
|
c.connReady <- struct{}{}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *outboundConn) Write(b []byte) (int, error) {
|
func (c *outboundConn) Write(b []byte) (int, error) {
|
||||||
|
c.dialOnce.Do(func() {
|
||||||
|
c.dial()
|
||||||
|
})
|
||||||
c.access.Lock()
|
c.access.Lock()
|
||||||
|
conn := c.conn
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
c.access.Unlock()
|
c.access.Unlock()
|
||||||
|
if conn == nil {
|
||||||
|
_, open := <-c.connReady
|
||||||
|
if !open {
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
conn = c.conn
|
||||||
|
}
|
||||||
return c.conn.Write(b)
|
return c.conn.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -417,11 +426,14 @@ func (c *outboundConn) Read(b []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *outboundConn) Close() error {
|
func (c *outboundConn) Close() error {
|
||||||
c.access.Lock()
|
c.closeOnce.Do(func() {
|
||||||
close(c.connReady)
|
c.access.Lock()
|
||||||
if c.conn != nil {
|
c.closed.Store(true)
|
||||||
c.conn.Close()
|
close(c.connReady)
|
||||||
}
|
if c.conn != nil {
|
||||||
c.access.Unlock()
|
c.conn.Close()
|
||||||
|
}
|
||||||
|
c.access.Unlock()
|
||||||
|
})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user