静看光阴荏苒
不管不顾不问不说也不念

使用Debian9自己打造一个旁路由

前言

还用个锤子固件哟,越用越不爽,昨天把装koolshare的虚拟机直接给删了,决定自己用Debian9搞个旁路由+扶墙功能,其实说成是旁路由不是很正确,准确点讲这种设备应该叫:透明网关/透明代理/之类的。。。

先说下我这个基本网络环境是:光猫改桥接,然后连到一台主路由上面,主路由负责拨号上网/WIFI接入。在主路由下面接一台电脑,接一台NAS,我在NAS里面装Proxmox虚拟机,虚拟机使用的网络是桥接到宿主机。最后我在虚拟机里面安装一个Debian9并弄成一个可以扶墙的透明网关。这样做的好处就是:全家翻墙,所有接入到局域网的设备无需配置任何客户端即可扶墙,说白了就是让你感觉没有墙的存在。

准备环境

我的这个主路由的IP是192.168.0.1,并且我这个NAS只有一个网口,直接用ISO装的Proxmox,所以装好之后默认就会帮你做一个vmbr0的桥接:

怎么装Debian9就不说了,开始前先装一下需要用到的工具和包:

apt -y install supervisor iptables-persistent net-tools curl wget nano

因为我们要做网关设备,所以不能用DHCP,最好是手动指定一个静态IP,所以这里修改网卡配置文件:

nano /etc/network/interfaces

改为静态IP并把这台虚拟机的网关IP设置为主路由的IP:

iface ens18 inet static
      address 192.168.0.233
      netmask 255.255.255.0
      gateway 192.168.0.1

重启网络服务有时候会有奇葩问题尤其是在Porxmox下面,所以这里直接重启虚拟机吧:

reboot

重新登录上来之后查看当前的网关:

netstat -r -n

回显看到网关IP已经是之前修改的就OK了:

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG        0 0          0 ens18
192.168.0.0     0.0.0.0         255.255.255.0   U         0 0          0 ens18

再看下网卡的信息:

ifconfig

外网口ens18是我们刚才设置的静态IP就行了:

