惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
月光博客
月光博客
S
Security Affairs
宝玉的分享
宝玉的分享
D
DataBreaches.Net
MongoDB | Blog
MongoDB | Blog
Cloudbric
Cloudbric
PCI Perspectives
PCI Perspectives
B
Blog RSS Feed
腾讯CDC
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
T
The Blog of Author Tim Ferriss
H
Help Net Security
Vercel News
Vercel News
W
WeLiveSecurity
U
Unit 42
S
SegmentFault 最新的问题
Microsoft Azure Blog
Microsoft Azure Blog
Google Online Security Blog
Google Online Security Blog
云风的 BLOG
云风的 BLOG
Google DeepMind News
Google DeepMind News
S
Schneier on Security
The Register - Security
The Register - Security
酷 壳 – CoolShell
酷 壳 – CoolShell
Recent Announcements
Recent Announcements
博客园 - Franky
H
Hacker News: Front Page
WordPress大学
WordPress大学
I
Intezer
M
MIT News - Artificial intelligence
博客园 - 叶小钗
The Last Watchdog
The Last Watchdog
T
Troy Hunt's Blog
Stack Overflow Blog
Stack Overflow Blog
Microsoft Security Blog
Microsoft Security Blog
L
Lohrmann on Cybersecurity
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
I
InfoQ
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Schneier on Security
Schneier on Security
P
Proofpoint News Feed
V
V2EX
Help Net Security
Help Net Security
小众软件
小众软件
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
大猫的无限游戏
大猫的无限游戏
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs

mephisto.cc

修复rime-ice无法弹出候选框的问题 开机滚动字体显示优化 个人网站安全防护 树莓派上部署Vaultwarden 使用goaccess实时分析Caddy日志 个人网站监控 Hugo全站AVIF记 Linux下尝试使用Godot开发小游戏 Arch linux dae 透明代理 Airflow接管galler-dl下载任务 如何使用gallery-dl批量下载图像 Arch核显下如何愉快玩Dota2和CS2 Arch/labwc 环境网络相关设置 Rime添加dota2词库 Arch 重装记录 Supertuxkart iOS版终于发布了 打造自己的流媒体音乐服务 剪切板管理工具clipcat推荐 MangoHud性能监控 微信小程序开发记 Rofi试用 解决Arch下VSCode无法输入中文的问题 独立窗口管理器下无法录屏问题处理 上海市二手房成交数据监控 OpenLDAP监控 Arch linux如何顺畅连接蓝牙设备 使用Git和Ansible管理配置文件 Arch Linux SSL VPN 客户端配置 简单获取celery任务实时结果 Arch linux下iNode客户端的安装和使用方法 券商费率调整记 迷你主机组装记 Labwc更换定制皮肤 实用自动代理配置示例 九年老鼠标复活记 Openvpn示例 Mac下如何旋转webp图片 如何隐藏Vscode中Markdown PDF插件的头部内容 解决Linux下网络配置无法变更保存的问题 labwc环境启用wlogout Ubuntu下新官方微信尝鲜 简单检测用户是否开启广告屏蔽 Atuin ZFS下卡顿问题解决 Wine安装64位微信 Fish 和 Atuin使用记 Fastapi简单实现临时下载链接 Fastapi微信公众号开发简要 Hugo根据语种展示不同内容 如何修复Waybar微信图标错误 信封加密简要 Linux环境下维护公众号记录 快速隐藏和呼出终端 Wayland环境自动切换壁纸 SuperTuxKart 试玩 简单抓取雪球股票快讯 为什么你的kill命令不能列出信号表 Wine安装微信保姆教程 Hysteria科学上网简要 Ubuntu 23.10 钉钉无法启动解决方法 网站导航栏防止插入Adsense自动广告 Linux环境按键检测 Hugo文末添加最后修改时间 Wayland环境ksnip无法复制问题解决 我眼中的股票市场 Caddy简单图片防盗链 greetd和regreet使用教程 Wayland环境下截图加后期修改 Wayland 环境下gif录屏 Firefox的一些有趣功能 Ubuntu 切换系统语言 Firefox 标题栏高度调整 网站添加回到顶部功能 Hugo自定义字体 Labwc 便捷配置 Wofi使用教程 窗口管理器labwc使用记 文件共享软件Dufs推荐 Hugo静态站点接入Adsense广告 Fcitx5配置详解 Ubuntu安装chrome的方法总结 一种Xterm.js的全屏方案 Google网址收录api Python示例 手机听离线音乐 Snipe it资产导入 应用启动器yofi使用配置 Snipe it资产管理系统安装使用 Ubuntu开机启动加速 Vue3 vite TypeScript跨域等相关设置 Ubuntu手动升级Libreoffice Ubuntu设置alacritty为默认终端 简体文章批量转换为繁体 使用Inkscape调整svg图片大小 实用命令 使用mitmproxy给手机app抓包 Python中rstrip方法细解 Github同主机多仓库部署deploy key问题处理 Linux安装最新版本Python KeePassXC使用教程 Ubuntu 22.10连接蓝牙耳机报错br-connection-profile-unavailable解决方法 Caddy日志配置轮转和格式化
树莓派跑分流代理
2024-04-28 · via mephisto.cc

