-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
126 lines (104 loc) · 2.77 KB
/
util.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
package main
import (
"errors"
"fmt"
"net"
"strings"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/ivoronin/ec2ssh/awsutil"
)
var ErrNoAddress = errors.New("no address found")
func GuessDestinationType(dst string) DstType {
switch {
case strings.HasPrefix(dst, "ip-"),
strings.HasSuffix(dst, ".ec2.internal"),
strings.HasSuffix(dst, ".compute.internal"):
return DstTypePrivateDNSName
case strings.HasPrefix(dst, "i-"):
return DstTypeID
case net.ParseIP(dst) != nil:
addr := net.ParseIP(dst)
if addr.To4() != nil {
if addr.IsPrivate() {
return DstTypePrivateIP
}
return DstTypePublicIP
}
return DstTypeIPv6
default:
return DstTypeNameTag
}
}
func GetInstance(dstType DstType, destination string) (types.Instance, error) {
if dstType == DstTypeAuto {
dstType = GuessDestinationType(destination)
DebugLogger.Printf("guessed destination type %d for %s", dstType, destination)
}
var filterName string
switch dstType {
case DstTypeID:
instance, err := awsutil.GetInstanceByID(destination)
return instance, err
case DstTypePrivateIP:
filterName = "private-ip-address"
case DstTypePublicIP:
filterName = "ip-address"
case DstTypeIPv6:
filterName = "ipv6-address"
case DstTypePrivateDNSName:
filterName = "private-dns-name"
if !strings.Contains(destination, ".") {
destination += ".*" /* e.g. ip-10-0-0-1.* */
}
case DstTypeNameTag:
filterName = "tag:Name"
case DstTypeAuto:
default:
// Should never happen
panic(dstType)
}
return awsutil.GetRunningInstanceByFilter(filterName, destination)
}
func GetInstanceAddr(instance types.Instance, addrType AddrType) (string, error) {
var addr *string
var typeStr string
switch addrType {
case AddrTypeAuto:
switch {
case instance.PrivateIpAddress != nil:
addr = instance.PrivateIpAddress
typeStr = "private"
case instance.PublicIpAddress != nil:
addr = instance.PublicIpAddress
typeStr = "public"
case instance.Ipv6Address != nil:
addr = instance.Ipv6Address
typeStr = "IPv6"
}
case AddrTypePrivate:
addr = instance.PrivateIpAddress
typeStr = "private"
case AddrTypePublic:
addr = instance.PublicIpAddress
typeStr = "public"
case AddrTypeIPv6:
addr = instance.Ipv6Address
typeStr = "IPv6"
}
if addr == nil {
return "", fmt.Errorf("%w: no %s IP address found for instance ID %s", ErrNoAddress, typeStr, *instance.InstanceId)
}
DebugLogger.Printf("using %s IP address %s for instance ID %s", typeStr, *addr, *instance.InstanceId)
return *addr, nil
}
func GetInstanceName(instance types.Instance) *string {
return getInstanceTagValue(instance, "Name")
}
func getInstanceTagValue(instance types.Instance, tagKey string) *string {
for _, tag := range instance.Tags {
if *tag.Key == tagKey {
return tag.Value
}
}
return nil
}