ens18: flags=4163  mtu 1500
        inet 192.168.0.233  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::d4e3:fff:fea6:7a5e  prefixlen 64  scopeid 0x20
        ether d6:e3:0f:a6:7a:5e  txqueuelen 1000  (Ethernet)
        RX packets 167  bytes 17517 (17.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 59  bytes 10165 (9.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1  (Local Loopback)
        RX packets 4  bytes 156 (156.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 156 (156.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

要让Debian9能够有一个基本的路由功能很简单,开一下IP转发就行:

echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf && sysctl -p
echo "net.ipv6.conf.all.forwarding = 1" >> /etc/sysctl.conf && sysctl -p

验证一下,回显是1说明转发开启成功:

cat /proc/sys/net/ipv4/ip_forward

Clash配置

接着今天的主角就要上场了:Clash。这个东西支持透明代理,并且可以根据规则来路由流量。简单点讲就是一个跨平台的免费版Surge。

Go语言写的,安装非常简单,基本上就是下载解压给执行权限即可:

mkdir -p /opt/clash && cd /opt/clash
wget https://github.com/Dreamacro/clash/releases/download/v0.14.0/clash-linux-amd64-v0.14.0.gz
gzip -d clash-linux-amd64-v0.14.0.gz
mv clash-linux-amd64-v0.14.0 clash-linux
chmod +x clash-linux

然后我们需要创建一个yaml的配置文件:

nano config.yml

我的配置如下:

port: 58652
socks-port: 58653
redir-port: 58654

allow-lan: true

mode: Rule

log-level: info

external-controller: 0.0.0.0:23333

dns:
  enable: true
  ipv6: false
  listen: 0.0.0.0:53
  enhanced-mode: redir-host
  nameserver:
     - 114.114.114.114
     - 223.5.5.5
  fallback:
     - tls://dns.rubyfish.cn:853
     - 8.8.8.8

Proxy:

- { name: "nf", type: ss, server: 1.2.3.4, port: 18488, cipher: AEAD_CHACHA20_POLY1305, password: "imlala", udp: true }

- { name: "aliyunsgp", type: ss, server: 2.2.3.4, port: 18588, cipher: AEAD_CHACHA20_POLY1305, password: "imlala", udp: true }

Proxy Group:

- { name: "auto", type: url-test, proxies: ["nf", "aliyunsgp"], url: "https://www.baidu.com", interval: 300 }

- { name: "Proxy", type: select, proxies: ["nf", "aliyunsgp"] }

Rule:

# Netflix
- DOMAIN-KEYWORD,netflix,nf
- DOMAIN-SUFFIX,netflix.com,nf
- DOMAIN-SUFFIX,nflxvideo.net,nf

# LAN
- DOMAIN-SUFFIX,local,DIRECT
- IP-CIDR,127.0.0.0/8,DIRECT
- IP-CIDR,192.168.0.0/16,DIRECT

# IP DataBase
- GEOIP,CN,DIRECT
- MATCH,Proxy

这里面有几个注意事项:

1.做透明代理,必须启用redir-port,也就是clash透明代理的端口。

2.做网关设备,你就必须得让其他局域网的机器能够连接到clash,所以allow-lan必须打开。

3.必须打开clash的DNS功能,并且使用redir-host模式,最重要的是你还需要在fallback:后面加上一个支持DoT的DNS。比如我这边是使用的dns.rubyfish.cn:853,如果你用普通的DNS无非解决域名被污染的问题。

配置文件弄好了之后,现在我们需要把机器所有的流量都转发到clash的透明代理端口上面,当然192.168.0.0这个网段的流量我们不做转发:

iptables -t nat -N Clash
iptables -t nat -A Clash -d 192.168.0.0/16 -j RETURN
iptables -t nat -A Clash -p tcp -j REDIRECT --to-ports 58654
iptables -t nat -A PREROUTING -p tcp -j Clash

永久保存iptables规则:

netfilter-persistent save

现在我们就可以在前台运行一遍clash让它把IP库下载下来,顺带测试一下看能不能正常工作:

./clash-linux -d .

测试方法很简单,现在打开你电脑的IPv4协议属性,手动把网关/DNS地址都修改成你这台旁路由的IP:

开一下推特啊,油管啊,看能不能正常打开,能打开的话,这边你也应该可以看到回显一些IP或者是域名等信息:

现在Ctrl+C组合键退出运行,配置supervisor把clash放到后台运行:

nano /etc/supervisor/conf.d/clash.conf

写入下面的配置:

[supervisord]
nodaemon=false

[program:clash]
priority=1
directory=/opt/clash
command=/opt/clash/clash-linux -d .
autorestart=true

启动supervisor即可:

systemctl restart supervisor
systemctl enable supervisor

最后我们在主路由上开启DHCP功能,在DHCP内把网关/DNS都设置为这台旁路由的IP:

重启你的路由器,然后记得把你电脑的IPv4协议属性也改回来,即改成自动分配让主路由器的DHCP去分配:

这样配置就完成了,现在所有接入这个局域网的设备都能够自动扶墙。

可选操作

现在就有一个问题出来了,使用Clash如果想要去切换节点就只能去登这台机器的SSH,这样太麻烦了,有没有方便一点的办法呢?有是有,但也比较鸡肋就是。。现在有两个WEB面板可以用,但这两个面板的功能都不是很完善,如果要添加节点的话还是得上SSH。。

Clash的WEBUI:clash-dashboard

那这里我也写一下如何去配置这两个面板程序,首先我们来配置官方的clash-dashboard,直接在这台旁路由上安装Node.js/Yarn:

apt -y install git build-essential
curl -sL https://deb.nodesource.com/setup_12.x | bash -
apt -y install nodejs
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
apt -y update && apt -y install yarn

完成之后再装一个nginx:

apt -y install nginx unzip

nginx安装完成之后把nginx的这个引用配置文件改个名:

mv /etc/nginx/sites-enabled/default /etc/nginx/sites-enabled/default.bak

拉取项目文件:

git clone https://github.com/Dreamacro/clash-dashboard.git
cd clash-dashboard

构建(注意内存,npmbuild的时候很耗内存,建议你这台虚拟机先设置个1GB,怼过去之后再把内存改小都可以的):

npm install
npm run build

把构建出来的文件全部移到nginx的站点目录

mkdir -p /usr/share/nginx/html/clash-dashboard
cp -r dist/. /usr/share/nginx/html/clash-dashboard

新建一个nginx的conf:

nano /etc/nginx/conf.d/clash-dashboard.conf

写入:

server {
    listen       80;
    server_name  192.168.0.233;
    root /usr/share/nginx/html/clash-dashboard;
    index index.html;
}

又一个Clash的WEBUI:yacd

接下来安装一下yacd,这个有一种简单的方法,直接下载已经build好的压缩包:

wget https://github.com/haishanh/yacd/archive/gh-pages.zip -O /usr/share/nginx/html/yacd.zip
unzip /usr/share/nginx/html/yacd.zip -d /usr/share/nginx/html
mv /usr/share/nginx/html/yacd-gh-pages /usr/share/nginx/html/yacd 
rm -rf /usr/share/nginx/html/yacd.zip

和之前一样写个nginx的配置文件:

nano /etc/nginx/conf.d/yacd.conf

配置如下:

server {
    listen       81;
    server_name  192.168.0.233;
    root /usr/share/nginx/html/yacd;
    index index.html;
}

但这种方法可能用到的是老版本,为了能够最快用到新版本,最好还是和之前一样自己build一下:

rm -rf /usr/share/nginx/html/yacd/*
git clone https://github.com/haishanh/yacd.git
cd yacd
yarn
yarn build
cp -r public/. /usr/share/nginx/html/yacd

最后启动nginx:

systemctl start nginx
systemctl enable nginx

看一看面板都长啥样

基于我上面的配置访问:192.168.0.233是clash-dashboard的控制面板,访问:192.168.0.233:81是yacd的控制面板,clash-dashboard长这样:

可以选择切换节点:

yacd长这样:

它也可以切换节点,同时还有一个非常实用的功能,实时流量查看:

go-shadowsocks2

使用Clash的话,服务端这里推荐用go-shadowsocks2这个版本,支持AEAD加密,安装很简单基本就是下载/解压运行,当然也不是说非要用这个,这只是我推荐而已。如果你的机器上已经安装了SS或者V2ray并且对这个go-shadowsocks2不怎么感冒的话,这些步骤你可以略过。

CentOS7:

yum -y install supervisor wget nano

Debian9:

apt -y install supervisor wget nano

然后下载二进制文件解压给执行权限就行:

mkdir -p /opt/shadowsocks2 && cd /opt/shadowsocks2
wget https://github.com/shadowsocks/go-shadowsocks2/releases/download/v0.0.11/shadowsocks2-linux.gz
gzip -d shadowsocks2-linux.gz
chmod +x shadowsocks2-linux

接下来使用supervisor将程序放到后台运行。用supervisor还有一个好处是可以监控这个服务端,如果服务端挂了能够实现自动重启。

CentOS7新建supervisor配置文件路径和格式:

nano /etc/supervisord.d/shadowsocks2.ini

Debian9新建supervisor配置文件路径和格式:

nano /etc/supervisor/conf.d/shadowsocks2.conf

配置文件内容通用:

[supervisord]
nodaemon=false

[program:shadowsocks2]
priority=1
directory=/opt/shadowsocks2
command=/opt/shadowsocks2/shadowsocks2-linux -s 'ss://AEAD_CHACHA20_POLY1305:imlala@:28588' -verbose
autorestart=true

重启supervisor即可完成部署。

CentOS7:

systemctl restart supervisord
systemctl enable supervisord

Debian9:

systemctl restart supervisor
systemctl enable supervisor

写在最后

这其实就是一套比较折腾的方案,但同时可玩性也是最高的,你想装什么都可以实现,在Debian上面装个PPPoE实现拨号有问题吗?没问题!在Debian上面装个DHCP服务器?so easy,在Debian上面装个离线下载,挂PT啥的那更是so easy。。你完全可以花点时间把它打造成一个全功能的软路由,再说了最近不是各种删库跑路吗?koolshare的插件都下架了,我还留着这玩意有何用。

赞(9)
未经允许不得转载:荒岛 » 使用Debian9自己打造一个旁路由
分享到: 更多 (0)

评论 30

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    第一 :idea:

    攸琛5年前 (2019-05-28) Google Chrome 74.0.3729.108 Google Chrome 74.0.3729.108 Windows 7 x64 Edition Windows 7 x64 Edition回复
    • 终于抢到一次沙发 :razz:

      攸琛5年前 (2019-05-28) Google Chrome 74.0.3729.108 Google Chrome 74.0.3729.108 Windows 7 x64 Edition Windows 7 x64 Edition回复
      • 这ID不眼熟啊。。

        LALA5年前 (2019-05-28) Google Chrome 72.0.3626.121 Google Chrome 72.0.3626.121 Windows 10 x64 Edition Windows 10 x64 Edition回复
        • 一直潜水。。。

          攸琛5年前 (2019-05-28) Google Chrome 74.0.3729.108 Google Chrome 74.0.3729.108 Windows 7 x64 Edition Windows 7 x64 Edition回复
  2. #2

    感觉还是openwrt实用,在j1900上装个pve虚拟个黑裙+自编译只带ssr的lean的openwrt还是很方便的,路由器就当个交换机+AP用了

    MADAO5年前 (2019-05-29) Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 找个合适自己的方案就行。。

      LALA5年前 (2019-05-29) Google Chrome 72.0.3626.121 Google Chrome 72.0.3626.121 Windows 10 x64 Edition Windows 10 x64 Edition回复
  3. #3

    请问如果是用v2ray的话应该怎么配置呢?

    heroares5年前 (2019-05-29) Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 你看一下clash默认的那个配置文件就知道了。

      LALA5年前 (2019-05-30) Google Chrome 72.0.3626.121 Google Chrome 72.0.3626.121 Windows 10 x64 Edition Windows 10 x64 Edition回复
  4. #4

    之前编译的openwrt不稳吗?

    harlon5年前 (2019-05-30) Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 不是很稳,有些域名会被DNS污染,具体是怎么回事我也不清楚。

      LALA5年前 (2019-05-30) Google Chrome 72.0.3626.121 Google Chrome 72.0.3626.121 Windows 10 x64 Edition Windows 10 x64 Edition回复
  5. #5

    我也是用软路由方案,我是在hyper-v上跑openwrt+ss-redir,下载、文件共享等就在windows下完成;同时还有一台newifi3刷了openwrt,并在软路由(主)和newifi3(从)上配置了keepalived做故障迁移,当主路由出现问题时,网关自动切换到newifi3上,这样nas怎么折腾也不会断网了。

    NICLAU5年前 (2019-05-30) Firefox 60.0 Firefox 60.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 这keepalived都用上了,在家都开始玩HA的节奏了。。

      LALA5年前 (2019-05-30) Google Chrome 72.0.3626.121 Google Chrome 72.0.3626.121 Windows 10 x64 Edition Windows 10 x64 Edition回复
      • LALA,怎么联系你?QQ貌似联系不上了,有偿帮忙 :idea: ~~,期待你的回复 :!:

        悟空5年前 (2019-06-03) Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 7 x64 Edition Windows 7 x64 Edition回复
        • 我帮不了忙。

          LALA5年前 (2019-06-05) Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 10 x64 Edition Windows 10 x64 Edition回复
  6. #6

    我等了好久好久,怎么不更新

    Yuger5年前 (2019-06-04) Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 10 x64 Edition Windows 10 x64 Edition回复
  7. #7

    网络环境跟我家一样。。不过nas就装了一个win7,实在不想折腾了 :eek:

    橘子5年前 (2019-06-05) Google Chrome 71.0.3578.88 Google Chrome 71.0.3578.88 Windows 8 x64 Edition Windows 8 x64 Edition回复
  8. #8

    为什么不使用Whonix

    python去马赛克5年前 (2019-06-07) Opera 45.0.2181.218 Opera 45.0.2181.218 GNU/Linux GNU/Linux回复
  9. #9

    经实测,不能看奶飞,提示使用proxy。其实就是没强制劫持局域网的dns请求,这问题怎么解决?

    嘟嘟5年前 (2019-07-18) Yandex Browser 19.6.0.612.00 Yandex Browser 19.6.0.612.00 Android 8.0.0 Android 8.0.0回复
  10. #10

    只是梯子还好. 去广告那些的…debian上没啥好用的…

    fcy5年前 (2019-09-06) Google Chrome 72.0.3626.121 Google Chrome 72.0.3626.121 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • adguard还可以的,可以配合使用。

      LALA5年前 (2019-09-07) Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 10 x64 Edition Windows 10 x64 Edition回复
      • 感谢. 试试去

        fcy5年前 (2019-09-08) Google Chrome 72.0.3626.121 Google Chrome 72.0.3626.121 Windows 10 x64 Edition Windows 10 x64 Edition回复
  11. #11

    用了几天, 感觉比lede好用@_@
    现在就一个毛病…配合爱快主路由(dhcp把网关 dns设到debian上), 流量监控里面mac地址全是debian的mac.
    用lede的时候 安装snmpd后, 爱快开启跨3️层应用后, 可以分出ledo过来的mac地址.
    debian 安装后就不行. 估计还是哪里没设好, 不过也啥大影响, 根据ip也能看, 流控也没问题.

    封尘印5年前 (2019-09-23) Firefox 69.0 Firefox 69.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 我已经老老实实回归客户端了。。

      LALA5年前 (2019-09-24) Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 10 x64 Edition Windows 10 x64 Edition回复
      • 有些设备不方便改网关, 也装不了客户端…全局路由还是要留一个…

        封尘印5年前 (2019-09-27) Firefox 69.0 Firefox 69.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
        • 主要是我用来用去这些软路由总是发现一些蛋疼的问题,没有一个比较完美的。。强迫症犯了就回归客户端了。。

          LALA5年前 (2019-09-28) Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 10 x64 Edition Windows 10 x64 Edition回复
  12. #12

    安装yacd,访问面板,总是弹出要求设置host,port,secret的一个界面,这里填入什么人

    hmgl4年前 (2020-02-03) Firefox 69.0 Firefox 69.0 Windows 7 x64 Edition Windows 7 x64 Edition回复
  13. #13

    yacd面板登陆页面里的API Base URL填什么东西?

    翔翎4年前 (2020-09-26) Microsoft Edge 85.0.564.60 Microsoft Edge 85.0.564.60 Mac OS X  10.15.6 Mac OS X 10.15.6回复
  14. #14

    大佬,我的主路由是openwrt,在debian11上设置这个透明网关一直失败,是不是跟op有一定关系

    网络小白2年前 (2022-07-03) Google Chrome 103.0.0.0 Google Chrome 103.0.0.0 Mac OS X  10.15.7 Mac OS X 10.15.7回复

分享创造快乐

广告合作资源投稿