-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
debugproxy.go
218 lines (189 loc) · 6.63 KB
/
debugproxy.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
package debugproxy
import (
"encoding/json"
"fmt"
"io"
"net"
"os"
"path/filepath"
"sync"
"time"
ios "github.com/danielpaulus/go-ios/ios"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
)
const connectionJSONFileName = "connections.json"
//DebugProxy can be used to dump and modify communication between mac and host
type DebugProxy struct {
mux sync.Mutex
serviceList []PhoneServiceInformation
connectionCounter int
WorkingDir string
}
//PhoneServiceInformation contains info about a service started on the phone via lockdown.
type PhoneServiceInformation struct {
ServicePort uint16
ServiceName string
UseSSL bool
}
//ProxyConnection keeps track of the pairRecord and uses an ID to identify connections.
type ProxyConnection struct {
id string
pairRecord ios.PairRecord
debugProxy *DebugProxy
info ConnectionInfo
log *logrus.Entry
mux sync.Mutex
closed bool
}
type ConnectionInfo struct {
ConnectionPath string
CreatedAt time.Time
ID string
}
func (p *ProxyConnection) LogClosed() {
p.mux.Lock()
defer p.mux.Unlock()
if p.closed {
return
}
p.closed = true
p.log.Trace("Connection closed")
}
func (d *DebugProxy) storeServiceInformation(serviceInfo PhoneServiceInformation) {
d.mux.Lock()
defer d.mux.Unlock()
d.serviceList = append(d.serviceList, serviceInfo)
}
func (d *DebugProxy) retrieveServiceInfoByPort(port uint16) (PhoneServiceInformation, error) {
d.mux.Lock()
defer d.mux.Unlock()
for _, element := range d.serviceList {
if element.ServicePort == port {
return element, nil
}
}
return PhoneServiceInformation{}, fmt.Errorf("No Service found for port %d", port)
}
//NewDebugProxy creates a new Default proxy
func NewDebugProxy() *DebugProxy {
return &DebugProxy{mux: sync.Mutex{}, serviceList: []PhoneServiceInformation{}}
}
//Launch moves the original /var/run/usbmuxd to /var/run/usbmuxd.real and starts the server at /var/run/usbmuxd
func (d *DebugProxy) Launch(device ios.DeviceEntry, binaryMode bool) error {
if binaryMode {
log.Info("Lauching proxy in full binary mode")
}
var pairRecord ios.PairRecord
if !binaryMode {
var err error
pairRecord, err = ios.ReadPairRecord(device.Properties.SerialNumber)
if err != nil {
return err
}
log.Infof("Successfully retrieved pairrecord: %s for device %s", pairRecord.HostID, device.Properties.SerialNumber)
}
originalSocket, err := MoveSock(ios.DefaultUsbmuxdSocket)
if err != nil {
log.WithFields(log.Fields{"error": err, "socket": ios.DefaultUsbmuxdSocket}).Error("Unable to move, lacking permissions?")
return err
}
d.setupDirectory()
listener, err := net.Listen("unix", ios.DefaultUsbmuxdSocket)
if err != nil {
log.Error("Could not listen on usbmuxd socket, do I have access permissions?", err)
return err
}
if err := os.Chmod(ios.DefaultUsbmuxdSocket, 0777); err != nil {
log.Error("Could not change permission on usbmuxd socket", err)
return err
}
for {
conn, err := listener.Accept()
if err != nil {
log.Errorf("error with connection: %e", err)
}
d.connectionCounter++
id := fmt.Sprintf("#%d", d.connectionCounter)
connectionPath := filepath.Join(".", d.WorkingDir, "connection-"+id+"-"+time.Now().UTC().Format("2006.01.02-15.04.05.000"))
os.MkdirAll(connectionPath, os.ModePerm)
info := ConnectionInfo{ConnectionPath: connectionPath, CreatedAt: time.Now(), ID: id}
d.addConnectionInfoToJsonFile(info)
bindumpHostProxyFile := filepath.Join(connectionPath, "bindump-hostservice-to-proxy.txt")
if !binaryMode {
//if the proxy is in full binary mode, there is no point in creating another binary dump
log.Infof("Creating binary dump of all communication between MAC OS and debugproxy at: %s", bindumpHostProxyFile)
conn = NewDumpingConn(bindumpHostProxyFile, conn)
}
startProxyConnection(conn, originalSocket, pairRecord, d, info, binaryMode)
}
}
func startProxyConnection(conn net.Conn, originalSocket string, pairRecord ios.PairRecord, debugProxy *DebugProxy, info ConnectionInfo, binaryMode bool) {
devConn, err := ios.NewDeviceConnection(originalSocket)
if err != nil {
log.Error(err)
return
}
logger := log.WithFields(log.Fields{"id": info.ID})
p := ProxyConnection{info.ID, pairRecord, debugProxy, info, logger, sync.Mutex{}, false}
if binaryMode {
binOnUnixSocket := BinaryForwardingProxy{ios.NewDeviceConnectionWithConn(conn), NewBinDumpOnly("does not matter", filepath.Join(info.ConnectionPath, "rawbindump-from-host-service.bin"), logger)}
binToDevice := BinaryForwardingProxy{devConn, NewBinDumpOnly("does not matter", filepath.Join(info.ConnectionPath, "rawbindump-from-device.bin"), logger)}
go proxyBinDumpConnection(&p, binOnUnixSocket, binToDevice)
return
}
connListeningOnUnixSocket := ios.NewUsbMuxConnection(ios.NewDeviceConnectionWithConn(conn))
connectionToDevice := ios.NewUsbMuxConnection(devConn)
go proxyUsbMuxConnection(&p, connListeningOnUnixSocket, connectionToDevice)
}
//Close moves /var/run/usbmuxd.real back to /var/run/usbmuxd and disconnects all active proxy connections
func (d *DebugProxy) Close() {
log.Info("Moving back original socket")
err := MoveBack(ios.DefaultUsbmuxdSocket)
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("Failed moving back socket")
}
}
func (d *DebugProxy) setupDirectory() {
newpath := filepath.Join(".", "dump-"+time.Now().UTC().Format("2006.01.02-15.04.05.000"))
d.WorkingDir = newpath
os.MkdirAll(newpath, os.ModePerm)
}
func (d DebugProxy) addConnectionInfoToJsonFile(connInfo ConnectionInfo) {
file, err := os.OpenFile(filepath.Join(d.WorkingDir, connectionJSONFileName),
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
data, err := json.Marshal(connInfo)
if err != nil {
log.Printf("Failed json:%s", err)
}
file.Write(data)
io.WriteString(file, "\n")
file.Close()
}
func (p ProxyConnection) logJSONMessageFromDevice(msg map[string]interface{}) {
const outPath = "jsondump.json"
msg["direction"] = "device->host"
writeJSON(filepath.Join(p.info.ConnectionPath, outPath), msg)
}
func (p ProxyConnection) logJSONMessageToDevice(msg map[string]interface{}) {
const outPath = "jsondump.json"
msg["direction"] = "host->device"
writeJSON(filepath.Join(p.info.ConnectionPath, outPath), msg)
}
func writeJSON(filePath string, JSON interface{}) {
file, err := os.OpenFile(filePath,
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(fmt.Sprintf("Could not write to file err: %v filepath:'%s'", err, filePath))
}
jsonmsg, err := json.Marshal(JSON)
if err != nil {
log.Warnf("Error encoding '%s' to json: %s", JSON, err)
}
file.Write(jsonmsg)
io.WriteString(file, "\n")
file.Close()
}