Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Added a buffered reader for dtx connections #435

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions ios/deviceconnection.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ios

import (
"bufio"
"crypto/tls"
"encoding/binary"
"io"
Expand All @@ -11,6 +12,8 @@ import (
log "github.com/sirupsen/logrus"
)

const readerBufferSize = 40 * 1024

// DeviceConnectionInterface contains a physical network connection to a usbmuxd socket.
type DeviceConnectionInterface interface {
Close() error
Expand All @@ -31,11 +34,12 @@ type DeviceConnectionInterface interface {
type DeviceConnection struct {
c net.Conn
unencryptedConn net.Conn
bufferedReader io.Reader
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's the right place to add a buffered reader. We have two connections to the device already in this struct. If we add buffering now to one of them, the buffer may hold more data than expected and switching between those sessions could break (I think this is mainly happening on older iOS versions)

If you want to add buffering to the DTX connection as the title says, it would be safer to do so in the DTX connection itself as nothing about the connection would change at that point anymore.

Copy link
Collaborator Author

@diegoperini diegoperini Jul 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I understand.

That would mean I may have to define a new type that implements net.Conn, wraps a real net.Conn and uses a buffered reader internally for the Read() operations. The rest of the operations would be delegated to the wrapped, original net.Conn instance.

MOSTY IMPORTANTLY, this should happen BEFORE we construct a DeviceConnection object.

Correct?

}

// Read reads incoming data from the connection to the device
func (conn *DeviceConnection) Read(p []byte) (n int, err error) {
return conn.c.Read(p)
return conn.bufferedReader.Read(p)
}

// Write writes data on the connection to the device
Expand All @@ -51,7 +55,8 @@ func NewDeviceConnection(socketToConnectTo string) (*DeviceConnection, error) {

// NewDeviceConnectionWithConn create a DeviceConnection with a already connected network conn.
func NewDeviceConnectionWithConn(conn net.Conn) *DeviceConnection {
return &DeviceConnection{c: conn}
bufferedReader := bufio.NewReaderSize(conn, readerBufferSize)
return &DeviceConnection{c: conn, bufferedReader: bufferedReader}
}

// ConnectToSocketAddress connects to the USB multiplexer with a specified socket addres
Expand All @@ -66,6 +71,7 @@ func (conn *DeviceConnection) connectToSocketAddress(socketAddress string) error
}
log.Tracef("Opening connection: %v", &c)
conn.c = c
conn.bufferedReader = bufio.NewReaderSize(c, readerBufferSize)
return nil
}

Expand Down Expand Up @@ -117,6 +123,7 @@ func (conn *DeviceConnection) DisableSessionSSL() {
}
// Use the underlying conn again to receive unencrypted bytes
conn.c = conn.unencryptedConn
conn.bufferedReader = bufio.NewReaderSize(conn.c, readerBufferSize)
// tls.Conn.CloseWrite() sets the writeDeadline to now, which will cause
// all writes to timeout immediately, for this hacky workaround
// we need to undo that
Expand Down Expand Up @@ -155,6 +162,7 @@ func (conn *DeviceConnection) EnableSessionSslServerMode(pairRecord PairRecord)

conn.unencryptedConn = conn.c
conn.c = net.Conn(tlsConn)
conn.bufferedReader = bufio.NewReaderSize(conn.c, readerBufferSize)
return nil
}

Expand All @@ -173,6 +181,7 @@ func (conn *DeviceConnection) EnableSessionSsl(pairRecord PairRecord) error {
}
conn.unencryptedConn = conn.c
conn.c = net.Conn(tlsConn)
conn.bufferedReader = bufio.NewReaderSize(conn.c, readerBufferSize)
return nil
}

Expand Down
Loading