“Headed” Chrome 测试
最近我一直在使用 Chrome 和 WebUSB 与实现 usbmuxd 协议的 Apple iPhone/iPad 通信。 在此过程中,开发者的使用体验和测试变得非常重要,而普通网站的“无头”测试不可行,因为 WebUSB 依赖完整的浏览器功能。 更重要的是,我希望以 Chromebook 作为理想的测试平台来使用 WebUSB。 我最终选择使用 NPM 上的 jsdom 和 webusb 进行轻量本地测试,并通过 Chrome DevTools 协议远程驱动测试到 Pixelbook Slate。
Big Scary - Much Danger
因为本指南会教你如何在 ChromeOS 中禁用安全保护,并且这会使浏览器通过(虽然使用的是相对安全的 SSH 配置)远程可访问,我不建议使用真实的 Google 账户登录此 Chrome 设备,也不要将其用作除测试设备之外的任何用途。 在重新用于其他目的之前,请将 ChromeOS 配置恢复到非开发者版本。 已警告。
进入开发者模式和调试功能
Chrome 设备可以进入“开发者模式”,这是一种允许运行已被修改、因而不再处于最安全配置的操作系统的设置。 具体步骤因设备而异,我发现直接参考 Google / Chromium 的说明更为简便(这里 https://chromium.googlesource.com/chromiumos/docs/+/master/developer_mode.md#dev-mode)。 进入开发者模式后,在设置过程的第一屏选择“启用调试功能”,并设置密码(https://www.chromium.org/chromium-os/how-tos-and-troubleshooting/debugging-features)。 我还注意到更新有时会破坏读写文件系统并重置 root / chronos 密码。 你可以尝试默认的 test0000,如果不行,执行 powerwash 并重新启用调试功能。
配置安全的 SSH 连接
现在设备已经相当于 Android 设备的“root”状态(拥有可用的 root 账户和可写文件系统),是时候设置远程 SSH 服务器了。 开启开发者模式并启用调试功能的设备默认已经运行 sshd,但使用密码认证。如果你在相对私密的网络中并且可以接受基于密码的认证,可以跳过此步骤。
你可以在与设备的 SSH 连接、本地 VT-2 终端([ Ctrl ] [ Alt ] [ → ])或 crosh shell 提示符([ Ctrl ] [ Alt ] [ T ])中执行以下步骤。
下面的示例仅从我的 GitHub 账户下载我的 SSH 公钥,你也可以在浏览器中下载公钥或通过 U 盘将其拷贝到设备上。
# Enable Pubkey authentication
$ echo "\nPubkeyAuthentication yes" >> /etc/ssh/sshd_config
# Disable Challenge-Response authentication
$ echo "\nChallengeResponseAuthentication no" >> /etc/ssh/sshd_config
# The following is an example of downloading all SSH keys for a GitHub user, replace `rickmark` with your username.
$ curl https://github.com/rickmark.keys > /root/.ssh/authorized_keys
# The folowing is an example of copying a SSH key downloaded in the browser with the file name `id_rsa.pub`
$ cp /home/chronos/user/Downloads/id_rsa.pub /root/.ssh/authorized_keys
# Next we set the ownership and mode of authorized_keys
$ chown root:root /root/.ssh/authorized_keys
$ chmod 600 /root/.ssh/authorized_keys
# Restart the SSH server to apply
$ initctl stop openssh-server & initctl start openssh-server
启用 Chrome DevTools 远程调试
现在我们已经配置好对设备的 SSH,剩余的步骤可以通过 SSH 完成。在此步骤中,我们向 Chrome 进程启动命令添加一个额外的标志。由于在 ChromeOS 设备上浏览器 就是 shell,我们实际上是在更改 OS 相当于 KDM / GDM 的配置。
# Append the parameter to the chrome process arguments
$ echo "--remote-debugging-port=1337" >> /etc/chrome_dev.conf
# Restart the device for the settings to take effect
$ shutdown -r now
为 Chrome 运行隧道
设备配置完成后,运行以下命令使用 SSH 在 Puppeteer 主机与 ChromeOS 设备之间创建加密隧道。 该命令必须保持运行状态,直到 Puppeteer 使用完毕,并且每次想要使用 ChromeOS 设备与 Puppeteer 时都需要启动它。 请将
$ ssh -L 1337:localhost:1337 root@<chromedevice>
从 Puppeteer Core 连接
下面是一个在远程 Chrome 机器上使用 puppeteer-core 的简单测试。 在运行前请确保已通过 yarn 或 npm 安装 puppeteer-core。 我们使用 puppeteer core 是因为它不会随模块下载 Chrome 副本,但两者都能正常工作。
import * as puppeteer from 'puppeteer-core'
const browser = await puppeteer.connect({browserURL: 'http://localhost:1337'})
const page = await browser.newPage()
await page.goto('https://google.com')
// If you do the following, the entire shell restarts (remember the browser is the OS)
await browser.close()
附加:在 ChromeOS 上使用 WebUSB 与 Apple 移动设备
近期的 ChromeOS 版本已内置 mtpd(媒体传输协议代理),以便 ChromeOS 设备复制照片,而 iPhone 实现了该协议,导致后台代理可能占用设备,从而在浏览器中通过 WebUSB 驱动设备时出现“访问被拒绝”错误。 同时 Linux 内核本身也可能将设备识别为 iPhone 绑定设备。我已提交 bug,期望在文件浏览器中提供 UI 以“弹出” MTP 设备,但目前请按照以下步骤在已启用调试功能的 ChromeOS 设备上禁用这些功能,以便在不受 ChromeOS 干扰的情况下使用设备:
# Blacklist the "iPhone Ethernet" linux kernel module
$ echo "blacklist ipheth" >> /etc/modprobe.d/blacklist.conf
# Move the 'mtpd' service to the home folder so it wont be started with the operating system. Move it back to restore
$ mv /etc/init/mtpd.conf ~/
# Restart to apply changes
$ shutdown -r now