Minor changes to MakeRequest function

This commit is contained in:
David J. Allen 2024-05-08 12:53:10 -06:00
parent efda3d8aa7
commit f1db827c61
No known key found for this signature in database
GPG key ID: 717C593FF60A2ACC
4 changed files with 21 additions and 12 deletions

View file

@ -43,7 +43,7 @@ func ScanForAssets() error {
func QueryScannedPorts() error { func QueryScannedPorts() error {
// Perform scan and collect from dora server // Perform scan and collect from dora server
url := makeEndpointUrl("/scanned_ports") url := makeEndpointUrl("/scanned_ports")
_, body, err := util.MakeRequest(url, "GET", nil, nil) _, body, err := util.MakeRequest(nil, url, "GET", nil, nil)
if err != nil { if err != nil {
return fmt.Errorf("could not discover assets: %v", err) return fmt.Errorf("could not discover assets: %v", err)
} }

View file

@ -116,7 +116,7 @@ func ScanForAssets(hosts []string, ports []int, threads int, timeout int, disabl
probeResults := []ScannedResult{} probeResults := []ScannedResult{}
for _, result := range scannedResults { for _, result := range scannedResults {
url := fmt.Sprintf("https://%s:%d/redfish/v1/", result.Host, result.Port) url := fmt.Sprintf("https://%s:%d/redfish/v1/", result.Host, result.Port)
res, _, err := util.MakeRequest(url, "GET", nil, nil) res, _, err := util.MakeRequest(nil, url, "GET", nil, nil)
if err != nil || res == nil { if err != nil || res == nil {
continue continue
} else if res.StatusCode != http.StatusOK { } else if res.StatusCode != http.StatusOK {

View file

@ -139,7 +139,7 @@ func UpdateFirmwareRemote(q *UpdateParams) error {
if err != nil { if err != nil {
return fmt.Errorf("could not marshal data: %v", err) return fmt.Errorf("could not marshal data: %v", err)
} }
res, body, err := util.MakeRequest(url, "POST", data, headers) res, body, err := util.MakeRequest(nil, url, "POST", data, headers)
if err != nil { if err != nil {
return fmt.Errorf("something went wrong: %v", err) return fmt.Errorf("something went wrong: %v", err)
} else if res == nil { } else if res == nil {
@ -153,7 +153,7 @@ func UpdateFirmwareRemote(q *UpdateParams) error {
func GetUpdateStatus(q *UpdateParams) error { func GetUpdateStatus(q *UpdateParams) error {
url := baseRedfishUrl(&q.QueryParams) + "/redfish/v1/UpdateService" url := baseRedfishUrl(&q.QueryParams) + "/redfish/v1/UpdateService"
res, body, err := util.MakeRequest(url, "GET", nil, nil) res, body, err := util.MakeRequest(nil, url, "GET", nil, nil)
if err != nil { if err != nil {
return fmt.Errorf("something went wrong: %v", err) return fmt.Errorf("something went wrong: %v", err)
} else if res == nil { } else if res == nil {

View file

@ -15,8 +15,12 @@ import (
func PathExists(path string) (bool, error) { func PathExists(path string) (bool, error) {
_, err := os.Stat(path) _, err := os.Stat(path)
if err == nil { return true, nil } if err == nil {
if os.IsNotExist(err) { return false, nil } return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err return false, err
} }
@ -36,8 +40,13 @@ func GetNextIP(ip *net.IP, inc uint) *net.IP {
return &r return &r
} }
func MakeRequest(url string, httpMethod string, body []byte, headers map[string]string) (*http.Response, []byte, error) { // Generic convenience function used to make HTTP requests.
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} func MakeRequest(client *http.Client, url string, httpMethod string, body []byte, headers map[string]string) (*http.Response, []byte, error) {
// use defaults if no client provided
if client == nil {
client = http.DefaultClient
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
req, err := http.NewRequest(httpMethod, url, bytes.NewBuffer(body)) req, err := http.NewRequest(httpMethod, url, bytes.NewBuffer(body))
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("could not create new HTTP request: %v", err) return nil, nil, fmt.Errorf("could not create new HTTP request: %v", err)
@ -46,7 +55,7 @@ func MakeRequest(url string, httpMethod string, body []byte, headers map[string]
for k, v := range headers { for k, v := range headers {
req.Header.Add(k, v) req.Header.Add(k, v)
} }
res, err := http.DefaultClient.Do(req) res, err := client.Do(req)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("could not make request: %v", err) return nil, nil, fmt.Errorf("could not make request: %v", err)
} }
@ -65,7 +74,7 @@ func MakeOutputDirectory(path string) (string, error) {
final := path + "/" + dirname final := path + "/" + dirname
// check if path is valid and directory // check if path is valid and directory
pathExists, err := PathExists(final); pathExists, err := PathExists(final)
if err != nil { if err != nil {
return final, fmt.Errorf("could not check for existing path: %v", err) return final, fmt.Errorf("could not check for existing path: %v", err)
} }