forked from dryark/ios_remote_provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controlfloor.go
503 lines (447 loc) · 14.9 KB
/
controlfloor.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package main
import (
"bufio"
"crypto/tls"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"strconv"
"sync"
"reflect"
log "github.com/sirupsen/logrus"
uj "github.com/nanoscopic/ujsonin/v2/mod"
ws "github.com/gorilla/websocket"
)
type ControlFloor struct {
config *Config
ready bool
base string
wsBase string
cookiejar *cookiejar.Jar
client *http.Client
root uj.JNode
pass string
lock *sync.Mutex
DevTracker *DeviceTracker
vidConns map[string] *ws.Conn
selfSigned bool
}
func NewControlFloor( config *Config ) (*ControlFloor) {
jar, err := cookiejar.New(&cookiejar.Options{})
if err != nil {
panic( err )
}
root := loadCFConfig( "cf.json" )
passNode := root.Get("pass")
if passNode == nil {
}
pass := passNode.String()
client := &http.Client{
Jar: jar,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
self := ControlFloor{
config: config,
ready: false,
base: "http://" + config.cfHost,
wsBase: "ws://" + config.cfHost,
cookiejar: jar,
client: client,
pass: pass,
lock: &sync.Mutex{},
vidConns: make( map[string] *ws.Conn ),
}
if config.https {
self.base = "https://" + config.cfHost
self.wsBase = "wss://" + config.cfHost
if config.selfSigned {
self.selfSigned = true
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
ForceAttemptHTTP2: false,
}
client.Transport = tr
}
} else {
self.base = "http://" + config.cfHost
self.wsBase = "ws://" + config.cfHost
}
success := self.login()
if success {
fmt.Println("Logged in to control floor")
} else {
fmt.Println("Could not login to control floor")
}
self.openWebsocket()
return &self
}
type CFResponse interface {
asText() (string)
}
type CFR_Pong struct {
id int
text string
}
func (self *CFR_Pong) asText() string {
return fmt.Sprintf("{id:%d,text:\"%s\"}\n",self.id, self.text)
}
func ( self *ControlFloor ) startVidStream( udid string ) {
dev := self.DevTracker.getDevice( udid )
dev.startVidStream()
}
func ( self *ControlFloor ) stopVidStream( udid string ) {
dev := self.DevTracker.getDevice( udid )
dev.stopVidStream()
}
// Called from the device object
func ( self *ControlFloor ) startAppStream( udid string ) ( *ws.Conn ) {
dialer := ws.Dialer{
Jar: self.cookiejar,
}
if self.selfSigned {
fmt.Printf("self signed option\n")
dialer.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
//ws.DefaultDialer.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
fmt.Printf("Connecting to CF imgStream\n")
conn, _, err := dialer.Dial( self.wsBase + "/provider/imgStream?udid=" + udid, nil )
if err != nil {
panic( err )
}
fmt.Printf("Connected to cf for imgStream\n")
//dev := self.DevTracker.getDevice( udid )
self.lock.Lock()
self.vidConns[ udid ] = conn
self.lock.Unlock()
return conn
//dev.startStream( conn )
}
// Called from the device object
func ( self *ControlFloor ) stopAppStream( udid string ) {
vidConn := self.vidConns[ udid ]
vidConn.Close()
self.lock.Lock()
delete( self.vidConns, udid )
self.lock.Unlock()
}
func ( self *ControlFloor ) openWebsocket() {
dialer := ws.Dialer{
Jar: self.cookiejar,
}
if self.selfSigned {
fmt.Printf("self signed option\n")
dialer.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
//ws.DefaultDialer.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
fmt.Printf("link for dialer = %s\n", self.wsBase + "/provider/ws" )
conn, _, err := dialer.Dial( self.wsBase + "/provider/ws", nil )
if err != nil {
panic( err )
}
respondChan := make( chan CFResponse )
doneChan := make( chan bool )
// response channel exists so that multiple threads can queue
// responses. WriteMessage is not thread safe
go func() { for {
select {
case <- doneChan:
break
case resp := <- respondChan:
rText := resp.asText()
err := conn.WriteMessage( ws.TextMessage, []byte(rText) )
if err != nil {
fmt.Printf("Error writing to ws\n")
break
}
}
} }()
// There is only a single websocket connection between a provider and controlfloor
// As a result, all messages sent here ought to be small, because if they aren't
// other messages will be delayed being received and some action started.
go func() {
for {
t, msg, err := conn.ReadMessage()
if err != nil {
fmt.Printf("Error reading from ws\n")
break
}
if t == ws.TextMessage {
//tMsg := string( msg )
b1 := []byte{ msg[0] }
if string(b1) == "{" {
root, _ := uj.Parse( msg )
id := root.Get("id").Int()
mType := root.Get("type").String()
if mType == "ping" {
respondChan <- &CFR_Pong{ id: id, text: "pong" }
} else if mType == "click" {
udid := root.Get("udid").String()
x := root.Get("x").Int()
y := root.Get("y").Int()
go func() {
dev := self.DevTracker.getDevice( udid )
if dev != nil {
dev.clickAt( x, y )
}
} ()
} else if mType == "hardPress" {
udid := root.Get("udid").String()
x := root.Get("x").Int()
y := root.Get("y").Int()
go func() {
dev := self.DevTracker.getDevice( udid )
if dev != nil {
dev.hardPress( x, y )
}
} ()
} else if mType == "longPress" {
udid := root.Get("udid").String()
x := root.Get("x").Int()
y := root.Get("y").Int()
go func() {
dev := self.DevTracker.getDevice( udid )
if dev != nil {
dev.longPress( x, y )
}
} ()
} else if mType == "home" {
udid := root.Get("udid").String()
go func() {
dev := self.DevTracker.getDevice( udid )
if dev != nil {
dev.home()
}
} ()
} else if mType == "swipe" {
udid := root.Get("udid").String()
x1 := root.Get("x1").Int()
y1 := root.Get("y1").Int()
x2 := root.Get("x2").Int()
y2 := root.Get("y2").Int()
go func() {
dev := self.DevTracker.getDevice( udid )
if dev != nil {
dev.swipe( x1, y1, x2, y2 )
}
} ()
} else if mType == "startStream" {
udid := root.Get("udid").String()
fmt.Printf("Got request to start video stream for %s\n", udid )
go func() { self.startVidStream( udid ) }()
} else if mType == "stopStream" {
udid := root.Get("udid").String()
go func() { self.stopVidStream( udid ) }()
}
}
}
}
doneChan <- true
}()
}
func loadCFConfig( configPath string ) (uj.JNode) {
fh, serr := os.Stat( configPath )
if serr != nil {
log.WithFields( log.Fields{
"type": "err_read_config",
"error": serr,
"config_path": configPath,
} ).Fatal(
"Could not read ControlFloor auth token. Have you run `./main register`?",
)
}
configFile := configPath
switch mode := fh.Mode(); {
case mode.IsDir(): configFile = fmt.Sprintf("%s/config.json", configPath)
}
content, err := ioutil.ReadFile( configFile )
if err != nil { log.Fatal( err ) }
root, _, perr := uj.ParseFull( content )
if perr != nil {
log.WithFields( log.Fields{
"error": perr,
} ).Fatal(
"ControlFloor auth token is invalid. Rerun `./main register`",
)
}
return root
}
func writeCFConfig( configPath string, pass string ) {
bytes := []byte(fmt.Sprintf("{pass:\"%s\"}\n",pass))
err := ioutil.WriteFile( configPath, bytes, 0644)
if err != nil {
panic( err )
}
}
func (self *ControlFloor) baseNotify( name string, udid string, vals url.Values ) {
ok := self.checkLogin()
if ok == false {
panic("Could not login when attempting '" + name + "' notify")
}
resp, err := self.client.PostForm( self.base + "/provider/devStatus", vals )
if err != nil {
panic( err )
}
if resp.StatusCode != 200 {
fmt.Printf("Got status %d from '%s' notify\n", resp.StatusCode, name )
} else {
fmt.Printf("Notified control floor of '%s'; uuid=%s\n", name, censorUuid( udid ) )
}
}
func (self *ControlFloor) notifyDeviceInfo( dev *Device ) {
info := dev.info
udid := dev.udid
str := "{"
for key, val := range info {
str = str + fmt.Sprintf("\"%s\":\"%s\",", key, val )
}
str = str + "}"
self.baseNotify("device info", udid, url.Values{
"status": {"info"},
"udid": {udid},
"info": {str},
} )
}
func (self *ControlFloor) notifyDeviceExists( udid string, width int, height int, clickWidth int, clickHeight int ) {
self.baseNotify("device existence", udid, url.Values{
"status": {"exists"},
"udid": {udid},
"width": {strconv.Itoa(width)},
"height": {strconv.Itoa(height)},
"clickWidth": {strconv.Itoa(clickWidth)},
"clickHeight": {strconv.Itoa(clickHeight)},
} )
}
func (self *ControlFloor) notifyProvisionStopped( udid string ) {
self.baseNotify("provision stop", udid, url.Values{
"status": {"provisionStopped"},
"udid": {udid},
} )
}
func (self *ControlFloor) notifyWdaStopped( udid string ) {
self.baseNotify("wda stop", udid, url.Values{
"status": {"wdaStopped"},
"udid": {udid},
} )
}
func (self *ControlFloor) notifyWdaStarted( udid string ) {
self.baseNotify("wda start", udid, url.Values{
"status": {"wdaStarted"},
"udid": {udid},
} )
}
func (self *ControlFloor) checkLogin() (bool) {
self.lock.Lock()
ready := self.ready
self.lock.Unlock()
if ready { return true }
return self.login()
}
func (self *ControlFloor) login() (bool) {
self.lock.Lock()
user := "first"
pass := self.pass
resp, err := self.client.PostForm( self.base + "/provider/login",
url.Values{
"user": {user},
"pass": {pass},
},
)
if err != nil {
var urlError *url.Error
if errors.As( err, &urlError ) {
var netOpError *net.OpError
if errors.As( urlError, &netOpError ) {
rootErr := netOpError.Err
if( rootErr.Error() == "connect: connection refused" ) {
fmt.Printf("Could not connect to ControlFarm; is it running?\n")
} else {
fmt.Printf("Err type:%s - %s\n", reflect.TypeOf(err), err )
fmt.Printf("urlError type:%s - %s\n", reflect.TypeOf(urlError), urlError );
fmt.Printf("netOpError type:%s - %s\n", reflect.TypeOf(netOpError), netOpError )
}
} else {
fmt.Printf("Err type:%s - %s\n", reflect.TypeOf(err), err )
fmt.Printf("urlError type:%s - %s\n", reflect.TypeOf(urlError), urlError );
}
} else {
fmt.Printf("Err type:%s - %s\n", reflect.TypeOf(err), err )
}
panic( err )
}
success := false
if resp.StatusCode != 302 {
success = false
fmt.Printf("StatusCode from controlfloor login:'%d'\n", resp.StatusCode )
} else {
loc, _ := resp.Location()
fmt.Printf("Location from redirect of controlfloor login:'%s'\n", loc )
q := loc.RawQuery
if q != "fail=1" { success = true }
}
if !success {
self.ready = false
self.lock.Unlock()
return false
}
self.ready = true
self.lock.Unlock()
return true
}
func doregister( config *Config ) (string) {
// query cli for registration password
reader := bufio.NewReader( os.Stdin )
fmt.Print("Enter registration password:")
regPass, _ := reader.ReadString('\n')
if regPass == "\n" {
regPass = "doreg"
fmt.Printf("Using default registration password of %s\n", regPass)
}
username := config.cfUsername
// send registration to control floor with id and public key
protocol := "http"
if config.https {
protocol = "https"
}
resp, err := http.PostForm( protocol + "://" + config.cfHost + "/register",
url.Values{
"regPass": {regPass},
"username": {username},
},
)
if err != nil {
panic( err )
}
if resp.Body == nil {
panic("registration respond body is empty")
}
defer resp.Body.Close()
body, readErr := ioutil.ReadAll( resp.Body )
if readErr != nil {
panic( readErr )
}
fmt.Println( string(body) )
root, _ := uj.Parse( body )
sNode := root.Get("Success")
if sNode == nil {
panic( "No Success node in registration result" )
}
success := sNode.Bool()
if !success {
panic("Registration failed")
}
existed := root.Get("Existed").Bool()
pass := root.Get("Password").String()
fmt.Printf("Registered and got password %s\n", pass)
if existed {
fmt.Printf("User %s existed so password was renewed\n", username )
}
writeCFConfig( "cf.json", pass )
return pass
}