Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulexus committed Apr 20, 2018
0 parents commit 0d3bdf3
Show file tree
Hide file tree
Showing 8 changed files with 655 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/netdiscover
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Netdiscover
[![](https://godoc.org/github.com/CyCoreSystems/netdiscover/discover?status.svg)](http://godoc.org/github.com/CyCoreSystems/netdiscover/discover)

Netdiscover is a Golang-based tool and library by which network information can
be discovered on various cloud platforms and bare metal installations. The
typical use case is, when running inside Kubernetes or a container, to discover
the public IP and/or hostname of the node on which the container is running.
This is commonly necessary to configure VoIP applications.

## CLI tool

In the root directory can be found a CLI tool which can be used to query network
information. There are two options:

* `-provider <name>`: if a cloud provider is specified, that provider's
metadata services will be used to determine the network information.
Otherwise, a best-effort approach will be used.
* `-field <name>`: if a field is specified, only that particular network
detail will be returned. Otherwise, a JSON object will be returned
containing all network information which was discovered.

## Supported providers

Currently, this tool supports three cloud providers:

* `aws`: Amazon Web Services
* `azure`: Microsoft Azure Cloud
* `gcp`: Google Cloud Platform

I am happy to accept pull requests to implement more providers.

## Supported Fields

Currently, this tool supports four network data fields:

* `hostname`: the public hostname of the node
* `privatev4`: the private (internal) IPv4 address of the node
* `publicv4`: the public (external) IPv4 address of the node
* `publicv6`: the public (external) IPv6 address of the node


## Examples

Retrieve the public version 4 IP address of the node instance on GCP:

```
netdiscover -provider gcp -field publicv4
```

Retrieve all network information on Amazon Wed Services platform:

```
netdiscover -provider aws
```

Retrieve the version 6 IP address of a baremetal machine:

```
netdiscover -field publicv6
```

33 changes: 33 additions & 0 deletions discover/aws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package discover

import (
"net"
)

const (
awsPrivateIPv4URL = "http://169.254.169.254/latest/meta-data/local-ipv4"
awsPublicIPv4URL = "http://169.254.169.254/latest/meta-data/public-ipv4"
awsHostnameURL = "http://169.254.169.254/latest/meta-data/public-hostname"
)

// NewAWSDiscoverer returns a new Amazon Web Services network discoverer
func NewAWSDiscoverer() Discoverer {
return NewDiscoverer(
PrivateIPv4DiscovererOption(awsPrivateIPv4),
PublicIPv4DiscovererOption(awsPublicIPv4),
PublicHostnameDiscovererOption(awsHostname),
)

}

func awsPrivateIPv4() (net.IP, error) {
return StandardIPFromHTTP(awsPrivateIPv4URL, nil)
}

func awsPublicIPv4() (net.IP, error) {
return StandardIPFromHTTP(awsPublicIPv4URL, nil)
}

func awsHostname() (string, error) {
return StandardHostnameFromHTTP(awsHostnameURL, nil)
}
26 changes: 26 additions & 0 deletions discover/azure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package discover

import (
"net"
)

const (
azurePrivateIPv4URL = "http://169.254.169.254/metadata/instance/network/interface/0/ipv4/ipAddress/0/privateIpAddress?api-version=2017-08-01&format=text"
azurePublicIPv4URL = "http://169.254.169.254/metadata/instance/network/interface/0/ipv4/ipAddress/0/publicIpAddress?api-version=2017-08-01&format=text"
)

// NewAzureDiscoverer returns a new Google Cloud Platform network discoverer
func NewAzureDiscoverer() Discoverer {
return NewDiscoverer(
PrivateIPv4DiscovererOption(azurePrivateIPv4),
PublicIPv4DiscovererOption(azurePublicIPv4),
)
}

func azurePrivateIPv4() (net.IP, error) {
return StandardIPFromHTTP(azurePrivateIPv4URL, map[string]string{"Metadata": "true"})
}

func azurePublicIPv4() (net.IP, error) {
return StandardIPFromHTTP(azurePublicIPv4URL, map[string]string{"Metadata": "true"})
}
Loading

0 comments on commit 0d3bdf3

Please sign in to comment.