From 344b2f467d34e470d13e5eec4a76dd79f08371b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=92=B1=E7=BA=AF=E5=87=80?= <1113655791@qq.com> Date: Wed, 5 Mar 2025 23:14:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0NewRandomDial,=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E9=9A=8F=E6=9C=BA=E8=BF=9E=E6=8E=A5,=E9=98=B2?= =?UTF-8?q?=E6=AD=A2=E8=BF=9E=E6=8E=A5=E6=B1=A0=E5=AF=B9=E4=B8=80=E4=B8=AA?= =?UTF-8?q?IP=E5=BB=BA=E7=AB=8B=E5=A4=A7=E9=87=8F=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dial.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/dial.go b/dial.go index ebc6e1d..562cf39 100644 --- a/dial.go +++ b/dial.go @@ -3,8 +3,10 @@ package tdx import ( "context" "github.com/injoyai/ios" + "math/rand" "net" "strings" + "time" ) func NewHostDial(hosts []string) ios.DialFunc { @@ -26,3 +28,18 @@ func NewHostDial(hosts []string) ios.DialFunc { return c, addr, err } } + +func NewRandomDial(hosts []string) ios.DialFunc { + if len(hosts) == 0 { + hosts = Hosts + } + r := rand.New(rand.NewSource(time.Now().UnixNano())) + return func(ctx context.Context) (ios.ReadWriteCloser, string, error) { + addr := hosts[r.Intn(len(hosts))] + if !strings.Contains(addr, ":") { + addr += ":7709" + } + c, err := net.Dial("tcp", addr) + return c, addr, err + } +}