Advertisement
rs232

wg-sh 1.1 (deprecated)

Mar 24th, 2023
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. #!/bin/sh
  2. # version 1.1
  3. export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/home/root:
  4. #<configuration>
  5. DIR="/jffs" #this script and the conf file are both expected to be found here
  6. int=wg0 #don't change unless you know what you're doing
  7. port=51820 # make sure this consistent amongst all the sites
  8. site=1 #this is unique per site and defines "the site" e.g. LAN network number like 10.10.$site.0/24 and the inter-VPN IP address e.g. 192.168.192.$site/32
  9. vnet="192.168.192.${site}/24" #define the site to site VPN as a global /24
  10. checkmin=15 # how often destination needs to be verified (in minutes 1-60)
  11. #</configuration>
  12. rnet=$(grep -Ev '^($|#|\[)' ${DIR}/${int}.conf | grep -Eo '(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])/..' | grep -v .*/32$)
  13. [ $# -gt 0 ] && {
  14. [ $1 == "stop" ] && {
  15. iptables -t mangle -nvL PREROUTING | grep -q '.*MARK.*all.*wg0.*0x1/0x7' && iptables -t mangle -D PREROUTING -i $int -j MARK --set-mark 0x01/0x7
  16. iptables -nvL INPUT | grep -q ".*ACCEPT.*udp.dpt.${port}$" && iptables -D INPUT -p udp --dport $port -j ACCEPT
  17. iptables -nvL INPUT | grep -q ".*ACCEPT.*all.*$int" && iptables -D INPUT -i $int -j ACCEPT
  18. iptables -nvL FORWARD | grep -q ".*ACCEPT.*all.*$int" && iptables -D FORWARD -i $int -j ACCEPT
  19. ip route | grep -Eo ".*dev.$int.*" | while read line; do ip route del $(echo $line | awk '{print $1}'); done
  20. ifconfig $int down 2>/dev/null
  21. ip link del dev $int 2>/dev/null
  22. rmmod wireguard 2>/dev/null
  23. cru l | grep -q '#wireguard-check#' && cru d wireguard-check
  24. echo stopped.
  25. exit
  26. }
  27. [ $1 == "check" ] && {
  28. echo $rnet | tr " " "\n" | while read line; do
  29. rsite=$(echo $line | cut -d. -f3 | grep -Eo '^[0-9]{0,3}')
  30. sitesup=$(echo $vnet | cut -d. -f0-3)
  31. sitehop=$(echo ${sitesup}.${rsite})
  32. ip route | grep -q "^${line}.*dev.${int}" && {
  33. ping -4 -I ${int} -c3 -A -q ${sitehop} &>/dev/null && echo "$line reachable" || { echo "$line = unreachable - removing" ; ip route del $(echo $line | awk '{print $1}') ;}
  34. } || {
  35. ping -4 -I ${int} -c3 -A -q ${sitehop} &>/dev/null && { echo "$line = reachable + adding" ; ip route add $line dev $int ;}
  36. }
  37. done
  38. }
  39. exit
  40. }
  41. ifconfig $int down 2>/dev/null
  42. ip link del dev $int 2>/dev/null
  43. rmmod wireguard 2>/dev/null
  44. modprobe wireguard
  45. ip link add dev $int type wireguard
  46. ip address add dev $int $vnet
  47. wg setconf $int $DIR/$int.conf
  48. sleep 1
  49. ifconfig $int up
  50. # bypass CTF for wireguard
  51. [ $(nvram get ctf_disable) -eq 0 ] && {
  52. iptables -t mangle -nvL PREROUTING | grep -q '.*MARK.*all.*wg0.*0x1/0x7' || iptables -t mangle -I PREROUTING -i $int -j MARK --set-mark 0x01/0x7
  53. }
  54. # Open WireGuard port
  55. iptables -nvL INPUT | grep -q ".*ACCEPT.*udp.dpt.${port}$" || iptables -A INPUT -p udp --dport $port -j ACCEPT
  56. # Accept packets from WireGuard internal subnet
  57. iptables -nvL INPUT | grep -q ".*ACCEPT.*all.*$int" || iptables -A INPUT -i $int -j ACCEPT
  58. # Set up forwarding
  59. iptables -nvL FORWARD | grep -q ".*ACCEPT.*all.*$int" || iptables -A FORWARD -i $int -j ACCEPT
  60. # add routes to opposite lan
  61. echo $rnet | tr " " "\n" | while read line; do ip route | grep -q "^${line}.*dev.$int.*" || ip route add $line dev $int; done
  62. # add periodic checks
  63. [ $checkmin -ne 0 ] && cru l | grep -q '#wireguard-check#' || cru a wireguard-check "*/${checkmin} * * * * $DIR/wg.sh check"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement