-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
131 lines (112 loc) · 2.9 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"github.com/CyCoreSystems/netdiscover/discover"
)
var (
debug bool
provider string
retField string
)
func init() {
flag.BoolVar(&debug, "debug", false, `debug mode`)
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"`)
}
// Response describes the response from an unlimited discovery
type Response struct {
// Hostname is the public hostname of the node
Hostname string `json:"hostname"`
// PrivateIPv4 is the private (internal) IPv4 address of the node
PrivateIPv4 string `json:"private_ipv4"`
// PublicIPv4 is the public (external) IPv4 address of the node
PublicIPv4 string `json:"public_ipv4"`
// PublicIPv6 is the public (external) IPv6 address of the node
PublicIPv6 string `json:"public_ipv6"`
}
// nolint: gocyclo
func main() {
if os.Getenv("CLOUD_PROVIDER") != "" {
provider = os.Getenv("CLOUD_PROVIDER")
}
flag.Parse()
var discoverer discover.Discoverer
switch provider {
case "aws":
discoverer = discover.NewAWSDiscoverer()
case "azure":
discoverer = discover.NewAzureDiscoverer()
case "do":
discoverer = discover.NewDigitalOceanDiscoverer()
case "gcp":
discoverer = discover.NewGCPDiscoverer()
default:
discoverer = discover.NewDiscoverer()
}
if retField != "" {
switch retField {
case "hostname":
h, err := discoverer.Hostname()
if err != nil {
log.Fatal(err)
}
fmt.Println(h)
os.Exit(0)
case "privatev4":
ip, err := discoverer.PrivateIPv4()
if err != nil {
log.Fatal(err)
}
fmt.Println(ip.String())
os.Exit(0)
case "publicv4":
ip, err := discoverer.PublicIPv4()
if err != nil {
log.Fatal(err)
}
fmt.Println(ip.String())
os.Exit(0)
case "publicv6":
ip, err := discoverer.PublicIPv6()
if err != nil {
log.Fatal(err)
}
fmt.Println(ip.String())
os.Exit(0)
default:
log.Fatal("valid fields are: hostname, privatev4, publicv4, publicv6")
}
}
var err error
ret := new(Response)
ret.Hostname, err = discoverer.Hostname()
if err != nil && debug {
log.Println("failed to get hostname:", err)
}
privateIP, err := discoverer.PrivateIPv4()
if err == nil {
ret.PrivateIPv4 = privateIP.String()
} else if debug {
log.Println("failed to get private IPv4 address:", err)
}
publicIP, err := discoverer.PublicIPv4()
if err == nil {
ret.PublicIPv4 = publicIP.String()
} else if debug {
log.Println("failed to get public IPv4 address:", err)
}
publicIPv6, err := discoverer.PublicIPv6()
if err == nil {
ret.PublicIPv6 = publicIPv6.String()
} else if debug {
log.Println("failed to get public IPv6 address:", err)
}
enc := json.NewEncoder(os.Stdout)
if err := enc.Encode(ret); err != nil {
log.Fatal("failed to encode response:", ret)
}
}