mirror of
https://github.com/netbirdio/libdetectcloud.git
synced 2026-05-22 17:09:55 -07:00
add IDMSv2 to AWS detection
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user