Refactor structure, use cobra
This commit is contained in:
@@ -31,7 +31,7 @@ func traceroute(ip string) (int, time.Duration, error) {
|
||||
var latency time.Duration
|
||||
if hopCount > 0 {
|
||||
lastLine := lines[hopCount-1]
|
||||
fmt.Println(lastLine)
|
||||
fmt.Printf("IP: %s, last hop: %s\n", ip, lastLine)
|
||||
parts := strings.Fields(lastLine)
|
||||
if len(parts) >= 3 {
|
||||
latencyValue, parseErr := strconv.ParseFloat(parts[2], 64)
|
||||
@@ -70,6 +70,7 @@ func RunTest() {
|
||||
|
||||
wg.Wait()
|
||||
close(infoChan)
|
||||
fmt.Printf("Network test complete.\n\n")
|
||||
|
||||
var ipInfos []IPInfo
|
||||
for info := range infoChan {
|
||||
@@ -77,30 +78,36 @@ func RunTest() {
|
||||
}
|
||||
|
||||
sort.Slice(ipInfos, func(i, j int) bool {
|
||||
if ipInfos[i].Hops == ipInfos[j].Hops {
|
||||
return ipInfos[i].Latency < ipInfos[j].Latency
|
||||
}
|
||||
return ipInfos[i].Hops < ipInfos[j].Hops
|
||||
return ipInfos[i].Latency < ipInfos[j].Latency
|
||||
})
|
||||
var lowestLatency time.Duration
|
||||
|
||||
const minResults = 10
|
||||
const maxResults = 20
|
||||
var okIPs []IPInfo
|
||||
|
||||
if len(ipInfos) > 0 {
|
||||
lowestLatency = ipInfos[0].Latency
|
||||
for _, info := range ipInfos {
|
||||
if info.Latency < lowestLatency {
|
||||
lowestLatency = info.Latency
|
||||
// Start by adding the best IPs based on hops and latency up to the minResults
|
||||
for i := 0; i < min(len(ipInfos), minResults); i++ {
|
||||
okIPs = append(okIPs, ipInfos[i])
|
||||
}
|
||||
|
||||
// Find the highest latency in the current okIPs list
|
||||
highestLatency := okIPs[len(okIPs)-1].Latency
|
||||
|
||||
// Add any additional IPs that have latency within a reasonable range of the highest latency
|
||||
for _, info := range ipInfos[minResults:] {
|
||||
if len(okIPs) >= maxResults {
|
||||
break // Stop adding IPs if maxResults is reached
|
||||
}
|
||||
if info.Latency <= highestLatency+(highestLatency/3) {
|
||||
okIPs = append(okIPs, info)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
latencyThreshold := lowestLatency + lowestLatency/3
|
||||
|
||||
var okIPs []IPInfo
|
||||
for _, info := range ipInfos {
|
||||
if info.Latency <= latencyThreshold {
|
||||
okIPs = append(okIPs, info)
|
||||
}
|
||||
}
|
||||
fmt.Printf("Here are the results, you can copy-paste the following to your config.yml:\n\n")
|
||||
fmt.Println("preferred_hosts:")
|
||||
for _, info := range okIPs {
|
||||
fmt.Printf("Host: %s, Hops: %d, Latency: %v\n", info.Address, info.Hops, info.Latency)
|
||||
fmt.Printf(" - %s # hops: %d latency: %v\n", info.Address, info.Hops, info.Latency)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user