rules.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #Captive portal iptables rule script
  2. #Copyright Deben Oldert
  3. #This script handles the rules for the captive portal
  4. #It is multifuncional
  5. #Initialize the new rules (startup)
  6. #Delete a rule for an ip to grand internet access
  7. #define version of the script
  8. version="V2.1.13"
  9. #load IPTables
  10. IPTABLES=/sbin/iptables
  11. #When it needs to initialize the rules, do this
  12. if [ "$1" == "initialize" ]
  13. then
  14. #First stop host apd so people can't connect
  15. sudo service hostapd stop
  16. #Flush all existing rules
  17. $IPTABLES -F
  18. $IPTABLES -X
  19. #Allow forwarding
  20. echo 1 > /proc/sys/net/ipv4/ip_forward
  21. #Boot up Wifi adapter
  22. echo "Booting up WiFi adapter..."
  23. sudo ifup wlan0
  24. #Add some basic rules to iptables
  25. while read rule
  26. do
  27. #Execute each rule in the file
  28. $($rule)
  29. done < /portal/firewall_Rules
  30. #Each ip from the list will be redirected to the captive portal
  31. echo "These IP's will be redirected to the Captive Portal"
  32. #define 1 (ip counter) and set it to zero
  33. i=0
  34. #Read all ips from ip file and make rule per ip
  35. #Read each line as $ip
  36. while read ip
  37. do
  38. #Shits getting serious now
  39. sudo $IPTABLES -t nat -A PREROUTING -s "$ip" -p tcp -j DNAT --to-destination 10.111.11.5:80
  40. #Print the ip
  41. echo $ip
  42. #Increment 1 on i so we can see how many ip where added
  43. ((i++))
  44. #Not the last line in file? Lets do it again!
  45. done < /portal/users
  46. #Show the amount of ip's
  47. echo "Added $i IP's to rule list"
  48. echo "Checking if site is up (might take a while)"
  49. #Set 1 to one, for another counter
  50. i=1
  51. #Lets check if our portal is online
  52. #The 3 here is for the amount of times you want to check (default is 3)
  53. while [ $i -le 3 ]
  54. do
  55. #Check if the site is online
  56. if curl -s --head http://localhost/Portal/ | head -n 1 | grep "200" >/dev/null 2>&1;
  57. then
  58. #If the site is up, do this:
  59. echo "Site is up!"
  60. echo "Starting hostapd"
  61. #Start hostapd so people can connect
  62. sudo service hostapd start
  63. #Set i to 4 to break the loop
  64. i=4
  65. echo "Everything is up and running!"
  66. else
  67. #If site is still down do this
  68. echo "Still checking if site is up ($i/3)"
  69. #Give Tomcat some time to think
  70. sleep 30s
  71. #Increment i with 1
  72. ((i++))
  73. #If i = 4, The site is still not up, Tell whats wrong!
  74. if [ "$i" == "4" ]
  75. then
  76. echo "Site is still not online!"
  77. echo "Exiting the program (maybe reboot)"
  78. fi
  79. fi
  80. done
  81. #Show some information
  82. echo "Captive Portal rule script"
  83. echo $version
  84. echo "Copy right: Deben Oldert"
  85. fi
  86. #If action is permitting acces to internet, delete rule to portal
  87. if [ "$1" == "grand" ]
  88. then
  89. #check if $2 (ip adress) not empty
  90. if [[ ! -z "$2" ]]
  91. then
  92. #Delete the rule
  93. sudo -u root $IPTABLES -t nat -D PREROUTING -s $2 -p tcp -j DNAT --to-destination 10.11$
  94. #Tell the system its fine
  95. echo "SUCCES"
  96. else
  97. #Missing the ip parameter
  98. echo "FAIL"
  99. fi
  100. fi
  101. #Uncomment for debugging
  102. #$IPTABLES -t nat -L PREROUTING