最近好像和各种代理杠上了,抽空把sing-box的文档看了看,加上各种视频教学,自己摸索倒腾了一番。

sing-box是个通用代理平台,很强大,入站和出站支持的协议很多,用起来很方便。

本文记录我的个人使用方式,或许对部分读者有用,可能部分描述不够专业,绝对实用,欢迎读者关注公众号留言交流。

因为有一个闲置多年的树莓派2(Raspberry Pi 2 Model B),查了一下额定功率 3.0 瓦 (5V/600mA),很适合用来做小网关 + dnsmasq域名缓存。网关能代理,也能屏蔽部分广告。

先画个网络架构图,方便读者理解。

architecture diagram

和常见的家庭网络一样。只是在家用路由器上多接入了一个树莓派。其它设备的网关和dns服务器都设置为树莓派的ip地址。

下面为操作步骤:

1. 树莓派安装官方系统

本来这一步没什么可写的,网上有很多教程教你怎么给树莓派刷官方系统。好多年没有给树莓派刷系统了,发现官方出了个叫Raspberry Pi Imager的小工具,很好用(记得以前都是用dd命令刷),能省点事。

pi imager

先选你的树莓派型号,再选你要安装的操作系统镜像(这一步如果你网络卡的话,可以选自定义镜像,就选你电脑里下载好的镜像),再选sd卡,点Next后可以设置hostname,用户密码、开启ssh、自动导入key密钥什么的。

我选的最简镜像,400多M,毕竟不需要桌面,也不需要过多功能。

这个小工具真的可以说是个贴心小管家,看文字描述就知道怎么用,装好后,拔掉sd卡,插入树莓派卡槽就好了。

仔细想想,这的确降低了操作门槛,方便好用。选存储的时候,记得确认选中的是sd卡,不然把系统给格杀了,那可麻烦大啦。

2. 树莓派上配置dnsmasq

ssh登陆树莓派,题外话,如果你不知道树莓派分得的ip(比如说路由器不受管控的环境),可以使用抓包软件wireshark抓arp广播包,或者挨个ping和你电脑ip差不多的ip😂,运气好能很快找到。

一般路由器挂载的设备不多的话,ip不怎么变,当然也可以设置固定ip。

查看基本信息,debian系,armv7l的cpu,已经切换好交大的源(选个离自己近的源,速度快)。

 1mephisto@raspberrypi:~ $ cat /etc/os-release
 2PRETTY_NAME="Raspbian GNU/Linux 12 (bookworm)"
 3NAME="Raspbian GNU/Linux"
 4VERSION_ID="12"
 5VERSION="12 (bookworm)"
 6VERSION_CODENAME=bookworm
 7ID=raspbian
 8ID_LIKE=debian
 9HOME_URL="http://www.raspbian.org/"
10SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
11BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
12mephisto@raspberrypi:~ $ uname -a
13Linux raspberrypi 6.6.28+rpt-rpi-v7 #1 SMP Raspbian 1:6.6.28-1+rpt1 (2024-04-22) armv7l GNU/Linux
14mephisto@raspberrypi:~ $ cat /etc/apt/sources.list
15#org
16#deb [ arch=armhf ] http://raspbian.raspberrypi.com/raspbian/ bookworm main contrib non-free rpi
17# Uncomment line below then 'apt-get update' to enable 'apt-get source'
18#deb-src http://raspbian.raspberrypi.com/raspbian/ bookworm main contrib non-free rpi
19
20#ustc
21# deb http://mirrors.ustc.edu.cn/raspberrypi/debian/ bookworm main
22#deb-src http://mirrors.ustc.edu.cn/raspberrypi/debian/ bookworm main
23
24#sjtu
25deb http://mirrors.sjtug.sjtu.edu.cn/raspbian/raspbian/ bookworm main contrib non-free rpi
26mephisto@raspberrypi:~ $

