forked from pwaller/jump
/
main.go
161 lines (126 loc) · 3.23 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
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
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"syscall"
"time"
"github.com/olekukonko/tablewriter"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
func ShowInstances(instances []*Instance) {
table := tablewriter.NewWriter(os.Stderr)
table.SetAlignment(tablewriter.ALIGN_RIGHT)
table.SetHeader([]string{
"N", "ID", "Name", "S", "Private", "Launch",
"ICMP", "SSH", "HTTP", "HTTPS"})
for n, i := range instances {
row := []string{
fmt.Sprint(n), i.InstanceID[2:], i.Name(), i.PrettyState(),
i.PrivateIP, fmtDuration(i.Up),
(<-i.ICMPPing).String(),
(<-i.SSHPing).String(),
(<-i.HTTPPing).String(),
(<-i.HTTPSPing).String(),
}
table.Append(row)
}
table.Render()
}
func GetInstanceFromUser(max int) int {
s := bufio.NewScanner(os.Stdin)
if !s.Scan() {
// User closed stdin before we read anything
os.Exit(1)
}
if s.Err() != nil {
log.Fatalln("Error reading stdin:", s.Err())
}
var n int
_, err := fmt.Sscan(s.Text(), &n)
if err != nil {
log.Fatalln("Unrecognised input:", s.Text())
}
if n >= max {
log.Fatalln("%d is not a valid instance", n)
}
return n
}
func InvokeSSH(instance *Instance) {
fmt.Fprintln(os.Stderr, "Connecting to", instance.Name())
args := []string{"/usr/bin/ssh"}
// Enable the user to specify arguments to the left and right of the host.
left, right := BreakArgsBySeparator()
args = append(args, left...)
args = append(args, instance.PrivateIP)
args = append(args, right...)
err := syscall.Exec("/usr/bin/ssh", args, os.Environ())
if err != nil {
log.Fatalln("Failed to exec:", err)
}
}
func CursorUp(n int) {
fmt.Fprint(os.Stderr, "[", n, "F")
}
func ClearToEndOfScreen() {
fmt.Fprint(os.Stderr, "[", "J")
}
func JumpTo(client *ec2.EC2) {
ec2Instances, err := client.DescribeInstances(&ec2.DescribeInstancesInput{})
if err != nil {
log.Fatal("DescribeInstances error:", err)
}
// Do this after querying the AWS endpoint (otherwise vulnerable to MITM.)
ConfigureHTTP(false)
instances := InstancesFromEC2Result(ec2Instances)
ShowInstances(instances)
n := GetInstanceFromUser(len(instances))
// +1 to account for final newline.
CursorUp(len(instances) + N_TABLE_DECORATIONS + 1)
ClearToEndOfScreen()
InvokeSSH(instances[n])
}
func Watch(c *ec2.EC2) {
finish := make(chan struct{})
go func() {
defer close(finish)
// Await stdin closure
io.Copy(ioutil.Discard, os.Stdin)
}()
goUp := func() {}
for {
queryStart := time.Now()
ConfigureHTTP(true)
ec2Instances, err := c.DescribeInstances(&ec2.DescribeInstancesInput{})
if err != nil {
log.Fatal("DescribeInstances error:", err)
}
ConfigureHTTP(false)
instances := InstancesFromEC2Result(ec2Instances)
goUp()
ShowInstances(instances)
queryDuration := time.Since(queryStart)
select {
case <-time.After(1*time.Second - queryDuration):
case <-finish:
return
}
goUp = func() { CursorUp(len(instances) + N_TABLE_DECORATIONS) }
}
}
const N_TABLE_DECORATIONS = 4
func main() {
if os.Getenv("SSH_AUTH_SOCK") == "" {
fmt.Fprintln(os.Stderr, "[41;1mWarning: agent forwarding not enabled[K[m")
}
client := ec2.New(session.New())
if len(os.Args) > 1 && os.Args[1] == "@" {
Watch(client)
return
}
JumpTo(client)
}