-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
conn.go
124 lines (105 loc) · 2.78 KB
/
conn.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
package utils
import (
"io"
"log"
"net"
"sync"
"time"
"github.com/spf13/viper"
"golang.org/x/crypto/ssh"
)
// SSHConnection handles state for a SSHConnection. It wraps an ssh.ServerConn
// and allows us to pass other state around the application.
// Listeners is a map[string]net.Listener.
type SSHConnection struct {
SSHConn *ssh.ServerConn
Listeners *sync.Map
Closed *sync.Once
Close chan bool
Messages chan string
ProxyProto byte
Session chan bool
CleanupHandler bool
SetupLock *sync.Mutex
}
// SendMessage sends a console message to the connection. If block is true, it
// will block until the message is sent. If it is false, it will try to send the
// message 5 times, waiting 100ms each time.
func (s *SSHConnection) SendMessage(message string, block bool) {
if block {
s.Messages <- message
return
}
for i := 0; i < 5; {
select {
case <-s.Close:
return
case s.Messages <- message:
return
default:
time.Sleep(100 * time.Millisecond)
i++
}
}
}
// CleanUp closes all allocated resources for a SSH session and cleans them up.
func (s *SSHConnection) CleanUp(state *State) {
s.Closed.Do(func() {
close(s.Close)
s.SSHConn.Close()
state.SSHConnections.Delete(s.SSHConn.RemoteAddr().String())
log.Println("Closed SSH connection for:", s.SSHConn.RemoteAddr().String(), "user:", s.SSHConn.User())
})
}
// IdleTimeoutConn handles the connection with a context deadline.
// code adapted from https://qiita.com/kwi/items/b38d6273624ad3f6ae79
type IdleTimeoutConn struct {
Conn net.Conn
}
// Read is needed to implement the reader part.
func (i IdleTimeoutConn) Read(buf []byte) (int, error) {
err := i.Conn.SetReadDeadline(time.Now().Add(viper.GetDuration("idle-connection-timeout")))
if err != nil {
return 0, err
}
return i.Conn.Read(buf)
}
// Write is needed to implement the writer part.
func (i IdleTimeoutConn) Write(buf []byte) (int, error) {
err := i.Conn.SetWriteDeadline(time.Now().Add(viper.GetDuration("idle-connection-timeout")))
if err != nil {
return 0, err
}
return i.Conn.Write(buf)
}
// CopyBoth copies betwen a reader and writer and will cleanup each.
func CopyBoth(writer net.Conn, reader io.ReadWriteCloser) {
closeBoth := func() {
reader.Close()
writer.Close()
}
var tcon io.ReadWriter
if viper.GetBool("idle-connection") {
tcon = IdleTimeoutConn{
Conn: writer,
}
} else {
tcon = writer
}
copyToReader := func() {
_, err := io.Copy(reader, tcon)
if err != nil && viper.GetBool("debug") {
log.Println("Error copying to reader:", err)
}
closeBoth()
}
copyToWriter := func() {
_, err := io.Copy(tcon, reader)
if err != nil && viper.GetBool("debug") {
log.Println("Error copying to writer:", err)
}
closeBoth()
}
go copyToReader()
copyToWriter()
}