p2p-action.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/bin/sh
  2. IFNAME=$1
  3. CMD=$2
  4. kill_daemon() {
  5. NAME=$1
  6. PF=$2
  7. if [ ! -r $PF ]; then
  8. return
  9. fi
  10. PID=`cat $PF`
  11. if [ $PID -gt 0 ]; then
  12. if ps $PID | grep -q $NAME; then
  13. kill $PID
  14. fi
  15. fi
  16. rm $PF
  17. }
  18. if [ "$CMD" = "P2P-GROUP-STARTED" ]; then
  19. GIFNAME=$3
  20. if [ "$4" = "GO" ]; then
  21. kill_daemon dhclient /var/run/dhclient-$GIFNAME.pid
  22. rm /var/run/dhclient.leases-$GIFNAME
  23. kill_daemon dnsmasq /var/run/dnsmasq.pid-$GIFNAME
  24. ifconfig $GIFNAME 192.168.42.1 up
  25. if ! dnsmasq -x /var/run/dnsmasq.pid-$GIFNAME \
  26. -i $GIFNAME \
  27. -F192.168.42.11,192.168.42.99; then
  28. # another dnsmasq instance may be running and blocking us; try to
  29. # start with -z to avoid that
  30. dnsmasq -x /var/run/dnsmasq.pid-$GIFNAME \
  31. -i $GIFNAME \
  32. -F192.168.42.11,192.168.42.99 --listen-address 192.168.42.1 -z -p 0
  33. fi
  34. fi
  35. if [ "$4" = "client" ]; then
  36. kill_daemon dhclient /var/run/dhclient-$GIFNAME.pid
  37. rm /var/run/dhclient.leases-$GIFNAME
  38. kill_daemon dnsmasq /var/run/dnsmasq.pid-$GIFNAME
  39. ipaddr=`echo "$*" | sed 's/.* ip_addr=\([^ ]*\).*/\1/'`
  40. ipmask=`echo "$*" | sed 's/.* ip_mask=\([^ ]*\).*/\1/'`
  41. goipaddr=`echo "$*" | sed 's/.* go_ip_addr=\([^ ]*\).*/\1/'`
  42. if echo "$ipaddr$ipmask$goipaddr" | grep -q ' '; then
  43. ipaddr=""
  44. ipmask=""
  45. goipaddr=""
  46. fi
  47. if [ -n "$ipaddr" ]; then
  48. sudo ifconfig $GIFNAME "$ipaddr" netmask "$ipmask"
  49. sudo ip ro re default via "$goipaddr"
  50. exit 0
  51. fi
  52. dhclient -pf /var/run/dhclient-$GIFNAME.pid \
  53. -lf /var/run/dhclient.leases-$GIFNAME \
  54. -nw \
  55. $GIFNAME
  56. fi
  57. fi
  58. if [ "$CMD" = "P2P-GROUP-REMOVED" ]; then
  59. GIFNAME=$3
  60. if [ "$4" = "GO" ]; then
  61. kill_daemon dnsmasq /var/run/dnsmasq.pid-$GIFNAME
  62. ifconfig $GIFNAME 0.0.0.0
  63. fi
  64. if [ "$4" = "client" ]; then
  65. kill_daemon dhclient /var/run/dhclient-$GIFNAME.pid
  66. rm /var/run/dhclient.leases-$GIFNAME
  67. ifconfig $GIFNAME 0.0.0.0
  68. fi
  69. fi
  70. if [ "$CMD" = "P2P-CROSS-CONNECT-ENABLE" ]; then
  71. GIFNAME=$3
  72. UPLINK=$4
  73. # enable NAT/masquarade $GIFNAME -> $UPLINK
  74. iptables -P FORWARD DROP
  75. iptables -t nat -A POSTROUTING -o $UPLINK -j MASQUERADE
  76. iptables -A FORWARD -i $UPLINK -o $GIFNAME -m state --state RELATED,ESTABLISHED -j ACCEPT
  77. iptables -A FORWARD -i $GIFNAME -o $UPLINK -j ACCEPT
  78. sysctl net.ipv4.ip_forward=1
  79. fi
  80. if [ "$CMD" = "P2P-CROSS-CONNECT-DISABLE" ]; then
  81. GIFNAME=$3
  82. UPLINK=$4
  83. # disable NAT/masquarade $GIFNAME -> $UPLINK
  84. sysctl net.ipv4.ip_forward=0
  85. iptables -t nat -D POSTROUTING -o $UPLINK -j MASQUERADE
  86. iptables -D FORWARD -i $UPLINK -o $GIFNAME -m state --state RELATED,ESTABLISHED -j ACCEPT
  87. iptables -D FORWARD -i $GIFNAME -o $UPLINK -j ACCEPT
  88. fi