-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontend.go
319 lines (291 loc) · 8.55 KB
/
frontend.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package chromedpproxy
import (
"context"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"log"
"net/http"
"time"
)
// avoid any packaging by including the front-end html as a variable
const html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">
<title>Remote Chrome Frontend</title>
<style>
canvas {
width: 90vw;
height: 90vh;
border: gray dotted 1px;
padding: 0;
margin: auto;
display: block;
}
input {
width: 90vw;
padding: 0;
margin: auto;
display: block;
font-size: 1em;
}
</style>
</head>
<body>
<canvas tabindex="1" oncontextmenu="return false;"></canvas>
<input id="insert" type="text" placeholder="Enter text to paste (press ENTER key to submit)"/>
</body>
<footer>
<script>
const urlParams = new URLSearchParams(window.location.search);
const target = urlParams.get('id') || "";
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
const insert = document.getElementById("insert");
let sessionId = null;
let _id = 0;
const id = () => {
return _id++;
};
const ws = new WebSocket(((window.location.protocol === "https:") ? "wss://" : "ws://") + window.location.host + "/ws/" + target);
ws.onopen = function() {
ws.send(JSON.stringify({
id: id(),
method: 'Target.attachToTarget',
params: {
targetId: target,
flatten: true,
},
}));
};
ws.onmessage = function(e) {
const data = JSON.parse(e.data)
if ('error' in data && data.error.message === 'No target with given id found') {
ws.close()
}
switch (data.method) {
case "Target.attachedToTarget":
sessionId = data.params.sessionId
ws.send(JSON.stringify({
sessionId,
id: id(),
method: 'Page.startScreencast',
params: {
format: "jpeg",
quality: 100
},
}));
resizeEvent()
break;
case "Page.screencastFrame":
let image = data.params.data;
let img = new Image();
img.onload = function() {
ctx.drawImage(img, 0, 0);
};
img.src = "data:image/jpeg;base64," + image;
ws.send(JSON.stringify({
sessionId,
id: id(),
method: 'Page.screencastFrameAck',
params: {
sessionId: data.params.sessionId
},
}));
break;
default:
break;
}
};
ws.onclose = function(e) {
const body = document.querySelector("body");
if (sessionId === null) {
body.innerText = "No session was found";
} else {
body.innerText = "Connection has been closed";
}
};
const mouseEvent = (e) => {
const buttons = { 1: 'left', 2: 'middle', 3: 'right' };
const event = e.type === 'wheel' ? window.event || e : e;
const types = {
mousedown: 'mousePressed',
mouseup: 'mouseReleased',
wheel: 'mouseWheel',
touchstart: 'mousePressed',
touchend: 'mouseReleased',
};
if (!(e.type in types) || (event.type !== 'wheel' && event.which === 0 && event.type !== 'mousemove')) {
return;
}
const type = types[event.type];
const isScroll = type.indexOf('wheel') !== -1;
const x = isScroll ? event.clientX : event.offsetX;
const y = isScroll ? event.clientY : event.offsetY;
const params = {
type: types[event.type],
x,
y,
button: event.type === 'wheel' ? 'none' : buttons[event.which],
clickCount: 1,
};
if (event.type === 'wheel') {
params.deltaX = event.wheelDeltaX || 0;
params.deltaY = -event.wheelDeltaY || -event.wheelDelta;
}
ws.send(JSON.stringify({
sessionId,
id: id(),
method: 'Input.dispatchMouseEvent',
params,
}));
}
canvas.addEventListener("mousedown", mouseEvent, true)
canvas.addEventListener("mouseup", mouseEvent, true)
canvas.addEventListener("wheel", mouseEvent, true)
canvas.addEventListener("touchstart", mouseEvent, true)
canvas.addEventListener("touchend", mouseEvent, true)
const keyEvent = (e) => {
if (e.keyCode === 8) {
e.preventDefault();
}
let type;
switch (e.type) {
case 'keydown':
type = 'keyDown';
break;
case 'keyup':
type = 'keyUp';
break;
case 'keypress':
type = 'char';
break;
default:
return;
}
const text = type === 'char' ? String.fromCharCode(e.charCode) : undefined;
ws.send(JSON.stringify({
sessionId,
id: id(),
method: 'Input.dispatchKeyEvent',
params: {
type,
text,
unmodifiedText: text ? text.toLowerCase() : undefined,
keyIdentifier: e.keyIdentifier,
code: e.code,
key: e.key,
windowsVirtualKeyCode: e.keyCode,
nativeVirtualKeyCode: e.keyCode,
autoRepeat: false,
isKeypad: false,
isSystemKey: false,
},
}));
};
canvas.addEventListener("keydown", keyEvent, true)
canvas.addEventListener("keyup", keyEvent, true)
canvas.addEventListener("keypress", keyEvent, true)
insert.addEventListener('keydown', (e) => {
if (e.keyCode === 13) {
const text = e.target.value;
ws.send(JSON.stringify({
sessionId,
id: id(),
method: 'Input.insertText',
params: {
text
},
}));
e.target.value = '';
}
}, false);
const resizeEvent = () => {
if (sessionId === null)
return
let { width, height } = canvas.getBoundingClientRect();
width = Math.floor(width);
height = Math.floor(height);
ctx.canvas.width = width;
ctx.canvas.height = height;
ws.send(JSON.stringify({
sessionId,
id: id(),
method: 'Emulation.setDeviceMetricsOverride',
params: {
width,
height,
deviceScaleFactor: 1,
mobile: (width <= 760)
},
}));
};
window.addEventListener('resize', resizeEvent, false);
</script>
</footer>
</html>`
var timeout = time.Second * 15
// startFrontEnd starts a blocking web server that serves the front-end alongside the websocket proxy
func startFrontEnd(frontendListenAddr string, cdpPort string, cancelChan chan bool) {
r := mux.NewRouter()
srv := &http.Server{
Addr: frontendListenAddr,
WriteTimeout: timeout,
ReadTimeout: timeout,
IdleTimeout: time.Second * 60,
Handler: r,
}
r.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
_, err := writer.Write([]byte(html))
if err != nil {
log.Printf("Error writing response: %s", err)
}
})
r.HandleFunc("/ws/{id}", func(writer http.ResponseWriter, request *http.Request) {
upgrader := websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
conn, err := upgrader.Upgrade(writer, request, nil)
if err != nil {
log.Printf("Error upgrading connection: %s", err)
return
}
id := mux.Vars(request)["id"]
proxy, err := startWebsocketProxy(cdpPort, id, conn)
if err != nil {
log.Printf("Error starting websocket proxy: %s", err)
return
}
for {
messageType, message, err := conn.ReadMessage()
if err != nil {
break
}
err = proxy.WriteMessage(messageType, message)
if err != nil {
break
}
}
_ = proxy.Close()
})
go func() {
if err := srv.ListenAndServe(); err != nil {
if err != http.ErrServerClosed {
log.Printf("Error starting frontend: %s", err)
}
}
}()
<-cancelChan
stopFrontEnd(srv)
}
func stopFrontEnd(srv *http.Server) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
err := srv.Shutdown(ctx)
if err != nil {
log.Printf("Error shutting down frontend: %v", err)
return
}
}