Skip to content

Commit 02e67d6

Browse files
committed
Code refactoring
1 parent 08b4401 commit 02e67d6

File tree

2 files changed

+44
-58
lines changed

2 files changed

+44
-58
lines changed

sslscan.go

Lines changed: 40 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@ import (
1313
"time"
1414

1515
"github.com/essentialkaos/ek/v13/req"
16+
"github.com/essentialkaos/ek/v13/strutil"
1617
)
1718

1819
// ////////////////////////////////////////////////////////////////////////////////// //
1920

21+
const VERSION = "14"
22+
23+
// ////////////////////////////////////////////////////////////////////////////////// //
24+
2025
const (
2126
API_URL_INFO = "https://api.ssllabs.com/api/v4/info"
2227
API_URL_REGISTER = "https://api.ssllabs.com/api/v4/register"
@@ -549,18 +554,15 @@ func NewAPI(name, version, email string) (*API, error) {
549554
return nil, ErrEmptyEmail
550555
}
551556

552-
api := &API{
553-
Engine: &req.Engine{},
554-
email: email,
555-
}
557+
api := &API{Engine: &req.Engine{}, email: email}
556558

557559
api.Engine.Init()
558-
api.Engine.SetUserAgent(name, version, "SSLScan/14")
560+
api.Engine.SetUserAgent(name, version, "SSLScan/"+VERSION)
559561

560562
api.Engine.Client.Timeout = 10 * time.Second
561563

562564
info := &Info{}
563-
err := api.doRequest(API_URL_INFO, nil, info)
565+
err := api.doRequest(API_URL_INFO, nil, nil, info)
564566

565567
if err != nil {
566568
return nil, err
@@ -591,7 +593,7 @@ func (a *API) Register(reg *RegisterRequest) (*RegisterResponse, error) {
591593
}
592594

593595
response := &RegisterResponse{}
594-
err := a.doRequest(API_URL_REGISTER, reg, response)
596+
err := a.doRequest(API_URL_REGISTER, nil, reg, response)
595597

596598
return response, err
597599
}
@@ -603,9 +605,11 @@ func (a *API) Analyze(host string, params AnalyzeParams) (*AnalyzeProgress, erro
603605
}
604606

605607
progress := &AnalyzeProgress{host: host, api: a, maxAge: params.MaxAge}
606-
query := "host=" + host + params.ToQuery()
607608

608-
err := a.doRequest(API_URL_ANALYZE+"?"+query, nil, nil)
609+
query := params.ToQuery()
610+
query.Set("host", host)
611+
612+
err := a.doRequest(API_URL_ANALYZE, query, nil, nil)
609613

610614
if err != nil {
611615
return nil, err
@@ -620,22 +624,13 @@ func (p *AnalyzeProgress) Info(detailed, fromCache bool) (*AnalyzeInfo, error) {
620624
return nil, ErrInvalid
621625
}
622626

623-
query := "host=" + p.host
624-
625-
if detailed {
626-
query += "&all=on"
627-
}
628-
629-
if fromCache {
630-
query += "&fromCache=" + formatBoolParam(fromCache)
631-
632-
if p.maxAge > 0 {
633-
query += "&maxAge=" + fmt.Sprintf("%d", p.maxAge)
634-
}
635-
}
627+
query := req.Query{"host": p.host}
628+
query.SetIf(detailed, "all", formatBoolParam(detailed))
629+
query.SetIf(fromCache, "fromCache", formatBoolParam(fromCache))
630+
query.SetIf(fromCache && p.maxAge > 0, "maxAge", fmt.Sprintf("%d", p.maxAge))
636631

637632
info := &AnalyzeInfo{}
638-
err := p.api.doRequest(API_URL_ANALYZE+"?"+query, nil, info)
633+
err := p.api.doRequest(API_URL_ANALYZE, query, nil, info)
639634

640635
if err != nil {
641636
return nil, err
@@ -666,18 +661,16 @@ func (p *AnalyzeProgress) GetEndpointInfo(ip string, fromCache bool) (*EndpointI
666661
}
667662
}
668663

669-
query := "host=" + p.host + "&s=" + ip
670-
671-
if fromCache {
672-
query += "&fromCache=" + formatBoolParam(fromCache)
673-
674-
if p.maxAge > 0 {
675-
query += "&maxAge=" + fmt.Sprintf("%d", p.maxAge)
676-
}
664+
query := req.Query{
665+
"host": p.host,
666+
"s": ip,
677667
}
678668

669+
query.SetIf(fromCache, "fromCache", formatBoolParam(fromCache))
670+
query.SetIf(fromCache && p.maxAge > 0, "maxAge", fmt.Sprintf("%d", p.maxAge))
671+
679672
info := &EndpointInfo{}
680-
err = p.api.doRequest(API_URL_DETAILED+"?"+query, nil, info)
673+
err = p.api.doRequest(API_URL_DETAILED, query, nil, info)
681674

682675
if err != nil {
683676
return nil, err
@@ -699,35 +692,32 @@ func (e *APIErrors) ToError() error {
699692

700693
// ////////////////////////////////////////////////////////////////////////////////// //
701694

702-
// String combines params into query
703-
func (p AnalyzeParams) ToQuery() string {
704-
var result string
705-
706-
result += "publish=" + formatBoolParam(p.Public) + "&"
707-
result += "startNew=" + formatBoolParam(p.StartNew) + "&"
708-
result += "fromCache=" + formatBoolParam(p.FromCache) + "&"
709-
710-
if p.MaxAge != 0 {
711-
result += "maxAge=" + fmt.Sprintf("%d", p.MaxAge) + "&"
695+
// ToQuery converts params into request query
696+
func (p AnalyzeParams) ToQuery() req.Query {
697+
query := req.Query{
698+
"publish": formatBoolParam(p.Public),
699+
"startNew": formatBoolParam(p.StartNew),
700+
"fromCache": formatBoolParam(p.FromCache),
701+
"ignoreMismatch": formatBoolParam(p.IgnoreMismatch),
712702
}
713703

714-
result += "ignoreMismatch=" + formatBoolParam(p.IgnoreMismatch)
715-
716-
if len(result) != 0 {
717-
return "&" + result
704+
if p.MaxAge != 0 {
705+
query.Set("maxAge", fmt.Sprintf("%d", p.MaxAge))
718706
}
719707

720-
return ""
708+
return query
721709
}
722710

723711
// ////////////////////////////////////////////////////////////////////////////////// //
724712

725713
// doRequest sends request using http client
726-
func (a *API) doRequest(uri string, request, response any) error {
714+
func (a *API) doRequest(url string, query req.Query, request, response any) error {
727715
r := req.Request{
728716
Method: req.GET,
729-
URL: uri,
717+
URL: url,
718+
Query: query,
730719
Headers: req.Headers{"email": a.email},
720+
Accept: req.CONTENT_TYPE_JSON,
731721
}
732722

733723
if request != nil {
@@ -766,9 +756,5 @@ func (a *API) doRequest(uri string, request, response any) error {
766756

767757
// formatBoolParam formats boolean parameter
768758
func formatBoolParam(v bool) string {
769-
if v == false {
770-
return "off"
771-
}
772-
773-
return "on"
759+
return strutil.B(v, "on", "off")
774760
}

sslscan_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,15 @@ func (s *SSLLabsSuite) TestAnalyze(c *check.C) {
113113

114114
fmt.Printf("Progress: ∙")
115115

116-
lastSuccess = time.Now()
117-
118116
for range time.NewTicker(5 * time.Second).C {
119117
info, err = progress.Info(false, false)
120118

121-
if info != nil && err == nil {
122-
lastSuccess = time.Now()
119+
if info == nil || err != nil {
120+
continue
123121
}
124122

123+
lastSuccess = time.Now()
124+
125125
if info.Status == STATUS_ERROR {
126126
c.Fatal(info.StatusMessage)
127127
}

0 commit comments

Comments
 (0)