Repository navigation
feat(ios): add MOBILECLI_IOS_AGENT_URL to use an already running agent, including over the Wi-Fi CoreDevice tunnel #504
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
Open
scelza
wants to merge
4
commits into
mobile-next:main
Choose a base branch
from
scelza:feat/ios-agent-url
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c00b5ad
feat(ios): add MOBILECLI_IOS_AGENT_URL to use an already running agent
scelza 8e8ced9
fix(ios): scope MOBILECLI_IOS_AGENT_URL per device, reject unsafe URLs
scelza 5c6a0cb
fix(ios): accept a bare agent URL only with exactly one iOS device
scelza 98a6fdf
fix(ios): accept '=' and ',' in the path of a bare agent URL
scelza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| package devices | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/url" | ||
| "os" | ||
| "strings" | ||
|
|
||
| goios "github.com/danielpaulus/go-ios/ios" | ||
| "github.com/mobile-next/mobilecli/devices/devicekit" | ||
| ) | ||
|
|
||
| // iosAgentURLEnv points real iOS devices at a DeviceKit agent that is already | ||
| // running and reachable at that URL (e.g. "http://localhost:8100", or the Xcode | ||
| // CoreDevice tunnel address of a Wi-Fi paired device such as | ||
| // "http://[fd34:13d9:5468::1]:12004"). When set, mobilecli neither launches the | ||
| // agent nor starts a tunnel or port forward to reach it. Calls that do not go | ||
| // through the agent (apps, logs, crashes) are unchanged. | ||
| // | ||
| // The value is either a comma-separated list of <udid>=<url> pairs, where each | ||
| // device uses only its own entry and a device without one takes the normal | ||
| // tunnel path, or a bare URL, which is accepted only while exactly one real iOS | ||
| // device is connected. The agent cannot confirm which device it runs on | ||
| // (device.info returns only the screen size and scale), so the device is bound | ||
| // to the agent by this mapping alone. | ||
| const iosAgentURLEnv = "MOBILECLI_IOS_AGENT_URL" | ||
|
|
||
| // countConnectedIOSDevices returns how many distinct real iOS devices usbmuxd | ||
| // reports. It only asks usbmuxd for its list, without opening a lockdown | ||
| // session per device as ListIOSDevices does. Tests replace it. | ||
| var countConnectedIOSDevices = func() (int, error) { | ||
| deviceList, err := goios.ListDevices() | ||
| if err != nil { | ||
| return 0, fmt.Errorf("failed getting device list: %w", err) | ||
| } | ||
|
|
||
| // go-ios returns one entry per connection (usb, network), count by udid | ||
| seen := make(map[string]bool) | ||
| for _, deviceEntry := range deviceList.DeviceList { | ||
| seen[deviceEntry.Properties.SerialNumber] = true | ||
| } | ||
| return len(seen), nil | ||
| } | ||
|
|
||
| // iosAgentURLOverride returns the agent URL that iosAgentURLEnv sets for the | ||
| // device with this udid, or "" when the device should take the normal path. | ||
| func iosAgentURLOverride(udid string) (string, error) { | ||
| value := strings.TrimSpace(os.Getenv(iosAgentURLEnv)) | ||
| if value == "" { | ||
| return "", nil | ||
| } | ||
|
|
||
| // a bare URL may have '=' or ',' in its path, so classify it before | ||
| // treating those characters as mapping separators | ||
| if isBareAgentURL(value) || !strings.ContainsAny(value, "=,") { | ||
| return bareAgentURL(value) | ||
| } | ||
|
|
||
| agentURLs, err := parseAgentURLMapping(value) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return agentURLs[strings.ToUpper(udid)], nil | ||
| } | ||
|
|
||
| // isBareAgentURL reports whether value parses as a single http(s) URL with a | ||
| // host. A <udid>=<url> mapping never does: '=' cannot appear in a URL scheme. | ||
| func isBareAgentURL(value string) bool { | ||
| parsed, err := url.Parse(value) | ||
| return err == nil && (parsed.Scheme == "http" || parsed.Scheme == "https") && parsed.Host != "" | ||
| } | ||
|
|
||
| // bareAgentURL accepts a URL without a udid only when it cannot reach the | ||
| // wrong device, that is with a single real iOS device connected. | ||
| func bareAgentURL(value string) (string, error) { | ||
| agentURL, err := parseAgentURL(value) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| count, err := countConnectedIOSDevices() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| if count != 1 { | ||
| return "", fmt.Errorf("%s holds a single URL, which needs exactly one connected iOS device but found %d, use <udid>=<url> to say which device runs the agent", iosAgentURLEnv, count) | ||
| } | ||
|
|
||
| return agentURL, nil | ||
| } | ||
|
|
||
| // parseAgentURLMapping parses "<udid>=<url>,<udid>=<url>" into URLs keyed by | ||
| // upper-case udid. | ||
| func parseAgentURLMapping(value string) (map[string]string, error) { | ||
| agentURLs := make(map[string]string) | ||
| for _, entry := range strings.Split(value, ",") { | ||
| udid, rawURL, found := strings.Cut(strings.TrimSpace(entry), "=") | ||
| udid = strings.TrimSpace(udid) | ||
| rawURL = strings.TrimSpace(rawURL) | ||
| if !found || udid == "" || rawURL == "" || strings.ContainsAny(udid, ":/") { | ||
| return nil, fmt.Errorf("%s has a malformed entry %q, expected <udid>=<url> such as 00008101-00161CEC3CDB001E=http://localhost:8100 (agent URLs cannot have a query)", iosAgentURLEnv, entry) | ||
| } | ||
|
|
||
| key := strings.ToUpper(udid) | ||
| if _, duplicate := agentURLs[key]; duplicate { | ||
| return nil, fmt.Errorf("%s lists device %s more than once", iosAgentURLEnv, udid) | ||
| } | ||
|
|
||
| agentURL, err := parseAgentURL(rawURL) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| agentURLs[key] = agentURL | ||
| } | ||
| return agentURLs, nil | ||
| } | ||
|
|
||
| // parseAgentURL checks that value can serve as the base of agent requests, | ||
| // which append /health, /rpc and /mjpeg to it. | ||
| func parseAgentURL(value string) (string, error) { | ||
| parsed, err := url.Parse(value) | ||
| if err != nil || (parsed.Scheme != "http" && parsed.Scheme != "https") || parsed.Host == "" { | ||
| return "", fmt.Errorf("%s=%q is not a valid agent URL, expected something like http://localhost:8100", iosAgentURLEnv, value) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| if parsed.RawQuery != "" || parsed.ForceQuery || parsed.Fragment != "" || strings.Contains(value, "#") { | ||
| return "", fmt.Errorf("%s=%q is not a valid agent URL, it cannot have a query or fragment", iosAgentURLEnv, value) | ||
| } | ||
| // the agent has no authentication, so credentials would only travel in the clear | ||
| if parsed.User != nil { | ||
| return "", fmt.Errorf("%s=%q is not a valid agent URL, it cannot carry a user name or password", iosAgentURLEnv, value) | ||
| } | ||
|
|
||
| return strings.TrimSuffix(value, "/"), nil | ||
| } | ||
|
|
||
| // useExternalAgent switches the device to the agent at agentURL and checks that | ||
| // it answers. It never launches the agent or sets up a tunnel or forward. | ||
| func (d *IOSDevice) useExternalAgent(agentURL string) error { | ||
| client := devicekit.NewDeviceKitClient(agentURL) | ||
| if _, err := client.GetStatus(); err != nil { | ||
| return fmt.Errorf("no agent answering at %s (from %s): %w", agentURL, iosAgentURLEnv, err) | ||
| } | ||
|
|
||
| d.mu.Lock() | ||
| d.deviceKitClient = client | ||
| d.mu.Unlock() | ||
|
|
||
| return nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
Check that the external agent belongs to this
IOSDevice.If two devices are available and the URL points to device A,
StartAgentalso binds anIOSDevicefor device B to A./healthestablishes liveness, not a match withd.Udid. Commands selected for B can therefore act on A. Check agent identity before storing the client, or require an explicit URL mapping by device ID.🤖 Prompt for AI Agents