ipv4 and ipv6 network test
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var configPath string // Variable to hold the config path
|
var configPath string // Variable to hold the config path
|
||||||
|
var netTestType string // Variable to hold the network test type
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "zurg",
|
Use: "zurg",
|
||||||
@@ -33,9 +34,11 @@ func main() {
|
|||||||
Use: "network-test",
|
Use: "network-test",
|
||||||
Short: "Run a network test",
|
Short: "Run a network test",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
internal.NetworkTest()
|
internal.NetworkTest(netTestType) // Pass the network test type to NetworkTest
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
// Adding a flag for network test type with default value "both"
|
||||||
|
networkTestCmd.Flags().StringVarP(&netTestType, "type", "t", "both", "network test type (ipv4, ipv6, both)")
|
||||||
|
|
||||||
var clearDownloadsCmd = &cobra.Command{
|
var clearDownloadsCmd = &cobra.Command{
|
||||||
Use: "clear-downloads",
|
Use: "clear-downloads",
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ func ShowVersion() {
|
|||||||
version.GetBuiltAt(), version.GetGitCommit(), version.GetVersion())
|
version.GetBuiltAt(), version.GetGitCommit(), version.GetVersion())
|
||||||
}
|
}
|
||||||
|
|
||||||
func NetworkTest() {
|
func NetworkTest(netTestType string) {
|
||||||
realdebrid.RunTest()
|
realdebrid.RunTest(netTestType)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ClearDownloads() {
|
func ClearDownloads() {
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package realdebrid
|
package realdebrid
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -17,21 +19,20 @@ type IPInfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func traceroute(ip string) (int, time.Duration, error) {
|
func traceroute(ip string) (int, time.Duration, error) {
|
||||||
|
// Try executing traceroute
|
||||||
cmd := exec.Command("traceroute", "-n", "-q", "1", "-w", "1", ip)
|
cmd := exec.Command("traceroute", "-n", "-q", "1", "-w", "1", ip)
|
||||||
out, err := cmd.CombinedOutput()
|
out, err := cmd.CombinedOutput()
|
||||||
if err != nil {
|
|
||||||
return 0, 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
// Traceroute executed successfully
|
||||||
output := string(out)
|
output := string(out)
|
||||||
lines := strings.Split(output, "\n")
|
lines := strings.Split(output, "\n")
|
||||||
|
|
||||||
hopCount := len(lines) - 1
|
hopCount := len(lines) - 1
|
||||||
|
|
||||||
var latency time.Duration
|
var latency time.Duration
|
||||||
|
|
||||||
if hopCount > 0 {
|
if hopCount > 0 {
|
||||||
lastLine := lines[hopCount-1]
|
lastLine := lines[hopCount-1]
|
||||||
fmt.Printf("IP: %s, last hop: %s\n", ip, lastLine)
|
fmt.Print(".")
|
||||||
parts := strings.Fields(lastLine)
|
parts := strings.Fields(lastLine)
|
||||||
if len(parts) >= 3 {
|
if len(parts) >= 3 {
|
||||||
latencyValue, parseErr := strconv.ParseFloat(parts[2], 64)
|
latencyValue, parseErr := strconv.ParseFloat(parts[2], 64)
|
||||||
@@ -40,14 +41,83 @@ func traceroute(ip string) (int, time.Duration, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return hopCount, latency, nil
|
return hopCount, latency, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunTest() {
|
// Traceroute not successful, measure latency using ping
|
||||||
fmt.Println("Running network test...")
|
pingCmd := exec.Command("ping", "-c", "1", ip)
|
||||||
|
pingOut, pingErr := pingCmd.CombinedOutput()
|
||||||
|
|
||||||
ips := []string{"20.download.real-debrid.cloud", "20.download.real-debrid.com", "21.download.real-debrid.cloud", "21.download.real-debrid.com", "22.download.real-debrid.cloud", "22.download.real-debrid.com", "23.download.real-debrid.cloud", "23.download.real-debrid.com", "30.download.real-debrid.cloud", "30.download.real-debrid.com", "31.download.real-debrid.cloud", "31.download.real-debrid.com", "32.download.real-debrid.cloud", "32.download.real-debrid.com", "34.download.real-debrid.cloud", "34.download.real-debrid.com", "40.download.real-debrid.cloud", "40.download.real-debrid.com", "41.download.real-debrid.cloud", "41.download.real-debrid.com", "42.download.real-debrid.cloud", "42.download.real-debrid.com", "43.download.real-debrid.cloud", "43.download.real-debrid.com", "44.download.real-debrid.cloud", "44.download.real-debrid.com", "45.download.real-debrid.cloud", "45.download.real-debrid.com", "50.download.real-debrid.cloud", "50.download.real-debrid.com", "51.download.real-debrid.cloud", "51.download.real-debrid.com", "52.download.real-debrid.cloud", "52.download.real-debrid.com", "53.download.real-debrid.cloud", "53.download.real-debrid.com", "54.download.real-debrid.cloud", "54.download.real-debrid.com", "55.download.real-debrid.cloud", "55.download.real-debrid.com", "56.download.real-debrid.cloud", "56.download.real-debrid.com", "57.download.real-debrid.cloud", "57.download.real-debrid.com", "58.download.real-debrid.cloud", "58.download.real-debrid.com", "59.download.real-debrid.cloud", "59.download.real-debrid.com", "60.download.real-debrid.cloud", "60.download.real-debrid.com", "61.download.real-debrid.cloud", "61.download.real-debrid.com", "62.download.real-debrid.cloud", "62.download.real-debrid.com", "63.download.real-debrid.cloud", "63.download.real-debrid.com", "64.download.real-debrid.cloud", "64.download.real-debrid.com", "65.download.real-debrid.cloud", "65.download.real-debrid.com", "66.download.real-debrid.cloud", "66.download.real-debrid.com", "67.download.real-debrid.cloud", "67.download.real-debrid.com", "68.download.real-debrid.cloud", "68.download.real-debrid.com", "69.download.real-debrid.cloud", "69.download.real-debrid.com", "70.download.real-debrid.cloud", "70.download.real-debrid.com", "71.download.real-debrid.cloud", "71.download.real-debrid.com", "72.download.real-debrid.cloud", "72.download.real-debrid.com", "73.download.real-debrid.cloud", "73.download.real-debrid.com", "74.download.real-debrid.cloud", "74.download.real-debrid.com", "75.download.real-debrid.cloud", "75.download.real-debrid.com", "76.download.real-debrid.cloud", "76.download.real-debrid.com", "77.download.real-debrid.cloud", "77.download.real-debrid.com", "78.download.real-debrid.cloud", "78.download.real-debrid.com", "79.download.real-debrid.cloud", "79.download.real-debrid.com", "80.download.real-debrid.com", "ams1.download.real-debrid.com", "hkg1.download.real-debrid.com", "jkt1.download.real-debrid.com", "lax1.download.real-debrid.com", "lax2.download.real-debrid.com", "lax3.download.real-debrid.com", "lax4.download.real-debrid.com", "lax5.download.real-debrid.com", "lon1.download.real-debrid.com", "mum1.download.real-debrid.com", "sgp1.download.real-debrid.com", "tlv1.download.real-debrid.com", "tyo1.download.real-debrid.com"}
|
if pingErr != nil {
|
||||||
|
return 0, 0, pingErr
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Print(".")
|
||||||
|
pingOutput := string(pingOut)
|
||||||
|
pingLines := strings.Split(pingOutput, "\n")
|
||||||
|
for _, line := range pingLines {
|
||||||
|
if strings.Contains(line, "time=") {
|
||||||
|
parts := strings.Split(line, " ")
|
||||||
|
for _, part := range parts {
|
||||||
|
if strings.Contains(part, "time=") {
|
||||||
|
timeStr := strings.Split(part, "=")[1]
|
||||||
|
timeStr = strings.TrimSuffix(timeStr, " ms")
|
||||||
|
latencyValue, parseErr := strconv.ParseFloat(timeStr, 64)
|
||||||
|
if parseErr == nil {
|
||||||
|
latency := time.Duration(latencyValue * float64(time.Millisecond))
|
||||||
|
return -1, latency, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, 0, fmt.Errorf("failed to measure latency")
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchIPs(url string) ([]string, error) {
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
var ips []string
|
||||||
|
scanner := bufio.NewScanner(resp.Body)
|
||||||
|
for scanner.Scan() {
|
||||||
|
ips = append(ips, scanner.Text())
|
||||||
|
}
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return ips, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func RunTest(netTestType string) {
|
||||||
|
fmt.Print("Running network test...")
|
||||||
|
|
||||||
|
ipv4URL := "https://gist.githubusercontent.com/yowmamasita/d0c1c7353500d0928cb5242484e8ed06/raw/ipv4.txt"
|
||||||
|
ipv6URL := "https://gist.githubusercontent.com/yowmamasita/d0c1c7353500d0928cb5242484e8ed06/raw/ipv6.txt"
|
||||||
|
|
||||||
|
var ips []string
|
||||||
|
|
||||||
|
if netTestType == "ipv4" || netTestType == "both" {
|
||||||
|
ipv4IPs, err := fetchIPs(ipv4URL)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error fetching IPv4 IPs:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ips = append(ips, ipv4IPs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
if netTestType == "ipv6" || netTestType == "both" {
|
||||||
|
ipv6IPs, err := fetchIPs(ipv6URL)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error fetching IPv6 IPs:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ips = append(ips, ipv6IPs...)
|
||||||
|
}
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
infoChan := make(chan IPInfo, len(ips))
|
infoChan := make(chan IPInfo, len(ips))
|
||||||
@@ -60,7 +130,7 @@ func RunTest() {
|
|||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
hops, latency, err := traceroute(ip)
|
hops, latency, err := traceroute(ip)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error performing traceroute:", err)
|
fmt.Printf("Error performing traceroute for %s:%s\n", ip, err)
|
||||||
} else {
|
} else {
|
||||||
infoChan <- IPInfo{Address: ip, Hops: hops, Latency: latency}
|
infoChan <- IPInfo{Address: ip, Hops: hops, Latency: latency}
|
||||||
}
|
}
|
||||||
@@ -71,7 +141,7 @@ func RunTest() {
|
|||||||
wg.Wait()
|
wg.Wait()
|
||||||
close(semaphore)
|
close(semaphore)
|
||||||
close(infoChan)
|
close(infoChan)
|
||||||
fmt.Printf("Network test complete.\n\n")
|
fmt.Printf("complete!\n\n")
|
||||||
|
|
||||||
var ipInfos []IPInfo
|
var ipInfos []IPInfo
|
||||||
for info := range infoChan {
|
for info := range infoChan {
|
||||||
|
|||||||
Reference in New Issue
Block a user