-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
142 additions
and
123 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package checks | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type Tls struct { | ||
client *http.Client | ||
} | ||
|
||
func NewTls(client *http.Client) *Tls { | ||
return &Tls{client: client} | ||
} | ||
|
||
func (t *Tls) initiateScan(ctx context.Context, domain string) (int, error) { | ||
const scanUrl = "https://tls-observatory.services.mozilla.com/api/v1/scan" | ||
|
||
formData := url.Values{"target": {domain}} | ||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, scanUrl, strings.NewReader(formData.Encode())) | ||
if err != nil { | ||
return -1, err | ||
} | ||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
|
||
resp, err := t.client.Do(req) | ||
if err != nil { | ||
return -1, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
var res struct { | ||
ScanID int `json:"scan_id"` | ||
} | ||
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil { | ||
return -1, err | ||
} | ||
|
||
if res.ScanID == 0 { | ||
return res.ScanID, errors.New("failed to get scan_id from TLS Observatory") | ||
} | ||
|
||
return res.ScanID, nil | ||
} | ||
|
||
func (t *Tls) GetScanResults(ctx context.Context, domain string) (map[string]interface{}, error) { | ||
scanID, err := t.initiateScan(ctx, domain) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
const scanUrl = "https://tls-observatory.services.mozilla.com/api/v1/results" | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, scanUrl, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
q := req.URL.Query() | ||
q.Add("id", strconv.Itoa(scanID)) | ||
req.URL.RawQuery = q.Encode() | ||
|
||
resp, err := t.client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
var result map[string]interface{} | ||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { | ||
return nil, err | ||
} | ||
|
||
return result, nil | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package checks | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/xray-web/web-check-api/testutils" | ||
) | ||
|
||
func TestTLS(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("Valid URL with successful scan", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
client := testutils.MockClient( | ||
testutils.Response(http.StatusOK, []byte(`{"scan_id": 12345}`)), | ||
testutils.Response(http.StatusOK, []byte(`{"grade": "A+"}`)), | ||
) | ||
|
||
tls, err := NewTls(client).GetScanResults(context.TODO(), "example.com") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "A+", tls["grade"]) | ||
}) | ||
} |
This file contains 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
This file contains 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
This file contains 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