一、前言

家庭服务器的第一要务便是帮助全家实现科学上网。

我们分为两部分实现:

第一部分是配置clash提供相关服务;

第二部分是实现端口转发,让服务器可以作为旁路由代理流量。

二、配置clash

1、下载并安装clash

我们从GitHub上提供的官方文件下载。

image-20230305095648845.png

选择相应的版本并复制下载链接

image-20230305095727716.png

在Linux上下载并解压、重命名

wget https://github.com/Dreamacro/clash/releases/download/v1.13.0/clash-linux-amd64-v1.13.0.gz
mv clash-linux-amd64-v1.13.0.gz clash.gz
gunzip clash.gz

image-20230305095915951.png

我们为clash文件加上可执行权限并执行它

chmod +x clash
./clash

按CTRL+Z暂时退出执行,因为我们还没有设置配置文件。

此时会在root目录的用户目录下生成一个./config/clash目录

我们进入这个目录

cd ~/.config/clash

我们会发现此时存在着三个文件

image-20230305100641487.png

如果第三个文件不存在,我们可以从GitHub上下载这个文件。

image-20230305100741293.png

2、对clash进行配置

我们先从机场获取订阅链接,下载。

wget "你的订阅链接" -O config.yaml --no-check-certificate

然后我们对下载下来的配置文件进行编辑。

vim config.yaml

其中我们加入两行,然后允许局域网连接。

external-ui: dashboard
secret: 'cdc1102'
allow-lan: true

第一行让用户知道在使用哪一个外部控制器

第二行为外部控制时的密码验证,防止别人随意要登陆进

第三行允许局域网内的主机连接到本机,是外部控制的必要条件,也可以让局域网内的主机实现http代理。

image-20230305101255069.png

3、配置yacd以外部控制clash

作为服务器我们想要切换节点则需要登陆进shell,通过命令行操控。

这样的方式十分麻烦,所以我们要通过一个外部控制器。

image-20230305104528947.png

wget https://github.com/haishanh/yacd/releases/download/v0.3.8/yacd.tar.xz
tar -xf yacd.tar.xz
mv public dashboard

我们将yacd下载下来并解压、重命名。

然后建立一个apache服务器并关闭防火墙

yum install httpd -y
systemctl stop firewalld
systemctl disable firewalld

我们将yacd放到apache服务器的目录中,启动并设置自启动。

cd dashboard
cp * /var/www/html/ -r
systemctl start httpd
systemctl enable httpd

此时,访问服务器的IP地址加端口、密码即可进入网页管理界面。

image-20230324215200998.png

4、为clash创建服务

通过创建一个服务让clash能够自启,并利于管理。

我们使用systemd的方式去创建服务。

我们先把clash移动到服务目录中

mv clash /usr/local/bin

配置文件

vim /etc/systemd/system/clash.service

在其中添加如下内容

[Unit]
Description=clash service
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/local/bin/clash
Restart=on-failure

[Install]
WantedBy=multi-user.target

启动并自启动

systemctl daemon-reload
systemctl enable clash
systemctl start clash

此时,clash就配置好了。

我们在终端中配置代理

# Proxy auto start
export https_proxy=http://127.0.0.1:7890
export http_proxy=http://127.0.0.1:7890
export all_proxy=socks5://127.0.0.1:7891
# Open proxy
on() {
    export https_proxy=http://127.0.0.1:7890
    export http_proxy=http://127.0.0.1:7890
    export all_proxy=socks5://127.0.0.1:7891
    echo "HTTP/HTTPS Proxy on"
}

# Close proxy
off() {
    unset http_proxy
    unset https_proxy
    unset all_proxy
    echo "HTTP/HTTPS Proxy off"
}

使其生效

source ~/.bashrc

三、配置路由功能

1、安装nftables

我们使用nftable来实现端口转发的功能。

yum install nftables -y
2、配置nftables

首先创建配置文件的目录

mkdir /etc/nftables.conf.d

在其中创建私有地址的定义文件

vim /etc/nftables.conf.d/private.nft

添加如下内容

define private_list = {
    0.0.0.0/8,
    10.0.0.0/8,
    127.0.0.0/8,
    169.254.0.0/16,
    172.16.0.0/12,
    192.168.0.0/16,
    224.0.0.0/4,
    240.0.0.0/4
}

修改nftables的配置文件

vim /etc/nftables.conf
#!/usr/sbin/nft -f

include "/etc/nftables.conf.d/private.nft"

table ip nat {
    chain proxy {
        ip daddr $private_list return
            ip protocol tcp redirect to :7892
    }
    chain prerouting {
        type nat hook prerouting priority 0; policy accept;
        jump proxy
    }
}

清空 nftalbes 规则,并使新规则生效

nft flush ruleset
nft -f /etc/nftables.conf

我们查看一下当前的规则

nft list ruleset

image-20230324220911169.png

最后设置 nftables开机自启动

systemctl start nftables
systemctl enable nftables

我们看一下状态

systemctl status nftables

image-20230324221017496.png

3、将网关配置成服务器的IP地址

此时将网关配置成服务器的IP地址即可实现科学上网。

大功告成!!

标签: none

添加新评论