Last active
May 20, 2024 18:10
-
-
Save suzumura-ss/c12759c84cacca1663ceb4a0c43b550f to your computer and use it in GitHub Desktop.
macos pfctl dnctl example script
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
#!/bin/bash | |
# https://techracho.bpsinc.jp/hachi8833/2017_04_04/38229 | |
# https://gist.github.com/mefellows/4f6ecd2e83de8b591726 | |
# | |
# for localhost:3000 | |
reset() { | |
dnctl -q flush | |
pfctl -f /etc/pf.conf | |
pfctl -d | |
} | |
status() { | |
echo | |
dnctl list | |
} | |
shape_traffic() { | |
pfctl -e | |
IN_OUT=in # in: connect to localhost, out: outgoing. | |
PORT=3000 # port number | |
(cat /etc/pf.conf && echo "dummynet-anchor \"mop\"" && echo "anchor \"mop\"") | pfctl -f - | |
echo "dummynet $IN_OUT quick proto tcp from any to any port $PORT pipe 1" | pfctl -a mop -f - | |
} | |
BW=56kbit/s | |
DELAY=1000 | |
case "$1" in | |
-r) | |
reset | |
status | |
;; | |
bw) | |
shape_traffic | |
dnctl pipe 1 config bw $BW | |
status | |
;; | |
delay) | |
shape_traffic | |
dnctl pipe 1 config delay $DELAY | |
status | |
;; | |
both) | |
shape_traffic | |
dnctl pipe 1 config bw $BW delay $DELAY | |
status | |
;; | |
*) | |
echo $0 "<-r|bw|delay|both>" | |
;; | |
esac |
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
#!/bin/bash | |
# https://techracho.bpsinc.jp/hachi8833/2017_04_04/38229 | |
# https://gist.github.com/mefellows/4f6ecd2e83de8b591726 | |
# | |
# for localhost:3000 and https | |
reset() { | |
dnctl -q flush | |
pfctl -f /etc/pf.conf | |
pfctl -d | |
} | |
status() { | |
echo | |
dnctl list | |
} | |
shape_traffic() { | |
pfctl -e | |
(cat /etc/pf.conf && cat) <<__PF__ | pfctl -f - | |
dummynet-anchor "mop" | |
anchor "mop" | |
__PF__ | |
cat <<__MOP__ | pfctl -a mop -f - | |
dummynet in quick proto tcp from any to any port 3000 pipe 1 | |
dummynet out quick proto tcp from any to any port 443 pipe 2 | |
__MOP__ | |
} | |
BW=56kbit/s | |
DELAY=1000 | |
case "$1" in | |
-r) | |
reset | |
status | |
;; | |
bw) | |
shape_traffic | |
dnctl pipe 1 config bw $BW | |
dnctl pipe 2 config bw $BW | |
status | |
;; | |
delay) | |
shape_traffic | |
dnctl pipe 1 config delay $DELAY | |
dnctl pipe 2 config delay $DELAY | |
status | |
;; | |
both) | |
shape_traffic | |
dnctl pipe 1 config bw $BW delay $DELAY | |
dnctl pipe 2 config bw $BW delay $DELAY | |
status | |
;; | |
*) | |
echo $0 "<-r|bw|delay|both>" | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment