@@ -4,59 +4,62 @@ import (
44 "bufio"
55 "errors"
66 "fmt"
7- "net/http"
87 "regexp"
98 "runtime"
109 "strings"
1110 "time"
1211
12+ log "github.com/sirupsen/logrus"
1313 rawLibvirt "libvirt.org/libvirt-go"
14- )
1514
16- func parrotInfoRetriever (url string , versionRegex * regexp.Regexp ) (* DownloadInfo , error ) {
17- resp , err := http .Get (url ) //#nosec G107
18- if err != nil {
19- return nil , err
20- }
15+ "github.com/eikendev/hackenv/internal/network"
16+ )
2117
22- if resp .StatusCode != http .StatusOK {
23- return nil , fmt .Errorf ("bad HTTP status code (%s)" , resp .Status )
24- }
18+ func findParrotChecksumLine (scanner * bufio.Scanner ) (string , error ) {
19+ const (
20+ sha256Section = "sha256"
21+ sha384Section = "sha384"
22+ )
2523
26- var line string
27- skipped := false
28- scanner := bufio .NewScanner (resp .Body )
24+ var inSha256Section bool
2925
3026 for scanner .Scan () {
31- line = scanner .Text ()
27+ line : = scanner .Text ()
3228
33- if skipped {
34- if strings .Contains (line , "Parrot-security" ) && strings . Contains ( line , "_" + runtime . GOARCH + ".iso" ) {
35- break
36- }
37- } else {
38- if strings . Contains ( line , "sha256" ) {
39- skipped = true
40- }
41- if strings .Contains (line , "sha384 " ) {
42- skipped = false
43- }
29+ switch {
30+ case strings .Contains (line , sha256Section ):
31+ inSha256Section = true
32+ continue
33+ case strings . Contains ( line , sha384Section ):
34+ inSha256Section = false
35+ continue
36+ case inSha256Section &&
37+ strings .Contains (line , "Parrot-security " ) &&
38+ strings . Contains ( line , "_" + runtime . GOARCH + ".iso" ):
39+ return strings . TrimSpace ( line ), nil
4440 }
4541 }
4642
47- if line == "" {
48- return nil , errors .New ("bad checksum file" )
43+ return "" , errors .New ("checksum not found in file" )
44+ }
45+
46+ func parrotInfoRetriever (url string , versionRegex * regexp.Regexp ) (* DownloadInfo , error ) {
47+ resp , err := network .GetResponse (url )
48+ if err != nil {
49+ return nil , fmt .Errorf ("failed to get response: %w" , err )
4950 }
51+ defer func () {
52+ if err := resp .Body .Close (); err != nil {
53+ log .Warnf ("failed to close response body: %v" , err )
54+ }
55+ }()
5056
51- line = strings .TrimSpace (line )
52- parts := strings .Split (line , " " )
53- filename := parts [len (parts )- 1 ]
57+ line , err := findParrotChecksumLine (bufio .NewScanner (resp .Body ))
58+ if err != nil {
59+ return nil , err
60+ }
5461
55- return & DownloadInfo {
56- parts [0 ],
57- versionRegex .FindString (filename ),
58- filename ,
59- }, nil
62+ return parseChecksumLine (line , versionRegex )
6063}
6164
6265func parrotBootInitializer (dom * rawLibvirt.Domain ) {
0 commit comments