add IDMSv2 to AWS detection

This commit is contained in:
Pascal Fischer
2024-02-08 12:48:19 +01:00
parent b0a0473b55
commit 95c3a6c14b
+38 -2
View File
@@ -5,9 +5,45 @@ import (
)
func detectAWS() string {
resp, err := hc.Get("http://169.254.169.254/latest/")
if err == nil && resp.StatusCode == http.StatusOK {
v1ResultChan := make(chan bool, 1)
v2ResultChan := make(chan bool, 1)
go func() {
v1ResultChan <- detectIDMSv1()
}()
go func() {
v2ResultChan <- detectIDMSv2()
}()
v1Result, v2Result := <-v1ResultChan, <-v2ResultChan
if v1Result || v2Result {
return "Amazon Web Services"
}
return ""
}
func detectIDMSv1() bool {
resp, err := hc.Get("http://169.254.169.254/latest/")
if err != nil {
return false
}
defer resp.Body.Close()
return resp.StatusCode == http.StatusOK
}
func detectIDMSv2() bool {
req, err := http.NewRequest("PUT", "http://169.254.169.254/latest/api/token", nil)
if err != nil {
return false
}
req.Header.Set("X-aws-ec2-metadata-token-ttl-seconds", "21600")
resp, err := hc.Do(req)
if err != nil {
return false
}
defer resp.Body.Close()
return resp.StatusCode == http.StatusOK
}