-
Notifications
You must be signed in to change notification settings - Fork 323
/
platform.go
169 lines (146 loc) · 4.67 KB
/
platform.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not
// use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing
// permissions and limitations under the License.
// Package platform contains platform specific utilities.
package platform
import (
"fmt"
"net"
"sort"
"unicode/utf8"
"github.com/aws/amazon-ssm-agent/agent/log"
)
const (
gettingPlatformDetailsMessage = "getting platform details"
notAvailableMessage = "NotAvailable"
commandOutputMessage = "Command output %v"
)
var getPlatformNameFn = getPlatformName
// IsPlatformWindowsServer2012OrEarlier represents whether it is Windows 2012 and earlier or not
func IsPlatformWindowsServer2012OrEarlier(log log.T) (bool, error) {
return isPlatformWindowsServer2012OrEarlier(log)
}
// PlatformName gets the OS specific platform name.
func PlatformName(log log.T) (name string, err error) {
name, err = getPlatformNameFn(log)
if err != nil {
return
}
platformName := ""
for i := range name {
runeVal, _ := utf8.DecodeRuneInString(name[i:])
if runeVal == utf8.RuneError {
// runeVal = rune(value[i]) - using this will convert \xa9 to valid unicode code point
continue
}
platformName = platformName + fmt.Sprintf("%c", runeVal)
}
return platformName, nil
}
// PlatformType gets the OS specific platform type, valid values are windows and linux.
func PlatformType(log log.T) (name string, err error) {
return getPlatformType(log)
}
// PlatformVersion gets the OS specific platform version.
func PlatformVersion(log log.T) (version string, err error) {
return getPlatformVersion(log)
}
// PlatformSku gets the OS specific platform SKU number
func PlatformSku(log log.T) (sku string, err error) {
return getPlatformSku(log)
}
// Hostname of the computer.
func Hostname(log log.T) (name string, err error) {
return fullyQualifiedDomainName(log), nil
}
// IP of the network interface
func IP() (selected string, err error) {
var interfaces []net.Interface
if interfaces, err = net.Interfaces(); err == nil {
interfaces = filterInterface(interfaces)
sort.Sort(byIndex(interfaces))
candidates := make([]net.IP, 0)
for _, i := range interfaces {
var addrs []net.Addr
if addrs, err = i.Addrs(); err != nil {
continue
}
for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPAddr:
candidates = append(candidates, v.IP.To4())
candidates = append(candidates, v.IP.To16())
case *net.IPNet:
candidates = append(candidates, v.IP.To4())
candidates = append(candidates, v.IP.To16())
}
}
}
selectedIp, err := selectIp(candidates)
if err == nil {
selected = selectedIp.String()
}
} else {
err = fmt.Errorf("failed to load network interfaces: %v", err)
}
if err != nil {
err = fmt.Errorf("failed to determine IP address: %v", err)
}
return
}
// Selects a single IP address to be reported for this instance.
func selectIp(candidates []net.IP) (result net.IP, err error) {
for _, ip := range candidates {
if ip != nil && !ip.IsUnspecified() {
if result == nil {
result = ip
} else if isLoopbackOrLinkLocal(result) {
// Prefer addresses that are not loopbacks or link-local
if !isLoopbackOrLinkLocal(ip) {
result = ip
}
} else if !isLoopbackOrLinkLocal(ip) {
// Among addresses that are not loopback or link-local, prefer IPv4
if !isIpv4(result) && isIpv4(ip) {
result = ip
}
}
}
}
if result == nil {
err = fmt.Errorf("no IP addresses found")
}
return
}
func isLoopbackOrLinkLocal(ip net.IP) bool {
return ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast()
}
func isIpv4(ip net.IP) bool {
return ip.To4() != nil
}
// filterInterface removes interface that's not up or is a loopback/p2p
func filterInterface(interfaces []net.Interface) (i []net.Interface) {
for _, v := range interfaces {
if (v.Flags&net.FlagUp != 0) && (v.Flags&net.FlagLoopback == 0) && (v.Flags&net.FlagPointToPoint == 0) {
i = append(i, v)
}
}
return
}
// byIndex implements sorting for net.Interface.
type byIndex []net.Interface
func (b byIndex) Len() int { return len(b) }
func (b byIndex) Less(i, j int) bool { return b[i].Index < b[j].Index }
func (b byIndex) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func IsPlatformNanoServer(log log.T) (bool, error) {
return isPlatformNanoServer(log)
}