Skip to content

Commit e9b4d16

Browse files
committed
feat: add validator response fields for ipv6, ipv4 and http status code
1 parent d31e8c0 commit e9b4d16

3 files changed

Lines changed: 64 additions & 1 deletion

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ Response:
3939
"message": "",
4040
"isHttps": true,
4141
"httpsForward": false,
42+
"httpStatus": 200,
4243
"reachable": true,
44+
"reachableIPv6": true,
45+
"reachableIPv4": true,
4346
"cors": true,
4447
"contentType": true,
4548
"certValid": true,

openapi.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var openapi = `{
44
"openapi": "3.0.2",
55
"info": {
66
"description": "This is the SpaceApi Validator api",
7-
"version": "1.2.0",
7+
"version": "1.3.0",
88
"title": "SpaceApi Validator"
99
},
1010
"servers": [
@@ -235,9 +235,18 @@ var openapi = `{
235235
"httpsForward": {
236236
"type": "boolean"
237237
},
238+
"httpStatus": {
239+
"type": "integer"
240+
},
238241
"reachable": {
239242
"type": "boolean"
240243
},
244+
"reachableIPv6": {
245+
"type": "boolean"
246+
},
247+
"reachableIPv4": {
248+
"type": "boolean"
249+
},
241250
"cors": {
242251
"type": "boolean"
243252
},
@@ -267,7 +276,10 @@ var openapi = `{
267276
"valid",
268277
"isHttps",
269278
"httpsForward",
279+
"httpStatus",
270280
"reachable",
281+
"reachableIPv6",
282+
"reachableIPv4",
271283
"cors",
272284
"contentType",
273285
"certValid"

v2/validator.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package v2
22

33
import (
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

185190
func 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

Comments
 (0)