Skip to content

Commit 7b5f3d4

Browse files
authored
XTLS Vision: Discard expired pre-connect conn automatically
https://t.me/projectXray/4538408 For #5270
1 parent 93312d2 commit 7b5f3d4

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

proxy/vless/outbound/outbound.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ type Handler struct {
5757

5858
testpre uint32
5959
initpre sync.Once
60-
preConns chan stat.Connection
60+
preConns chan *ConnExpire
61+
}
62+
63+
type ConnExpire struct {
64+
Conn stat.Connection
65+
Expire time.Time
6166
}
6267

6368
// New creates a new VLess outbound handler.
@@ -141,25 +146,33 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
141146

142147
if h.testpre > 0 && h.reverse == nil {
143148
h.initpre.Do(func() {
144-
h.preConns = make(chan stat.Connection)
149+
h.preConns = make(chan *ConnExpire)
145150
for range h.testpre { // TODO: randomize
146151
go func() {
147152
defer func() { recover() }()
148153
ctx := xctx.ContextWithID(context.Background(), session.NewID())
149154
for {
150-
time.Sleep(time.Millisecond * 200) // TODO: randomize
151155
conn, err := dialer.Dial(ctx, rec.Destination)
152156
if err != nil {
153157
errors.LogWarningInner(ctx, err, "pre-connect failed")
154158
continue
155159
}
156-
h.preConns <- conn
160+
h.preConns <- &ConnExpire{Conn: conn, Expire: time.Now().Add(time.Minute * 2)} // TODO: customize & randomize
161+
time.Sleep(time.Millisecond * 200) // TODO: customize & randomize
157162
}
158163
}()
159164
}
160165
})
161-
if conn = <-h.preConns; conn == nil {
162-
return errors.New("closed handler").AtWarning()
166+
for {
167+
connTime := <-h.preConns
168+
if connTime == nil {
169+
return errors.New("closed handler").AtWarning()
170+
}
171+
if time.Now().Before(connTime.Expire) {
172+
conn = connTime.Conn
173+
break
174+
}
175+
connTime.Conn.Close()
163176
}
164177
}
165178

0 commit comments

Comments
 (0)