Created
October 15, 2021 16:06
-
-
Save pnc/87ca699fc0e6bee2d6466f513c2b4a1f to your computer and use it in GitHub Desktop.
Simulate network chaos on macOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simulate a fairly realistic "bad network" (e.g. bufferbloat, | |
# flaky Wi-Fi, LTE from a brick building) to help find bugs | |
# in code that never expects network responses to arrive out of order. | |
# Run with `sudo ruby network-chaos.rb` and stop with control-C. | |
# Test with: | |
# | |
# $ ping 1.1.1.1 | |
# PING 1.1.1.1 (1.1.1.1): 56 data bytes | |
# Request timeout for icmp_seq 0 | |
# Request timeout for icmp_seq 1 | |
# Request timeout for icmp_seq 2 | |
# 64 bytes from 1.1.1.1: icmp_seq=0 ttl=59 time=4006.725 ms | |
# 64 bytes from 1.1.1.1: icmp_seq=2 ttl=59 time=2028.279 ms | |
# 64 bytes from 1.1.1.1: icmp_seq=4 ttl=59 time=47.303 ms | |
# 64 bytes from 1.1.1.1: icmp_seq=1 ttl=59 time=4009.890 ms | |
# 64 bytes from 1.1.1.1: icmp_seq=3 ttl=59 time=4008.366 ms | |
# 64 bytes from 1.1.1.1: icmp_seq=5 ttl=59 time=4010.062 ms | |
# 64 bytes from 1.1.1.1: icmp_seq=6 ttl=59 time=4008.589 ms | |
# 64 bytes from 1.1.1.1: icmp_seq=8 ttl=59 time=2030.234 ms | |
Signal.trap('INT') { throw :sigint } | |
unless system("pfctl -s all") | |
puts "You must run this script this script with `sudo` or as root so it can run `pfctl`." | |
exit(1) | |
end | |
catch :sigint do | |
system("pfctl -e") # enable the firewall | |
system("pfctl -f /etc/pf.conf") # reset to default macOS rules | |
# See `man dnctl` for info. | |
system("dnctl pipe 1 config bw 5Mbit/s delay 20") | |
system("dnctl pipe 2 config bw 5Mbit/s delay 2000") | |
# Randomly reassign traffic to either the low-latency or high-latency pipe. | |
# This simulates intermittent networks and networks with bad bufferbloat, where | |
# bandwidth is not an issue, but sequential requests complete out of order. | |
while true do | |
pipe = rand(1..2) | |
cmd = "echo \"dummynet out all pipe #{pipe}\ndummynet in all pipe #{pipe}\" | pfctl -f - " | |
puts cmd | |
system(cmd) | |
sleep(rand() * 2) | |
end | |
end | |
system("pfctl -d") | |
puts "Firewall has been disabled. If you rely on the macOS firewall, you need to go turn it back on." | |
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment