Compare commits

..

3 Commits

Author SHA1 Message Date
patterniha
b668333546 set splice-timeout to 24 hours 2025-08-22 20:27:10 +03:30
patterniha
a22c4cf301 fix splice-timeout 2025-08-22 19:04:44 +03:30
patterniha
eba1f7258e Timer: prevent creating redundant check task 2025-08-22 09:12:10 +03:30
7 changed files with 48 additions and 17 deletions

View File

@@ -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)

View File

@@ -15,9 +15,10 @@ type ActivityUpdater interface {
type ActivityTimer struct {
sync.RWMutex
updated chan struct{}
checkTask *task.Periodic
onTimeout func()
updated chan struct{}
checkTask *task.Periodic
onTimeout func()
overridden bool
}
func (t *ActivityTimer) Update() {
@@ -31,14 +32,16 @@ func (t *ActivityTimer) check() error {
select {
case <-t.updated:
default:
t.finish()
t.finish(false)
}
return nil
}
func (t *ActivityTimer) finish() {
t.Lock()
defer t.Unlock()
func (t *ActivityTimer) finish(locked bool) {
if !locked {
t.Lock()
defer t.Unlock()
}
if t.onTimeout != nil {
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 {
t.finish()
t.finish(true)
return
}
@@ -61,14 +67,26 @@ func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
Execute: t.check,
}
t.Lock()
if t.checkTask != nil {
t.checkTask.Close()
t.overridden = true
}
t.checkTask = checkTask
t.Update()
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()
}

View File

@@ -595,10 +595,10 @@ func CopyRawConnIfExist(ctx context.Context, readerConn net.Conn, writerConn net
errors.LogInfo(ctx, "CopyRawConn splice")
statWriter, _ := writer.(*dispatcher.SizeStatWriter)
//runtime.Gosched() // necessary
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
time.Sleep(time.Millisecond) // without this, there will be a rare ssl error for freedom splice
timer.SetTimeoutIfNotOverridden(24 * time.Hour) // prevent leak, just in case
if inTimer != nil {
inTimer.SetTimeout(8 * time.Hour)
inTimer.SetTimeoutIfNotOverridden(24 * time.Hour)
}
w, err := tc.ReadFrom(readerConn)
if readCounter != nil {

View File

@@ -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

View File

@@ -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() {

View File

@@ -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

View File

@@ -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{