mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-11-13 21:52:50 +00:00
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b668333546 | ||
|
|
a22c4cf301 | ||
|
|
eba1f7258e |
+29
-11
@@ -15,9 +15,10 @@ type ActivityUpdater interface {
|
|||||||
|
|
||||||
type ActivityTimer struct {
|
type ActivityTimer struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
updated chan struct{}
|
updated chan struct{}
|
||||||
checkTask *task.Periodic
|
checkTask *task.Periodic
|
||||||
onTimeout func()
|
onTimeout func()
|
||||||
|
overridden bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ActivityTimer) Update() {
|
func (t *ActivityTimer) Update() {
|
||||||
@@ -31,14 +32,16 @@ func (t *ActivityTimer) check() error {
|
|||||||
select {
|
select {
|
||||||
case <-t.updated:
|
case <-t.updated:
|
||||||
default:
|
default:
|
||||||
t.finish()
|
t.finish(false)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ActivityTimer) finish() {
|
func (t *ActivityTimer) finish(locked bool) {
|
||||||
t.Lock()
|
if !locked {
|
||||||
defer t.Unlock()
|
t.Lock()
|
||||||
|
defer t.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
if t.onTimeout != nil {
|
if t.onTimeout != nil {
|
||||||
t.onTimeout()
|
t.onTimeout()
|
||||||
@@ -50,9 +53,12 @@ func (t *ActivityTimer) finish() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
|
func (t *ActivityTimer) setTimeout(timeout time.Duration) {
|
||||||
|
if t.onTimeout == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
t.finish()
|
t.finish(true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,14 +67,26 @@ func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
|
|||||||
Execute: t.check,
|
Execute: t.check,
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Lock()
|
|
||||||
|
|
||||||
if t.checkTask != nil {
|
if t.checkTask != nil {
|
||||||
t.checkTask.Close()
|
t.checkTask.Close()
|
||||||
|
t.overridden = true
|
||||||
}
|
}
|
||||||
t.checkTask = checkTask
|
t.checkTask = checkTask
|
||||||
t.Update()
|
t.Update()
|
||||||
common.Must(checkTask.Start())
|
common.Must(checkTask.Start())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
|
||||||
|
t.Lock()
|
||||||
|
t.setTimeout(timeout)
|
||||||
|
t.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *ActivityTimer) SetTimeoutIfNotOverridden(timeout time.Duration) {
|
||||||
|
t.Lock()
|
||||||
|
if !t.overridden {
|
||||||
|
t.setTimeout(timeout)
|
||||||
|
}
|
||||||
t.Unlock()
|
t.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+15
-27
@@ -5,7 +5,6 @@ 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"
|
||||||
@@ -370,10 +369,6 @@ 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{}
|
||||||
}
|
}
|
||||||
@@ -383,28 +378,24 @@ 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
|
|
||||||
c.access.Unlock()
|
if c.conn == nil {
|
||||||
if conn == nil {
|
if err := c.dial(); err != nil {
|
||||||
_, open := <-c.connReady
|
c.access.Unlock()
|
||||||
if !open {
|
errors.LogWarningInner(context.Background(), err, "failed to dial outbound connection")
|
||||||
return 0, io.EOF
|
return len(b), nil
|
||||||
}
|
}
|
||||||
conn = c.conn
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.access.Unlock()
|
||||||
|
|
||||||
return c.conn.Write(b)
|
return c.conn.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -426,14 +417,11 @@ func (c *outboundConn) Read(b []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *outboundConn) Close() error {
|
func (c *outboundConn) Close() error {
|
||||||
c.closeOnce.Do(func() {
|
c.access.Lock()
|
||||||
c.access.Lock()
|
close(c.connReady)
|
||||||
c.closed.Store(true)
|
if c.conn != nil {
|
||||||
close(c.connReady)
|
c.conn.Close()
|
||||||
if c.conn != nil {
|
}
|
||||||
c.conn.Close()
|
c.access.Unlock()
|
||||||
}
|
|
||||||
c.access.Unlock()
|
|
||||||
})
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -595,10 +595,10 @@ func CopyRawConnIfExist(ctx context.Context, readerConn net.Conn, writerConn net
|
|||||||
errors.LogInfo(ctx, "CopyRawConn splice")
|
errors.LogInfo(ctx, "CopyRawConn splice")
|
||||||
statWriter, _ := writer.(*dispatcher.SizeStatWriter)
|
statWriter, _ := writer.(*dispatcher.SizeStatWriter)
|
||||||
//runtime.Gosched() // necessary
|
//runtime.Gosched() // necessary
|
||||||
time.Sleep(time.Millisecond) // without this, there will be a rare ssl error for freedom splice
|
time.Sleep(time.Millisecond) // without this, there will be a rare ssl error for freedom splice
|
||||||
timer.SetTimeout(8 * time.Hour) // prevent leak, just in case
|
timer.SetTimeoutIfNotOverridden(24 * time.Hour) // prevent leak, just in case
|
||||||
if inTimer != nil {
|
if inTimer != nil {
|
||||||
inTimer.SetTimeout(8 * time.Hour)
|
inTimer.SetTimeoutIfNotOverridden(24 * time.Hour)
|
||||||
}
|
}
|
||||||
w, err := tc.ReadFrom(readerConn)
|
w, err := tc.ReadFrom(readerConn)
|
||||||
if readCounter != nil {
|
if readCounter != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user