安装 dnsmasq,适合在小网络场景使用,主要用来缓存域名,添加自定义域名,结合NetworkManager使用的。

1sudo apt install dnsmasq

配置dnsmasq,请参考这个arch文档: https://wiki.archlinux.org/title/NetworkManager#dnsmasq

 1mephisto@raspberrypi:~ $ cd /etc/NetworkManager/
 2mephisto@raspberrypi:/etc/NetworkManager $ ls
 3NetworkManager.conf  conf.d  dispatcher.d  dnsmasq-shared.d  dnsmasq.d  system-connections
 4mephisto@raspberrypi:/etc/NetworkManager $ cat conf.d/dns.conf
 5[main]
 6dns=dnsmasq
 7mephisto@raspberrypi:/etc/NetworkManager $ cat dnsmasq.d/
 8cache.conf   listen.conf
 9mephisto@raspberrypi:/etc/NetworkManager $ cat dnsmasq.d/cache.conf
10server=223.5.5.5   # 域名服务器阿里的
11cache-size=1000    # 缓存1000条dns记录,还是一定内存大小的dns记录,有兴趣的自己去翻dnsmasq源码确认。
12strict-order       # 不重要,看dnsmasq文档
13address=/www.example.com/ 192.168.18.190    # 自己定义域名,因为我在树莓派傻姑娘跑lighttpd托管pac,也用来验证dnsmasq是否配置正确。
14mephisto@raspberrypi:/etc/NetworkManager $ cat dnsmasq.d/listen.conf
15listen-address=::1,127.0.0.1,192.168.18.190  # dns服务监听的的网卡,53端口 
16mephisto@raspberrypi:/etc/NetworkManager $

具体设置如上所示,dns=dnsmasq 意思是启用NetworkManager的dns部分由dnsmasq接管。

注意!这种使用方式不用开机启动dnsmasq,NetworkManager会帮你搞定。

1mephisto@raspberrypi:/etc/NetworkManager $ systemctl is-enabled dnsmasq
2disabled
3mephisto@raspberrypi:/etc/NetworkManager $ ps -ef | grep NetworkManager
4root       508     1  0 Apr27 ?        00:00:10 /usr/sbin/NetworkManager --no-daemon
5nobody    1524   508  0 Apr27 ?        00:00:46 /usr/sbin/dnsmasq --no-resolv --keep-in-foreground --no-hosts --bind-interfaces --pid-file=/run/NetworkManager/dnsmasq.pid --listen-address=127.0.0.1 --cache-size=400 --clear-on-reload --conf-file=/dev/null --proxy-dnssec --enable-dbus=org.freedesktop.NetworkManager.dnsmasq --conf-dir=/etc/NetworkManager/dnsmasq.d
6mephisto 17990 17355 50 14:29 pts/0    00:00:00 grep --color=auto NetworkManager
7mephisto@raspberrypi:/etc/NetworkManager $

3. 树莓派上配置sing-box客户端

先开ip_forward

1mephisto@raspberrypi:~ $ cat /etc/sysctl.conf |grep ip_forward
2net.ipv4.ip_forward=1
3mephisto@raspberrypi:~ $ sysctl -p
4sysctl: permission denied on key "net.ipv4.ip_forward"
5mephisto@raspberrypi:~ $ sysctl net.ipv4.ip_forward
6net.ipv4.ip_forward = 1

安装sing-box

1sudo curl -fsSL https://sing-box.app/gpg.key -o /etc/apt/keyrings/sagernet.asc
2sudo chmod a+r /etc/apt/keyrings/sagernet.asc
3echo "deb [arch=`dpkg --print-architecture` signed-by=/etc/apt/keyrings/sagernet.asc] https://deb.sagernet.org/ * *" | \
4  sudo tee /etc/apt/sources.list.d/sagernet.list > /dev/null
5sudo apt-get update
6sudo apt-get install sing-box

为了方便理解sing-box客户端,我建议不怕麻烦的读者,先看下官方文档,毕竟软件一直在更新,配置也会变动。理解配置项后,可以自己写规则。

或者去各大视频网站看视频,视频有的也讲得不清楚,或者过时导致误解。

