mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-11-30 13:48:36 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
964ebe8ffa |
@@ -68,5 +68,6 @@ func RCodeFromError(err error) uint16 {
|
|||||||
if r, ok := cause.(RCodeError); ok {
|
if r, ok := cause.(RCodeError); ok {
|
||||||
return uint16(r)
|
return uint16(r)
|
||||||
}
|
}
|
||||||
return 0
|
// unknown error
|
||||||
|
return 2
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-72
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/xtls/xray-core/common"
|
"github.com/xtls/xray-core/common"
|
||||||
"github.com/xtls/xray-core/common/buf"
|
"github.com/xtls/xray-core/common/buf"
|
||||||
@@ -13,7 +12,6 @@ import (
|
|||||||
"github.com/xtls/xray-core/common/net"
|
"github.com/xtls/xray-core/common/net"
|
||||||
"github.com/xtls/xray-core/common/protocol"
|
"github.com/xtls/xray-core/common/protocol"
|
||||||
"github.com/xtls/xray-core/common/session"
|
"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/core"
|
||||||
"github.com/xtls/xray-core/features/policy"
|
"github.com/xtls/xray-core/features/policy"
|
||||||
"github.com/xtls/xray-core/features/routing"
|
"github.com/xtls/xray-core/features/routing"
|
||||||
@@ -178,7 +176,7 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn st
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
writer = NewPacketWriter(ctx, pConn, &dest, mark, back)
|
writer = NewPacketWriter(pConn, &dest, mark, back)
|
||||||
defer writer.(*PacketWriter).Close() // close fake UDP conns
|
defer writer.(*PacketWriter).Close() // close fake UDP conns
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -192,35 +190,22 @@ 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()
|
return nil // Unlike Dispatch(), DispatchLink() will not return until the outbound finishes Process()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPacketWriter(ctx context.Context, conn net.PacketConn, d *net.Destination, mark int, back *net.UDPAddr) buf.Writer {
|
func NewPacketWriter(conn net.PacketConn, d *net.Destination, mark int, back *net.UDPAddr) buf.Writer {
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
|
||||||
writer := &PacketWriter{
|
writer := &PacketWriter{
|
||||||
ctx: ctx,
|
conn: conn,
|
||||||
cancel: cancel,
|
conns: make(map[net.Destination]net.PacketConn),
|
||||||
conn: conn,
|
mark: mark,
|
||||||
conns: utils.NewTypedSyncMap[net.Destination, *PacketConnTimeWrapper](),
|
back: back,
|
||||||
inactiveConns: make([]*PacketConnTimeWrapper, 0),
|
|
||||||
mark: mark,
|
|
||||||
back: back,
|
|
||||||
}
|
}
|
||||||
timedconn := &PacketConnTimeWrapper{
|
writer.conns[*d] = conn
|
||||||
PacketConn: conn,
|
|
||||||
lastUsedTime: time.Now(),
|
|
||||||
isMainConn: true,
|
|
||||||
}
|
|
||||||
writer.conns.Store(*d, timedconn)
|
|
||||||
go writer.cleanInactiveConns()
|
|
||||||
return writer
|
return writer
|
||||||
}
|
}
|
||||||
|
|
||||||
type PacketWriter struct {
|
type PacketWriter struct {
|
||||||
ctx context.Context
|
conn net.PacketConn
|
||||||
cancel context.CancelFunc
|
conns map[net.Destination]net.PacketConn
|
||||||
conn net.PacketConn
|
mark int
|
||||||
conns *utils.TypedSyncMap[net.Destination, *PacketConnTimeWrapper]
|
back *net.UDPAddr
|
||||||
inactiveConns []*PacketConnTimeWrapper
|
|
||||||
mark int
|
|
||||||
back *net.UDPAddr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
||||||
@@ -232,30 +217,26 @@ func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
|||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
if b.UDP != nil && b.UDP.Address.Family().IsIP() {
|
if b.UDP != nil && b.UDP.Address.Family().IsIP() {
|
||||||
conn, _ := w.conns.Load(*b.UDP)
|
conn := w.conns[*b.UDP]
|
||||||
if conn == nil {
|
if conn == nil {
|
||||||
fakeudpconn, err := FakeUDP(
|
conn, err = FakeUDP(
|
||||||
&net.UDPAddr{
|
&net.UDPAddr{
|
||||||
IP: b.UDP.Address.IP(),
|
IP: b.UDP.Address.IP(),
|
||||||
Port: int(b.UDP.Port),
|
Port: int(b.UDP.Port),
|
||||||
},
|
},
|
||||||
w.mark,
|
w.mark,
|
||||||
)
|
)
|
||||||
conn = &PacketConnTimeWrapper{
|
|
||||||
PacketConn: fakeudpconn,
|
|
||||||
lastUsedTime: time.Now(),
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errors.LogInfo(context.Background(), err.Error())
|
errors.LogInfo(context.Background(), err.Error())
|
||||||
b.Release()
|
b.Release()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
w.conns.Store(*b.UDP, conn)
|
w.conns[*b.UDP] = conn
|
||||||
}
|
}
|
||||||
_, err = conn.WriteTo(b.Bytes(), w.back)
|
_, err = conn.WriteTo(b.Bytes(), w.back)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errors.LogInfo(context.Background(), err.Error())
|
errors.LogInfo(context.Background(), err.Error())
|
||||||
w.conns.Delete(*b.UDP)
|
w.conns[*b.UDP] = nil
|
||||||
conn.Close()
|
conn.Close()
|
||||||
}
|
}
|
||||||
b.Release()
|
b.Release()
|
||||||
@@ -272,43 +253,10 @@ func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *PacketWriter) Close() error {
|
func (w *PacketWriter) Close() error {
|
||||||
w.cancel()
|
for _, conn := range w.conns {
|
||||||
w.conns.Range(func(key net.Destination, conn *PacketConnTimeWrapper) bool {
|
if conn != nil {
|
||||||
common.CloseIfExists(conn)
|
conn.Close()
|
||||||
return true
|
}
|
||||||
})
|
}
|
||||||
return nil
|
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)
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user