最近调研 flutter,甚是大爱。

虽然 flutter 非常友好提供了针对中国用户的镜像(Using Flutter in China · flutter/flutter Wiki · GitHub),解决了一些不可说的难题,但偶尔还是会遇到类似的问题。比如我今天就遇到了,在 VS Code 调试编译的时候,遇到 “Download Failed” 的问题。Could not resolve all files for configuration ':image_picker:lintClassPath'. 具体原因是 Connect to d29vzk4ow07wi7.cloudfront.net:443 [d29vzk4ow07wi7.cloudfront.net/13.33.69.104, d29vzk4ow07wi7.cloudfront.net/13.33.69.3, d29vzk4ow07wi7.cloudfront.net/13.33.69.38, d29vzk4ow07wi7.cloudfront.net/13.33.69.111] failed: Read timed out

原来是碰上了墙外面的老朋友,CDN 服务商 cloudflare 的域名。

中间还是比较曲折,最后解决的办法是用 proxychain4 解决问题。

brew install proxychains-ng
sed -i '.bak' 's@^\(socks4.*\)@#\1@g' /usr/local/etc/proxychains.conf
echo -e "socks5\t127.0.0.1 1080" >> /usr/local/etc/proxychains.conf

proxychains4 curl ip.gs

等等,好像不对,IP 是本市的。

$ proxychains4 curl ip.gs
[proxychains] config file found: /usr/local/etc/proxychains.conf
[proxychains] preloading /usr/local/Cellar/proxychains-ng/4.13/lib/libproxychains4.dylib
Current IP / 当前 IP: 118.XXX.XXX.XXX
ISP / 运营商:  ChinaTelecom
City / 城市: Chengdu Sichuan
Country / 国家: China
IP.GS is now IP.SB, please visit https://ip.sb/ for more information. / IP.GS 已更改为 IP.SB ,请访问 https://ip.sb/ 获取更详细 IP 信息!
Please join Telegram group https://t.me/sbfans if you have any issues. / 如有问题,请加入 Telegram 群 https://t.me/sbfans

  /\_/\
=( °w° )=
  )   (  //
 (__ __)//

哪里不对?

原来 MacOS X 10.11 以后的版本,还需要禁用 SIP,proxychains 才能正常工作。因为它会修改可执行文件本身,而位于 /usr/bin 下面的 curl 并不能被修改。见这里——Proxychains 是不是不支持 10.11 了? - V2EX

我不想禁用 SIP。安全方面,我还是比较强迫症的。所以用了另外一种方式,就是把可执行文件,从受系统保护的目录里面移出来。

cd /Volumes/RamDisk
cp /usr/bin/curl ./
proxychains4 ./curl ip.gs

这下终于行了。

$ proxychains4 ./curl ip.gs
[proxychains] config file found: /usr/local/etc/proxychains.conf
[proxychains] preloading /usr/local/Cellar/proxychains-ng/4.13/lib/libproxychains4.dylib
Current IP / 当前 IP: XXX.XX.XXX.XX
ISP / 运营商:  xxxxx
City / 城市: Osaka Osaka
Country / 国家: Japan
IP.GS is now IP.SB, please visit https://ip.sb/ for more information. / IP.GS 已更改为 IP.SB ,请访问 https://ip.sb/ 获取更详细 IP 信息!
Please join Telegram group https://t.me/sbfans if you have any issues. / 如有问题,请加入 Telegram 群 https://t.me/sbfans

  /\_/\
=( °w° )=
  )   (  //
 (__ __)//

用在 flutter 上面试试看。

$ proxychains4 flutter run
[proxychains] config file found: /usr/local/etc/proxychains.conf
[proxychains] preloading /usr/local/Cellar/proxychains-ng/4.13/lib/libproxychains4.dylib
Launching lib/main.dart on BLN AL10 in debug mode...
Initializing gradle...                                       0.8s
Resolving dependencies...                                    0.8s
Running 'gradlew assembleDebug'...

FAILURE: Build failed with an exception.

* What went wrong:
Could not resolve all files for configuration ':image_picker:lintClassPath'.

............

BUILD FAILED in 2m 3s
123.9s
Gradle build failed: 1

还是不行?

考虑到上面 curl 遇到的问题,确认 flutter 可执行文件权限和路径没问题,试试用绝对路径。

$ proxychains4 ~/workspace/flutter_development/flutter/bin/flutter run
[proxychains] config file found: /usr/local/etc/proxychains.conf
[proxychains] preloading /usr/local/Cellar/proxychains-ng/4.13/lib/libproxychains4.dylib
Launching lib/main.dart on BLN AL10 in debug mode...
Initializing gradle...                                       0.8s
Resolving dependencies...                                    0.8s
Running 'gradlew assembleDebug'...                          11.8s
Built build/app/outputs/apk/debug/app-debug.apk.
Installing build/app/outputs/apk/app.apk...                  3.8s
^C

欧耶!终于成了。

PS:^C 是我终止了 flutter build 的过程。

看得出来,proxychains 通过独特的方式来让 command 程序走代理。虽然不是很方便,但总归解决了问题。还是值得点赞。