Skip to content

Instantly share code, notes, and snippets.

@itsuwari
Last active December 4, 2024 02:34
Show Gist options
  • Save itsuwari/3096a15b7e7e6b28a3f8ec1815ab4246 to your computer and use it in GitHub Desktop.
Save itsuwari/3096a15b7e7e6b28a3f8ec1815ab4246 to your computer and use it in GitHub Desktop.
mape-macos.sh
#!/bin/sh
# Define variables
BR='スクショのoption peeraddrのアドレスをそのまま書く' # Remote IPv6 address
CE='スクショのCEを書く' # Local IPv6 address
IP4='スクショのIPv4アドレスを書く' # Local IPv4 address
PSID='スクショのPSIDを書く' # PSID
WANDEV='上記のifconfigで特定したインターフェース' # WAN interface
TUNDEV='gif0' # Tunnel interface name
# Step 1: Create the GIF tunnel interface
sudo ifconfig $TUNDEV create
# Step 2: Configure the tunnel endpoints
sudo ifconfig $TUNDEV tunnel $CE $BR
# Step 3: Assign IPv6 addresses to the tunnel
sudo ifconfig $TUNDEV inet6 $CE $BR prefixlen 128
# Step 4: Set MTU for the tunnel interface
sudo ifconfig $TUNDEV mtu 1460
# Step 5: Bring up the tunnel interface
sudo ifconfig $TUNDEV up
# Step 6: Update default IPv4 route to use the tunnel
sudo route -n delete default
sudo route -n add default -interface $TUNDEV
# Step 7: Set up port forwarding using pf
echo "Flushing existing pf rules..."
sudo pfctl -F all
echo "Generating pf rules..."
cat <<EOF | sudo tee /etc/pf.conf
nat on $TUNDEV proto { tcp, udp, icmp } from $IP4 to any -> ($TUNDEV)
EOF
echo "Reloading pf configuration..."
sudo pfctl -f /etc/pf.conf
sudo pfctl -e
# Step 8: Port mappings for MAP-E
rule=1
while [ $rule -le 15 ]; do
mark=`expr $rule + 16`
pn=`expr $rule - 1`
portl=`expr $rule \* 4096 + $PSID \* 16`
portr=`expr $portl + 15`
echo "Adding NAT rules for ports $portl-$portr"
echo "nat on $TUNDEV proto { tcp, udp, icmp } from $IP4 to any port $portl:$portr -> ($TUNDEV)" >> /etc/pf.conf
rule=`expr $rule + 1`
done
echo "Reloading pf configuration with port mappings..."
sudo pfctl -f /etc/pf.conf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment