Skip to content

Commit

Permalink
Automatically connect to os TUN device without constructing a device …
Browse files Browse the repository at this point in the history
…entry
  • Loading branch information
diegoperini committed Jul 23, 2024
1 parent 6cc0b7c commit 33fd69b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ios/deviceinfo/deviceinfo_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestGetDisplayInfos(t *testing.T) {
t.Skipf("GO_IOS_ADDRESS missing")
}

rsdService, err := ios.NewWithAddrPort(address, rsdPort)
rsdService, err := ios.NewWithAddrPortDevice(address, rsdPort)
require.NoError(t, err)

defer rsdService.Close()
Expand Down
2 changes: 1 addition & 1 deletion ios/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func checkEntry(ctx context.Context, device DeviceEntry, interfaceName string, e

func tryHandshake(ip6 net.IP, port int, interfaceName string, device DeviceEntry, result chan<- string) {
addr := fmt.Sprintf("%s%%%s", ip6.String(), interfaceName)
s, err := NewWithAddrPort(addr, port, device)
s, err := NewWithAddrPortDevice(addr, port, device)
udid := device.Properties.SerialNumber
if err != nil {
slog.Error("failed to connect to remote service discovery", "error", err, "address", addr)
Expand Down
31 changes: 23 additions & 8 deletions ios/rsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
"net"
"strconv"

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

// NewWithAddr creates a new RsdService with the given address and port 58783 using a HTTP2 based XPC connection.
func NewWithAddr(addr string, d DeviceEntry) (RsdService, error) {
return NewWithAddrPort(addr, port, d)
// NewWithAddrPort creates a new RsdService with the given address and port 58783 using a HTTP2 based XPC connection,
// connecting to an operating system level TUN device.
func NewWithAddrPort(addr string, port int) (RsdService, error) {
conn, err := connectTUN(addr, port)
if err != nil {
return RsdService{}, fmt.Errorf("NewWithAddrPort: failed to connect to device: %w", err)
}
return newRsdServiceFromTcpConn(conn)
}

// NewWithAddrDevice creates a new RsdService with the given address and port 58783 using a HTTP2 based XPC connection.
func NewWithAddrDevice(addr string, d DeviceEntry) (RsdService, error) {
return NewWithAddrPortDevice(addr, port, d)
}

// NewWithAddrPort creates a new RsdService with the given address and port using a HTTP2 based XPC connection.
func NewWithAddrPort(addr string, port int, d DeviceEntry) (RsdService, error) {
// NewWithAddrPortDevice creates a new RsdService with the given address and port using a HTTP2 based XPC connection.
func NewWithAddrPortDevice(addr string, port int, d DeviceEntry) (RsdService, error) {
conn, err := ConnectTUNDevice(addr, port, d)
if err != nil {
return RsdService{}, fmt.Errorf("NewWithAddrPort: failed to connect to device: %w", err)
return RsdService{}, fmt.Errorf("NewWithAddrPortTUNDevice: failed to connect to device: %w", err)
}
return newRsdServiceFromTcpConn(conn)
}

func newRsdServiceFromTcpConn(conn *net.TCPConn) (RsdService, error) {
h, err := http.NewHttpConnection(conn)
if err != nil {
return RsdService{}, fmt.Errorf("NewWithAddrPort: failed to connect to http2: %w", err)
return RsdService{}, fmt.Errorf("newRsdServiceFromTcpConn: failed to connect to http2: %w", err)
}

x, err := CreateXpcConnection(h)
if err != nil {
return RsdService{}, fmt.Errorf("NewWithAddrPort: failed to create xpc connection: %w", err)
return RsdService{}, fmt.Errorf("newRsdServiceFromTcpConn: failed to create xpc connection: %w", err)
}

return RsdService{
Expand Down
2 changes: 1 addition & 1 deletion ios/tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func ManualPairAndConnectToTunnel(ctx context.Context, device ios.DeviceEntry, p
}

func getUntrustedTunnelServicePort(addr string, device ios.DeviceEntry) (int, error) {
rsdService, err := ios.NewWithAddr(addr, device)
rsdService, err := ios.NewWithAddrDevice(addr, device)
if err != nil {
return 0, fmt.Errorf("getUntrustedTunnelServicePort: failed to connect to RSD service: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2052,7 +2052,7 @@ func startTunnel(ctx context.Context, recordsPath string, tunnelInfoPort int, us
}

func deviceWithRsdProvider(device ios.DeviceEntry, udid string, address string, rsdPort int) ios.DeviceEntry {
rsdService, err := ios.NewWithAddrPort(address, rsdPort, device)
rsdService, err := ios.NewWithAddrPortDevice(address, rsdPort, device)
exitIfError("could not connect to RSD", err)
defer rsdService.Close()
rsdProvider, err := rsdService.Handshake()
Expand Down

0 comments on commit 33fd69b

Please sign in to comment.