树莓派+facebook/ATC打造弱网测试环境

利用树莓派 4B 和 facebook 的 ATC 打造一个弱网测试环境

树莓派系统安装

树莓派系统安装

理论上与 ubuntu/debian + 无线网卡效果相同

开启无线热点

配置 hostapd

1
2
3
4
5
6
7
# 安装 hostapd
sudo apt update
sudo apt install hostapd -y
sudo systemctl stop hostapd
# 启动wifi,默认名是wlan0
sudo rfkill unblock wifi
sudo ifconfig wlan0 up

编辑 ap 的配置

/etc/hostapd/hostapd.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
interface=wlan0 #wifi网卡名
driver=nl80211
ssid=autotest #热点名
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=12345678 #密码
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

启动热点

1
2
3
sudo systemctl unmask hostapd
sudo systemctl enable hostapd
sudo systemctl start hostapd

此时应该有一个 autotest 的 wifi,连接时处于“正在获取 IP”的状态

配置 dhcpd

备注:本文使用的网关地址是 192.168.9.1,ip 段分配为 192.168.9.2~192.168.9.10,如需修改自行调整。

设置 ap 的 wlan0 为静态,ip 地址为 192.168.9.1

/etc/dhcpcd.conf
1
2
3
4
...
# 最下面添加
interface wlan0
static ip_address=192.168.9.1/24

安装 isc-dhcp-server

1
sudo apt install isc-dhcp-server -y

配置 isc-dhcp-server

/etc/dhcp/dhcpd.conf
1
2
3
4
5
6
7
8
9
...
# 最下面添加,网关和ip范围与上面的配置保持一致
subnet 192.168.9.0 netmask 255.255.255.0 {
# 这里配置了9个设备,可自行设置范围
range 192.168.9.2 192.168.9.10;
option subnet-mask 255.255.255.0;
option routers 192.168.9.1;
option domain-name-servers 114.114.114.114, 8.8.8.8;
}

重启服务

1
2
sudo systemctl restart dhcpcd
sudo systemctl restart isc-dhcp-server

此时应该 wifi 可以连接,但是没有网络

配置 NAT

1
2
3
4
# 开启 ipv4 转发
sudo sysctl net.ipv4.ip_forward=1
# 配置 iptables 将 eth0(网线网卡)转发出去
sudo iptables -t nat -A POSTROUTING -s 192.168.9.1/24 -o eth0 -j MASQUERADE

此时 wifi 可以正常连接网络,热点配置完成

配置 ATC

安装 python2.7

1
2
3
4
sudo apt install virtualenv python2.7 -y
virtualenv -p /usr/bin/python2.7 atc
cd atc/bin
source activate

安装 ATC

1
2
3
4
# 之后的操作都在 ~/atc 目录下
cd ~/atc
pip2 install atc_thrift atcd django-atc-api django-atc-demo-ui django-atc-profile-storage
django-admin startproject atcui

配置 django

配置 settings,在 INSTALLED_APPS 中添加 ATC 的应用

atcui/atcui/settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
...
INSTALLED_APPS = (
...
# Django ATC API
'rest_framework',
'atc_api',
# Django ATC Demo UI
'bootstrap_themes',
'django_static_jquery',
'atc_demo_ui',
# Django ATC Profile Storage
'atc_profile_storage',
)

配置 django 路由

atcui/atcui/urls.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
...
from django.views.generic.base import RedirectView
from django.conf.urls import include

urlpatterns = [
...
# Django ATC API
url(r'^api/v1/', include('atc_api.urls')),
# Django ATC Demo UI
url(r'^atc_demo_ui/', include('atc_demo_ui.urls')),
# Django ATC profile storage
url(r'^api/v1/profiles/', include('atc_profile_storage.urls')),
url(r'^$', RedirectView.as_view(url='/atc_demo_ui/', permanent=False)),
]

更新 django 的数据库

1
python atcui/manage.py migrate

运行 ATC

运行 atcd

没有设置后台运行,如果需要自行设置

1
2
3
4
# 需要使用 sudo 命令,如果找不到 atcd,把 atcd 的 bin 的路径加上
# eth0 是网线网卡
# wlan0 是ap的网卡
sudo atcd --atcd-wan eth0 --atcd-lan wlan0 --atcd-dont-drop-packets

运行 ATC UI

不要关闭运行 atcd 的进程,再开个 ssh 窗口

1
2
# 记得先激活python2的虚拟环境
python atcui/manage.py runserver 0.0.0.0:8000

添加 facebook 的默认网络配置

可选操作,上面两个服务不要关闭

1
2
git clone https://github.com/facebookarchive/augmented-traffic-control.git
augmented-traffic-control/utils/restore-profiles.sh 192.168.9.1:8000

使用

设备链接 wifi:autotest,访问 http://192.168.9.1:8000,选择已经设置好的profile。

atc 支持多个设备同时连接并且配置不同,具体可以自行研究

facebookarchive/augmented-traffic-control

树莓派+facebook/ATC打造弱网测试环境

https://blog.feijidui.com/raspberry-pi-atc/

作者

Wiley

发布于

2023-02-12

更新于

2024-05-26

许可协议