myfreax

如何配置 dart/flutter pub 代理和镜像

dart默认是使用pub.dev作为pub包管理器的仓库。当你从dart的包仓库获取一个包时,你可能会遇到一直在等待下载的状态

5 min read
By myfreax
如何配置 dart/flutter pub 代理和镜像
如何配置 dart/flutter pub 代理和镜像

Dart 默认是使用 pub.dev 作为 pub 包管理器的仓库。当你从Dart的包仓库获取一个包时,你可能会遇到一直在等待下载的状态。

如果你发布一个包也会是同样的情况。在这种情况下你可以使用第三方镜像源作为pub 包管理器的仓库。

在本教程中,将说明如何配置 Dart 包管理器 pub 代理以及配置第三方镜像源作为pub的镜像。

包括使用 V2ray 作为 pub 代理,配置 Shaodowsocks 作为 pub 代理,Linux/macOS/Windows 配置 pub 代理,使用清华大学镜像配置 pub 第三方镜像源。

前提条件

如果你需要为Dart或者 Flutter 配置代理,你必须在你的系统 Shadowsocks/V2ray。这里就不详细说明 Shadowsocks/V2ray 安装过程。

v2ray 作为 pub 代理

如果你使用的是 Qv2ray 作为客户端。默认情况 Qv2ray 会创建 HTTP 的入站方式并监听端口 8889

Qv2ray 还会配置系统代理和设置环境变量 https_proxy 等。你可以在终端运行 env 命令并通过 grep 命令过滤输出来验证 https_proxy环境变量是否正确设置。

env | grep http
HTTPS_PROXY=http://127.0.0.1:8889/
https_proxy=http://127.0.0.1:8889/
HTTP_PROXY=http://127.0.0.1:8889/
http_proxy=http://127.0.0.1:8889/
ftp_proxy=http://127.0.0.1:8889/
FTP_PROXY=http://127.0.0.1:8889/

Shaodowsocks 作为 pub 代理

Polipo 是一个Web缓存代理,它支持从 Sock5 协议中获取返回数据,而且还支持其它类型的科学上网工具作为后端。

并且可以缓存数据来加速网页的打开速度。我们需要使用它来转换 Shadowsocks 的Sock5 协议为 HTTP 代理。为了完成这个步骤,必须先安装 Polipo 软件。

如果你的计算机运行的是基于 Debian 的 Linux 发行版,例如 Ubuntu,Linux mint。请运行 apt 命令 sudo apt-get install polipo -y 安装 Polipo。

如果你的计算机运行的是基于 Redhat 的 Linux 发行版,例如 CentOS,Fedora。请运行 yum 命令 sudo yum install polipo -y 安装 Polipo。

安装 Polipo

sudo apt-get install polipo -y
sudo yum install polipo -y

配置 Polipo

以下命令以 root 用户启动一个Shell的子进程执行 echo 命令 echo "socksParentProxy = localhost:1080" >> /etc/polipo/config

将会追加行 socksParentProxy = localhost:1080 到文件 /etc/polipo/config 。配置 Polipo 使用 Shadowsocks 作为后端获取数据的方式。

sudo sh -c 'echo "socksParentProxy = localhost:1080" >> /etc/polipo/config'
1080 是 shadowsocks 默认使用端口,如你已更改 Shadowsocks 自行修改端口

然后重启polipo服务,以配置生效:

sudo service polipo restart

验证 Polipo 配置

正常情况下,朝里的网络是不能访问 Google.com 的,因此我们使用 Curl 获取www.google.com 页面,即可验证是否使用代理。输出将打印google的搜索页面的源码。

http_proxy=http://localhost:8123 curl www.google.com

Linux/macOS配置pub代理

export https_proxy=localhost:8123

Windows CMD配置pub代理

export https_proxy=localhost:8123

Windows PowerShell配置pub代理

 $Env:https_proxy="hostname:port"

如果你为你代理设置了验证保护,请使用以下方式配置 pub 代理。在不同系统配置设置了验证保护的代理。与上述各系统命令类似,只需要简单更改即可:

export https_proxy=username:password@hostname:port

持久化 pub 代理设置

上述步骤 pub 代理设置的环境变量都是仅在当前会话中可用,一旦退出后,就需要重新设置。

可以将环境变量写入 Shell 启动配置文件中,例如全局的 Shell 配置文件 /etc/profile,基于用户的配置文件 ~/.bashrc~/.zshrc 等。

Linux / MacOS

如果你是 Linux / MacOS 用户,请运行 echo 命令并使用重定向操作符 >> 追加行export https_proxy=hostname:port 到 ~/.bashrc 文件。

如果你使用的是 zsh,请将 ~/.bashrc 改为 ~/.zshrc。要将新添加的环境变量加载到当前 Shell 会话,请运行 source 命令:

 echo "export https_proxy=hostname:port" >> ~/.bashrc
 source ~/.bashrc

Windows

我的电脑右击鼠标,在弹出菜单中选择 属性。在系统界面选择 高级系统设置,打开 系统属性,从而进行系统属性配置。

系统属性 界面,选择 高级,在高级界面选择 环境变量,即可打开变量配置界面。可以添加基于用户的环境变量或者系统环境变量,你随意选择一种即可。

配置 pub 镜像源

这里我们将使用清华大学的 Dart 镜像作为 pub 的第三方镜像源。如果你不想使用清华大学镜像,只需要替换命令行 URL 即可。

Linux / macOS 配置 Pub 镜像源

追加行 export PUB_HOSTED_URL="https://mirrors.tuna.tsinghua.edu.cn/dart-pub/ 到~/.bashrc 文件。

然后运行 source 命令加载环境变量 PUB_HOSTED_URL ,在当前 Shell会话可用。运行以下命令以设置环境变量 PUB_HOSTED_URL

echo 'export PUB_HOSTED_URL="https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"' >> ~/.bashrc

source ~/.bashrc

Windows CMD 配置 pub 镜像源

set PUB_HOSTED_URL="https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"

Windows PowerShell 配置 pub 镜像源

 $Env:PUB_HOSTED_URL="https://mirrors.tuna.tsinghua.edu.cn/dart-pub/"

结论

至此,你已经知道如何在Window / Linux / MacOS配置 pub 代理的环境变量。如你有任何疑问,请在下面进行反馈。

Related Articles