-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f239de
commit ff30e37
Showing
5 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,6 @@ | |
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# Pycharms | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Fetchall fetches URLs in parallel and reports their times and sizes. | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/common-nighthawk/go-figure" | ||
"io" | ||
"net/http" | ||
"os" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func main() { | ||
start := time.Now() | ||
ch := make(chan string) | ||
|
||
if len(os.Args[1:]) >= 1 { | ||
for _, url := range os.Args[1:] { | ||
go fetch(url, ch) // start a goroutine | ||
} | ||
for range os.Args[1:] { | ||
fmt.Println(<-ch) // receive from channel ch | ||
} | ||
argCount := strconv.Itoa(len(os.Args[1:])) | ||
fmt.Printf("%.2fs elapsed %s %s", time.Since(start).Seconds(), argCount, "lookup(s)") | ||
} else { | ||
banner := figure.NewColorFigure("curlRED", "graffiti", "red", true) | ||
go fetch("", ch) // start a goroutine | ||
banner.Print() | ||
fmt.Println(<-ch) // receive from channel ch | ||
fmt.Println("\n\nUsage: curlRed.exe / curlRed\nExample (ipv4): curlRed.exe 1.1.1.1\nExample (ipv6): curlRed.exe 2a03:2880:2130:cf05:face:b00c::1.Faceb00c\nExample (domain): curlRed.exe google.com\nExample (batch mode): curlRed.exe 1.1.1.1 2a03:2880:2130:cf05:face:b00c::1.Faceb00c google.com") | ||
} | ||
fmt.Println() | ||
|
||
} | ||
|
||
func fetch(lookup string, ch chan<- string) { | ||
start := time.Now() | ||
resp, err := http.Get("https://curl.red/" + lookup) | ||
if err != nil { | ||
ch <- fmt.Sprint(err) // send to channel ch | ||
return | ||
} | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
|
||
resp.Body.Close() // don't leak resources | ||
if err != nil { | ||
ch <- fmt.Sprintf("while reading %s: %v", lookup, err) | ||
return | ||
} | ||
secs := time.Since(start).Seconds() | ||
ch <- fmt.Sprintf("%.2fs %s %s %s %s", secs, "|", lookup, "|", body) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module curlRed | ||
|
||
go 1.19 | ||
|
||
require github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be // indirect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be h1:J5BL2kskAlV9ckgEsNQXscjIaLiOYiZ75d4e94E6dcQ= | ||
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be/go.mod h1:mk5IQ+Y0ZeO87b858TlA645sVcEcbiX6YqP98kt+7+w= |