This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
/
websocket_connection_factory.go
77 lines (61 loc) · 1.86 KB
/
websocket_connection_factory.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
package websockets
import (
"context"
"net"
"net/http"
"github.com/google/uuid"
"github.com/gorilla/websocket"
internalAPI "github.com/vmware-tanzu/octant/internal/api"
"github.com/vmware-tanzu/octant/pkg/api"
"github.com/vmware-tanzu/octant/pkg/config"
)
type WebsocketConnectionFactory struct {
upgrader websocket.Upgrader
}
var DefaultWebsocketUpgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return false
}
return internalAPI.ShouldAllowHost(host, internalAPI.AcceptedHosts())
},
}
func NewWebsocketConnectionFactory() *WebsocketConnectionFactory {
return &WebsocketConnectionFactory{
upgrader: DefaultWebsocketUpgrader,
}
}
var _ api.StreamingClientFactory = (*WebsocketConnectionFactory)(nil)
func (wcf *WebsocketConnectionFactory) NewConnection(
w http.ResponseWriter, r *http.Request, m api.ClientManager, dashConfig config.Dash,
) (api.StreamingClient, context.CancelFunc, error) {
clientID, err := uuid.NewUUID()
if err != nil {
return nil, nil, err
}
conn, err := wcf.upgrader.Upgrade(w, r, nil)
if err != nil {
return nil, nil, err
}
ctx, cancel := context.WithCancel(m.Context())
client := NewWebsocketClient(ctx, conn, m, dashConfig, m.ActionDispatcher(), clientID)
return client, cancel, nil
}
func (wcf *WebsocketConnectionFactory) NewTemporaryConnection(
w http.ResponseWriter, r *http.Request, m api.ClientManager,
) (api.StreamingClient, context.CancelFunc, error) {
clientID, err := uuid.NewUUID()
if err != nil {
return nil, nil, err
}
conn, err := wcf.upgrader.Upgrade(w, r, nil)
if err != nil {
return nil, nil, err
}
ctx, cancel := context.WithCancel(m.Context())
client := NewTemporaryWebsocketClient(ctx, conn, m, m.ActionDispatcher(), clientID)
return client, cancel, nil
}