-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnect.go
152 lines (124 loc) · 2.73 KB
/
connect.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
package main
import (
"bytes"
"fmt"
"net/url"
"os"
"os/exec"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/net/websocket"
)
const (
SSH = 1 + iota
CONSOLE
)
type Connect struct {
config Config
}
func (c *Connect) Run() error {
log.Debugf("Connection type: %s", c.config.ConnType.String())
if c.config.ConnType == CON_SSH {
return c.ssh()
} else if c.config.ConnType == CON_CONSOLE {
return c.console()
} else {
return fmt.Errorf("Undefined connection type")
}
}
// Connect to an instance via SSH
func (c *Connect) ssh() error {
var server string
if c.config.SshUser != "" {
server += c.config.SshUser + "@"
}
server += c.config.SshHost
cmd := exec.Command(c.config.SshCommand, append(c.config.SshOptions, server, c.config.SshRemoteCommand)...)
log.Debugf("ssh command:%v", cmd.Args)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// Connect to an instance via nova console
// http://docs.openstack.org/developer/nova/testing/serial-console.html
//
// Type "Ctrl+[ q" to disconnect
func (c *Connect) console() error {
log.Debugf("WebSocket url: %s", c.config.ConsoleUrl)
u, err := url.Parse(c.config.ConsoleUrl)
if err != nil {
return err
}
config, err := websocket.NewConfig(u.String(), u.Scheme+"://"+u.Host)
if err != nil {
return err
}
config.Protocol = []string{"binary", "base64"}
config.Version = 13
con, err := websocket.DialConfig(config)
if err != nil {
return err
}
done := make(chan error)
state, _ := terminal.MakeRaw(0)
term := terminal.NewTerminal(os.Stdin, "")
defer terminal.Restore(0, state)
// Print initial message
msg := `
Connected.
Type "Ctrl+[ q" to disconnect.
接続を切る場合は Ctrl+[ q と入力して下さい
`
term.Write([]byte(msg))
// Read from nova console and send to client.
go func() {
len := 32 * 1024
buf := bytes.NewBuffer(make([]byte, len))
for {
buf.Truncate(len)
b := buf.Bytes()
n, err := con.Read(b)
if err != nil {
done <- err
return
} else if n == 0 {
continue
}
term.Write(b[0:n])
}
}()
// Read from standard input and send to nova console.
go func() {
len := 4
buf := bytes.NewBuffer(make([]byte, len))
fw, _ := con.NewFrameWriter(con.PayloadType)
var prevKey byte
for {
buf.Truncate(len)
b := buf.Bytes()
nr, err := os.Stdin.Read(b)
if err != nil {
done <- err
return
} else if nr == 0 {
continue
}
// Ctrl+[ q
if prevKey == 0x1b && b[0] == 'q' {
done <- nil
return
} else {
prevKey = b[0]
}
nw, err := fw.Write(b[0:nr])
if err != nil {
done <- err
return
} else if nr != nw {
done <- fmt.Errorf("Short written")
}
}
}()
return <-done
}