/
main.go
431 lines (393 loc) · 12.5 KB
/
main.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package main
import (
"bufio"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
// Termbox seems to be one of the best projects in any language for handling terminal input.
// It"s cross-platform and the maintainer is disciplined about supporting the baseline of escape
// codes that work across the majority of terminals.
"github.com/nsf/termbox-go"
"github.com/gorilla/websocket"
"github.com/shibukawa/configdir"
)
var (
logfile string
webSocketAddresss = flag.String("port", ":3334", "Web socket service address")
firefoxBinary = flag.String("firefox", "firefox", "Path to Firefox executable")
isFFGui = flag.Bool("with-gui", false, "Don't use headless Firefox")
isUseExistingFirefox = flag.Bool("use-existing-ff", false, "Whether Browsh should launch Firefox or not")
useFFProfile = flag.String("ff-profile", "default", "Firefox profile to use")
isDebug = flag.Bool("debug", false, "Log to ./debug.log")
upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
stdinChannel = make(chan string)
marionette net.Conn
ffCommandCount = 0
defaultFFPrefs = map[string]string{
"browser.startup.homepage": "'about:blank'",
"startup.homepage_welcome_url": "'about:blank'",
"startup.homepage_welcome_url.additional": "''",
"devtools.errorconsole.enabled": "true",
"devtools.chrome.enabled": "true",
// Send Browser Console (different from Devtools console) output to
// STDOUT.
"browser.dom.window.dump.enabled": "true",
// From:
// http://hg.mozilla.org/mozilla-central/file/1dd81c324ac7/build/automation.py.in//l388
// Make url-classifier updates so rare that they won"t affect tests.
"urlclassifier.updateinterval": "172800",
// Point the url-classifier to a nonexistent local URL for fast failures.
"browser.safebrowsing.provider.0.gethashURL": "'http://localhost/safebrowsing-dummy/gethash'",
"browser.safebrowsing.provider.0.keyURL": "'http://localhost/safebrowsing-dummy/newkey'",
"browser.safebrowsing.provider.0.updateURL": "'http://localhost/safebrowsing-dummy/update'",
// Disable self repair/SHIELD
"browser.selfsupport.url": "'https://localhost/selfrepair'",
// Disable Reader Mode UI tour
"browser.reader.detectedFirstArticle": "true",
// Set the policy firstURL to an empty string to prevent
// the privacy info page to be opened on every "web-ext run".
// (See #1114 for rationale)
"datareporting.policy.firstRunURL": "''",
}
)
func setupLogging() {
dir, err := os.Getwd()
if err != nil {
shutdown(err.Error())
}
logfile = fmt.Sprintf(filepath.Join(dir, "debug.log"))
if _, err := os.Stat(logfile); err == nil {
os.Truncate(logfile, 0)
}
if err != nil {
shutdown(err.Error())
}
}
func log(msg string) {
if !*isDebug {
return
}
f, oErr := os.OpenFile(logfile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if oErr != nil {
shutdown(oErr.Error())
}
defer f.Close()
msg = msg + "\n"
if _, wErr := f.WriteString(msg); wErr != nil {
shutdown(wErr.Error())
}
}
func initialise() {
flag.Parse()
setupTermbox()
setupLogging()
}
func setupTermbox() {
err := termbox.Init()
if err != nil {
shutdown(err.Error())
}
termbox.SetInputMode(termbox.InputAlt | termbox.InputMouse)
}
func shutdown(message string) {
exitCode := 0
if message != "normal" {
exitCode = 1
}
println(message)
log("Shutting down with: " + message)
termbox.Close()
os.Exit(exitCode)
}
func sendTtySize() {
x, y := termbox.Size()
sendMessageToWebExtension(fmt.Sprintf("/tty_size,%d,%d", x, y))
}
func readStdin() {
defer termbox.Close()
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
if ev.Key == termbox.KeyCtrlQ {
if !*isUseExistingFirefox {
sendFirefoxCommand("quitApplication", map[string]interface{}{})
}
shutdown("normal")
}
log(fmt.Sprintf("EventKey: k: %d, c: %c, mod: %s", ev.Key, ev.Ch, ev.Mod))
eventMap := map[string]interface{}{
"key": int(ev.Key),
"char": string(ev.Ch),
"mod": int(ev.Mod),
}
marshalled, _ := json.Marshal(eventMap)
sendMessageToWebExtension("/stdin," + string(marshalled))
case termbox.EventResize:
// Need to flush STDOUT before getting the new TTY size because there
// can be a discrepancy between the "internal buffer" size and the
// actual size.
termbox.Flush()
sendTtySize()
case termbox.EventMouse:
log(fmt.Sprintf("Mouse: k: %d, x: %d, y: %d, mod: %s", ev.Key, ev.MouseX, ev.MouseY, ev.Mod))
eventMap := map[string]interface{}{
"key": int(ev.Key),
"mouse_x": int(ev.MouseX),
"mouse_y": int(ev.MouseY),
"mod": int(ev.Mod),
}
marshalled, _ := json.Marshal(eventMap)
sendMessageToWebExtension("/stdin," + string(marshalled))
case termbox.EventError:
shutdown(ev.Err.Error())
}
}
}
func sendMessageToWebExtension(message string) {
stdinChannel <- message
}
func webSocketReader(ws *websocket.Conn) {
defer ws.Close()
for {
_, message, err := ws.ReadMessage()
handleWebextensionCommand(message)
if err != nil {
if websocket.IsCloseError(err, websocket.CloseGoingAway) {
log("Socket reader detected that the browser closed the websocket")
triggerSocketWriterClose()
return
}
shutdown(err.Error())
}
}
}
func handleWebextensionCommand(message []byte) {
parts := strings.Split(string(message), ",")
command := parts[0]
switch command {
case "/frame":
renderFrame(strings.Join(parts[1:], ","))
case "/screenshot":
saveScreenshot(parts[1])
default:
log("WEBEXT: " + string(message))
}
}
func renderFrame(frame string) {
termbox.SetCursor(0, 0)
os.Stdout.Write([]byte(frame))
termbox.HideCursor()
termbox.Flush()
}
func saveScreenshot(base64String string) {
dec, err := base64.StdEncoding.DecodeString(base64String)
if err != nil {
shutdown(err.Error())
}
file, err := ioutil.TempFile(os.TempDir(), "browsh-screenshot")
if err != nil {
shutdown(err.Error())
}
if _, err := file.Write(dec); err != nil {
shutdown(err.Error())
}
if err := file.Sync(); err != nil {
shutdown(err.Error())
}
fullPath := file.Name() + ".jpg"
if err := os.Rename(file.Name(), fullPath); err != nil {
shutdown(err.Error())
}
message := "Screenshot saved to " + fullPath
sendMessageToWebExtension("/status," + message)
file.Close()
}
// When the socket reader attempts to read from a closed websocket it quickly and
// simply closes its associated Go routine. However the socket writer won't
// automatically notice until it actually needs to send something. So we force that
// by sending this NOOP text.
// TODO: There's a potential race condition because new connections share the same
// Go channel. So we need to setup a new channel for every connection.
func triggerSocketWriterClose() {
stdinChannel <- "BROWSH CLIENT FORCING CLOSE OF WEBSOCKET WRITER"
}
func webSocketWriter(ws *websocket.Conn) {
var message string
defer ws.Close()
for {
message = <-stdinChannel
log(fmt.Sprintf("TTY sending: %s", message))
if err := ws.WriteMessage(websocket.TextMessage, []byte(message)); err != nil {
if err == websocket.ErrCloseSent {
log("Socket writer detected that the browser closed the websocket")
return
}
shutdown(err.Error())
}
log(fmt.Sprintf("TTY sent: %s", message))
}
}
func webSocketServer(w http.ResponseWriter, r *http.Request) {
log("Incoming web request from browser")
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
shutdown(err.Error())
}
go webSocketWriter(ws)
go webSocketReader(ws)
sendTtySize()
}
// Gets a cross-platform path to store Browsh config
func getConfigFolder() string {
configDirs := configdir.New("browsh", "firefox_profile")
folders := configDirs.QueryFolders(configdir.Global)
folders[0].MkdirAll()
return folders[0].Path
}
func startHeadlessFirefox() {
println("Starting...")
log("Starting Firefox in headless mode")
args := []string{"--marionette"}
if !*isFFGui {
args = append(args, "--headless")
}
if *useFFProfile != "default" {
log("Using profile: " + *useFFProfile)
args = append(args, "-P", *useFFProfile)
} else {
profilePath := getConfigFolder()
log("Using default profile at: " + profilePath)
args = append(args, "--profile", profilePath)
}
firefoxProcess := exec.Command(*firefoxBinary, args...)
defer firefoxProcess.Process.Kill()
stdout, err := firefoxProcess.StdoutPipe()
if err != nil {
shutdown(err.Error())
}
if err := firefoxProcess.Start(); err != nil {
shutdown(err.Error())
}
in := bufio.NewScanner(stdout)
for in.Scan() {
log("FF-CONSOLE: " + in.Text()) // write each line to your log, or anything you need
}
}
// Connect to Firefox's Marionette service.
// RANT: Firefox's remote control tools are so confusing. There seem to be 2
// services that come with your Firefox binary; Marionette and the Remote
// Debugger. The latter you would expect to follow the widely supported
// Chrome standard, but no, it's merely on the roadmap. There is very little
// documentation on either. I have the impression, but I'm not sure why, that
// the Remote Debugger is better, seemingly more API methods, and as mentioned
// is on the roadmap to follow the Chrome standard.
// I've used Marionette here, simply because it was easier to reverse engineer
// from the Python Marionette package.
func firefoxMarionette() {
log("Attempting to connect to Firefox Marionette")
conn, err := net.Dial("tcp", "127.0.0.1:2828")
if err != nil {
shutdown(err.Error())
}
marionette = conn
readMarionette()
sendFirefoxCommand("newSession", map[string]interface{}{})
}
// Install the Browsh extension that was bundled with `go-bindata` under
// `webextension.go`.
func installWebextension() {
data, err := Asset("webext/dist/web-ext-artifacts/browsh.xpi")
if err != nil {
shutdown(err.Error())
}
file, err := ioutil.TempFile(os.TempDir(), "prefix")
defer os.Remove(file.Name())
ioutil.WriteFile(file.Name(), []byte(data), 0644)
args := map[string]interface{}{"path": file.Name()}
sendFirefoxCommand("addon:install", args)
}
// Set a Firefox preference as you would in `about:config`
// `value` needs to be supplied with quotes if it's to be used as a JS string
func setFFPreference(key string, value string) {
sendFirefoxCommand("setContext", map[string]interface{}{"value": "chrome"})
script := fmt.Sprintf(`
Components.utils.import("resource://gre/modules/Preferences.jsm");
prefs = new Preferences({defaultBranch: false});
prefs.set("%s", %s);`, key, value)
args := map[string]interface{}{"script": script}
sendFirefoxCommand("executeScript", args)
sendFirefoxCommand("setContext", map[string]interface{}{"value": "content"})
}
// Consume output from Marionette, we don't do anything with it. It"s just
// useful to have it in the logs.
func readMarionette() {
buffer := make([]byte, 4096)
count, err := marionette.Read(buffer)
if err != nil {
shutdown(err.Error())
}
log("FF-MRNT: " + string(buffer[:count]))
}
func sendFirefoxCommand(command string, args map[string]interface{}) {
log("Sending `" + command + "` to Firefox Marionette")
fullCommand := []interface{}{0, ffCommandCount, command, args}
marshalled, _ := json.Marshal(fullCommand)
message := fmt.Sprintf("%d:%s", len(marshalled), marshalled)
fmt.Fprintf(marionette, message)
ffCommandCount++
readMarionette()
}
func loadHomePage() {
// Wait for the CLI websocket server to start listening
time.Sleep(200 * time.Millisecond)
args := map[string]interface{}{
"url": "https://google.com",
}
sendFirefoxCommand("get", args)
}
func setDefaultPreferences() {
for key, value := range defaultFFPrefs {
setFFPreference(key, value)
}
}
// Note that everything executed in and from this function is not covered by the integration
// tests, because it uses the officially signed webextension, of which there can be only one.
// We can't bump the version and create a new signed webextension for every commit.
func setupFirefox() {
go startHeadlessFirefox()
// TODO: Do something better than just waiting
time.Sleep(3 * time.Second)
firefoxMarionette()
setDefaultPreferences()
installWebextension()
go loadHomePage()
}
func main() {
initialise()
if !*isUseExistingFirefox {
println("Starting Browsh...")
setupFirefox()
} else {
println("Waiting for a Firefox instance to connect...")
}
log("Starting Browsh CLI client")
go readStdin()
http.HandleFunc("/", webSocketServer)
if err := http.ListenAndServe(*webSocketAddresss, nil); err != nil {
shutdown(err.Error())
}
log("Exiting at end of main()")
}