11package v2
22
33import (
4+ "context"
45 "crypto/tls"
56 "encoding/json"
67 "fmt"
@@ -10,6 +11,7 @@ import (
1011 "golang.org/x/time/rate"
1112 "io"
1213 "io/ioutil"
14+ "net"
1315 "net/http"
1416 "net/url"
1517 "strings"
@@ -31,7 +33,10 @@ type urlValidationResponse struct {
3133 Message string `json:"message,omitempty"`
3234 IsHTTPS bool `json:"isHttps"`
3335 HTTPSForward bool `json:"httpsForward"`
36+ HTTPStatus int `json:"httpStatus"`
3437 Reachable bool `json:"reachable"`
38+ ReachableIPv4 bool `json:"reachableIPv4"`
39+ ReachableIPv6 bool `json:"reachableIPv6"`
3540 Cors bool `json:"cors"`
3641 ContentType bool `json:"contentType"`
3742 CertValid bool `json:"certValid"`
@@ -183,10 +188,33 @@ func checkHeader(response *urlValidationResponse, header http.Header) {
183188}
184189
185190func fetchURL (validationResponse * urlValidationResponse , url * url.URL , skipVerify bool ) (http.Header , string , error ) {
191+ dialer := net.Dialer {}
192+ trv6 := & http.Transport {
193+ TLSClientConfig : & tls.Config {InsecureSkipVerify : skipVerify },
194+ DialContext : func (ctx context.Context , network , addr string ) (net.Conn , error ) {
195+ return dialer .DialContext (ctx , "tcp6" , addr )
196+ },
197+ }
198+ trv4 := & http.Transport {
199+ TLSClientConfig : & tls.Config {InsecureSkipVerify : skipVerify },
200+ DialContext : func (ctx context.Context , network , addr string ) (net.Conn , error ) {
201+ return dialer .DialContext (ctx , "tcp4" , addr )
202+ },
203+ }
186204 tr := & http.Transport {
187205 TLSClientConfig : & tls.Config {InsecureSkipVerify : skipVerify },
188206 }
189207
208+ clientv6 := http.Client {
209+ Timeout : time .Second * 10 ,
210+ Transport : trv6 ,
211+ }
212+ defer clientv6 .CloseIdleConnections ()
213+ clientv4 := http.Client {
214+ Timeout : time .Second * 10 ,
215+ Transport : trv4 ,
216+ }
217+ defer clientv4 .CloseIdleConnections ()
190218 client := http.Client {
191219 Timeout : time .Second * 10 ,
192220 CheckRedirect : func (req * http.Request , via []* http.Request ) error {
@@ -206,6 +234,25 @@ func fetchURL(validationResponse *urlValidationResponse, url *url.URL, skipVerif
206234 }
207235
208236 req .Header .Add ("Origin" , "https://validator.spaceapi.io" )
237+
238+ responsev6 , err := clientv6 .Do (req )
239+ if err == nil {
240+ validationResponse .ReachableIPv6 = true
241+ err = responsev6 .Body .Close ()
242+ if err != nil {
243+ panic (err )
244+ }
245+ }
246+
247+ responsev4 , err := clientv4 .Do (req )
248+ if err == nil {
249+ validationResponse .ReachableIPv4 = true
250+ err = responsev4 .Body .Close ()
251+ if err != nil {
252+ panic (err )
253+ }
254+ }
255+
209256 response , err := client .Do (req )
210257 if err != nil {
211258 if skipVerify == false {
@@ -223,6 +270,7 @@ func fetchURL(validationResponse *urlValidationResponse, url *url.URL, skipVerif
223270 }
224271 }()
225272
273+ validationResponse .HTTPStatus = response .StatusCode
226274 if response .StatusCode >= 400 {
227275 validationResponse .Reachable = false
228276 _ , _ = io .Copy (ioutil .Discard , response .Body )
0 commit comments