由于 GFW 的存在,在使用或下载国外相关软件时,通常需要使用代理。代理软件中常见的代理模式是系统代理,即在系统层面设置代理,将系统中所有的网络流量通过指定的代理服务器进行转发。在设置系统代理之后,通常所有软件都会通过代理访问网络。然而,如果使用 Linux 系统并在终端进行操作,终端通常不会通过系统代理访问网络。这种情况下,通常需要使用 tun 模式
。除了 tun 模式
,还可以通过环境变量设置终端代理,或者使用 proxychains-ng
。
tun模式会在系统中创建一个虚拟的网络接口,将系统中所有的网络请求都走该虚拟网络接口
设置终端环境代理#
设置终端环境代理,通常可以分为两种情况。一是直接在 shell 中执行对应的设置命令,在当前终端不关闭的情况下,环境代理会一直生效;二是将对应的环境变量写入到对应的 shell 文件中,这样即使终端关闭后重新打开终端,仍然可以使用代理。我推荐的方式是第二种。
由于我使用的shell是fish
,因此需要在~/.config/fish/config.fish
中写入一下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
| # 设置代理
function proxy
set -gx http_proxy "http://127.0.0.1:port"
set -gx https_proxy "http://127.0.0.1:port"
echo "✅ Proxy started"
end
# 取消代理
function unproxy
set -e http_proxy
set -e https_proxy
echo "❌ Proxy stopped"
end
|
bash/zsh
的写法如下:
1
2
3
4
5
6
7
8
9
10
11
| function proxy {
export http_proxy="http://127.0.0.1:port"
export https_proxy="http://127.0.0.1:port"
echo "✅ Proxy started"
}
function unproxy {
unset http_proxy
unset https_proxy
echo "❌ Proxy stopped"
}
|
bash
的配置文件通常在~/.bashrc
,zsh
的配置文件通常在~/zshrc
安装proxychains-ng#
安装命令如下:
1
| sudo pacman -S proxychains-ng
|
在安装完proxychains-ng
之后,通常需要在/etc/proxychains.conf
中设置代理对应的路径和端口,设置内容如下:
1
2
| socks5 127.0.0.1 port
http 127.0.0.1 port
|
yay使用proxychains-ng出现的问题#
proxychains-ng
的原理是通过一个动态链接库(LD_PRELOAD
)劫持网络调用工具,强制应用程序通过指定的代理进行网络访问。yay
是Go语言编写的一个程序,由于go语言的特性,导致Go语言在编译时会静态链接其标准库(包括net
库),这就导致在网络调用的时候不会通过动态链接库的方式进行,从而在yay
使用proxychains-ng
会出现网络问题。
解决方案:
1
2
| # 下载gcc-go,使用gcc-go替换go来重新编译yay
yay -S gcc-go
|
编译yay有两种方式,方式一:
1
2
| # 在下载gcc-go之后,重新下载yay,并通过添加参数的方式重新编译yay
yay -S yay --rebuild
|
方式二:
1
2
3
4
| # 克隆AUR中yay仓库,之后使用 makepkg 重新编译 yay:
git clone https://aur.archlinux.org/yay.git
cd yay
makepkg -si
|
测试终端代理是否生效#
设置终端环境代理可以通过以下命令查询环境代理是否设置正确。
1
2
3
4
5
| # 查询环境代理的命令
env | grep -i proxy
# 正常结果,如出现错误则为空
http_proxy=http://127.0.0.1:port
https_proxy=http://127.0.0.1:port
|
终端环境代理测试代理是否生效:
1
2
| # 在执行proxy方法之后,可使用该命令测试
curl -I https://www.google.com
|
proxychains-ng
测试代理是否生效:
1
| proxychains curl -I https://www.google.com
|
正确测试解决如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| curl -I https://www.google.com
HTTP/1.1 200 Connection established
HTTP/2 200
content-type: text/html; charset=ISO-8859-1
content-security-policy-report-only: object-src 'none';base-uri 'self';script-src 'nonce-CEM
Jfgk-QlEcSWxpqHWFBg' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: h
ttp:;report-uri https://csp.withgoogle.com/csp/gws/other-hp
accept-ch: Sec-CH-Prefers-Color-Scheme
p3p: CP="This is not a P3P policy! See g.co/p3phelp for more info."
date: Thu, 27 Feb 2025 12:34:14 GMT
server: gws
x-xss-protection: 0
x-frame-options: SAMEORIGIN
expires: Thu, 27 Feb 2025 12:34:14 GMT
cache-control: private
set-cookie: AEC=AVcja2cm7HsWHO8EalaFr76QfZHlawHX8Q5jqvkHmCBtJ4ZYpYsAFuiEBA; expires=Tue, 26-
Aug-2025 12:34:14 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax
set-cookie: NID=522=ESYsfQ_Q-ezQQSlz4TBjAW1ni7HtuuMEUphgmtw4ViwFobUuv7sOIuUgEWBWCbIX6eLcOuUc
cIzh3mKsEInI0go5rL-UJcdy2V_I4mrN6zcK7stCwMml2xAOBMjtF0Dc-CjP1eQW1Ft3mcGBy5lwsspMZTaoJbasewZC
U4IA6Ix4j8Lz9IdyxXfF4dzRR3ZOUgpMmaUrXw; expires=Fri, 29-Aug-2025 12:34:14 GMT; path=/; domai
n=.google.com; HttpOnly
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
|