From 497b36a0c4e78328f246d697f247c1865d6861b1 Mon Sep 17 00:00:00 2001 From: danielpaulus Date: Sun, 20 Nov 2022 09:03:50 +0100 Subject: [PATCH] fail if more than one device is connected, add panic handlers to move back socket (#206) I finally got that in golang to properly handle panics, you have to add the panic handler in every goroutine. I added that so dproxy will now always fix and move back the broken socket even if it fails with a panic. Also, dproxy currently does not work with multiple devices. I made it fail with an error in case more than one device is connected. --- ios/debugproxy/binforward.go | 22 ++++++++++++++++++++++ ios/debugproxy/debugproxy.go | 4 ++++ ios/debugproxy/muxhandler.go | 11 +++++++++++ 3 files changed, 37 insertions(+) diff --git a/ios/debugproxy/binforward.go b/ios/debugproxy/binforward.go index 9291ddd1..63d7f569 100644 --- a/ios/debugproxy/binforward.go +++ b/ios/debugproxy/binforward.go @@ -94,6 +94,17 @@ func handleConnectToService(connectRequest ios.UsbMuxMessage, } func proxyBinDumpConnection(p *ProxyConnection, binOnUnixSocket BinaryForwardingProxy, binToDevice BinaryForwardingProxy) { + defer func() { + log.Println("done") // Println executes normally even if there is a panic + if x := recover(); x != nil { + log.Printf("run time panic, moving back socket %v", x) + err := MoveBack(ios.DefaultUsbmuxdSocket) + if err != nil { + log.WithFields(log.Fields{"error": err}).Error("Failed moving back socket") + } + panic(x) + } + }() go proxyBinFromDeviceToHost(p, binOnUnixSocket, binToDevice) for { bytes, err := binOnUnixSocket.ReadMessage() @@ -114,6 +125,17 @@ func proxyBinDumpConnection(p *ProxyConnection, binOnUnixSocket BinaryForwarding } func proxyBinFromDeviceToHost(p *ProxyConnection, binOnUnixSocket BinaryForwardingProxy, binToDevice BinaryForwardingProxy) { + defer func() { + log.Println("done") // Println executes normally even if there is a panic + if x := recover(); x != nil { + log.Printf("run time panic, moving back socket %v", x) + err := MoveBack(ios.DefaultUsbmuxdSocket) + if err != nil { + log.WithFields(log.Fields{"error": err}).Error("Failed moving back socket") + } + panic(x) + } + }() for { bytes, err := binToDevice.ReadMessage() binToDevice.decoder.decode(bytes) diff --git a/ios/debugproxy/debugproxy.go b/ios/debugproxy/debugproxy.go index 1d5fe74e..02886e4f 100644 --- a/ios/debugproxy/debugproxy.go +++ b/ios/debugproxy/debugproxy.go @@ -84,6 +84,10 @@ func NewDebugProxy() *DebugProxy { //Launch moves the original /var/run/usbmuxd to /var/run/usbmuxd.real and starts the server at /var/run/usbmuxd func (d *DebugProxy) Launch(device ios.DeviceEntry, binaryMode bool) error { + list, _ := ios.ListDevices() + if len(list.DeviceList) > 1 { + return fmt.Errorf("dproxy currently does not work when more than one device is connected to the host. please disconnect all but one device.") + } if binaryMode { log.Info("Lauching proxy in full binary mode") } diff --git a/ios/debugproxy/muxhandler.go b/ios/debugproxy/muxhandler.go index 19050b96..eecbe14a 100644 --- a/ios/debugproxy/muxhandler.go +++ b/ios/debugproxy/muxhandler.go @@ -11,6 +11,17 @@ import ( ) func proxyUsbMuxConnection(p *ProxyConnection, muxOnUnixSocket *ios.UsbMuxConnection, muxToDevice *ios.UsbMuxConnection) { + defer func() { + log.Println("done") // Println executes normally even if there is a panic + if x := recover(); x != nil { + log.Printf("run time panic, moving back socket %v", x) + err := MoveBack(ios.DefaultUsbmuxdSocket) + if err != nil { + log.WithFields(log.Fields{"error": err}).Error("Failed moving back socket") + } + panic(x) + } + }() for { request, err := muxOnUnixSocket.ReadMessage() if err != nil {