Skip to content

Commit 6aa4da5

Browse files
author
Medium1992
committed
XHTTP server: Allow Mux.Cool and send KeepAlive
XTLS#4128 limited Mux.Cool over XHTTP to pure XUDP. Allowing TCP sessions alone makes idle cuts worse: a quiet Mux connection is still cut by CDN idle timers, and every session on it drops together. SessionStatusKeepAlive is handled on both sides but never sent. The server now sends it every 3-8s, but only on XHTTP packet-up and stream-up, whose downlink is a long-lived response of its own. Other transports, and stream-one, which is what a client picks for REALITY, get no extra traffic.
1 parent c412e77 commit 6aa4da5

4 files changed

Lines changed: 37 additions & 4 deletions

File tree

‎app/proxyman/inbound/inbound.go‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/xtls/xray-core/app/proxyman"
88
"github.com/xtls/xray-core/common"
99
"github.com/xtls/xray-core/common/errors"
10-
"github.com/xtls/xray-core/common/net"
1110
"github.com/xtls/xray-core/common/serial"
1211
"github.com/xtls/xray-core/common/session"
1312
"github.com/xtls/xray-core/core"
@@ -174,9 +173,6 @@ func NewHandler(ctx context.Context, config *core.InboundHandlerConfig) (inbound
174173
Mark: streamSettings.SocketSettings.Mark,
175174
})
176175
}
177-
if streamSettings != nil && streamSettings.ProtocolName == "splithttp" {
178-
ctx = session.ContextWithAllowedNetwork(ctx, net.Network_UDP)
179-
}
180176

181177
return NewAlwaysOnInboundHandler(ctx, tag, receiverSettings, proxySettings)
182178
}

‎common/mux/server.go‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/xtls/xray-core/common"
99
"github.com/xtls/xray-core/common/buf"
10+
"github.com/xtls/xray-core/common/dice"
1011
"github.com/xtls/xray-core/common/errors"
1112
"github.com/xtls/xray-core/common/log"
1213
"github.com/xtls/xray-core/common/net"
@@ -16,6 +17,7 @@ import (
1617
"github.com/xtls/xray-core/core"
1718
"github.com/xtls/xray-core/features/routing"
1819
"github.com/xtls/xray-core/transport"
20+
"github.com/xtls/xray-core/transport/internet/stat"
1921
"github.com/xtls/xray-core/transport/pipe"
2022
)
2123

@@ -102,6 +104,9 @@ func NewServerWorker(ctx context.Context, d routing.Dispatcher, link *transport.
102104
}
103105
if inbound := session.InboundFromContext(ctx); inbound != nil {
104106
inbound.CanSpliceCopy = 3
107+
if conn, ok := stat.TryUnwrapStatsConn(inbound.Conn).(idleTimedConn); ok && conn.IdleTimed() {
108+
go worker.keepAlive()
109+
}
105110
}
106111
go worker.run(ctx)
107112
go worker.monitor()
@@ -139,6 +144,30 @@ func (w *ServerWorker) monitor() {
139144
}
140145
}
141146

147+
// idleTimedConn is implemented by transports whose downlink is an HTTP response
148+
// that middleboxes cut once it carries nothing for a while.
149+
type idleTimedConn interface {
150+
IdleTimed() bool
151+
}
152+
153+
// keepAlive sends the KeepAlive frame every client already discards, so that a
154+
// downlink whose sessions are all quiet is not taken for idle on the way. It
155+
// only runs where that timer exists, so other transports gain no pattern.
156+
func (w *ServerWorker) keepAlive() {
157+
for {
158+
select {
159+
case <-w.done.Wait():
160+
return
161+
case <-time.After(time.Duration(3000+dice.Roll(5000)) * time.Millisecond):
162+
}
163+
b := buf.New()
164+
common.Must(FrameMetadata{SessionStatus: SessionStatusKeepAlive}.WriteTo(b))
165+
if err := w.link.Writer.WriteMultiBuffer(buf.MultiBuffer{b}); err != nil {
166+
return
167+
}
168+
}
169+
}
170+
142171
func (w *ServerWorker) ActiveConnections() uint32 {
143172
return uint32(w.sessionManager.Size())
144173
}

‎transport/internet/splithttp/connection.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ type splitConn struct {
1212
remoteAddr net.Addr
1313
localAddr net.Addr
1414
onClose func()
15+
idleTimed bool
16+
}
17+
18+
// IdleTimed reports that the downlink is a long-lived response of its own, as
19+
// in packet-up and stream-up, which is what CDN idle timers cut.
20+
func (c *splitConn) IdleTimed() bool {
21+
return c.idleTimed
1522
}
1623

1724
func (c *splitConn) Write(b []byte) (int, error) {

‎transport/internet/splithttp/hub.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
386386
}
387387
if sessionId != "" { // if not stream-one
388388
conn.reader = currentSession.uploadQueue
389+
conn.idleTimed = true
389390
}
390391

391392
h.ln.addConn(stat.Connection(&conn))

0 commit comments

Comments
 (0)