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 {
// Perform scan and collect from dora server
url := makeEndpointUrl("/scanned_ports")
_, body, err := util.MakeRequest(url, "GET", nil, nil)
_, body, err := util.MakeRequest(nil, url, "GET", nil, nil)
if err != nil {
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{}
for _, result := range scannedResults {
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 {
continue
} else if res.StatusCode != http.StatusOK {

View file

@ -139,7 +139,7 @@ func UpdateFirmwareRemote(q *UpdateParams) error {
if err != nil {
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 {
return fmt.Errorf("something went wrong: %v", err)
} else if res == nil {
@ -153,7 +153,7 @@ func UpdateFirmwareRemote(q *UpdateParams) error {
func GetUpdateStatus(q *UpdateParams) error {
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 {
return fmt.Errorf("something went wrong: %v", err)
} else if res == nil {

View file

@ -15,8 +15,12 @@ import (
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil { return true, nil }
if os.IsNotExist(err) { return false, nil }
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
@ -36,8 +40,13 @@ func GetNextIP(ip *net.IP, inc uint) *net.IP {
return &r
}
func MakeRequest(url string, httpMethod string, body []byte, headers map[string]string) (*http.Response, []byte, error) {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
// Generic convenience function used to make HTTP requests.
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))
if err != nil {
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 {
req.Header.Add(k, v)
}
res, err := http.DefaultClient.Do(req)
res, err := client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("could not make request: %v", err)
}
@ -65,15 +74,15 @@ func MakeOutputDirectory(path string) (string, error) {
final := path + "/" + dirname
// check if path is valid and directory
pathExists, err := PathExists(final);
pathExists, err := PathExists(final)
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)
}
if pathExists {
// make sure it is directory with 0o644 permissions
return final, fmt.Errorf("found existing path: %v", final)
}
// create directory with data + time
err = os.MkdirAll(final, 0766)
if err != nil {