Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change output logs format to table formatting #21

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added 403unlocker
Binary file not shown.
17 changes: 13 additions & 4 deletions internal/check/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ func CheckWithDNS(c *cli.Context) error {
url := c.Args().First()
url = ensureHTTPS(url)

fmt.Printf("check: %s\n", url)
// Print header
fmt.Println("\n+--------------------+------------+")
fmt.Printf("| %-18s | %-10s |\n", "DNS Server", "Status")
fmt.Println("+--------------------+------------+")

dnsList, err := ReadDNSFromFile("config/dns.conf")
if err != nil {
fmt.Println(err)
Expand All @@ -64,15 +68,20 @@ func CheckWithDNS(c *cli.Context) error {
fmt.Println("Error converting status code:", err)
return
}
if statusCodeInt == http.StatusForbidden {
fmt.Printf("DNS: %s %s%s%s\n", dns, common.Red, code[1], common.Reset)

// Format table row with colored status
if statusCodeInt != http.StatusOK {
fmt.Printf("| %-18s | %s%-10s%s |\n", dns, common.Red, code[1], common.Reset)
} else {
fmt.Printf("DNS: %s %s%s%s\n", dns, common.Green, code[1], common.Reset)
fmt.Printf("| %-18s | %s%-10s%s |\n", dns, common.Green, code[1], common.Reset)
}

}(dns)
}
wg.Wait()

// Print footer
fmt.Println("+--------------------+------------+")
return nil
}

Expand Down
41 changes: 29 additions & 12 deletions internal/dns/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,46 @@ func CheckWithURL(c *cli.Context) error {
fmt.Println("Error reading DNS list:", err)
return err
}
// Map to store the total size downloaded by each DNS

dnsSizeMap := make(map[string]int64)
fmt.Println("Timeout:", timeout)
fmt.Println("URL: ", fileToDownload)
fmt.Printf("\nTimeout: %d seconds\n", timeout)
fmt.Printf("URL: %s\n\n", fileToDownload)

// Print table header
fmt.Println("+--------------------+----------------+")
fmt.Printf("| %-18s | %-14s |\n", "DNS Server", "Download Speed")
fmt.Println("+--------------------+----------------+")

tempDir := time.Now().UnixMilli()
for _, dns := range dnsList {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()
// Create a custom HTTP client with the specified DNS

clientWithCustomDNS := check.ChangeDNS(dns)
client := grab.NewClient()
client.HTTPClient = clientWithCustomDNS
// Create a new download request

req, err := grab.NewRequest(fmt.Sprintf("/tmp/%v", tempDir), fileToDownload)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating request for DNS %s: %v\n", dns, err)
}
req = req.WithContext(ctx)
// Start the download

resp := client.Do(req)
// Update the total size downloaded by this DNS
dnsSizeMap[dns] += resp.BytesComplete() // Use BytesComplete() for partial downloads
dnsSizeMap[dns] = resp.BytesComplete()

speed := common.FormatDataSize(resp.BytesComplete() / int64(timeout))
if resp.BytesComplete() == 0 {
fmt.Printf("DNS: %s\t%s%v/s%s\n", dns, common.Red, common.FormatDataSize(resp.BytesComplete()/int64(timeout)), common.Reset)
fmt.Printf("| %-18s | %s%-14s%s |\n", dns, common.Red, speed+"/s", common.Reset)
} else {
fmt.Printf("DNS: %s\t%s/s\n", dns, common.FormatDataSize(resp.BytesComplete()/int64(timeout)))
fmt.Printf("| %-18s | %-14s |\n", dns, speed+"/s")
}
}
// Determine which DNS downloaded the most data

// Print table footer
fmt.Println("+--------------------+----------------+")

// Find and display the best DNS
var maxDNS string
var maxSize int64
for dns, size := range dnsSizeMap {
Expand All @@ -74,11 +85,17 @@ func CheckWithURL(c *cli.Context) error {
maxSize = size
}
}

fmt.Println() // Add a blank line for separation
if maxDNS != "" {
fmt.Printf("best DNS is %s%s%s and downloaded the most data: %s%v/s%s\n", common.Green, maxDNS, common.Reset, common.Green, common.FormatDataSize(maxSize/int64(timeout)), common.Reset)
bestSpeed := common.FormatDataSize(maxSize / int64(timeout))
fmt.Printf("Best DNS: %s%s%s (%s%s/s%s)\n",
common.Green, maxDNS, common.Reset,
common.Green, bestSpeed, common.Reset)
} else {
fmt.Println("No DNS server was able to download any data.")
}

os.RemoveAll(fmt.Sprintf("/tmp/%v", tempDir))
return nil
}
49 changes: 40 additions & 9 deletions internal/docker/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ func CheckWithDockerImage(c *cli.Context) error {
timeout := c.Int("timeout")
imageName := c.Args().First()
tempDir := time.Now().UnixMilli()
fmt.Println("Timeout:", timeout)
fmt.Println("Docker Image: ", imageName)

fmt.Printf("\nTimeout: %d seconds\n", timeout)
fmt.Printf("Docker Image: %s\n\n", imageName)

if imageName == "" {
return fmt.Errorf("image name cannot be empty")
Expand All @@ -117,31 +118,61 @@ func CheckWithDockerImage(c *cli.Context) error {
return err
}

// Find the longest registry name first
maxLength := 0
for _, registry := range registryList {
if len(registry) > maxLength {
maxLength = len(registry)
}
}

// Create table formatting based on longest name
borderLine := "+" + strings.Repeat("-", maxLength+2) + "+------------------+"
format := "| %-" + fmt.Sprintf("%d", maxLength) + "s | %-16s |\n"

// Print table header
fmt.Println(borderLine)
fmt.Printf(format, "Registry", "Download Speed")
fmt.Println(borderLine)

for _, registry := range registryList {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()

size, err := DownloadDockerImage(ctx, imageName, registry, fmt.Sprintf("/tmp/%v", tempDir))
if err != nil {
fmt.Printf("%s: %s%v%s\n", registry, common.Red, "failed", common.Reset)
fmt.Printf("| %-*s | %s%-16s%s |\n",
maxLength, registry,
common.Red, "failed", common.Reset)
continue
}

registrySizeMap[registry] += size
fmt.Printf("%s downloaded : %v/s\n", registry, common.FormatDataSize(size/int64(timeout)))
speed := common.FormatDataSize(size / int64(timeout))
fmt.Printf(format, registry, speed+"/s")
}
// Determine which DNS downloaded the most data

fmt.Println(borderLine)

var maxRegistry string
var maxSize int64
for dns, size := range registrySizeMap {
for registry, size := range registrySizeMap {
if size > maxSize {
maxRegistry = dns
maxRegistry = registry
maxSize = size
}
}

fmt.Println()
if maxRegistry != "" {
fmt.Printf("best Registry is %s%s%s and downloaded the most data: %s%v/s%s\n", common.Green, maxRegistry, common.Reset, common.Green, common.FormatDataSize(maxSize/int64(timeout)), common.Reset)
bestSpeed := common.FormatDataSize(maxSize / int64(timeout))
fmt.Printf("Best Registry: %s%s%s (%s%s/s%s)\n",
common.Green, maxRegistry, common.Reset,
common.Green, bestSpeed, common.Reset)
} else {
fmt.Println("No DNS server was able to download any data.")
fmt.Println("No registry was able to download any data.")
}

os.RemoveAll(fmt.Sprintf("/tmp/%v", tempDir))
return nil
}
Loading