p2p-action.sh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. dnsmasq -x /var/run/dnsmasq.pid-$GIFNAME \
  26. -i $GIFNAME \
  27. -F192.168.42.11,192.168.42.99
  28. fi
  29. if [ "$4" = "client" ]; then
  30. kill_daemon dhclient /var/run/dhclient-$GIFNAME.pid
  31. rm /var/run/dhclient.leases-$GIFNAME
  32. kill_daemon dnsmasq /var/run/dnsmasq.pid-$GIFNAME
  33. dhclient -pf /var/run/dhclient-$GIFNAME.pid \
  34. -lf /var/run/dhclient.leases-$GIFNAME \
  35. -nw \
  36. $GIFNAME
  37. fi
  38. fi
  39. if [ "$CMD" = "P2P-GROUP-REMOVED" ]; then
  40. GIFNAME=$3
  41. if [ "$4" = "GO" ]; then
  42. kill_daemon dnsmasq /var/run/dnsmasq.pid-$GIFNAME
  43. ifconfig $GIFNAME 0.0.0.0
  44. fi
  45. if [ "$4" = "client" ]; then
  46. kill_daemon dhclient /var/run/dhclient-$GIFNAME.pid
  47. rm /var/run/dhclient.leases-$GIFNAME
  48. ifconfig $GIFNAME 0.0.0.0
  49. fi
  50. fi
  51. if [ "$CMD" = "P2P-CROSS-CONNECT-ENABLE" ]; then
  52. GIFNAME=$3
  53. UPLINK=$4
  54. # enable NAT/masquarade $GIFNAME -> $UPLINK
  55. iptables -P FORWARD DROP
  56. iptables -t nat -A POSTROUTING -o $UPLINK -j MASQUERADE
  57. iptables -A FORWARD -i $UPLINK -o $GIFNAME -m state --state RELATED,ESTABLISHED -j ACCEPT
  58. iptables -A FORWARD -i $GIFNAME -o $UPLINK -j ACCEPT
  59. sysctl net.ipv4.ip_forward=1
  60. fi
  61. if [ "$CMD" = "P2P-CROSS-CONNECT-DISABLE" ]; then
  62. GIFNAME=$3
  63. UPLINK=$4
  64. # disable NAT/masquarade $GIFNAME -> $UPLINK
  65. sysctl net.ipv4.ip_forward=0
  66. iptables -t nat -D POSTROUTING -o $UPLINK -j MASQUERADE
  67. iptables -D FORWARD -i $UPLINK -o $GIFNAME -m state --state RELATED,ESTABLISHED -j ACCEPT
  68. iptables -D FORWARD -i $GIFNAME -o $UPLINK -j ACCEPT
  69. fi