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 (#439)

* Automatically connect to os TUN device without constructing a device entry
  • Loading branch information
diegoperini authored Aug 20, 2024
1 parent d8bb278 commit bc47af1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
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 @@ -159,25 +160,39 @@ func (r RsdHandshakeResponse) GetServices() map[string]RsdServiceEntry {
return r.Services
}

// 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 @@ -2109,7 +2109,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 bc47af1

Please sign in to comment.