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

Adds response status code filter #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ Sometimes you don't care about checking HTTP if HTTPS is working. You can do tha
▶ cat domains.txt | httprobe --prefer-https
```

## Filter Status Code

Show only results with given response status code. You can do that with the `--status-codes` flag:

```
▶ cat domains.txt | httprobe --status-codes 200,301
```

## Docker

Build the docker container:
Expand Down
13 changes: 9 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"os"
"strings"
"strconv"
"sync"
"time"
)
Expand Down Expand Up @@ -52,6 +53,10 @@ func main() {
var method string
flag.StringVar(&method, "method", "GET", "HTTP method to use")

// Filter status code
var statusCodes string
flag.StringVar(&statusCodes, "status-codes", "", "Status codes to display.")

flag.Parse()

// make an actual time.Duration out of the timeout
Expand Down Expand Up @@ -96,7 +101,7 @@ func main() {

// always try HTTPS first
withProto := "https://" + url
if isListening(client, withProto, method) {
if isListening(client, withProto, method, statusCodes) {
output <- withProto

// skip trying HTTP if --prefer-https is set
Expand All @@ -120,7 +125,7 @@ func main() {
go func() {
for url := range httpURLs {
withProto := "http://" + url
if isListening(client, withProto, method) {
if isListening(client, withProto, method, statusCodes) {
output <- withProto
continue
}
Expand Down Expand Up @@ -210,7 +215,7 @@ func main() {
outputWG.Wait()
}

func isListening(client *http.Client, url, method string) bool {
func isListening(client *http.Client, url, method string, statusCodes string) bool {

req, err := http.NewRequest(method, url, nil)
if err != nil {
Expand All @@ -226,7 +231,7 @@ func isListening(client *http.Client, url, method string) bool {
resp.Body.Close()
}

if err != nil {
if err != nil || !strings.Contains(statusCodes, strconv.Itoa(resp.StatusCode)) && statusCodes != "" {
return false
}

Expand Down