1
0
Fork 0
mirror of https://github.com/Ysurac/openmptcprouter-vps.git synced 2025-02-12 11:21:56 +00:00
openmptcprouter-vps/omr-service

130 lines
4.6 KiB
Text
Raw Normal View History

#!/bin/bash
# OpenMPTCProuter VPS service script
2019-01-02 08:57:31 +00:00
_multipath() {
# Force multipath status
source /etc/shorewall/params.net
for intf in `ls -1 /sys/class/net`; do
if [ "$intf" != "bonding_masters" ]; then
if [ "$intf" = "$NET_IFACE" ]; then
[ "$(multipath $intf | tr -d '\n')" != "$intf is in default mode" ] && multipath $intf on
else
[ "$(multipath $intf | tr -d '\n')" != "$intf is deactivated" ] && multipath $intf off
fi
2019-01-02 08:57:31 +00:00
fi
done
}
2020-03-12 18:38:43 +00:00
_glorytun_udp() {
2020-10-20 08:31:12 +00:00
[ -z "$(glorytun show dev gt-udp-tun0 2>/dev/null | grep tunnel)" ] && {
2020-03-15 22:41:24 +00:00
logger -t "OMR-Service" "Restart Glorytun-UDP"
2020-07-20 14:01:51 +00:00
systemctl -q restart 'glorytun-udp@*'
2020-03-15 22:41:24 +00:00
}
2020-07-20 14:01:51 +00:00
for intf in /etc/glorytun-udp/tun*; do
[ "$(echo $intf | grep key)" = "" ] && /etc/glorytun-udp/post.sh ${intf}
2020-04-07 14:06:54 +00:00
done
}
_glorytun_tcp() {
2020-07-20 14:01:51 +00:00
for intf in /etc/glorytun-tcp/tun*; do
[ "$(echo $intf | grep key)" = "" ] && /etc/glorytun-tcp/post.sh ${intf}
2020-04-07 14:06:54 +00:00
done
2020-11-03 16:47:41 +00:00
if [ -f /etc/openmptcprouter-vps-admin/current-vpn ] && [ "$(cat /etc/openmptcprouter-vps-admin/current-vpn)" = "glorytun_tcp" ]; then
2021-03-02 08:52:33 +00:00
localip="$(cat /etc/glorytun-tcp/tun0 | grep LOCALIP | cut -d '=' -f2)"
[ -z "$localip" ] && localip="10.255.255.1"
remoteip="$(echo $localip | sed 's/\.1/\.2/')"
if [ "$(ping -c 5 -w 5 $remoteip | grep '100%')" != "" ] && [ "$(expr $(date +%s) - $(stat -c %Y /proc/$(pgrep glorytun-tcp)/exe ))" -gt "300" ]; then
logger -t "OMR-Service" "No answer from VPN client end, restart Glorytun-TCP"
systemctl restart glorytun-tcp@tun0
fi
fi
2020-03-15 22:41:24 +00:00
}
2021-04-19 19:15:43 +00:00
_dsvpn() {
[ -n "$(ip -6 r show 64:ff9b::/96 dev dsvpn0)" ] && ip -6 r del 64:ff9b::/96 dev dsvpn0 2>&1 >/dev/null
}
2021-05-09 08:35:55 +00:00
_wireguard() {
[ -n "$(ip a show dev wg0)" ] && ip a add 10.255.247.1/24 dev wg0 2>&1 >/dev/null
}
2021-04-19 19:15:43 +00:00
2020-03-15 22:41:24 +00:00
_omr_api() {
2020-09-15 12:27:08 +00:00
[ -z "$(curl -s -k -m 30 https://127.0.0.1:65500/)" ] && {
2020-03-15 22:41:24 +00:00
logger -t "OMR-Service" "Restart OMR-Admin"
systemctl -q restart omr-admin
}
2020-03-12 18:38:43 +00:00
}
2020-07-20 14:01:51 +00:00
_lan_route() {
cat /etc/openmptcprouter-vps-admin/omr-admin-config.json | jq -c '.users[0][]' |
while IFS=$"\n" read -r c; do
vpnremoteip=$(echo "$c" | jq -r '.vpnremoteip')
if [ -n "$vpnremoteip" ] && [ "$vpnremoteip" != "null" ]; then
2021-03-24 14:02:18 +00:00
echo "$c" | jq -c -r '.lanips[] //empty' |
2020-07-20 14:01:51 +00:00
while IFS=$"\n" read -r d; do
network=$(ipcalc -n $d | grep Network | awk '{print $2}')
[ -n "$network" ] && [ -z "$(ip r show $network via $vpnremoteip)" ] && ip r replace $network via $vpnremoteip 2>&1 >/dev/null
done
fi
done
}
_gre_tunnels() {
. "$(readlink -f "/etc/shorewall/params.vpn")"
for intf in /etc/openmptcprouter-vps-admin/intf/*; do
2020-07-28 13:51:00 +00:00
if [ -f "$intf" ]; then
. "$(readlink -f "$intf")"
iface="$(basename $intf)"
if [ "$(ip tunnel show $iface 2>/dev/null | awk '{print $4}')" != "$OMR_ADDR" ]; then
2020-07-28 13:51:00 +00:00
ip tunnel del $iface 2>&1 >/dev/null
2021-03-02 08:52:33 +00:00
ip tunnel add $iface mode gre local $INTFADDR remote $OMR_ADDR 2>&1 >/dev/null
ip link set $iface up 2>&1 >/dev/null
ip addr add $LOCALIP dev $iface 2>&1 >/dev/null
2020-07-28 13:51:00 +00:00
ip route add $NETWORK dev $iface 2>&1 >/dev/null
fi
fi
done
}
_openvpn_bonding() {
if [ "$(ip link show ovpnbonding1)" != "" ] && ([ "$(ip link show ovpnbonding1 | grep SLAVE)" = "" ] || [ "$(ip link show omr-bonding | grep DOWN)" != "" ] || [ "$(ip link show | grep ovpnbonding | grep -c SLAVE | tr -d '\n')" != "8" ]); then
2021-03-02 08:52:33 +00:00
echo 0 > /sys/class/net/omr-bonding/bonding/mode 2>&1 >/dev/null
2020-12-17 15:48:26 +00:00
ip link set ovpnbonding1 master omr-bonding 2>&1 >/dev/null
2021-03-02 08:52:33 +00:00
ip link set ovpnbonding1 up 2>&1 >/dev/null
2020-12-17 15:48:26 +00:00
ip link set ovpnbonding2 master omr-bonding 2>&1 >/dev/null
2021-03-02 08:52:33 +00:00
ip link set ovpnbonding2 up 2>&1 >/dev/null
2020-12-17 15:48:26 +00:00
ip link set ovpnbonding3 master omr-bonding 2>&1 >/dev/null
2021-03-02 08:52:33 +00:00
ip link set ovpnbonding3 up 2>&1 >/dev/null
2020-12-17 15:48:26 +00:00
ip link set ovpnbonding4 master omr-bonding 2>&1 >/dev/null
2021-03-02 08:52:33 +00:00
ip link set ovpnbonding4 up 2>&1 >/dev/null
2020-12-17 15:48:26 +00:00
ip link set ovpnbonding5 master omr-bonding 2>&1 >/dev/null
2021-03-02 08:52:33 +00:00
ip link set ovpnbonding5 up 2>&1 >/dev/null
2020-12-17 15:48:26 +00:00
ip link set ovpnbonding6 master omr-bonding 2>&1 >/dev/null
2021-03-02 08:52:33 +00:00
ip link set ovpnbonding6 up 2>&1 >/dev/null
2020-12-17 15:48:26 +00:00
ip link set ovpnbonding7 master omr-bonding 2>&1 >/dev/null
2021-03-02 08:52:33 +00:00
ip link set ovpnbonding7 up 2>&1 >/dev/null
2020-12-17 15:48:26 +00:00
ip link set ovpnbonding8 master omr-bonding 2>&1 >/dev/null
2021-03-02 08:52:33 +00:00
ip link set ovpnbonding8 up 2>&1 >/dev/null
2020-12-17 15:48:26 +00:00
ip link set omr-bonding up mtu 1440 2>&1 >/dev/null
ip a add 10.255.248.1 dev omr-bonding 2>&1 >/dev/null
ip r add 10.255.248.0/24 dev omr-bonding 2>&1 >/dev/null
ip r add 10.255.248.2 dev omr-bonding src 10.255.248.1 2>&1 >/dev/null
fi
}
2020-12-17 15:48:26 +00:00
modprobe bonding 2>&1 >/dev/null
ip link add omr-bonding type bond 2>&1 >/dev/null
while true; do
2020-03-12 18:38:43 +00:00
_glorytun_udp
2020-04-07 14:06:54 +00:00
_glorytun_tcp
2021-04-19 19:15:43 +00:00
_dsvpn
2021-05-09 08:35:55 +00:00
_wireguard
2020-03-10 09:45:06 +00:00
_multipath
2020-03-15 22:41:24 +00:00
_omr_api
2020-07-20 14:01:51 +00:00
_lan_route
_gre_tunnels
_openvpn_bonding
2019-05-12 02:45:49 +00:00
sleep 10
done