-
-
Save jehiah/42e2b2208cbfc6a0c13e6e4238dcf2ec to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package main | |
import ( | |
"bufio" | |
"errors" | |
"flag" | |
"fmt" | |
"net/http" | |
"os" | |
"sync" | |
"time" | |
) | |
var parallel = flag.Int("parallel", 5, "parallel requests") | |
var timeout = flag.Duration("timeout", 2*time.Second, "request timeout") | |
func noRedirect(req *http.Request, via []*http.Request) error { | |
return errors.New("stop redirects") | |
} | |
func main() { | |
flag.Parse() | |
client := http.Client{ | |
CheckRedirect: noRedirect, | |
Timeout: *timeout, | |
} | |
names := make(chan string) | |
var wg sync.WaitGroup | |
for i := 0; i < *parallel; i++ { | |
wg.Add(1) | |
go func() { | |
for name := range names { | |
resp, err := client.Get("https://" + name + "/") | |
if resp != nil { | |
fmt.Printf("%s: ok status code %d\n", name, resp.StatusCode) | |
// we got a redirect response | |
continue | |
} | |
if err != nil { | |
fmt.Printf("%s: error %s\n", name, err) | |
} else { | |
fmt.Printf("%s: ok status code %d\n", name, resp.StatusCode) | |
} | |
} | |
wg.Done() | |
}() | |
} | |
reader := bufio.NewScanner(os.Stdin) | |
for reader.Scan() { | |
name := reader.Text() | |
if name != "" { | |
names <- name | |
} | |
} | |
close(names) | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment