-
-
Notifications
You must be signed in to change notification settings - Fork 167
/
debugproxy.go
181 lines (154 loc) · 5.18 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
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) error {
pairRecord := ios.ReadPairRecord(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.Fatal("Could not listen on usbmuxd socket, do I have access permissions?", 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)
startProxyConnection(conn, originalSocket, pairRecord, d, info)
}
}
func startProxyConnection(conn net.Conn, originalSocket string, pairRecord ios.PairRecord, debugProxy *DebugProxy, info ConnectionInfo) {
connListeningOnUnixSocket := ios.NewUsbMuxConnection(ios.NewDeviceConnectionWithConn(conn))
connectionToDevice := ios.NewUsbMuxConnection(ios.NewDeviceConnection(originalSocket))
p := ProxyConnection{info.ID, pairRecord, debugProxy, info, log.WithFields(log.Fields{"id": info.ID}), sync.Mutex{}, false}
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 interface{}) {
const outPath = "jsondump-fromdevice.bin"
writeJSON(filepath.Join(p.info.ConnectionPath, outPath), msg)
}
func (p ProxyConnection) logJSONMessageToDevice(msg interface{}) {
const outPath = "jsondump-todevice.bin"
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 {
log.Fatal("Could not write to file, this should not happen", 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()
}