Skip to content

Instantly share code, notes, and snippets.

@zonpantli
Last active December 18, 2015 02:49
Show Gist options
  • Save zonpantli/5714021 to your computer and use it in GitHub Desktop.
Save zonpantli/5714021 to your computer and use it in GitHub Desktop.
Simulate various network links. Useful e.g. for mobile development on localhost.
# Usage
#
# To simulate lossy 3G on local connections to and from port 3004
# ruby linksim.rb up 3004 3g_lossy
#
# To clear ipfw rules
# ruby linksim down
conf = {
:"3g_lossy" => {
down: {
bw: 780,
delay: 100,
plr: 0.01
},
up: {
bw: 330,
delay: 100,
plr: 0
}
}
}
if __FILE__ == $0
if ARGV[0] == "up"
port = ARGV[1] || 3004
key = (ARGV[2] || :"3g_lossy").to_sym
c = conf[key]
puts "Setting up ipfw pipes..."
system("sudo ipfw pipe 1 config bw #{c[:down][:bw]}KByte/s delay #{c[:down][:delay]}ms plr #{c[:down][:plr]}")
system("sudo ipfw pipe 2 config bw #{c[:up][:bw]}KByte/s delay #{c[:up][:delay]}ms plr #{c[:up][:plr]}")
system("sudo ipfw add 1 pipe 1 ip from me to any src-port #{port}")
system("sudo ipfw add 2 pipe 2 ip from any to me dst-port #{port}")
else
puts "Deleting ipfw pipes..."
system("sudo ipfw delete 1")
system("sudo ipfw delete 2")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment