使用debian搭建软路由

作者:
淡白
创建时间:
2025-03-15 10:00:00
软路由 debian dae

摘要:本文介绍了如何将一台基于x86架构的j4125多网卡机器配置为Debian 12网关,并安装名为daed的基于eBPF的代理程序。首先,作者开启了IP转发功能并使用systemd-network配置网络接口,将`enp1s0`设置为WAN口,其余网口作为LAN口。接着,配置了NAT和过滤规则的nftables,以便LAN设备能够通过WAN口访问外网。最后,安装了daed并开启其网页控制界面,用户可通过设置密码访问控制页面。

0x00 简介

手里有个j4125多网卡x86机器,之前一直装的是openwrt系列,之前在网上看到有关dae的一个基于Ebpf的代理程序。
先在pve里试了下发现还行,后想着干脆直接就debian+dae算了。

0x01 将debian12作为网关

打开ip转发

编辑 /etc/sysctl.conf 文件:

  1. 使用文本编辑器打开文件:

    vim /etc/sysctl.conf
    
  2. 找到以下行(如果不存在,则添加):

    net.ipv4.ip_forward=1
    
  3. 保存并关闭文件,运行以下命令以应用更改:

    sudo sysctl -p
    

配置网口

我使用systemd-network来配置网络
配置路径在:/etc/systemd/network有如下文件我的设备有4个网口我将enp1s0作为wan口其余为lan

10-wan-enp1s0.network #wan口配置
20-br-lan.netdev
30-br-lan.network
40-lan-eno1.network 
40-lan-enp2s0.network
40-lan-enp4s0.network

下面看看分别里面的内容:

[Match]
Name=enp1s0   # 匹配网络接口名称
 
[Network]
DHCP=yes      # 启用DHCP客户端,从上级路由获取IP地址
[NetDev]
Name=br-lan   # 创建名为br-lan的网络设备
Kind=bridge   # 设备类型为桥接
[Match]
Name=br-lan   # 匹配之前创建的br-lan桥接设备

[Network]
Address=192.168.2.1/24   # 设置桥接设备的IP地址和子网掩码
DHCPServer=yes           # 启用DHCP服务器功能
IPMasquerade=yes         # 启用IP伪装(NAT),允许内网设备通过此路由器访问外网
IPv4Forwarding=yes       # 启用IPv4转发功能

[DHCPServer]
PoolOffset=100           # DHCP地址池起始偏移量,实际分配从192.168.2.100开始
PoolSize=100             # DHCP地址池大小,最多分配100个IP
EmitDNS=yes              # DHCP服务向客户端提供DNS服务器信息
DNS=192.168.2.1          # 设置DHCP客户端使用的DNS服务器地址为路由器自身
[Match]
Name=eno1     # 匹配物理网口eno1
 
[Network]
Bridge=br-lan # 将此网口加入到br-lan桥接设备
[Match]
Name=enp2s0   # 匹配物理网口enp2s0
 
[Network]
Bridge=br-lan # 将此网口加入到br-lan桥接设备
[Match]
Name=enp4s0   # 匹配物理网口enp4s0
 
[Network]
Bridge=br-lan # 将此网口加入到br-lan桥接设备

配置nftables /etc/nftables.conf

#!/usr/sbin/nft -f

# 清空现有规则集,确保配置的干净
flush ruleset

###############################################################################
# 定义 NAT 表
table ip nat {
    # 预路由链(可根据需要进行扩展)
    chain prerouting {
        type nat hook prerouting priority 0; policy accept;
    }

    # 输入链(用于目的地 NAT 等)
    chain input {
        type nat hook input priority 0; policy accept;
    }

    # 输出链
    chain output {
        type nat hook output priority 0; policy accept;
    }

    # 后路由链,用于源地址转换(Masquerade)
    chain postrouting {
        type nat hook postrouting priority 100; policy accept;
        # 对出接口为 enp1s0 的流量进行 Masquerade
        oif "enp1s0" masquerade
    }
}

###############################################################################
# 定义过滤表
table ip filter {
    # 输入链
    chain input {
        type filter hook input priority 0; policy drop;

        # 允许本地回环接口的流量
        iif "lo" accept

        # 允许已建立和相关的连接
        ct state established,related accept

        # 允许来自 LAN 的流量
        iif "br-lan" accept

        # 允许 SSH 访问(如果需要)
        tcp dport 22 accept

        # 其他流量默认拒绝
    }

    # 转发链
    chain forward {
        type filter hook forward priority 0; policy drop;

        # 允许已建立和相关的连接
        ct state established,related accept

        # 允许从 LAN 到 WAN 的转发
        iif "br-lan" oif "enp1s0" accept

    }

    # 输出链
    chain output {
        type filter hook output priority 0; policy accept;
    }
}

###############################################################################

0x02 安装daed

有个带控制页面的daed

# Download
wget -P /tmp https://github.com/daeuniverse/daed/releases/latest/download/installer-daed-linux-$(arch).deb

# install
sudo dpkg -i /tmp/installer-daed-linux-$(arch).deb
rm /tmp/installer-daed-linux-$(arch).deb

# Start daed
sudo systemctl start daed

# enable daed start automatically
sudo systemctl enable daed

使用daed resetpass [password] 设置密码

访问http://192.168.2.1:2023/ 控制页面