/
comms.go
152 lines (140 loc) · 3.93 KB
/
comms.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
package browsh
import (
"strings"
"fmt"
"net/http"
"encoding/json"
"github.com/gorilla/websocket"
)
var (
upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
stdinChannel = make(chan string)
isConnectedToWebExtension = false
)
type incomingRawText struct {
RequestID string `json:"request_id"`
RawText string `json:"body"`
}
func startWebSocketServer() {
serverMux := http.NewServeMux()
serverMux.HandleFunc("/", webSocketServer)
if err := http.ListenAndServe(":" + *webSocketPort, serverMux); err != nil {
Shutdown(err)
}
}
func sendMessageToWebExtension(message string) {
if (!isConnectedToWebExtension) {
Log("Webextension not connected. Message not sent: " + message)
return
}
stdinChannel <- message
}
// Listen to all messages coming from the webextension
// TODO: It seems this *also* receives sent to the webextention!?
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)
}
}
}
func handleWebextensionCommand(message []byte) {
parts := strings.Split(string(message), ",")
command := parts[0]
if *IsHTTPServer {
handleRawFrameTextCommands(parts)
return
}
switch command {
case "/frame_text":
parseJSONFrameText(strings.Join(parts[1:], ","))
renderCurrentTabWindow()
case "/frame_pixels":
parseJSONFramePixels(strings.Join(parts[1:], ","))
renderCurrentTabWindow()
case "/tab_state":
parseJSONTabState(strings.Join(parts[1:], ","))
if CurrentTab != nil {
renderUI()
}
case "/screenshot":
saveScreenshot(parts[1])
default:
Log("WEBEXT: " + string(message))
}
}
func handleRawFrameTextCommands(parts []string) {
var incoming incomingRawText
command := parts[0]
if command == "/raw_text" {
jsonBytes := []byte(strings.Join(parts[1:], ","))
if err := json.Unmarshal(jsonBytes, &incoming); err != nil {
Shutdown(err)
}
if incoming.RequestID != "" {
Log("Raw text for " + incoming.RequestID)
rawTextRequests[incoming.RequestID] = incoming.RawText
} else {
Log("Raw text but no associated request ID")
}
} else {
Log("WEBEXT: " + strings.Join(parts[0:], ","))
}
}
// 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"
}
// Send a message to the webextension
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)
}
}
}
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)
}
isConnectedToWebExtension = true
go webSocketWriter(ws)
go webSocketReader(ws)
if *IsHTTPServer {
sendMessageToWebExtension("/raw_text_mode")
} else {
sendTtySize()
}
if IsTesting {
// For some reason, using Firefox's CLI arg `--url https://google.com` doesn't consistently
// work on Travis. So we do it here inse
sendMessageToWebExtension("/new_tab," + *StartupURL)
}
}