# 查看config projector config list Checking for updates ... done. 1. Android_Studio
# 编辑配置 projector config edit Android_Studio Checking for updates ... done. Edit configuration Android_Studio Enter the path to IDE (<enter> for /home/jzj/apps/android-studio/, <tab> for complete): Use separate configuration directory for this config? [y/N] Enter a Projector listening port (press ENTER for default) [9999]: # 注意这里需要自定义listen的地址,建议直接填0.0.0.0,监听所有地址来的请求 Would you like to specify listening address (or host) for Projector? [y/N]y Enter a Projector listening address (press ENTER for default) [0.0.0.0]: 0.0.0.0 Would you like to specify hostname for Projector access? [y/N]N # 这里要选择使用secure connection,也就是https,如果用http,每次复制粘贴浏览器都会弹窗,只能通过弹窗复制里面的内容,巨麻烦 Use secure connection (this option requires installing a projector's certificate to browser)? [y/N]y Would you like to set password for connection? [y/N]N 1. tested 2. not_tested Choose update channel or 0 to keep current(unknown): [0-2]: 1 done.
startProjectorClient() { local HOST=$1 echo"Start projector client" local APP_PATH="$HOME/Applications/Edge Apps.localized/Projector Web Client.app" if [ -d "$APP_PATH" ]; then open "$APP_PATH" return fi
# Projector must be ruuning with https, otherwise copy paste will be blocked by browser and unconvenient open "https://$HOST:9999" }
setMachineAuto() { local IP=$(getSuggestedMachineIp) echo"Use '$IP' as develop machine" setHostToIp 'develop'$IP }
getSuggestedMachineIp() { for IP in"192.168.5.100""10.0.0.123"; do if checkPing $IP; then echo$IP return fi done }
checkPing() { local target=$1 if ping -c 1 -W 1 "$target" > /dev/null 2>&1; then return 0 else return 1 fi }
refreshDNSCache() { echo"Refresh DNS Cache..." if [[ "$OSTYPE" == "darwin"* ]]; then sudo dscacheutil -flushcache sudo killall -HUP mDNSResponder else sudo systemctl restart nscd 2>/dev/null || echo"No nscd service. Need to refresh DNS manually." fi }
# set host to ip mapping in /etc/hosts setHostToIp() { local host="$1" local ip="$2"
if [[ -z "$ip" || -z "$host" ]]; then echo"Usage: setHostToIp <HOST> <IP>" return 1 fi
if grep -q -E "^$ip\s+$host(\s|$)" /etc/hosts; then echo"Mapping already exists: ($(grep "$host" /etc/hosts))" return 0 fi
echo"Old mapping: ($(grep "$host" /etc/hosts))"
sudo cp /etc/hosts /etc/hosts.bak
# check if host exists if grep -q -E "^[^#]*\s+$host(\s|$)" /etc/hosts; then echo" Set mapping: '$host -> $ip'" if [[ "$OSTYPE" == "darwin"* ]]; then sudo sed -i ''"/$host/ s/.*/$ip\t$host/g" /etc/hosts else sudo sed -i "s/^[^#]*\s\+$host.*/$ip$host/" /etc/hosts fi grep "$host" /etc/hosts else echo" Add mapping: '$host -> $ip'" echo"$ip$host" | sudo tee -a /etc/hosts > /dev/null grep "$host" /etc/hosts fi
echo"New mapping: ($(grep "$host" /etc/hosts))"
echo"Remove ssh known_hosts for '$host'..." ssh-keygen -R $host
refreshDNSCache
echo"Mapping done: '$host -> $ip'" }
生成和安装Self-Signed Certificate
这么做又会遇到新的问题,浏览器再次报Certificate错误。
借助ChatGPT查了一番发现了问题所在,原来Certificate里是指定了域名的,而develop这个域名不在Projector自动生成的cert里,所以浏览器报错了。Cert里的这个东西叫做SAN(Subject Alternative Name)。
Subject Alternative Name Missing The certificate for this site does not contain a Subject Alternative Name extension containing a domain name or IP address.
Certificate Error There are issues with the site's certificate chain (net::ERR_CERT_COMMON_NAME_INVALID).
运行完就会生成证书cert.pem 和 私钥key.pem,运行下面的命令可以查看cert的信息:
1
openssl x509 -in cert.pem -noout -text
如果cert没问题,应该可以看到SAN如下:
1 2 3 4
X509v3 extensions: X509v3 Subject Alternative Name: DNS:develop, DNS:projector, DNS:localhost, IP Address:192.168.8.100, IP Address:192.168.5.211, IP Address:127.0.0.1
实际在浏览器中,也可以看到一个网站的证书详情,点击URL左边的锁,点Connection is secure,再点里面的证书图标就可以了,知道了这个可以更方便的排查问题。比如Google的证书: