Files

63 lines
928 B
Go
Raw Permalink Normal View History

2019-07-08 15:44:17 -04:00
package libdetectcloud
2019-02-26 17:03:16 -05:00
import (
"context"
2019-02-26 17:03:16 -05:00
"net/http"
"sync"
"time"
)
2020-11-20 16:52:42 -05:00
var hc = &http.Client{Timeout: 300 * time.Millisecond}
2019-02-26 17:03:16 -05:00
func Detect() string {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2020-11-20 16:52:42 -05:00
detectFuncs := []func() string{
detectAlibabaCloud,
detectAWS,
detectAzure,
detectDigitalOcean,
detectGCE,
2024-02-08 17:49:31 +01:00
detectOracle,
2024-02-09 11:03:39 +01:00
detectIBMCloud,
detectOpenStack,
detectSoftlayer,
detectVultr,
detectContainer,
}
results := make(chan string, len(detectFuncs))
var wg sync.WaitGroup
wg.Add(len(detectFuncs))
for _, fn := range detectFuncs {
go func(f func() string) {
defer wg.Done()
select {
case <-ctx.Done():
return
default:
if result := f(); result != "" {
results <- result
cancel()
}
}
}(fn)
}
go func() {
wg.Wait()
close(results)
}()
for result := range results {
if result != "" {
return result
2020-11-20 16:52:42 -05:00
}
2019-02-26 17:03:16 -05:00
}
2020-11-20 16:52:42 -05:00
return ""
2019-02-26 17:03:16 -05:00
}