-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal.go
41 lines (35 loc) · 859 Bytes
/
terminal.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
package terminal
import (
"errors"
"net/http"
)
type InteractiveTerminal interface {
Connect() (Conn, error)
}
type Conn interface {
Start(w http.ResponseWriter, r *http.Request, timeoutCh, disconnectCh chan error)
Close() error
}
func ProxyTerminal(timeoutCh, disconnectCh, proxyStopCh chan error, proxyFunc func()) {
disconnected := make(chan bool, 1)
// terminal exit handler
go func() {
// wait for either session timeout or disconnection from the client
select {
case err := <-timeoutCh:
proxyStopCh <- err
case <-disconnected:
// forward the disconnection event if there is any waiting receiver
nonBlockingSend(disconnectCh,
errors.New("finished proxying (client disconnected?)"))
}
}()
proxyFunc()
disconnected <- true
}
func nonBlockingSend(ch chan error, err error) {
select {
case ch <- err:
default:
}
}