This repository has been archived by the owner on Dec 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
195 lines (179 loc) · 3.62 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"context"
"crypto/rand"
"crypto/rsa"
"io"
"log"
"net"
"sync"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"golang.org/x/crypto/ssh"
)
func main() {
// Open listen socket
listener, err := net.Listen("tcp", "0.0.0.0:2222")
if err != nil {
log.Fatalln(err)
}
for {
// Accept TCP connection
conn, err := listener.Accept()
if err != nil {
break
}
config := getServerConfig(err)
// Perform SSH handshake
_, newChannels, requests, err := ssh.NewServerConn(conn, config)
if err != nil {
_ = conn.Close()
continue
}
// Handle non-channel requests
go handleRequests(requests)
// Handle new channels
go handleChannels(newChannels)
}
}
func getServerConfig(err error) *ssh.ServerConfig {
config := &ssh.ServerConfig{
NoClientAuth: true,
}
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
log.Fatalln(err)
}
hostKey, err := ssh.NewSignerFromKey(key)
if err != nil {
log.Fatalln(err)
}
config.AddHostKey(hostKey)
return config
}
func handleChannels(channels <-chan ssh.NewChannel) {
for {
// When a new channel comes in, handle it
newChannel, ok := <-channels
if !ok {
// Connection is closed
break
}
go handleChannel(newChannel)
}
}
func handleChannel(newChannel ssh.NewChannel) {
// Accept all channels. Normally, we would check if it s a "session "channel
channel, requests, err := newChannel.Accept()
if err != nil {
log.Println(err)
return
}
tty := false
for {
req, ok := <-requests
if !ok {
break
}
switch req.Type {
case "pty-req":
// Interactive session
tty = true
if req.WantReply {
_ = req.Reply(true, []byte{})
}
case "shell":
if req.WantReply {
_ = req.Reply(true, []byte{})
}
go launchShell(channel, tty)
default:
if req.WantReply {
_ = req.Reply(false, []byte("unsupported request"))
}
}
}
}
func launchShell(channel ssh.Channel, tty bool) {
cli, err := client.NewClientWithOpts()
if err != nil {
log.Fatalln(err)
}
// region Create container
cnt, err := cli.ContainerCreate(
context.TODO(),
&container.Config{
Image: "ubuntu",
Tty: tty,
AttachStdin: true,
StdinOnce: true,
OpenStdin: true,
},
nil,
nil,
nil,
"",
)
if err != nil {
log.Fatalln(err)
}
// endregion
// region Attach to container
attach, err := cli.ContainerAttach(
context.Background(),
cnt.ID,
types.ContainerAttachOptions{
Stream: true,
Stdin: true,
Stdout: true,
Stderr: true,
Logs: true,
},
)
if err != nil {
log.Fatalln(err)
}
// endregion
// region Start container
err = cli.ContainerStart(context.TODO(), cnt.ID, types.ContainerStartOptions{})
if err != nil {
log.Fatalln(err)
}
// endregion
closeChannel := func() {
_ = channel.Close()
_ = cli.ContainerRemove(context.TODO(), cnt.ID, types.ContainerRemoveOptions{})
}
var once sync.Once
if tty {
go func() {
// Copy container output only to stdout (TTY is already multiplexed)
_, _ = io.Copy(channel, attach.Reader)
once.Do(closeChannel)
}()
} else {
go func() {
// Copy from to stdout and stderr
_, _ = stdcopy.StdCopy(channel, channel.Stderr(), attach.Reader)
once.Do(closeChannel)
}()
}
go func() {
// Copy stdin from channel to container
_, _ = io.Copy(attach.Conn, channel)
once.Do(closeChannel)
}()
}
func handleRequests(requests <-chan *ssh.Request) {
for {
request, ok := <-requests
if !ok {
break
}
if request.WantReply {
_ = request.Reply(false, []byte("request type not supported"))
}
}
}