2019-07-08 15:44:17 -04:00
|
|
|
package libdetectcloud
|
2019-02-26 17:03:16 -05:00
|
|
|
|
|
|
|
|
import (
|
2024-02-08 12:51:09 +01:00
|
|
|
"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 {
|
2024-02-08 15:09:25 +01:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
defer cancel()
|
2020-11-20 16:52:42 -05:00
|
|
|
|
2024-02-08 15:09:25 +01: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,
|
2024-02-08 15:09:25 +01:00
|
|
|
detectOpenStack,
|
|
|
|
|
detectSoftlayer,
|
|
|
|
|
detectVultr,
|
|
|
|
|
detectContainer,
|
|
|
|
|
}
|
2024-02-08 12:51:09 +01:00
|
|
|
|
2024-02-08 15:09:25 +01:00
|
|
|
results := make(chan string, len(detectFuncs))
|
2024-02-08 12:51:09 +01:00
|
|
|
|
2024-02-08 15:09:25 +01:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
wg.Add(len(detectFuncs))
|
2024-02-08 12:51:09 +01:00
|
|
|
|
2024-02-08 15:09:25 +01:00
|
|
|
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()
|
2024-02-08 12:51:09 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-02-08 15:09:25 +01:00
|
|
|
}(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
|
|
|
}
|
2024-02-08 15:09:25 +01:00
|
|
|
|
2020-11-20 16:52:42 -05:00
|
|
|
return ""
|
2019-02-26 17:03:16 -05:00
|
|
|
}
|