Compare commits

..
Author SHA1 Message Date
风扇滑翔翼 a3248b2d54 wrap WriteTo 2025-10-29 12:39:49 +00:00
风扇滑翔翼 26b246fa92 Dokodemo: Recycle inactive fakeudp connections 2025-10-29 09:28:57 +00:00
9 changed files with 92 additions and 146 deletions
+1 -1
View File
@@ -132,7 +132,7 @@ jobs:
mv build_assets Xray-${{ env.ASSET_NAME }}
- name: Upload files to Artifacts
uses: actions/upload-artifact@v5
uses: actions/upload-artifact@v4
with:
name: Xray-${{ env.ASSET_NAME }}
path: |
+1 -1
View File
@@ -238,7 +238,7 @@ jobs:
mv build_assets Xray-${{ env.ASSET_NAME }}
- name: Upload files to Artifacts
uses: actions/upload-artifact@v5
uses: actions/upload-artifact@v4
with:
name: Xray-${{ env.ASSET_NAME }}
path: |
+2 -6
View File
@@ -317,12 +317,8 @@ func (h *Handler) Dial(ctx context.Context, dest net.Destination) (stat.Connecti
conn, err := internet.Dial(ctx, dest, h.streamSettings)
conn = h.getStatCouterConnection(conn)
outbounds := session.OutboundsFromContext(ctx)
if outbounds != nil {
ob := outbounds[len(outbounds)-1]
ob.Conn = conn
} else {
// for Vision's pre-connect
}
ob := outbounds[len(outbounds)-1]
ob.Conn = conn
return conn, err
}
-2
View File
@@ -212,7 +212,6 @@ type VLessOutboundConfig struct {
Seed string `json:"seed"`
Encryption string `json:"encryption"`
Reverse *vless.Reverse `json:"reverse"`
Testpre uint32 `json:"testpre"`
Vnext []*VLessOutboundVnext `json:"vnext"`
}
@@ -259,7 +258,6 @@ func (c *VLessOutboundConfig) Build() (proto.Message, error) {
//account.Seed = c.Seed
account.Encryption = c.Encryption
account.Reverse = c.Reverse
account.Testpre = c.Testpre
} else {
if err := json.Unmarshal(rawUser, account); err != nil {
return nil, errors.New(`VLESS users: invalid user`).Base(err)
+72 -20
View File
@@ -4,6 +4,7 @@ import (
"context"
"strconv"
"strings"
"time"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
@@ -12,6 +13,7 @@ import (
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/protocol"
"github.com/xtls/xray-core/common/session"
"github.com/xtls/xray-core/common/utils"
"github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/policy"
"github.com/xtls/xray-core/features/routing"
@@ -176,7 +178,7 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn st
if err != nil {
return err
}
writer = NewPacketWriter(pConn, &dest, mark, back)
writer = NewPacketWriter(ctx, pConn, &dest, mark, back)
defer writer.(*PacketWriter).Close() // close fake UDP conns
}
}
@@ -190,22 +192,35 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn st
return nil // Unlike Dispatch(), DispatchLink() will not return until the outbound finishes Process()
}
func NewPacketWriter(conn net.PacketConn, d *net.Destination, mark int, back *net.UDPAddr) buf.Writer {
func NewPacketWriter(ctx context.Context, conn net.PacketConn, d *net.Destination, mark int, back *net.UDPAddr) buf.Writer {
ctx, cancel := context.WithCancel(ctx)
writer := &PacketWriter{
conn: conn,
conns: make(map[net.Destination]net.PacketConn),
mark: mark,
back: back,
ctx: ctx,
cancel: cancel,
conn: conn,
conns: utils.NewTypedSyncMap[net.Destination, *PacketConnTimeWrapper](),
inactiveConns: make([]*PacketConnTimeWrapper, 0),
mark: mark,
back: back,
}
writer.conns[*d] = conn
timedconn := &PacketConnTimeWrapper{
PacketConn: conn,
lastUsedTime: time.Now(),
isMainConn: true,
}
writer.conns.Store(*d, timedconn)
go writer.cleanInactiveConns()
return writer
}
type PacketWriter struct {
conn net.PacketConn
conns map[net.Destination]net.PacketConn
mark int
back *net.UDPAddr
ctx context.Context
cancel context.CancelFunc
conn net.PacketConn
conns *utils.TypedSyncMap[net.Destination, *PacketConnTimeWrapper]
inactiveConns []*PacketConnTimeWrapper
mark int
back *net.UDPAddr
}
func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
@@ -217,26 +232,30 @@ func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
}
var err error
if b.UDP != nil && b.UDP.Address.Family().IsIP() {
conn := w.conns[*b.UDP]
conn, _ := w.conns.Load(*b.UDP)
if conn == nil {
conn, err = FakeUDP(
fakeudpconn, err := FakeUDP(
&net.UDPAddr{
IP: b.UDP.Address.IP(),
Port: int(b.UDP.Port),
},
w.mark,
)
conn = &PacketConnTimeWrapper{
PacketConn: fakeudpconn,
lastUsedTime: time.Now(),
}
if err != nil {
errors.LogInfo(context.Background(), err.Error())
b.Release()
continue
}
w.conns[*b.UDP] = conn
w.conns.Store(*b.UDP, conn)
}
_, err = conn.WriteTo(b.Bytes(), w.back)
if err != nil {
errors.LogInfo(context.Background(), err.Error())
w.conns[*b.UDP] = nil
w.conns.Delete(*b.UDP)
conn.Close()
}
b.Release()
@@ -253,10 +272,43 @@ func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
}
func (w *PacketWriter) Close() error {
for _, conn := range w.conns {
if conn != nil {
conn.Close()
}
}
w.cancel()
w.conns.Range(func(key net.Destination, conn *PacketConnTimeWrapper) bool {
common.CloseIfExists(conn)
return true
})
return nil
}
func (w *PacketWriter) cleanInactiveConns() {
ticker := time.NewTicker(60 * time.Second)
defer ticker.Stop()
select {
case <-ticker.C:
if len(w.inactiveConns) > 0 {
for _, conn := range w.inactiveConns {
common.CloseIfExists(conn)
}
}
w.conns.Range(func(key net.Destination, conn *PacketConnTimeWrapper) bool {
if conn != nil && !conn.isMainConn && time.Since(conn.lastUsedTime) > 120*time.Second {
w.conns.Delete(key)
}
w.inactiveConns = append(w.inactiveConns, conn)
return true
})
case <-w.ctx.Done():
return
}
}
type PacketConnTimeWrapper struct {
net.PacketConn
lastUsedTime time.Time
isMainConn bool
}
func (c *PacketConnTimeWrapper) WriteTo(b []byte, addr net.Addr) (n int, err error) {
c.lastUsedTime = time.Now()
return c.PacketConn.WriteTo(b, addr)
}
-4
View File
@@ -22,7 +22,6 @@ func (a *Account) AsAccount() (protocol.Account, error) {
Seconds: a.Seconds,
Padding: a.Padding,
Reverse: a.Reverse,
Testpre: a.Testpre,
}, nil
}
@@ -39,8 +38,6 @@ type MemoryAccount struct {
Padding string
Reverse *Reverse
Testpre uint32
}
// Equals implements protocol.Account.Equals().
@@ -61,6 +58,5 @@ func (a *MemoryAccount) ToProto() proto.Message {
Seconds: a.Seconds,
Padding: a.Padding,
Reverse: a.Reverse,
Testpre: a.Testpre,
}
}
+8 -18
View File
@@ -79,7 +79,6 @@ type Account struct {
Seconds uint32 `protobuf:"varint,5,opt,name=seconds,proto3" json:"seconds,omitempty"`
Padding string `protobuf:"bytes,6,opt,name=padding,proto3" json:"padding,omitempty"`
Reverse *Reverse `protobuf:"bytes,7,opt,name=reverse,proto3" json:"reverse,omitempty"`
Testpre uint32 `protobuf:"varint,8,opt,name=testpre,proto3" json:"testpre,omitempty"`
}
func (x *Account) Reset() {
@@ -161,13 +160,6 @@ func (x *Account) GetReverse() *Reverse {
return nil
}
func (x *Account) GetTestpre() uint32 {
if x != nil {
return x.Testpre
}
return 0
}
var File_proxy_vless_account_proto protoreflect.FileDescriptor
var file_proxy_vless_account_proto_rawDesc = []byte{
@@ -175,7 +167,7 @@ var file_proxy_vless_account_proto_rawDesc = []byte{
0x63, 0x6f, 0x75, 0x6e, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x10, 0x78, 0x72, 0x61,
0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x76, 0x6c, 0x65, 0x73, 0x73, 0x22, 0x1b, 0x0a,
0x07, 0x52, 0x65, 0x76, 0x65, 0x72, 0x73, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x74, 0x61, 0x67, 0x18,
0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x74, 0x61, 0x67, 0x22, 0xea, 0x01, 0x0a, 0x07, 0x41,
0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x74, 0x61, 0x67, 0x22, 0xd0, 0x01, 0x0a, 0x07, 0x41,
0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01,
0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x66, 0x6c, 0x6f, 0x77, 0x18, 0x02,
0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x66, 0x6c, 0x6f, 0x77, 0x12, 0x1e, 0x0a, 0x0a, 0x65, 0x6e,
@@ -188,15 +180,13 @@ var file_proxy_vless_account_proto_rawDesc = []byte{
0x07, 0x70, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67, 0x12, 0x33, 0x0a, 0x07, 0x72, 0x65, 0x76, 0x65,
0x72, 0x73, 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x19, 0x2e, 0x78, 0x72, 0x61, 0x79,
0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x76, 0x6c, 0x65, 0x73, 0x73, 0x2e, 0x52, 0x65, 0x76,
0x65, 0x72, 0x73, 0x65, 0x52, 0x07, 0x72, 0x65, 0x76, 0x65, 0x72, 0x73, 0x65, 0x12, 0x18, 0x0a,
0x07, 0x74, 0x65, 0x73, 0x74, 0x70, 0x72, 0x65, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x07,
0x74, 0x65, 0x73, 0x74, 0x70, 0x72, 0x65, 0x42, 0x52, 0x0a, 0x14, 0x63, 0x6f, 0x6d, 0x2e, 0x78,
0x72, 0x61, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x76, 0x6c, 0x65, 0x73, 0x73, 0x50,
0x01, 0x5a, 0x25, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x74,
0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x70, 0x72, 0x6f,
0x78, 0x79, 0x2f, 0x76, 0x6c, 0x65, 0x73, 0x73, 0xaa, 0x02, 0x10, 0x58, 0x72, 0x61, 0x79, 0x2e,
0x50, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x56, 0x6c, 0x65, 0x73, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x33,
0x65, 0x72, 0x73, 0x65, 0x52, 0x07, 0x72, 0x65, 0x76, 0x65, 0x72, 0x73, 0x65, 0x42, 0x52, 0x0a,
0x14, 0x63, 0x6f, 0x6d, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e,
0x76, 0x6c, 0x65, 0x73, 0x73, 0x50, 0x01, 0x5a, 0x25, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e,
0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f,
0x72, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2f, 0x76, 0x6c, 0x65, 0x73, 0x73, 0xaa, 0x02,
0x10, 0x58, 0x72, 0x61, 0x79, 0x2e, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x56, 0x6c, 0x65, 0x73,
0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
-2
View File
@@ -22,6 +22,4 @@ message Account {
string padding = 6;
Reverse reverse = 7;
uint32 testpre = 8;
}
+8 -92
View File
@@ -7,7 +7,6 @@ import (
"encoding/base64"
"reflect"
"strings"
"sync"
"time"
"unsafe"
@@ -53,12 +52,6 @@ type Handler struct {
cone bool
encryption *encryption.ClientInstance
reverse *Reverse
testpre uint32
initConns sync.Once
preConns chan stat.Connection
preConnWait chan struct{}
preConnStop chan struct{}
}
// New creates a new VLess outbound handler.
@@ -112,20 +105,11 @@ func New(ctx context.Context, config *Config) (*Handler, error) {
}()
}
handler.testpre = a.Testpre
return handler, nil
}
// Close implements common.Closable.Close().
func (h *Handler) Close() error {
if h.preConnStop != nil {
close(h.preConnStop)
for range h.testpre {
conn := <-h.preConns
common.CloseIfExists(conn)
}
}
if h.reverse != nil {
return h.reverse.Close()
}
@@ -144,38 +128,18 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
rec := h.server
var conn stat.Connection
if h.testpre > 0 && h.reverse == nil {
h.initConns.Do(func() {
h.preConns = make(chan stat.Connection, h.testpre)
h.preConnStop = make(chan struct{})
go h.preConnWorker(dialer, rec.Destination)
})
select {
case h.preConnWait <- struct{}{}:
default:
}
select {
case conn = <-h.preConns:
default:
}
}
if conn == nil {
if err := retry.ExponentialBackoff(5, 200).On(func() error {
var err error
conn, err = dialer.Dial(ctx, rec.Destination)
if err != nil {
return err
}
return nil
}); err != nil {
return errors.New("failed to find an available destination").Base(err).AtWarning()
if err := retry.ExponentialBackoff(5, 200).On(func() error {
var err error
conn, err = dialer.Dial(ctx, rec.Destination)
if err != nil {
return err
}
return nil
}); err != nil {
return errors.New("failed to find an available destination").Base(err).AtWarning()
}
defer conn.Close()
ob.Conn = conn // for Vision's pre-connect
iConn := conn
if statConn, ok := iConn.(*stat.CounterConnection); ok {
iConn = statConn.Connection
@@ -462,51 +426,3 @@ func (r *Reverse) Start() error {
func (r *Reverse) Close() error {
return r.monitorTask.Close()
}
func (h *Handler) preConnWorker(dialer internet.Dialer, dest net.Destination) {
// conn in conns may be nil
conns := make(chan stat.Connection)
dial := func() {
conn, err := dialer.Dial(context.Background(), dest)
if err != nil {
errors.LogError(context.Background(), "failed to dial VLESS pre connection: ", err)
common.CloseIfExists(conn)
}
conns <- conn
}
go func() {
go dial() // get a conn immediately
for range h.testpre - 1 {
select {
case <-h.preConnWait:
go dial()
case <-h.preConnStop:
return
}
}
}()
for {
select {
case conn := <-conns:
if conn != nil {
select {
case h.preConns <- conn:
case <-h.preConnStop:
common.CloseIfExists(conn)
return
}
go dial()
} else {
// sleep until next client try if dial failed
select {
case <-h.preConnWait:
go dial()
case <-h.preConnStop:
return
}
}
case <-h.preConnStop:
return
}
}
}