客户端配置:

 1
 2mephisto@raspberrypi:/etc/sing-box $ cat config.json
 3{
 4  "dns": {
 5    "servers": [
 6      {
 7        "tag": "cf",
 8        "address": "https://1.1.1.1/dns-query"
 9      },
10      {
11        "tag": "local",
12        "address": "223.5.5.5",
13        "detour": "direct"
14      },
15      {
16        "tag": "block",
17        "address": "rcode://success"
18      }
19    ],
20    "rules": [
21      {
22        "outbound": "any",
23        "server": "local"
24      },
25      {
26        "geosite": "cn",
27        "server": "local"
28      }
29    ],
30    "strategy": "ipv4_only"
31  },
32  "inbounds": [
33    {
34      "type": "tun",    # tun模式,树莓派上最终会多一个tun虚拟网卡。
35      "stack": "system",
36      "inet4_address": "172.19.0.1/30",
37      "auto_route": true,
38      "strict_route": true,
39      "sniff": true
40    }
41  ],
42  "outbounds": [
43    {
44      "type": "hysteria2",
45      "tag": "proxy",
46      "server": "stock.mephisto.cc", # 填你的域名
47      "server_port": 443,
48      "up_mbps": 20,
49      "down_mbps": 100,
50      "password": "your_password",   # 密码
51      "tls": {
52        "enabled": true,
53        "server_name": "stock.mephisto.cc", # 证书域名
54        "insecure": true            # 如果帧数校验不通过,就开启,我的情况就是说我证书过期,估计是服务端证书没有刷新,浏览器访问正常的。
55      }
56    },
57    {
58      "type": "direct",
59      "tag": "direct"
60    },
61    {
62      "type": "block",
63      "tag": "block"
64    },
65    {
66      "type": "dns",
67      "tag": "dns-out"
68    }
69  ],
70  "route": {
71    "rules": [
72      {
73        "protocol": "dns",
74        "outbound": "dns-out"
75      },
76      {
77        "geosite": "cn",
78        "geoip": [
79          "private",
80          "cn"
81        ],
82        "outbound": "direct"
83      }
84    ],
85    "auto_detect_interface": true
86  }
87}

本来我参考的配置里面还有广告屏蔽功能,自己去掉了,毕竟自己开小站,对广告不是很反感。有兴趣的读者自己可以去网上搜索相关配置。

4. 电脑上配置网关和dns

这一步,网上教程千千万,截个图凑合下😂

网关,我是先自动dhcp连 Wi-Fi,再更改的,不然链接无法建立,有线的话,直接设定没有这种问题。

gateway

dns先设置的树莓派ip,再设置路由器ip,双保险,应该是优先使用树莓派ip

dns

5. 测试验证

选择在Mac上测试,其它设备都差不多。

测试域名缓存,我没有确认这个到底sing-box还是dnsmasq缓存的。

1mephisto@RMBP ~> dig omg.com | grep "Query time"
2;; Query time: 716 msec
3mephisto@RMBP ~> dig omg.com | grep "Query time"
4;; Query time: 49 msec

反正效果达到了,懒得区分了。716ms --> 49ms,下降了一个数量级。如果是你电脑本地的dnsmasq缓存,通常是0ms。

测试自定义域名,直接访问 lighttpd 的默认域名 www.example.com,默认测试页面正常返回。

lighttpd

dig 确认,也是ok的。

 1mephisto@RMBP ~> dig www.example.com
 2
 3; <<>> DiG 9.10.6 <<>> www.example.com
 4;; global options: +cmd
 5;; Got answer:
 6;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 10055
 7;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
 8
 9;; OPT PSEUDOSECTION:
10; EDNS: version: 0, flags:; udp: 4096
11;; QUESTION SECTION:
12;www.example.com.               IN      A
13
14;; ANSWER SECTION:
15www.example.com.        0       IN      A       192.168.18.190
16
17;; Query time: 108 msec
18;; SERVER: 192.168.18.190#53(192.168.18.190)
19;; WHEN: Sun Apr 28 15:24:03 CST 2024
20;; MSG SIZE  rcvd: 60

以上可以确认树莓派上的dnsmasq正常工作。

接下来确认分流代理功能,ip测试

china ip
japan ip

再访问域名测试:

 1mephisto@RMBP ~> curl baidu.com
 2<html>
 3<meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
 4</html>
 5mephisto@RMBP ~> curl google.com
 6<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
 7<TITLE>301 Moved</TITLE></HEAD><BODY>
 8<H1>301 Moved</H1>
 9The document has moved
10<A HREF="http://www.google.com/">here</A>.
11</BODY></HTML>
12mephisto@RMBP ~>

可见sing-box功能正常,个人觉得还挺好用的,树莓派比一般的路由器性能强,还能当作一个Linux服务器来玩,手机等设备上也不用装额外软件。