func retrieveCurrentScan() { var client http.Client url := "" if len(config.ShelterConfig.WebClient.Listeners) > 0 { url = fmt.Sprintf("http://%s:%d", config.ShelterConfig.WebClient.Listeners[0].IP, config.ShelterConfig.WebClient.Listeners[0].Port) } if len(url) == 0 { utils.Fatalln("There's no interface to connect to", nil) } r, err := http.NewRequest("GET", fmt.Sprintf("%s%s", url, "/scans/?current"), nil) if err != nil { utils.Fatalln("Error creating the HTTP request", err) } utils.BuildHTTPHeader(r, nil) response, err := client.Do(r) if err != nil { utils.Fatalln("Error sending request", err) } responseContent, err := ioutil.ReadAll(response.Body) if err != nil { utils.Fatalln("Error reading response content", err) } if response.StatusCode != http.StatusOK { utils.Fatalln(fmt.Sprintf("Expected HTTP status %d and got %d for method GET "+ "and URI /scan/currentscan", http.StatusOK, response.StatusCode), errors.New(string(responseContent)), ) } var scansResponse protocol.ScansResponse if err := json.Unmarshal(responseContent, &scansResponse); err != nil { utils.Fatalln("Error decoding scan response", err) } if len(scansResponse.Scans) != 1 { utils.Fatalln("Not returning the current scan in the result set or "+ "more items than expected were found", nil) } if scansResponse.Scans[0].Status != model.ScanStatusToString(model.ScanStatusWaitingExecution) { utils.Fatalln(fmt.Sprintf("Invalid status returned by current scan. Expected %s and got %s", model.ScanStatusToString(model.ScanStatusWaitingExecution), scansResponse.Scans[0].Status), nil) } }
// Convert a scan object data of the system into a format easy to interpret by the user func ScanToScanResponse(scan model.Scan) ScanResponse { return ScanResponse{ Status: model.ScanStatusToString(scan.Status), StartedAt: PreciseTime{scan.StartedAt}, FinishedAt: PreciseTime{scan.FinishedAt}, DomainsToBeScanned: 0, DomainsScanned: scan.DomainsScanned, DomainsWithDNSSECScanned: scan.DomainsWithDNSSECScanned, NameserverStatistics: scan.NameserverStatistics, DSStatistics: scan.DSStatistics, Links: []Link{ { Types: []LinkType{LinkTypeSelf}, HRef: fmt.Sprintf("/scan/%s", scan.StartedAt.Format(time.RFC3339Nano)), }, }, } }
// Convert a current scan object data being executed of the system into a format easy to interpret // by the user func CurrentScanToScanResponse(currentScan model.CurrentScan) ScanResponse { return ScanResponse{ Status: model.ScanStatusToString(currentScan.Status), ScheduledAt: PreciseTime{currentScan.ScheduledAt}, StartedAt: PreciseTime{currentScan.StartedAt}, FinishedAt: PreciseTime{currentScan.FinishedAt}, DomainsToBeScanned: currentScan.DomainsToBeScanned, DomainsScanned: currentScan.DomainsScanned, DomainsWithDNSSECScanned: currentScan.DomainsWithDNSSECScanned, NameserverStatistics: currentScan.NameserverStatistics, DSStatistics: currentScan.DSStatistics, Links: []Link{ { Types: []LinkType{LinkTypeSelf}, HRef: "/scan/current", }, { Types: []LinkType{LinkTypeArchives}, HRef: fmt.Sprintf("/scans"), }, }, } }
func retrieveScan(startedAt time.Time) { var client http.Client url := "" if len(config.ShelterConfig.WebClient.Listeners) > 0 { url = fmt.Sprintf("http://%s:%d", config.ShelterConfig.WebClient.Listeners[0].IP, config.ShelterConfig.WebClient.Listeners[0].Port) } if len(url) == 0 { utils.Fatalln("There's no interface to connect to", nil) } uri := fmt.Sprintf("/scan/%s", startedAt.Format(time.RFC3339Nano)) r, err := http.NewRequest("GET", fmt.Sprintf("%s%s", url, uri), nil) if err != nil { utils.Fatalln("Error creating the HTTP request", err) } utils.BuildHTTPHeader(r, nil) response, err := client.Do(r) if err != nil { utils.Fatalln("Error sending request", err) } responseContent, err := ioutil.ReadAll(response.Body) if err != nil { utils.Fatalln("Error reading response content", err) } if response.StatusCode != http.StatusOK { utils.Fatalln(fmt.Sprintf("Expected HTTP status %d and got %d for method GET "+ "and URI /scan/currentscan", http.StatusOK, response.StatusCode), errors.New(string(responseContent)), ) } var scanResponse protocol.ScanResponse if err := json.Unmarshal(responseContent, &scanResponse); err != nil { utils.Fatalln("Error decoding scan response", err) } if scanResponse.Status != model.ScanStatusToString(model.ScanStatusExecuted) { utils.Fatalln("Invalid status returned by scan", nil) } // Tolerance of one second if scanResponse.StartedAt.Sub(startedAt) > (1*time.Second) || scanResponse.StartedAt.Sub(startedAt) < (-1*time.Second) { utils.Fatalln("Invalid start date returned by scan", nil) } if scanResponse.DomainsScanned != 5000000 { utils.Fatalln("Invalid number of domains scanned returned by scan", nil) } if scanResponse.DomainsWithDNSSECScanned != 250000 { utils.Fatalln("Invalid number of domains with DNSSEC scanned returned by scan", nil) } if len(scanResponse.NameserverStatistics) != 2 { utils.Fatalln("Invalid nameserver statistics returned by scan", nil) } if len(scanResponse.DSStatistics) != 2 { utils.Fatalln("Invalid dsset statistics returned by scan", nil) } }