-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add validator response fields for ipv6, ipv4 and http status code #70
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package v2 | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
@@ -10,6 +11,7 @@ import ( | |
| "golang.org/x/time/rate" | ||
| "io" | ||
| "io/ioutil" | ||
| "net" | ||
| "net/http" | ||
| "net/url" | ||
| "strings" | ||
|
|
@@ -31,7 +33,10 @@ type urlValidationResponse struct { | |
| Message string `json:"message,omitempty"` | ||
| IsHTTPS bool `json:"isHttps"` | ||
| HTTPSForward bool `json:"httpsForward"` | ||
| HTTPStatus int `json:"httpStatus"` | ||
| Reachable bool `json:"reachable"` | ||
| ReachableIPv4 bool `json:"reachableIPv4"` | ||
| ReachableIPv6 bool `json:"reachableIPv6"` | ||
| Cors bool `json:"cors"` | ||
| ContentType bool `json:"contentType"` | ||
| CertValid bool `json:"certValid"` | ||
|
|
@@ -183,10 +188,33 @@ func checkHeader(response *urlValidationResponse, header http.Header) { | |
| } | ||
|
|
||
| func fetchURL(validationResponse *urlValidationResponse, url *url.URL, skipVerify bool) (http.Header, string, error) { | ||
| dialer := net.Dialer{} | ||
| trv6 := &http.Transport{ | ||
| TLSClientConfig: &tls.Config{InsecureSkipVerify: skipVerify}, | ||
| DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { | ||
| return dialer.DialContext(ctx, "tcp6", addr) | ||
| }, | ||
| } | ||
| trv4 := &http.Transport{ | ||
| TLSClientConfig: &tls.Config{InsecureSkipVerify: skipVerify}, | ||
| DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { | ||
| return dialer.DialContext(ctx, "tcp4", addr) | ||
| }, | ||
| } | ||
| tr := &http.Transport{ | ||
| TLSClientConfig: &tls.Config{InsecureSkipVerify: skipVerify}, | ||
| } | ||
|
|
||
| clientv6 := http.Client{ | ||
| Timeout: time.Second * 10, | ||
| Transport: trv6, | ||
| } | ||
| defer clientv6.CloseIdleConnections() | ||
| clientv4 := http.Client{ | ||
| Timeout: time.Second * 10, | ||
| Transport: trv4, | ||
| } | ||
| defer clientv4.CloseIdleConnections() | ||
| client := http.Client{ | ||
| Timeout: time.Second * 10, | ||
| CheckRedirect: func(req *http.Request, via []*http.Request) error { | ||
|
|
@@ -206,6 +234,25 @@ func fetchURL(validationResponse *urlValidationResponse, url *url.URL, skipVerif | |
| } | ||
|
|
||
| req.Header.Add("Origin", "https://validator.spaceapi.io") | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it intentional that the checks below happen after setting the Also, couldn't the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The thing that happens in L230-234 is the preparation of the request, not the actual sending. The single request will then be used 3 times below. As far as I understand it, setting
Unfortunately not. If we decided to change the semantics of |
||
| responsev6, err := clientv6.Do(req) | ||
| if err == nil { | ||
| validationResponse.ReachableIPv6 = true | ||
| err = responsev6.Body.Close() | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| } | ||
|
|
||
| responsev4, err := clientv4.Do(req) | ||
| if err == nil { | ||
| validationResponse.ReachableIPv4 = true | ||
| err = responsev4.Body.Close() | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| } | ||
|
|
||
| response, err := client.Do(req) | ||
| if err != nil { | ||
| if skipVerify == false { | ||
|
|
@@ -223,6 +270,7 @@ func fetchURL(validationResponse *urlValidationResponse, url *url.URL, skipVerif | |
| } | ||
| }() | ||
|
|
||
| validationResponse.HTTPStatus = response.StatusCode | ||
| if response.StatusCode >= 400 { | ||
| validationResponse.Reachable = false | ||
| _, _ = io.Copy(ioutil.Discard, response.Body) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.