git 使用squid设置http代理

### 起因

windows下的git很不舒服--莫名其妙的报错,莫名其妙的连接问题。现在clone github上的代码一直使用的是 wsl下的git,奈何速度太慢(除了开始几秒,最后速度固定在4kib/s);并且在clone大项目的时候经常超时/报错;所以要安装代理

安装主机是ubuntu 18 lts
### 安装squid
安装命令:
```
apt-get update
apt-get install squid
vi /etc/squid/squid.conf  #编辑配置文件
```
将`http_port 3128`加入配置文件或更改,3128为squid的端口号(这行应该是默认配置)
将`http_access deny all`更改为`http_access allow all`
重启squid服务 `systemctl restart squid`
启用squid服务 `systemctl enable squid`  (这行可能不是必要的)
检查squid运行状态 `systemctl status squid` 输出
`Active: active (running) since Mon 2020-09-07 23:46:11 CST; 15h ago`
 表示状态正常
测试squid服务`curl -x http://127.0.0.1:3128  -L https://www.baidu.com` 返回百度的首页

安装squid完成

### 添加登陆验证
使用密码工具,需要安装`apt-get install apache2-utils`
(另一篇post中使用的是httpd-tools,在ubuntu中没有--可能是我的包管理器的问题,所以使用的是`apache2-utils`)
在squid.conf所在目录下建文件passwd
执行命令,设置账号密码
```
chown proxy: /etc/squid/passwd
htpasswd /etc/squid/passwd username #回车后会要求输入密码和确认密码
```
更改配置文件,在squid.conf中添加以下内容
```
# 这几个配置在网上copy的,/usr/lib/squid/basic_ncsa_auth路径不对,启动报错
# 现在这个路径是我服务器上的路径
auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/passwd
auth_param basic children 5
auth_param basic realm Squid Basic Authentication
auth_param basic credentialsttl 2 hours
acl auth_users proxy_auth REQUIRED
http_access allow auth_users
```

重启squid服务`systemctl restart squid`,确认squid的状态。
一切正常后确认代理功能`curl -x http://127.0.0.1:3128 --proxy-user username:password  -L https://www.baidu.com`,能返回百度页面表示正常。
使用`curl -x http://127.0.0.1:3128  -L https://www.baidu.com`确认代理认证生效,会提示需要账号

### 代理的使用
安装代理的目的是提高github的下载速度,本地windows抽风,使用telnet 连不上squid的端口,使用其他服务器确认squid服务是正常的。

安装windows的子系统 wsl或者wsl2,安装git 配置git的代理。
之后克隆github的资源使用子系统的git
```
#设置代理 其中,如果密码中有@字符,使用%40代替
git config --global http.proxy http://proxyUsername:proxyPassword@proxy.server.com:port
#查看代理
git config --global http.proxy
# 删除代理
git config --global --unset
```

### 结果

使用http.proxy后clone项目速度可以达到 100kib/s(测试的clone spring-boot项目,可能是受限于服务器的带宽吧)

Receiving objects: 100% (601632/601632), 122.19 MiB | 109.00 KiB/s, done.


##参考资料
squid安装
https://phoenixnap.com/kb/setup-install-squid-proxy-server-ubuntu
https://devopscube.com/setup-and-configure-proxy-server/
git配置http.proxy
https://gist.github.com/evantoli/f8c23a37eb3558ab8765
一些问题 http://stackoverflow.com/

原文地址:https://www.cnblogs.com/peng18/p/13632915.html