-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
165 lines (145 loc) · 3.63 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
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/danielpaulus/go-ios/usbmux"
"github.com/danielpaulus/quicktime_video_hack/screencapture"
log "github.com/sirupsen/logrus"
"net/http"
"os"
"os/signal"
"strings"
)
type detailsEntry struct {
Udid string
ProductName string
ProductType string
ProductVersion string
}
func main() {
log.SetLevel(log.DebugLevel)
addr := "127.0.0.1:8080"
if len(os.Args) > 1 {
addr = os.Args[1]
}
startWebSocketServer(addr)
}
func startWebSocketServer(addr string) {
log.Println("Starting WebSocket server")
stopSignal := make(chan interface{})
stopHub := make(chan interface{})
shutdown := make(chan interface{})
waitForSigInt(stopSignal)
hub := newHub()
go hub.run(stopHub)
m := http.NewServeMux()
s := http.Server{Addr: addr, Handler: m}
m.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
serveWs(hub, w, r)
})
go func() {
err := s.ListenAndServe()
if err != nil {
log.Info("s.ListenAndServe(): ", err)
}
stopHub <- nil
<-stopHub
shutdown <- nil
}()
<-stopSignal
err := s.Shutdown(context.TODO())
if err != nil {
log.Error(err)
} else {
log.Info("No error on shutdown")
}
<-shutdown
log.Info("Program finished")
}
func getValues(device usbmux.DeviceEntry) usbmux.GetAllValuesResponse {
muxConnection := usbmux.NewUsbMuxConnection()
defer muxConnection.Close()
pairRecord := muxConnection.ReadPair(device.Properties.SerialNumber)
lockdownConnection, err := muxConnection.ConnectLockdown(device.DeviceID)
if err != nil {
log.Fatal(err)
}
lockdownConnection.StartSession(pairRecord)
allValues := lockdownConnection.GetValues()
lockdownConnection.StopSession()
return allValues
}
func screenCaptureDevices() []byte {
deviceList, err := screencapture.FindIosDevices()
if err != nil {
log.Fatalf("Error finding iOS Devices, error: %s", err)
}
result := make([]detailsEntry, len(deviceList))
for i, device := range deviceList {
udid := strings.Trim(device.SerialNumber, "\x00")
if len(udid) == 24 {
udid = fmt.Sprintf("%s-%s", udid[0:8], udid[8:])
}
result[i] = detailsEntry{
Udid: udid,
ProductName: device.ProductName,
ProductType: "",
ProductVersion: "",
}
}
text, err := json.Marshal(result)
if err != nil {
log.Fatalf("Broken json serialization, error: %s", err)
}
return text
}
// This command is for testing if we can enable the hidden Quicktime device config
func activate(udid string) []byte {
device, err := screencapture.FindIosDevice(udid)
if err != nil {
return toErrJSON(err, "no device found to activate")
}
log.Debugf("Enabling device: %v", device)
device, err = screencapture.EnableQTConfig(device)
if err != nil {
return toErrJSON(err, "Error enabling QT config")
}
return toJSON(map[string]interface{}{
"device_activated": device.DetailsMap(),
})
}
func formatUdid(udid string) (string, error) {
if len(udid) == 40 {
return udid, nil
}
if len(udid) == 25 {
return strings.Replace(udid, "-", "", 1), nil
}
return udid, fmt.Errorf("Invalid udid: %s", udid)
}
func waitForSigInt(stopSignalChannel chan interface{}) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for sig := range c {
log.Debugf("Signal received: %s", sig)
var stopSignal interface{}
stopSignalChannel <- stopSignal
}
}()
}
func toErrJSON(err error, msg string) []byte {
log.Debug(msg, err)
return toJSON(map[string]interface{}{
"original_error": err.Error(),
"error_message": msg,
})
}
func toJSON(output interface{}) []byte {
text, err := json.Marshal(output)
if err != nil {
log.Fatalf("Broken json serialization, error: %s", err)
}
return text
}