forked from johnzhd/guacamole_client_go
/
GuacamoleWebSocketTunnelEndpoint.go
85 lines (74 loc) · 2.44 KB
/
GuacamoleWebSocketTunnelEndpoint.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
package gwebsocket
import (
ws "github.com/gorilla/websocket"
gc "github.com/jholowczak/guacamole_client_go"
gn "github.com/jholowczak/guacamole_client_go/gnet"
gp "github.com/jholowczak/guacamole_client_go/gprotocol"
"strconv"
"sync"
"time"
)
const (
BUFFER_SIZE int = 8192
PING_OPCODE string = "ping"
)
type GuacamoleWebSocketTunnelEndpoint struct {
tunnel *gn.GuacamoleTunnel
conn *ws.Conn
mu *sync.Mutex
}
func CreateTunnelEndpoint(conn *ws.Conn, socket *gn.GuacamoleTunnel) (ret *GuacamoleWebSocketTunnelEndpoint) {
ret = &GuacamoleWebSocketTunnelEndpoint{
tunnel: socket,
conn: conn,
mu: &sync.Mutex{},
}
return
}
type GuacamoleWebSocketTunnelEndpointInterface interface {
closeConnection(int, int) error
closeConnectionWithStatus(gc.GuacamoleStatus) error
sendInstruction(string) error
sendGuacamoleInstruction(gp.GuacamoleInstruction) error
//OnOpen(*ws.Conn)
OnMessage(message string)
//OnClose(*ws.Conn)
Tunnel() gn.GuacamoleTunnel
}
func (te *GuacamoleWebSocketTunnelEndpoint) CloseConnection(guacStatusCode, webSocketCode int) (err error) {
te.mu.Lock()
defer te.mu.Unlock()
err = te.conn.WriteControl(webSocketCode, []byte(strconv.Itoa(guacStatusCode)), time.Now().Add(time.Second*5))
return
}
func (te *GuacamoleWebSocketTunnelEndpoint) CloseConnectionWithStatus(guacStatus gc.GuacamoleStatus) (err error) {
err = te.CloseConnection(guacStatus.GetGuacamoleStatusCode(), guacStatus.GetWebSocketCode())
return
}
func (te *GuacamoleWebSocketTunnelEndpoint) SendInstruction(instruction string) (err error) {
te.mu.Lock()
defer te.mu.Unlock()
err = te.conn.WriteMessage(1, []byte(instruction))
return
}
func (te *GuacamoleWebSocketTunnelEndpoint) SendGuacamoleInstruction(instruction gp.GuacamoleInstruction) (err error) {
err = te.SendInstruction(instruction.String())
return
}
func (te *GuacamoleWebSocketTunnelEndpoint) Tunnel() *gn.GuacamoleTunnel {
return te.tunnel
}
func (te GuacamoleWebSocketTunnelEndpoint) Filter(instruction gp.GuacamoleInstruction) (ret gp.GuacamoleInstruction, exc gc.ExceptionInterface) {
if instruction.GetOpcode() == gn.InternalDataOpcode {
args := instruction.GetArgs()
if len(args) >= 2 && args[0] == PING_OPCODE {
err := te.SendGuacamoleInstruction(gp.NewGuacamoleInstruction(gn.InternalDataOpcode, PING_OPCODE, args[1]))
if err != nil {
exc = gc.GuacamoleException.Throw("Unable to send ping response for websocket tunnel")
}
return
}
}
ret = instruction
return
}