diff --git a/README.md b/README.md index 3c65354..0ee9762 100644 --- a/README.md +++ b/README.md @@ -21,12 +21,15 @@ information. There are two options: ## Supported providers -Currently, this tool supports three cloud providers: +Currently, this tool supports four cloud providers: * `aws`: Amazon Web Services * `azure`: Microsoft Azure Cloud + * `do`: Digital Ocean * `gcp`: Google Cloud Platform +Not all providers support all network details. + I am happy to accept pull requests to implement more providers. ## Supported Fields diff --git a/discover/digitalocean.go b/discover/digitalocean.go new file mode 100644 index 0000000..6b92dec --- /dev/null +++ b/discover/digitalocean.go @@ -0,0 +1,37 @@ +package discover + +// TODO: decide out how, when, and if to return the Floating IP (URLs noted below) + +import ( + "net" +) + +const ( + doPrivateIPv4URL = "http://169.254.169.254/metadata/v1/interfaces/private/0/ipv4/address" + doPublicIPv4URL = "http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address" + doFloatingPublicIPv4EnabledURL = "http://169.254.169.254/metadata/v1/floating_ip/ipv4/active" + doFloatingPublicIPv4URL = "http://169.254.169.254/metadata/v1/interfaces/public/0/anchor_ipv4/address" + doPublicIPv6URL = "http://169.254.169.254/metadata/v1/interfaces/public/0/ipv6/address" +) + +// NewDigitalOceanDiscoverer returns a new Digital Ocean network discoverer +func NewDigitalOceanDiscoverer() Discoverer { + return NewDiscoverer( + PublicIPv4DiscovererOption(doPublicIPv4), + PublicIPv6DiscovererOption(doPublicIPv6), + ) + +} + +// FIXME: this URL never seems to be populated +func doPrivateIPv4() (net.IP, error) { + return StandardIPFromHTTP(doPrivateIPv4URL, nil) +} + +func doPublicIPv4() (net.IP, error) { + return StandardIPFromHTTP(doPublicIPv4URL, nil) +} + +func doPublicIPv6() (net.IP, error) { + return StandardIPFromHTTP(doPublicIPv6URL, nil) +} diff --git a/discover/util.go b/discover/util.go index b4e2988..bd2311d 100644 --- a/discover/util.go +++ b/discover/util.go @@ -17,7 +17,7 @@ func ParseIPv4FromBody(in io.Reader) (net.IP, error) { return nil, fmt.Errorf("failed to read response: %v", err) } - if len(data) < 8 || len(data) > 18 { + if len(data) < 8 || len(data) > 42 { return nil, fmt.Errorf("invalid response: %s", string(data)) } diff --git a/main.go b/main.go index 7ea85cd..6e28723 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,7 @@ var ( func init() { flag.BoolVar(&debug, "debug", false, `debug mode`) - flag.StringVar(&provider, "provider", "", `provider type. Options are: "aws", "azure", "gcp"`) + flag.StringVar(&provider, "provider", "", `provider type. Options are: "aws", "azure", "do", gcp"`) flag.StringVar(&retField, "field", "", `return only a single field. Options are: "hostname", "publicv4", publicv6", "privatev4"`) } @@ -50,6 +50,8 @@ func main() { discoverer = discover.NewAWSDiscoverer() case "azure": discoverer = discover.NewAzureDiscoverer() + case "do": + discoverer = discover.NewDigitalOceanDiscoverer() case "gcp": discoverer = discover.NewGCPDiscoverer() default: