Skip to content

Commit 33fd69b

Browse files
committed
Automatically connect to os TUN device without constructing a device entry
1 parent 6cc0b7c commit 33fd69b

File tree

5 files changed

+27
-12
lines changed

5 files changed

+27
-12
lines changed

ios/deviceinfo/deviceinfo_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestGetDisplayInfos(t *testing.T) {
2020
t.Skipf("GO_IOS_ADDRESS missing")
2121
}
2222

23-
rsdService, err := ios.NewWithAddrPort(address, rsdPort)
23+
rsdService, err := ios.NewWithAddrPortDevice(address, rsdPort)
2424
require.NoError(t, err)
2525

2626
defer rsdService.Close()

ios/discover.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func checkEntry(ctx context.Context, device DeviceEntry, interfaceName string, e
6565

6666
func tryHandshake(ip6 net.IP, port int, interfaceName string, device DeviceEntry, result chan<- string) {
6767
addr := fmt.Sprintf("%s%%%s", ip6.String(), interfaceName)
68-
s, err := NewWithAddrPort(addr, port, device)
68+
s, err := NewWithAddrPortDevice(addr, port, device)
6969
udid := device.Properties.SerialNumber
7070
if err != nil {
7171
slog.Error("failed to connect to remote service discovery", "error", err, "address", addr)

ios/rsd.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"io"
7+
"net"
78
"strconv"
89

910
"github.com/danielpaulus/go-ios/ios/http"
@@ -139,25 +140,39 @@ func (r RsdHandshakeResponse) GetPort(service string) int {
139140
return 0
140141
}
141142

142-
// NewWithAddr creates a new RsdService with the given address and port 58783 using a HTTP2 based XPC connection.
143-
func NewWithAddr(addr string, d DeviceEntry) (RsdService, error) {
144-
return NewWithAddrPort(addr, port, d)
143+
// NewWithAddrPort creates a new RsdService with the given address and port 58783 using a HTTP2 based XPC connection,
144+
// connecting to an operating system level TUN device.
145+
func NewWithAddrPort(addr string, port int) (RsdService, error) {
146+
conn, err := connectTUN(addr, port)
147+
if err != nil {
148+
return RsdService{}, fmt.Errorf("NewWithAddrPort: failed to connect to device: %w", err)
149+
}
150+
return newRsdServiceFromTcpConn(conn)
151+
}
152+
153+
// NewWithAddrDevice creates a new RsdService with the given address and port 58783 using a HTTP2 based XPC connection.
154+
func NewWithAddrDevice(addr string, d DeviceEntry) (RsdService, error) {
155+
return NewWithAddrPortDevice(addr, port, d)
145156
}
146157

147-
// NewWithAddrPort creates a new RsdService with the given address and port using a HTTP2 based XPC connection.
148-
func NewWithAddrPort(addr string, port int, d DeviceEntry) (RsdService, error) {
158+
// NewWithAddrPortDevice creates a new RsdService with the given address and port using a HTTP2 based XPC connection.
159+
func NewWithAddrPortDevice(addr string, port int, d DeviceEntry) (RsdService, error) {
149160
conn, err := ConnectTUNDevice(addr, port, d)
150161
if err != nil {
151-
return RsdService{}, fmt.Errorf("NewWithAddrPort: failed to connect to device: %w", err)
162+
return RsdService{}, fmt.Errorf("NewWithAddrPortTUNDevice: failed to connect to device: %w", err)
152163
}
164+
return newRsdServiceFromTcpConn(conn)
165+
}
166+
167+
func newRsdServiceFromTcpConn(conn *net.TCPConn) (RsdService, error) {
153168
h, err := http.NewHttpConnection(conn)
154169
if err != nil {
155-
return RsdService{}, fmt.Errorf("NewWithAddrPort: failed to connect to http2: %w", err)
170+
return RsdService{}, fmt.Errorf("newRsdServiceFromTcpConn: failed to connect to http2: %w", err)
156171
}
157172

158173
x, err := CreateXpcConnection(h)
159174
if err != nil {
160-
return RsdService{}, fmt.Errorf("NewWithAddrPort: failed to create xpc connection: %w", err)
175+
return RsdService{}, fmt.Errorf("newRsdServiceFromTcpConn: failed to create xpc connection: %w", err)
161176
}
162177

163178
return RsdService{

ios/tunnel/tunnel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func ManualPairAndConnectToTunnel(ctx context.Context, device ios.DeviceEntry, p
8989
}
9090

9191
func getUntrustedTunnelServicePort(addr string, device ios.DeviceEntry) (int, error) {
92-
rsdService, err := ios.NewWithAddr(addr, device)
92+
rsdService, err := ios.NewWithAddrDevice(addr, device)
9393
if err != nil {
9494
return 0, fmt.Errorf("getUntrustedTunnelServicePort: failed to connect to RSD service: %w", err)
9595
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2052,7 +2052,7 @@ func startTunnel(ctx context.Context, recordsPath string, tunnelInfoPort int, us
20522052
}
20532053

20542054
func deviceWithRsdProvider(device ios.DeviceEntry, udid string, address string, rsdPort int) ios.DeviceEntry {
2055-
rsdService, err := ios.NewWithAddrPort(address, rsdPort, device)
2055+
rsdService, err := ios.NewWithAddrPortDevice(address, rsdPort, device)
20562056
exitIfError("could not connect to RSD", err)
20572057
defer rsdService.Close()
20582058
rsdProvider, err := rsdService.Handshake()

0 commit comments

Comments
 (0)