-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient-panel.go
More file actions
220 lines (184 loc) · 5.16 KB
/
client-panel.go
File metadata and controls
220 lines (184 loc) · 5.16 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"log"
"net/http"
"strconv"
"sync"
"time"
"github.com/gorilla/websocket"
"github.com/tyler-sommer/stick"
"github.com/creativenucleus/bytejammer/embed"
)
// ClientPanel is the web interface for the client to manage their connection and the port should be private to them.
// It handles identity creation, and launching of a connection to the host.
// It does not handle the connection with the host itself.
const (
fileCheckPeriod = 3 * time.Second
)
type ClientPanel struct {
// #TODO: lock down to receiver only
chSendServerStatus chan ClientServerStatus
wsClient *websocket.Conn
wsMutex sync.Mutex
}
func startClientPanel(port int) error {
// Replace this with a random string...
session := "session"
webServer := &http.Server{
Addr: fmt.Sprintf(":%d", port),
ReadHeaderTimeout: 3 * time.Second,
}
subFs, err := fs.Sub(embed.WebStaticAssets, "web-static")
if err != nil {
return err
}
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(subFs))))
fmt.Printf("In a web browser, go to http://localhost:%d/%s\n", port, session)
cp := ClientPanel{
chSendServerStatus: make(chan ClientServerStatus),
}
http.HandleFunc(fmt.Sprintf("/%s", session), cp.webClientIndex)
http.HandleFunc(fmt.Sprintf("/%s/api/identity.json", session), cp.webClientApiIdentityJSON)
http.HandleFunc(fmt.Sprintf("/%s/api/join-server.json", session), cp.webClientApiJoinServerJSON)
http.HandleFunc(fmt.Sprintf("/%s/ws-client", session), cp.wsWebClient())
if err := webServer.ListenAndServe(); err != nil {
return err
}
return nil
}
func (cp *ClientPanel) webClientIndex(w http.ResponseWriter, r *http.Request) {
env := stick.New(nil)
err := env.Execute(string(embed.ClientIndexHtml), w, map[string]stick.Value{"session_key": "session"})
if err != nil {
log.Println("write:", err)
}
}
func (cp *ClientPanel) webClientApiIdentityJSON(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
identities, err := getIdentities()
if err != nil {
apiOutErr(w, err, http.StatusInternalServerError)
return
}
apiOutResponse(w, identities, http.StatusOK)
case "POST":
// #TODO: Cleaner way to do this?
type reqType struct {
DisplayName string `json:"displayName"`
}
var req reqType
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
apiOutErr(w, err, http.StatusBadRequest)
return
}
err = makeIdentity(req.DisplayName)
if err != nil {
apiOutErr(w, err, http.StatusInternalServerError)
return
}
apiOutResponse(w, nil, http.StatusCreated)
default:
apiOutErr(w, errors.New("Method not allowed"), http.StatusMethodNotAllowed)
}
}
func (cp *ClientPanel) webClientApiJoinServerJSON(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
cp.chSendServerStatus <- ClientServerStatus{isConnected: false}
// #TODO: Cleaner way to do this?
type reqType struct {
Host string `json:"host"`
Port string `json:"port"`
IdentityId string `json:"identity-id"`
Message string `json:"message"`
}
var req reqType
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
apiOutErr(w, err, http.StatusBadRequest)
return
}
identity, err := getIdentity(req.IdentityId)
if err != nil {
apiOutErr(w, err, http.StatusBadRequest)
return
}
port, err := strconv.Atoi(req.Port)
if err != nil {
apiOutErr(w, err, http.StatusBadRequest)
return
}
err = startClientServerConn(req.Host, port, identity, cp.chSendServerStatus)
if err != nil {
apiOutErr(w, err, http.StatusInternalServerError)
return
}
apiOutResponse(w, nil, http.StatusCreated)
default:
apiOutErr(w, errors.New("Method not allowed"), http.StatusMethodNotAllowed)
}
}
func (cp *ClientPanel) wsWebClient() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var err error
cp.wsClient, err = wsUpgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
defer cp.wsClient.Close()
go cp.wsRead()
go cp.wsWrite()
// #TODO: handle exit
for {
}
}
}
func (cp *ClientPanel) wsRead() {
for {
var msg Msg
err := cp.wsClient.ReadJSON(&msg)
if err != nil {
log.Println("read:", err)
break
}
switch msg.Type {
default:
log.Printf("Message not understood: %s\n", msg.Type)
}
}
}
func (cp *ClientPanel) wsWrite() {
/*
statusTicker := time.NewTicker(statusSendPeriod)
defer func() {
statusTicker.Stop()
}()
*/
for {
select {
// case <-done:
// return
// case <-statusTicker.C:
// fmt.Println("TICKER!")
case status := <-cp.chSendServerStatus:
msg := Msg{Type: "server-status", ServerStatus: status}
err := cp.sendData(&msg)
if err != nil {
// #TODO: relax
log.Fatal(err)
}
}
}
}
func (cp *ClientPanel) sendData(data interface{}) error {
cp.wsMutex.Lock()
defer cp.wsMutex.Unlock()
return cp.wsClient.WriteJSON(data)
}