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
|
package channel
import (
"fmt"
"net"
"time"
"github.com/gorilla/websocket"
)
// ANSI "end of channel" code
var eot = []byte{0x04}
// An abstraction of gorilla's *websocket.Conn
type Connection interface {
UnderlyingConn() net.Conn
ReadMessage() (int, []byte, error)
WriteMessage(int, []byte) error
WriteControl(int, []byte, time.Time) error
}
type Proxy struct {
StopCh chan error
}
// stoppers is the number of goroutines that may attempt to call Stop()
func NewProxy(stoppers int) *Proxy {
return &Proxy{
StopCh: make(chan error, stoppers+2), // each proxy() call is a stopper
}
}
func (p *Proxy) Serve(upstream, downstream Connection, upstreamAddr, downstreamAddr string) error {
// This signals the upstream channel to kill the exec'd process
defer upstream.WriteMessage(websocket.BinaryMessage, eot)
go p.proxy(upstream, downstream, upstreamAddr, downstreamAddr)
go p.proxy(downstream, upstream, downstreamAddr, upstreamAddr)
return <-p.StopCh
}
func (p *Proxy) proxy(to, from Connection, toAddr, fromAddr string) {
for {
messageType, data, err := from.ReadMessage()
if err != nil {
p.StopCh <- fmt.Errorf("reading from %s: %s", fromAddr, err)
break
}
if err := to.WriteMessage(messageType, data); err != nil {
p.StopCh <- fmt.Errorf("writing to %s: %s", toAddr, err)
break
}
}
}
|