Clash.Meta/transport/tuic/pool_client.go

194 lines
4.9 KiB
Go
Raw Normal View History

2022-11-26 23:53:59 +08:00
package tuic
import (
"context"
"errors"
"net"
"runtime"
"sync"
"time"
"github.com/Dreamacro/clash/common/generics/list"
N "github.com/Dreamacro/clash/common/net"
2022-11-26 23:53:59 +08:00
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
2023-06-03 16:45:35 +08:00
"github.com/metacubex/quic-go"
2022-11-26 23:53:59 +08:00
)
type dialResult struct {
2023-06-03 16:45:35 +08:00
transport *quic.Transport
addr net.Addr
err error
2022-11-26 23:53:59 +08:00
}
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) {
2023-06-03 16:45:35 +08:00
newDialFn := func(ctx context.Context, dialer C.Dialer) (transport *quic.Transport, addr net.Addr, err error) {
return t.dial(ctx, dialer, dialFn)
}
conn, err := t.getClient(false, dialer).DialContextWithDialer(ctx, metadata, dialer, newDialFn)
2022-11-26 23:53:59 +08:00
if errors.Is(err, TooManyOpenStreams) {
2023-06-03 16:45:35 +08:00
conn, err = t.newClient(false, dialer).DialContextWithDialer(ctx, metadata, dialer, newDialFn)
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) {
2023-06-03 16:45:35 +08:00
newDialFn := func(ctx context.Context, dialer C.Dialer) (transport *quic.Transport, addr net.Addr, err error) {
return t.dial(ctx, dialer, dialFn)
}
pc, err := t.getClient(true, dialer).ListenPacketWithDialer(ctx, metadata, dialer, newDialFn)
2022-12-20 00:11:02 +08:00
if errors.Is(err, TooManyOpenStreams) {
2023-06-03 16:45:35 +08:00
pc, err = t.newClient(true, dialer).ListenPacketWithDialer(ctx, metadata, dialer, newDialFn)
2022-12-20 00:11:02 +08:00
}
if err != nil {
return nil, err
}
return N.NewRefPacketConn(pc, t), nil
}
2023-06-03 16:45:35 +08:00
func (t *PoolClient) dial(ctx context.Context, dialer C.Dialer, dialFn DialFunc) (transport *quic.Transport, 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 {
2023-06-03 16:45:35 +08:00
return dr.transport, dr.addr, dr.err
2022-11-26 23:53:59 +08:00
}
2023-06-03 16:45:35 +08:00
transport, addr, err = dialFn(ctx, dialer)
2022-11-26 23:53:59 +08:00
if err != nil {
return nil, nil, err
}
2023-06-03 16:45:35 +08:00
if _, ok := transport.Conn.(*net.UDPConn); ok { // only cache the system's UDPConn
transport.SetSingleUse(false) // don't close transport in each dial
dr.transport, dr.addr, dr.err = transport, addr, err
t.dialResultMutex.Lock()
t.dialResultMap[dialer] = dr
t.dialResultMutex.Unlock()
}
2022-11-26 23:53:59 +08:00
2023-06-03 16:45:35 +08:00
return transport, addr, err
2022-11-26 23:53:59 +08:00
}
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 {
2023-06-03 16:45:35 +08:00
transport := t.dialResultMap[key].transport
if transport != nil {
_ = transport.Close()
2022-11-26 23:53:59 +08:00
}
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()
}
}()
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
}
}
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)
log.Debugln("New Tuic PoolClient at %p", p)
2022-11-26 23:53:59 +08:00
return p
}
func closeClientPool(client *PoolClient) {
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
}