-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
usbmuxconnection.go
executable file
·177 lines (156 loc) · 5.38 KB
/
usbmuxconnection.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
package ios
import (
"encoding/binary"
"fmt"
"io"
"os"
"reflect"
"runtime"
"strings"
log "github.com/sirupsen/logrus"
)
func GetSocketTypeAndAddress(socketAddress string) (string, string) {
chunks := strings.Split(socketAddress, "://")
if len(chunks) != 2 {
panic("Needs scheme://address")
}
return chunks[0], chunks[1]
}
func ToUnixSocketPath(socketAddress string) string {
scheme, name := GetSocketTypeAndAddress(socketAddress)
if scheme != "unix" {
panic("Needs a unix socket")
}
return name
}
// GetUsbmuxdSocket this is the default socket address for the platform to connect to.
func GetUsbmuxdSocket() string {
socket_override := os.Getenv("USBMUXD_SOCKET_ADDRESS")
if socket_override != "" {
if strings.Contains(socket_override, ":") {
return "tcp://" + socket_override
} else {
return "unix://" + socket_override
}
}
switch runtime.GOOS {
case "windows":
return "tcp://127.0.0.1:27015"
default:
return "unix:///var/run/usbmuxd"
}
}
// UsbMuxConnection can send and read messages to the usbmuxd process to manage pairrecors, listen for device changes
// and connect to services on the phone. Usually messages follow a request-response pattern. there is a tag integer
// in the message header, that is increased with every sent message.
type UsbMuxConnection struct {
// tag will be incremented for every message, so responses can be correlated to requests
tag uint32
deviceConn DeviceConnectionInterface
}
// NewUsbMuxConnection creates a new UsbMuxConnection from an already initialized DeviceConnectionInterface
func NewUsbMuxConnection(deviceConn DeviceConnectionInterface) *UsbMuxConnection {
muxConn := &UsbMuxConnection{tag: 0, deviceConn: deviceConn}
return muxConn
}
// NewUsbMuxConnectionSimple creates a new UsbMuxConnection with a connection to /var/run/usbmuxd
func NewUsbMuxConnectionSimple() (*UsbMuxConnection, error) {
deviceConn, err := NewDeviceConnection(GetUsbmuxdSocket())
muxConn := &UsbMuxConnection{tag: 0, deviceConn: deviceConn}
return muxConn, err
}
// ReleaseDeviceConnection dereferences this UsbMuxConnection from the underlying DeviceConnection and it returns the DeviceConnection for later use.
// This UsbMuxConnection cannot be used after calling this.
func (muxConn *UsbMuxConnection) ReleaseDeviceConnection() DeviceConnectionInterface {
conn := muxConn.deviceConn
muxConn.deviceConn = nil
return conn
}
// Close calls close on the underlying DeviceConnection
func (muxConn *UsbMuxConnection) Close() error {
return muxConn.deviceConn.Close()
}
// UsbMuxMessage contains header and payload for a message to usbmux
type UsbMuxMessage struct {
Header UsbMuxHeader
Payload []byte
}
// UsbMuxHeader contains the header for plist messages for the usbmux daemon.
type UsbMuxHeader struct {
Length uint32
Version uint32
Request uint32
Tag uint32
}
// Send sends and encodes a Plist using the usbmux Encoder. Increases the connection tag by one.
func (muxConn *UsbMuxConnection) Send(msg interface{}) error {
if muxConn.deviceConn == nil {
return io.EOF
}
writer := muxConn.deviceConn.Writer()
muxConn.tag++
err := muxConn.encode(msg, writer)
if err != nil {
log.Error("Error sending mux")
return err
}
return nil
}
// SendMuxMessage serializes and sends a UsbMuxMessage to the underlying DeviceConnection.
// This does not increase the tag on the connection. Is used mainly by the debug proxy to
// forward messages between device and host
func (muxConn *UsbMuxConnection) SendMuxMessage(msg UsbMuxMessage) error {
if muxConn.deviceConn == nil {
return io.EOF
}
writer := muxConn.deviceConn.Writer()
err := binary.Write(writer, binary.LittleEndian, msg.Header)
if err != nil {
return err
}
_, err = writer.Write(msg.Payload)
return err
}
// ReadMessage blocks until the next muxMessage is available on the underlying DeviceConnection and returns it.
func (muxConn *UsbMuxConnection) ReadMessage() (UsbMuxMessage, error) {
if muxConn.deviceConn == nil {
return UsbMuxMessage{}, io.EOF
}
reader := muxConn.deviceConn.Reader()
msg, err := muxConn.decode(reader)
if err != nil {
return UsbMuxMessage{}, err
}
return msg, nil
}
// encode serializes a MuxMessage struct to a Plist and writes it to the io.Writer.
func (muxConn *UsbMuxConnection) encode(message interface{}, writer io.Writer) error {
log.Tracef("UsbMux send %v on %v", reflect.TypeOf(message), &muxConn.deviceConn)
mbytes := ToPlistBytes(message)
err := writeHeader(len(mbytes), muxConn.tag, writer)
if err != nil {
return err
}
_, err = writer.Write(mbytes)
return err
}
func writeHeader(length int, tag uint32, writer io.Writer) error {
header := UsbMuxHeader{Length: 16 + uint32(length), Request: 8, Version: 1, Tag: tag}
return binary.Write(writer, binary.LittleEndian, header)
}
// decode reads all bytes for the next MuxMessage from r io.Reader and
// returns a UsbMuxMessage
func (muxConn UsbMuxConnection) decode(r io.Reader) (UsbMuxMessage, error) {
var muxHeader UsbMuxHeader
err := binary.Read(r, binary.LittleEndian, &muxHeader)
if err != nil {
return UsbMuxMessage{}, err
}
payloadBytes := make([]byte, muxHeader.Length-16)
n, err := io.ReadFull(r, payloadBytes)
if err != nil {
return UsbMuxMessage{}, fmt.Errorf("Error '%s' while reading usbmux package. Only %d bytes received instead of %d", err.Error(), n, muxHeader.Length-16)
}
log.Tracef("UsbMux Receive on %v", &muxConn.deviceConn)
return UsbMuxMessage{muxHeader, payloadBytes}, nil
}