github.com/kelleygo/clashcore@v1.0.2/component/proxydialer/slowdown.go (about)

     1  package proxydialer
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  	"net/netip"
     7  
     8  	"github.com/kelleygo/clashcore/component/slowdown"
     9  	C "github.com/kelleygo/clashcore/constant"
    10  )
    11  
    12  type SlowDownDialer struct {
    13  	C.Dialer
    14  	Slowdown *slowdown.SlowDown
    15  }
    16  
    17  func (d SlowDownDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
    18  	return slowdown.Do(d.Slowdown, ctx, func() (net.Conn, error) {
    19  		return d.Dialer.DialContext(ctx, network, address)
    20  	})
    21  }
    22  
    23  func (d SlowDownDialer) ListenPacket(ctx context.Context, network, address string, rAddrPort netip.AddrPort) (net.PacketConn, error) {
    24  	return slowdown.Do(d.Slowdown, ctx, func() (net.PacketConn, error) {
    25  		return d.Dialer.ListenPacket(ctx, network, address, rAddrPort)
    26  	})
    27  }
    28  
    29  func NewSlowDownDialer(d C.Dialer, sd *slowdown.SlowDown) SlowDownDialer {
    30  	return SlowDownDialer{
    31  		Dialer:   d,
    32  		Slowdown: sd,
    33  	}
    34  }