mirror of
https://github.com/injoyai/tdx.git
synced 2025-11-26 21:25:35 +08:00
增加tdx.FastHosts,用于测试排序更快的服务器地址
This commit is contained in:
14
example/FastHosts/main.go
Normal file
14
example/FastHosts/main.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ls := tdx.FastHosts(tdx.Hosts...)
|
||||
for _, v := range ls {
|
||||
logs.Debug(v)
|
||||
}
|
||||
logs.Debug("总数量:", len(ls))
|
||||
}
|
||||
35
hosts.go
35
hosts.go
@@ -1,5 +1,12 @@
|
||||
package tdx
|
||||
|
||||
import (
|
||||
"github.com/injoyai/logs"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
// Hosts 所有服务器地址(2024-11-30测试通过)
|
||||
@@ -67,3 +74,31 @@ var (
|
||||
"119.97.185.59", //电信
|
||||
}
|
||||
)
|
||||
|
||||
// FastHosts 通过tcp(ping不可用)的方式筛选可用的地址,并排序(有点误差)
|
||||
func FastHosts(hosts ...string) []string {
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(len(hosts))
|
||||
mu := sync.Mutex{}
|
||||
ls := []string(nil)
|
||||
for _, host := range hosts {
|
||||
go func(host string) {
|
||||
defer wg.Done()
|
||||
addr := host
|
||||
if !strings.Contains(addr, ":") {
|
||||
addr += ":7709"
|
||||
}
|
||||
c, err := net.Dial("tcp", addr)
|
||||
if err != nil {
|
||||
logs.Err(err)
|
||||
return
|
||||
}
|
||||
defer c.Close()
|
||||
mu.Lock()
|
||||
ls = append(ls, host)
|
||||
mu.Unlock()
|
||||
}(host)
|
||||
}
|
||||
wg.Wait()
|
||||
return ls
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user