2022-11-26 23:53:59 +08:00
|
|
|
package tuic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/common/generics/list"
|
2022-12-02 16:56:17 +08:00
|
|
|
N "github.com/Dreamacro/clash/common/net"
|
2022-11-26 23:53:59 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2022-12-02 16:56:17 +08:00
|
|
|
"github.com/Dreamacro/clash/log"
|
2022-11-26 23:53:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type dialResult struct {
|
|
|
|
pc net.PacketConn
|
|
|
|
addr net.Addr
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
type PoolClient struct {
|
|
|
|
*ClientOption
|
|
|
|
|
2022-11-29 09:23:28 +08:00
|
|
|
newClientOption *ClientOption
|
2022-12-22 09:53:11 +08:00
|
|
|
dialResultMap map[C.Dialer]dialResult
|
2022-11-26 23:53:59 +08:00
|
|
|
dialResultMutex *sync.Mutex
|
|
|
|
tcpClients *list.List[*Client]
|
|
|
|
tcpClientsMutex *sync.Mutex
|
|
|
|
udpClients *list.List[*Client]
|
|
|
|
udpClientsMutex *sync.Mutex
|
|
|
|
}
|
|
|
|
|
2022-12-22 09:53:11 +08:00
|
|
|
func (t *PoolClient) DialContextWithDialer(ctx context.Context, metadata *C.Metadata, dialer C.Dialer, dialFn DialFunc) (net.Conn, error) {
|
|
|
|
conn, err := t.getClient(false, dialer).DialContextWithDialer(ctx, metadata, dialer, dialFn)
|
2022-11-26 23:53:59 +08:00
|
|
|
if errors.Is(err, TooManyOpenStreams) {
|
2022-12-22 09:53:11 +08:00
|
|
|
conn, err = t.newClient(false, dialer).DialContextWithDialer(ctx, metadata, dialer, dialFn)
|
2022-12-20 00:11:02 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return N.NewRefConn(conn, t), err
|
|
|
|
}
|
|
|
|
|
2022-12-22 09:53:11 +08:00
|
|
|
func (t *PoolClient) ListenPacketWithDialer(ctx context.Context, metadata *C.Metadata, dialer C.Dialer, dialFn DialFunc) (net.PacketConn, error) {
|
|
|
|
pc, err := t.getClient(true, dialer).ListenPacketWithDialer(ctx, metadata, dialer, dialFn)
|
2022-12-20 00:11:02 +08:00
|
|
|
if errors.Is(err, TooManyOpenStreams) {
|
2022-12-22 09:53:11 +08:00
|
|
|
pc, err = t.newClient(true, dialer).ListenPacketWithDialer(ctx, metadata, dialer, dialFn)
|
2022-12-20 00:11:02 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return N.NewRefPacketConn(pc, t), nil
|
|
|
|
}
|
|
|
|
|
2022-12-22 09:53:11 +08:00
|
|
|
func (t *PoolClient) dial(ctx context.Context, dialer C.Dialer, dialFn DialFunc) (pc net.PacketConn, addr net.Addr, err error) {
|
2022-11-26 23:53:59 +08:00
|
|
|
t.dialResultMutex.Lock()
|
2022-12-22 09:53:11 +08:00
|
|
|
dr, ok := t.dialResultMap[dialer]
|
2022-11-26 23:53:59 +08:00
|
|
|
t.dialResultMutex.Unlock()
|
|
|
|
if ok {
|
|
|
|
return dr.pc, dr.addr, dr.err
|
|
|
|
}
|
|
|
|
|
2022-12-22 09:53:11 +08:00
|
|
|
pc, addr, err = dialFn(ctx, dialer)
|
2022-11-26 23:53:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-10 12:22:12 +08:00
|
|
|
if _, ok := pc.(*net.UDPConn); ok { // only cache the system's UDPConn
|
|
|
|
dr.pc, dr.addr, dr.err = pc, addr, err
|
|
|
|
|
|
|
|
t.dialResultMutex.Lock()
|
|
|
|
t.dialResultMap[dialer] = dr
|
|
|
|
t.dialResultMutex.Unlock()
|
|
|
|
}
|
2022-11-26 23:53:59 +08:00
|
|
|
|
|
|
|
return pc, addr, err
|
|
|
|
}
|
|
|
|
|
2022-11-28 17:09:25 +08:00
|
|
|
func (t *PoolClient) forceClose() {
|
2022-11-26 23:53:59 +08:00
|
|
|
t.dialResultMutex.Lock()
|
|
|
|
defer t.dialResultMutex.Unlock()
|
|
|
|
for key := range t.dialResultMap {
|
|
|
|
pc := t.dialResultMap[key].pc
|
|
|
|
if pc != nil {
|
|
|
|
_ = pc.Close()
|
|
|
|
}
|
|
|
|
delete(t.dialResultMap, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 09:53:11 +08:00
|
|
|
func (t *PoolClient) newClient(udp bool, dialer C.Dialer) *Client {
|
2022-11-26 23:53:59 +08:00
|
|
|
clients := t.tcpClients
|
|
|
|
clientsMutex := t.tcpClientsMutex
|
|
|
|
if udp {
|
|
|
|
clients = t.udpClients
|
|
|
|
clientsMutex = t.udpClientsMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
clientsMutex.Lock()
|
|
|
|
defer clientsMutex.Unlock()
|
|
|
|
|
2022-11-29 09:23:28 +08:00
|
|
|
client := NewClient(t.newClientOption, udp)
|
2022-12-22 09:53:11 +08:00
|
|
|
client.dialerRef = dialer
|
2022-11-26 23:53:59 +08:00
|
|
|
client.lastVisited = time.Now()
|
|
|
|
|
|
|
|
clients.PushFront(client)
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
2022-12-22 09:53:11 +08:00
|
|
|
func (t *PoolClient) getClient(udp bool, dialer C.Dialer) *Client {
|
2022-11-26 23:53:59 +08:00
|
|
|
clients := t.tcpClients
|
|
|
|
clientsMutex := t.tcpClientsMutex
|
|
|
|
if udp {
|
|
|
|
clients = t.udpClients
|
|
|
|
clientsMutex = t.udpClientsMutex
|
|
|
|
}
|
|
|
|
var bestClient *Client
|
|
|
|
|
|
|
|
func() {
|
|
|
|
clientsMutex.Lock()
|
|
|
|
defer clientsMutex.Unlock()
|
|
|
|
for it := clients.Front(); it != nil; {
|
|
|
|
client := it.Value
|
|
|
|
if client == nil {
|
|
|
|
next := it.Next()
|
|
|
|
clients.Remove(it)
|
|
|
|
it = next
|
|
|
|
continue
|
|
|
|
}
|
2022-12-22 09:53:11 +08:00
|
|
|
if client.dialerRef == dialer {
|
2022-11-26 23:53:59 +08:00
|
|
|
if bestClient == nil {
|
|
|
|
bestClient = client
|
|
|
|
} else {
|
|
|
|
if client.openStreams.Load() < bestClient.openStreams.Load() {
|
|
|
|
bestClient = client
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
it = it.Next()
|
|
|
|
}
|
|
|
|
}()
|
2022-12-02 16:56:17 +08:00
|
|
|
for it := clients.Front(); it != nil; {
|
|
|
|
client := it.Value
|
|
|
|
if client != bestClient && client.openStreams.Load() == 0 && time.Now().Sub(client.lastVisited) > 30*time.Minute {
|
|
|
|
client.Close()
|
|
|
|
next := it.Next()
|
|
|
|
clients.Remove(it)
|
|
|
|
it = next
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
it = it.Next()
|
|
|
|
}
|
2022-11-26 23:53:59 +08:00
|
|
|
|
|
|
|
if bestClient == nil {
|
2022-12-22 09:53:11 +08:00
|
|
|
return t.newClient(udp, dialer)
|
2022-11-26 23:53:59 +08:00
|
|
|
} else {
|
|
|
|
bestClient.lastVisited = time.Now()
|
|
|
|
return bestClient
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 16:56:17 +08:00
|
|
|
func NewPoolClient(clientOption *ClientOption) *PoolClient {
|
2022-11-26 23:53:59 +08:00
|
|
|
p := &PoolClient{
|
|
|
|
ClientOption: clientOption,
|
2022-12-22 09:53:11 +08:00
|
|
|
dialResultMap: make(map[C.Dialer]dialResult),
|
2022-11-26 23:53:59 +08:00
|
|
|
dialResultMutex: &sync.Mutex{},
|
|
|
|
tcpClients: list.New[*Client](),
|
|
|
|
tcpClientsMutex: &sync.Mutex{},
|
|
|
|
udpClients: list.New[*Client](),
|
|
|
|
udpClientsMutex: &sync.Mutex{},
|
|
|
|
}
|
2022-11-29 09:23:28 +08:00
|
|
|
newClientOption := *clientOption
|
|
|
|
p.newClientOption = &newClientOption
|
2022-11-26 23:53:59 +08:00
|
|
|
runtime.SetFinalizer(p, closeClientPool)
|
2022-12-02 16:56:17 +08:00
|
|
|
log.Debugln("New Tuic PoolClient at %p", p)
|
2022-11-26 23:53:59 +08:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func closeClientPool(client *PoolClient) {
|
2022-12-02 16:56:17 +08:00
|
|
|
log.Debugln("Close Tuic PoolClient at %p", client)
|
2022-11-28 17:09:25 +08:00
|
|
|
client.forceClose()
|
2022-11-26 23:53:59 +08:00
|
|
|
}
|